Class: SmartPrompt::AnthropicAdapter
- Inherits:
-
LLMAdapter
- Object
- LLMAdapter
- SmartPrompt::AnthropicAdapter
- Defined in:
- lib/smart_prompt/anthropic_adapter.rb
Constant Summary collapse
- DEFAULT_URL =
"https://api.anthropic.com"- DEFAULT_VERSION =
"2023-06-01"- DEFAULT_MAX_TOKENS =
4096
Instance Attribute Summary
Attributes inherited from LLMAdapter
Instance Method Summary collapse
-
#initialize(config) ⇒ AnthropicAdapter
constructor
A new instance of AnthropicAdapter.
- #send_request(messages, model = nil, temperature = 0.7, tools = nil, proc = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ AnthropicAdapter
Returns a new instance of AnthropicAdapter.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/smart_prompt/anthropic_adapter.rb', line 11 def initialize(config) super @api_key = resolve_api_key(@config["api_key"]) || ENV["ANTHROPIC_API_KEY"] @url = (@config["url"] || DEFAULT_URL).chomp("/") @anthropic_version = @config["anthropic_version"] || DEFAULT_VERSION @request_timeout = @config["request_timeout"] || 240 raise LLMAPIError, "Invalid Anthropic configuration: missing api_key" if @api_key.nil? || @api_key.empty? @messages_uri = URI("#{@url}/v1/messages") SmartPrompt.logger.info "Successful creation an Anthropic client." rescue URI::InvalidURIError => e SmartPrompt.logger.error "Failed to initialize Anthropic client: #{e.}" raise LLMAPIError, "Invalid Anthropic configuration: #{e.}" rescue LLMAPIError raise rescue => e SmartPrompt.logger.error "Failed to initialize Anthropic client: #{e.}" raise Error, "Unexpected error initializing Anthropic client: #{e.}" end |
Instance Method Details
#send_request(messages, model = nil, temperature = 0.7, tools = nil, proc = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/smart_prompt/anthropic_adapter.rb', line 32 def send_request(, model = nil, temperature = 0.7, tools = nil, proc = nil) SmartPrompt.logger.info "AnthropicAdapter: Sending request to Anthropic" temperature = 0.7 if temperature.nil? model_name = model || @config["model"] SmartPrompt.logger.info "AnthropicAdapter: Using model #{model_name}" parameters = build_parameters(, model_name, temperature, tools, !proc.nil?) SmartPrompt.logger.info "Send parameters is: #{parameters}" response = (parameters, proc) SmartPrompt.logger.info "AnthropicAdapter: Received response from Anthropic" return if proc @last_response = response extract_content(response) rescue JSON::ParserError SmartPrompt.logger.error "Failed to parse Anthropic API response" raise LLMAPIError, "Failed to parse Anthropic API response" rescue LLMAPIError raise rescue => e SmartPrompt.logger.error "Unexpected error during Anthropic request: #{e.}" raise Error, "Unexpected error during Anthropic request: #{e.}" ensure SmartPrompt.logger.info "Successful send a message" end |