Class: OxAiWorkers::Models::AnthropicMax
- Inherits:
-
LLMBase
- Object
- LLMBase
- OxAiWorkers::Models::AnthropicMax
show all
- Defined in:
- lib/oxaiworkers/models/anthropic_max.rb
Instance Attribute Summary
Attributes inherited from LLMBase
#api_key, #frequency_penalty, #max_tokens, #model, #temperature, #uri_base
Instance Method Summary
collapse
-
#add_base64(binary:, text:, mime_type:) ⇒ Object
-
#add_url(url:, text:, mime_type:) ⇒ Object
-
#build_parameters(messages:, tools: [], filtered_functions: [], tool_choice: nil) ⇒ Object
-
#client ⇒ Object
-
#initialize(uri_base: nil, api_key: nil, model: nil, max_tokens: nil, temperature: nil, frequency_penalty: nil) ⇒ AnthropicMax
constructor
A new instance of AnthropicMax.
-
#parse_one_choice(choice) ⇒ Object
-
#parse_response(response) ⇒ Object
-
#request(parameters) ⇒ Object
-
#tool_call(name:, args:, call_id:, out:) ⇒ Object
Methods inherited from LLMBase
#_parse_tool_calls
Constructor Details
#initialize(uri_base: nil, api_key: nil, model: nil, max_tokens: nil, temperature: nil, frequency_penalty: nil) ⇒ AnthropicMax
Returns a new instance of AnthropicMax.
6
7
8
9
10
11
12
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 6
def initialize(uri_base: nil, api_key: nil, model: nil, max_tokens: nil, temperature: nil, frequency_penalty: nil)
@model = model || 'claude-3-7-sonnet-latest'
@uri_base = uri_base @temperature = temperature || 0.7
@api_key = api_key || OxAiWorkers.configuration.access_token_anthropic
super(uri_base: @uri_base, api_key: @api_key, model: @model, max_tokens:, temperature:, frequency_penalty:)
end
|
Instance Method Details
#add_base64(binary:, text:, mime_type:) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 73
def add_base64(binary:, text:, mime_type:)
content = []
content << { type: 'text', text: } if text.present?
content << {
type: mime_type.include?('image') ? 'image' : 'document',
source: {
type: 'base64',
media_type: mime_type,
data: Base64.strict_encode64(binary)
}
}
content
end
|
#add_url(url:, text:, mime_type:) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 87
def add_url(url:, text:, mime_type:)
content = []
content << { type: 'text', text: } if text.present?
content << {
type: mime_type.include?('image') ? 'image' : 'document',
source: {
type: 'url',
url: url
}
}
content
end
|
#build_parameters(messages:, tools: [], filtered_functions: [], tool_choice: nil) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 26
def build_parameters(messages:, tools: [], filtered_functions: [], tool_choice: nil)
parameters = {
model: @model,
system: messages.select { |m| m[:role] == :system }.map { |m| m[:content] }.join("\n\n"),
messages: messages.reject { |m| m[:role] == :system },
temperature: @temperature,
max_tokens: @max_tokens
}
if tools.present?
functions = tools.map(&:to_anthropic_format).flatten
@names = functions.map { |f| f[:name] }
parameters[:tools] = functions.reject { |f| filtered_functions.include?(f[:name]) }
parameters[:tool_choice] =
tool_choice.nil? ? { type: 'any' } : { type: 'tool', name: tool_choice }
end
parameters
end
|
#client ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 14
def client
@client ||= Anthropic::Client.new(
access_token: @api_key,
uri_base: @uri_base,
log_errors: true
)
end
|
#parse_one_choice(choice) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 111
def parse_one_choice(choice)
return unless choice
@result = nil
@tool_calls = []
if choice['type'] == 'tool_use'
begin
args = JSON.parse(choice['input'].to_json, symbolize_names: true)
rescue JSON::ParserError => e
OxAiWorkers.logger.error("Failed to parse tool call arguments: #{e.message}", for: self.class)
OxAiWorkers.logger.debug("Raw arguments: #{choice['input']}", for: self.class)
end
fname = @names.find { |n| n.end_with?(choice['name']) }
if fname != choice['name']
OxAiWorkers.logger.error("Tool call name #{choice['name']} not found. Using #{fname} instead.",
for: self.class)
end
tool_call = {
class: fname.split('__').first,
name: fname.split('__').last,
args:
}
@tool_calls << tool_call
elsif choice['type'] == 'text'
@result = choice['text']
end
[@result, @tool_calls]
end
|
#parse_response(response) ⇒ Object
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 100
def parse_response(response, &)
choices = response['content']
@is_truncated = (response['stop_reason'] == 'max_tokens')
return if choices.nil? || choices.empty?
choices.each do |choice|
result, tool_calls = parse_one_choice(choice)
yield(result, @is_truncated, tool_calls)
end
end
|
#request(parameters) ⇒ Object
22
23
24
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 22
def request(parameters)
client.messages(parameters:)
end
|
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/oxaiworkers/models/anthropic_max.rb', line 47
def tool_call(name:, args:, call_id:, out:)
[
{
role: :assistant,
content: [
{
type: 'tool_use',
id: "call_#{call_id}",
name:,
input: args
}
]
},
{
role: :user,
content: [
{
type: 'tool_result',
tool_use_id: "call_#{call_id}",
content: out.present? ? out : "Successful"
}
]
}
]
end
|