Class: Google::ADK::AnthropicClient

Inherits:
Object
  • Object
show all
Defined in:
lib/google/adk/clients/anthropic_client.rb

Overview

Client for interacting with Anthropic’s Claude API

Constant Summary collapse

API_BASE_URL =
"https://api.anthropic.com"
API_VERSION =
"2023-06-01"
DEFAULT_MODEL =
"claude-3-5-sonnet-20241022"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ AnthropicClient

Returns a new instance of AnthropicClient.

Raises:



17
18
19
20
21
22
23
24
25
26
# File 'lib/google/adk/clients/anthropic_client.rb', line 17

def initialize(api_key: nil)
  @api_key = api_key || ENV["ANTHROPIC_API_KEY"]
  raise ConfigurationError, "ANTHROPIC_API_KEY not set" unless @api_key
  
  @client = Faraday.new(API_BASE_URL) do |conn|
    conn.request :json
    conn.response :json
    conn.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



15
16
17
# File 'lib/google/adk/clients/anthropic_client.rb', line 15

def api_key
  @api_key
end

Instance Method Details

#generate_content(model:, messages:, tools: nil, system_instruction: nil) ⇒ Hash

Generate content using Anthropic API

Parameters:

  • model (String)

    Model name (e.g., “claude-3-5-sonnet-20241022”)

  • messages (Array<Hash>)

    Conversation messages

  • tools (Array<Hash>) (defaults to: nil)

    Available tools (optional)

  • system_instruction (String) (defaults to: nil)

    System instruction (optional)

Returns:

  • (Hash)

    API response formatted to match Gemini response structure



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/google/adk/clients/anthropic_client.rb', line 35

def generate_content(model:, messages:, tools: nil, system_instruction: nil)
  url = "/v1/messages"
  
  # Convert messages to Anthropic format
  anthropic_messages = format_messages(messages)
  
  # Adjust max_tokens based on model
  max_tokens = case model
  when /haiku/
    4096
  when /sonnet/
    4096
  when /opus/
    4096
  else
    4096  # Safe default for Claude models
  end
  
  payload = {
    model: model || DEFAULT_MODEL,
    messages: anthropic_messages,
    max_tokens: max_tokens,
    temperature: 0.7
  }
  
  # Add system instruction if provided
  payload[:system] = system_instruction if system_instruction
  
  # Add tools if provided
  if tools && !tools.empty?
    payload[:tools] = format_tools(tools)
  end
  
  response = @client.post(url) do |req|
    req.headers["x-api-key"] = @api_key
    req.headers["anthropic-version"] = API_VERSION
    req.headers["content-type"] = "application/json"
    req.body = payload
  end
  
  handle_response(response)
end