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
43
44
45
46
47
48
|
# File 'lib/ruby_llm_mesh/providers/openai.rb', line 10
def complete(prompt:, system: nil, model: nil, **options)
api_key = config.openai_api_key
raise AuthenticationError.new("OPENAI_API_KEY is not configured", provider: name) if api_key.to_s.empty?
model ||= config.openai_model
messages = []
messages << { role: "system", content: system } if system
messages << { role: "user", content: prompt }
body = {
model: model,
messages: messages,
temperature: options.fetch(:temperature, 0.7)
}
body[:max_tokens] = options[:max_tokens] if options[:max_tokens]
body[:tools] = options[:tools] if options[:tools]
response, latency_ms = http_post(
"#{config.openai_base_url.chomp('/')}/chat/completions",
headers: {
"Authorization" => "Bearer #{api_key}",
"Content-Type" => "application/json"
},
body: body
)
raise_for_status!(response)
data = parse_json(response)
choice = data.dig("choices", 0, "message", "content").to_s
Response.new(
content: choice,
provider: name,
model: data["model"] || model,
usage: data["usage"] || {},
raw: data,
latency_ms: latency_ms
)
end
|