Class: LlmHub::Completion::Providers::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/llm_hub/completion/providers/anthropic.rb

Overview

Anthropic completion provider

Constant Summary collapse

COMPLETIONS_URI =
'https://api.anthropic.com/v1/messages'

Instance Attribute Summary

Attributes inherited from Base

#api_key

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from LlmHub::Common::AbstractMethods

included

Constructor Details

This class inherits a constructor from LlmHub::Completion::Providers::Base

Instance Method Details

#extract_answer(response_body) ⇒ Object



35
36
37
38
39
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 35

def extract_answer(response_body)
  content_array = response_body&.dig('content')
  first_content = content_array&.first
  first_content&.dig('text')
end

#extract_tokens(response_body) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 41

def extract_tokens(response_body)
  usage = response_body&.dig('usage')
  {
    total_tokens: (usage&.dig('input_tokens') || 0) + (usage&.dig('output_tokens') || 0),
    prompt_tokens: usage&.dig('input_tokens'),
    completion_tokens: usage&.dig('output_tokens')
  }
end

#headersObject



14
15
16
17
18
19
20
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 14

def headers
  {
    'Content-Type' => 'application/json',
    'x-api-key' => @api_key,
    'anthropic-version' => '2023-06-01'
  }
end

#request_body(system_prompt, content, model_name, option_params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 22

def request_body(system_prompt, content, model_name, option_params)
  base_params = {
    model: model_name,
    max_tokens: 1024,
    temperature: 0.2,
    system: system_prompt,
    messages: [
      { role: 'user', content: content }
    ]
  }
  base_params.merge(option_params)
end

#urlObject



10
11
12
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 10

def url
  COMPLETIONS_URI
end