Class: LlmHub::Completion::Providers::Anthropic
- Inherits:
-
Base
- Object
- Base
- LlmHub::Completion::Providers::Anthropic
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
included
Instance Method Details
35
36
37
38
39
|
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 35
def (response_body)
content_array = response_body&.dig('content')
first_content = content_array&.first
first_content&.dig('text')
end
|
41
42
43
44
45
46
47
48
|
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 41
def (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
|
14
15
16
17
18
19
20
|
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 14
def
{
'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
|
#url ⇒ Object
10
11
12
|
# File 'lib/llm_hub/completion/providers/anthropic.rb', line 10
def url
COMPLETIONS_URI
end
|