10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/ruby_llm/provider.rb', line 10
def complete(messages, tools: [], model: nil, &block)
payload = build_payload(messages, tools, model: model, stream: block_given?)
content = String.new
model_id = nil
input_tokens = 0
output_tokens = 0
response = connection.post(completion_url, payload) do |req|
req..merge!
if block_given?
req.options.on_data = handle_stream do |chunk|
model_id ||= chunk.model_id
content << (chunk.content || '')
input_tokens += chunk.input_tokens if chunk.input_tokens
output_tokens += chunk.output_tokens if chunk.output_tokens
block.call(chunk)
end
end
end
if block_given?
Message.new(
role: :assistant,
content: content,
model_id: model_id,
input_tokens: input_tokens.positive? ? input_tokens : nil,
output_tokens: output_tokens.positive? ? output_tokens : nil
)
else
parse_completion_response(response)
end
end
|