Class: ActiveAgent::GenerationProvider::AnthropicProvider

Inherits:
Base
  • Object
show all
Includes:
MessageFormatting, StreamProcessing, ToolManagement
Defined in:
lib/active_agent/generation_provider/anthropic_provider.rb

Instance Attribute Summary

Attributes inherited from Base

#access_token, #client, #config, #model_name, #prompt, #response

Instance Method Summary collapse

Methods included from ToolManagement

#format_tools, #handle_actions

Methods included from MessageFormatting

#provider_messages

Methods included from StreamProcessing

#provider_stream

Methods inherited from Base

#embed

Methods included from ParameterBuilder

#prompt_parameters

Methods included from ErrorHandling

#with_error_handling

Constructor Details

#initialize(config) ⇒ AnthropicProvider

Returns a new instance of AnthropicProvider.



23
24
25
26
27
28
# File 'lib/active_agent/generation_provider/anthropic_provider.rb', line 23

def initialize(config)
  super
  @access_token ||= config["api_key"] || config["access_token"] || Anthropic.configuration.access_token || ENV["ANTHROPIC_ACCESS_TOKEN"]
  @extra_headers = config["extra_headers"] || {}
  @client = Anthropic::Client.new(access_token: @access_token, extra_headers: @extra_headers)
end

Instance Method Details

#chat_prompt(parameters: prompt_parameters) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/active_agent/generation_provider/anthropic_provider.rb', line 38

def chat_prompt(parameters: prompt_parameters)
  if prompt.options[:stream] || config["stream"]
    parameters[:stream] = provider_stream
    @streaming_request_params = parameters
  end

  chat_response(@client.messages(parameters: parameters), parameters)
end

#generate(prompt) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/active_agent/generation_provider/anthropic_provider.rb', line 30

def generate(prompt)
  @prompt = prompt

  with_error_handling do
    chat_prompt(parameters: prompt_parameters)
  end
end