Class: CompletionKit::OpenRouterClient
- Inherits:
-
LlmClient
- Object
- LlmClient
- CompletionKit::OpenRouterClient
show all
- Defined in:
- app/services/completion_kit/open_router_client.rb
Constant Summary
collapse
- BASE_URL =
"https://openrouter.ai/api/v1".freeze
- REFERER =
"https://completionkit.com".freeze
- APP_TITLE =
"CompletionKit".freeze
Instance Method Summary
collapse
Methods inherited from LlmClient
for_model, for_provider, #initialize
Instance Method Details
#available_models ⇒ Object
49
50
51
|
# File 'app/services/completion_kit/open_router_client.rb', line 49
def available_models
[]
end
|
#configuration_errors ⇒ Object
57
58
59
60
61
|
# File 'app/services/completion_kit/open_router_client.rb', line 57
def configuration_errors
errors = []
errors << "OpenRouter API key is not configured" unless api_key.present?
errors
end
|
53
54
55
|
# File 'app/services/completion_kit/open_router_client.rb', line 53
def configured?
api_key.present?
end
|
#generate_completion(prompt, options = {}) ⇒ Object
7
8
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
|
# File 'app/services/completion_kit/open_router_client.rb', line 7
def generate_completion(prompt, options = {})
return "Error: API key not configured" unless configured?
require "faraday"
require "faraday/retry"
require "json"
model = options[:model] || "openai/gpt-4o-mini"
max_tokens = options[:max_tokens] || 1000
temperature = options[:temperature] || 0.7
conn = Faraday.new(url: BASE_URL) do |f|
f.options.timeout = 30
f.options.open_timeout = 5
f.request :retry, max: 2, interval: 0.5
f.adapter Faraday.default_adapter
end
response = conn.post do |req|
req.url "/chat/completions"
req.["Content-Type"] = "application/json"
req.["Authorization"] = "Bearer #{api_key}"
req.["HTTP-Referer"] = REFERER
req.["X-Title"] = APP_TITLE
req.body = {
model: model,
messages: [{ role: "user", content: prompt }],
max_tokens: max_tokens,
temperature: temperature
}.to_json
end
if response.success?
data = JSON.parse(response.body)
data.dig("choices", 0, "message", "content").to_s.strip
else
"Error: #{response.status} - #{response.body}"
end
rescue => e
"Error: #{e.message}"
end
|