Class: OxAiWorkers::Models::LLMBase
- Inherits:
-
Object
- Object
- OxAiWorkers::Models::LLMBase
show all
- Defined in:
- lib/oxaiworkers/models/llm_base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#_parse_tool_calls(tool_calls_raw) ⇒ Object
-
#add_base64(binary:, text:, mime_type:, detail: 'high') ⇒ Object
-
#add_url(url:, text:, detail: 'high') ⇒ Object
-
#build_parameters(messages:, tools: [], filtered_functions: [], tool_choice: nil) ⇒ Object
-
#client ⇒ Object
-
#initialize(uri_base:, api_key:, model:, max_tokens: nil, temperature: nil, frequency_penalty: nil) ⇒ LLMBase
constructor
A new instance of LLMBase.
-
#parse_one_choice(choice) ⇒ Object
-
#parse_response(response) ⇒ Object
-
#request(parameters) ⇒ Object
-
#tool_call(name:, args:, call_id:, out:) ⇒ Object
Constructor Details
#initialize(uri_base:, api_key:, model:, max_tokens: nil, temperature: nil, frequency_penalty: nil) ⇒ LLMBase
8
9
10
11
12
13
14
15
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 8
def initialize(uri_base:, api_key:, model:, max_tokens: nil, temperature: nil, frequency_penalty: nil)
@max_tokens = max_tokens || OxAiWorkers.configuration.max_tokens
@temperature = temperature
@frequency_penalty = frequency_penalty || 0
@api_key = api_key
@uri_base = uri_base
@model = model
end
|
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
6
7
8
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 6
def api_key
@api_key
end
|
#frequency_penalty ⇒ Object
Returns the value of attribute frequency_penalty.
6
7
8
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 6
def frequency_penalty
@frequency_penalty
end
|
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
6
7
8
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 6
def max_tokens
@max_tokens
end
|
#model ⇒ Object
Returns the value of attribute model.
6
7
8
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 6
def model
@model
end
|
#temperature ⇒ Object
Returns the value of attribute temperature.
6
7
8
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 6
def temperature
@temperature
end
|
#uri_base ⇒ Object
Returns the value of attribute uri_base.
6
7
8
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 6
def uri_base
@uri_base
end
|
Instance Method Details
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 127
def _parse_tool_calls(tool_calls_raw)
tool_calls = []
tool_calls_raw.each do |tool_call|
next unless tool_call['type'] == 'function'
function = tool_call['function']
next unless function && function['name'] && function['arguments']
begin
args = JSON.parse(function['arguments'], 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: #{function['arguments']}", for: self.class)
next
end
OxAiWorkers.logger.debug("function: #{function.inspect}", for: self.class)
next if function['name'].empty?
tool_calls << {
class: function['name'].split('__').first,
name: function['name'].split('__').last,
args:
}
end
tool_calls
end
|
#add_base64(binary:, text:, mime_type:, detail: 'high') ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 69
def add_base64(binary:, text:, mime_type:, detail: 'high')
content = []
content << { type: 'text', text: } if text.present?
content << if mime_type.include?('image')
{ type: 'image_url',
image_url: {
url: "data:#{mime_type};base64,#{Base64.strict_encode64(binary)}",
detail:
}
}
else
{
type: 'file',
file: {
filename: "file.#{mime_type.split('/').last}",
file_data: "data:#{mime_type};base64,#{Base64.strict_encode64(binary)}"
}
}
end
content
end
|
#add_url(url:, text:, detail: 'high') ⇒ Object
91
92
93
94
95
96
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 91
def add_url(url:, text:, detail: 'high')
content = []
content << { type: 'text', text: } if text.present?
content << { type: 'image_url', image_url: { url:, detail: } }
content
end
|
#build_parameters(messages:, tools: [], filtered_functions: [], tool_choice: nil) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 29
def build_parameters(messages:, tools: [], filtered_functions: [], tool_choice: nil)
parameters = {
model: @model,
messages:,
temperature: @temperature,
max_completion_tokens: @max_tokens,
frequency_penalty: @frequency_penalty
}
if tools.present?
functions = tools.map(&:to_openai_format).flatten
parameters[:tools] = functions.reject { |f| filtered_functions.include?(f[:name]) }
parameters[:tool_choice] =
tool_choice.nil? ? 'required' : { type: 'function', function: { name: tool_choice } }
end
parameters
end
|
#client ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 21
def client
@client ||= OpenAI::Client.new(
access_token: @api_key,
uri_base: @uri_base,
log_errors: true
)
end
|
#parse_one_choice(choice) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 108
def parse_one_choice(choice)
message = choice['message']
return unless message
tool_calls_raw = message['tool_calls']
current_result = message['content']
@result = current_result if current_result.present?
current_finish_reason = choice['finish_reason']
@is_truncated = (current_finish_reason == 'length')
@tool_calls = _parse_tool_calls(tool_calls_raw) unless @is_truncated || tool_calls_raw.empty?
[@result, @is_truncated, @tool_calls]
end
|
#parse_response(response) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 98
def parse_response(response, &)
choices = response['choices']
return if choices.nil? || choices.empty?
choices.each do |choice|
arr = parse_one_choice(choice)
yield(arr)
end
end
|
#request(parameters) ⇒ Object
17
18
19
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 17
def request(parameters)
client.chat(parameters:)
end
|
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/oxaiworkers/models/llm_base.rb', line 48
def tool_call(name:, args:, call_id:, out:)
[
{
role: :assistant,
tool_calls: [{
id: "call_#{call_id}",
type: 'function',
function: {
name:,
arguments: args.to_json
}
}]
},
{
role: :tool,
content: out.present? ? out : "Tool call #{name} successful.",
tool_call_id: "call_#{call_id}"
}
]
end
|