Class: RailsAgents::Providers::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_agents/providers/anthropic.rb,
lib/rails_agents/providers/anthropic/files.rb

Defined Under Namespace

Classes: Files

Constant Summary collapse

API_VERSION =
"2023-06-01"

Instance Method Summary collapse

Constructor Details

#initialize(api_key: RailsAgents.config.anthropic_api_key) ⇒ Anthropic

Returns a new instance of Anthropic.

Raises:



11
12
13
14
# File 'lib/rails_agents/providers/anthropic.rb', line 11

def initialize(api_key: RailsAgents.config.anthropic_api_key)
  raise ConfigurationError, "Set anthropic_api_key in config/initializers/rails_agents.rb" if api_key.to_s.empty?
  @api_key = api_key
end

Instance Method Details

#chat(messages:, client_tools: [], skills: nil, model:, json: false, &block) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
# File 'lib/rails_agents/providers/anthropic.rb', line 16

def chat(messages:, client_tools: [], skills: nil, model:, json: false, &block)
  anthropic_skills = skills&.anthropic_request || {}
  body = build_body(messages:, client_tools:, skills: anthropic_skills, model:)

  response = connection(anthropic_skills[:beta_headers]).post("messages", body.to_json)
  raise ProviderError, parse_error(response) unless response.success?

  parse_response(JSON.parse(response.body), json:)
end

#filesObject



26
# File 'lib/rails_agents/providers/anthropic.rb', line 26

def files = @files ||= Files.new(api_key: @api_key)