Class: PromptObjects::LLM::AnthropicAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/llm/anthropic_adapter.rb

Overview

Anthropic API adapter for LLM calls.

Constant Summary collapse

DEFAULT_MODEL =
"claude-haiku-4-5"
DEFAULT_MAX_TOKENS =
4096

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil, max_tokens: nil) ⇒ AnthropicAdapter

Returns a new instance of AnthropicAdapter.



10
11
12
13
14
15
16
17
# File 'lib/prompt_objects/llm/anthropic_adapter.rb', line 10

def initialize(api_key: nil, model: nil, max_tokens: nil)
  @api_key = api_key || ENV.fetch("ANTHROPIC_API_KEY") do
    raise Error, "ANTHROPIC_API_KEY environment variable not set"
  end
  @model = model || DEFAULT_MODEL
  @max_tokens = max_tokens || DEFAULT_MAX_TOKENS
  @client = Anthropic::Client.new(api_key: @api_key)
end

Instance Method Details

#chat(system:, messages:, tools: []) ⇒ Response

Make a chat completion request.

Parameters:

  • system (String)

    System prompt

  • messages (Array<Hash>)

    Conversation history

  • tools (Array<Hash>) (defaults to: [])

    Tool descriptors (optional)

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prompt_objects/llm/anthropic_adapter.rb', line 24

def chat(system:, messages:, tools: [])
  params = {
    model: @model,
    max_tokens: @max_tokens,
    system: system,
    messages: build_messages(messages)
  }

  # Only include tools if we have any
  if tools.any?
    params[:tools] = convert_tools(tools)
  end

  raw_response = @client.messages.create(**params)
  parse_response(raw_response)
end