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
50
51
52
53
54
55
56
|
# File 'app/services/completion_kit/open_ai_client.rb', line 13
def generate_completion(prompt, options = {})
@temperature_dropped = false
return "Error: API key not configured" unless configured?
model = options[:model] || "gpt-4.1-mini"
max_tokens = options[:max_tokens] || 8192
temperature = options[:temperature] || 0.7
response = post_responses(model: model, prompt: prompt, max_tokens: max_tokens, temperature: temperature)
if response.status == 400 && temperature_unsupported?(response.body)
@temperature_dropped = true
response = post_responses(model: model, prompt: prompt, max_tokens: max_tokens, temperature: nil)
end
if response.status == 429
raise CompletionKit::RateLimitError.new(
response.body.to_s.truncate(500),
provider: "openai",
status: 429,
retry_after: response. && response.["Retry-After"]&.to_i
)
end
if response.success?
data = JSON.parse(response.body)
if data["status"] == "incomplete"
reason = data.dig("incomplete_details", "reason") || "unknown"
return "Error: response incomplete (#{reason}) — increase max_tokens=#{max_tokens} or pick a non-reasoning judge model"
end
message = Array(data["output"]).find { |o| o["type"] == "message" }
content = message&.dig("content", 0, "text").to_s.strip
return "Error: model returned empty content" if content.empty?
content
else
"Error: #{response.status} - #{response.body}"
end
rescue CompletionKit::RateLimitError
raise
rescue Faraday::Error => e
raise
rescue => e
"Error: #{e.message}"
end
|