Class: CompletionKit::OpenAiClient
- Inherits:
-
LlmClient
show all
- Defined in:
- app/services/completion_kit/open_ai_client.rb
Constant Summary
collapse
- STATIC_MODELS =
[
{ id: "gpt-5.4-mini", name: "GPT-5.4 Mini" },
{ id: "gpt-4.1-mini", name: "GPT-4.1 Mini" },
{ id: "gpt-4o-mini", name: "GPT-4o Mini" }
].freeze
Instance Method Summary
collapse
Methods inherited from LlmClient
for_model, for_provider, #initialize
Instance Method Details
#available_models ⇒ Object
51
52
53
|
# File 'app/services/completion_kit/open_ai_client.rb', line 51
def available_models
STATIC_MODELS
end
|
#configuration_errors ⇒ Object
59
60
61
62
63
|
# File 'app/services/completion_kit/open_ai_client.rb', line 59
def configuration_errors
errors = []
errors << "OpenAI API key is not configured" unless api_key.present?
errors
end
|
55
56
57
|
# File 'app/services/completion_kit/open_ai_client.rb', line 55
def configured?
api_key.present?
end
|
#generate_completion(prompt, options = {}) ⇒ Object
9
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
49
|
# File 'app/services/completion_kit/open_ai_client.rb', line 9
def generate_completion(prompt, options = {})
return "Error: API key not configured" unless configured?
require "faraday"
require "faraday/retry"
require "json"
model = options[:model] || "gpt-4.1-mini"
max_tokens = options[:max_tokens] || 1000
temperature = options[:temperature] || 0.7
conn = Faraday.new(url: "https://api.openai.com") do |f|
f.request :retry, max: 2, interval: 0.5
f.adapter Faraday.default_adapter
end
response = conn.post do |req|
req.url "/v1/responses"
req.["Content-Type"] = "application/json"
req.["Authorization"] = "Bearer #{api_key}"
req.body = {
model: model,
input: prompt,
instructions: "You are a helpful assistant.",
max_output_tokens: max_tokens,
temperature: temperature,
store: false
}.to_json
end
if response.success?
data = JSON.parse(response.body)
data["output"][0]["content"][0]["text"].strip
else
"Error: #{response.status} - #{response.body}"
end
rescue Faraday::Error => e
raise
rescue => e
"Error: #{e.message}"
end
|