Class: LlmGateway::Clients::Anthropic

Inherits:
BaseClient
  • Object
show all
Defined in:
lib/llm_gateway/clients/anthropic.rb

Constant Summary collapse

CLAUDE_CODE_VERSION =
"2.1.2"

Instance Attribute Summary

Attributes inherited from BaseClient

#api_key, #base_endpoint, #model_key

Instance Method Summary collapse

Methods inherited from BaseClient

#get, #post, #post_file, #post_stream

Constructor Details

#initialize(model_key: "claude-3-7-sonnet-20250219", api_key: ENV["ANTHROPIC_API_KEY"]) ⇒ Anthropic

Returns a new instance of Anthropic.



13
14
15
16
# File 'lib/llm_gateway/clients/anthropic.rb', line 13

def initialize(model_key: "claude-3-7-sonnet-20250219", api_key: ENV["ANTHROPIC_API_KEY"])
  @base_endpoint = "https://api.anthropic.com/v1"
  super(model_key: model_key, api_key: api_key)
end

Instance Method Details

#chat(messages, **kwargs) ⇒ Object



18
19
20
# File 'lib/llm_gateway/clients/anthropic.rb', line 18

def chat(messages, **kwargs)
  post("messages", build_body(messages, **kwargs))
end

#download_file(file_id) ⇒ Object



37
38
39
# File 'lib/llm_gateway/clients/anthropic.rb', line 37

def download_file(file_id)
  get("files/#{file_id}/content")
end

#get_oauth_access_token(access_token:, refresh_token:, expires_at:, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/llm_gateway/clients/anthropic.rb', line 26

def get_oauth_access_token(access_token:, refresh_token:, expires_at:, &block)
  token_manager = LlmGateway::Clients::ClaudeCode::TokenManager.new(
    access_token: access_token,
    refresh_token: refresh_token,
    expires_at: expires_at
  )
  token_manager.on_token_refresh = block if block_given?
  token_manager.ensure_valid_token
  token_manager.access_token
end

#stream(messages, **kwargs, &block) ⇒ Object



22
23
24
# File 'lib/llm_gateway/clients/anthropic.rb', line 22

def stream(messages, **kwargs, &block)
  post_stream("messages", build_body(messages, **kwargs), &block)
end

#upload_file(filename, content, mime_type = "application/octet-stream") ⇒ Object



41
42
43
# File 'lib/llm_gateway/clients/anthropic.rb', line 41

def upload_file(filename, content, mime_type = "application/octet-stream")
  post_file("files", content, filename, mime_type: mime_type)
end