Class: CompletionKit::AnthropicClient
- Inherits:
-
LlmClient
- Object
- LlmClient
- CompletionKit::AnthropicClient
show all
- Defined in:
- app/services/completion_kit/anthropic_client.rb
Constant Summary
collapse
- STATIC_MODELS =
[
{ id: "claude-3-7-sonnet-latest", name: "Claude 3.7 Sonnet" },
{ id: "claude-3-5-haiku-latest", name: "Claude 3.5 Haiku" }
].freeze
Instance Method Summary
collapse
Methods inherited from LlmClient
for_model, for_provider, #initialize
Instance Method Details
#available_models ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'app/services/completion_kit/anthropic_client.rb', line 40
def available_models
return STATIC_MODELS unless configured?
response = build_connection("https://api.anthropic.com").get("/v1/models?limit=100") do |req|
req.["x-api-key"] = api_key
req.["anthropic-version"] = "2023-06-01"
end
return STATIC_MODELS unless response.success?
entries = JSON.parse(response.body).fetch("data", [])
models = entries.map { |entry| { id: entry["id"], name: entry["display_name"] || entry["id"] } }
models.presence || STATIC_MODELS
rescue StandardError
STATIC_MODELS
end
|
#configuration_errors ⇒ Object
61
62
63
64
65
|
# File 'app/services/completion_kit/anthropic_client.rb', line 61
def configuration_errors
errors = []
errors << "Anthropic API key is not configured" unless api_key.present?
errors
end
|
57
58
59
|
# File 'app/services/completion_kit/anthropic_client.rb', line 57
def configured?
api_key.present?
end
|
#generate_completion(prompt, options = {}) ⇒ Object
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
|
# File 'app/services/completion_kit/anthropic_client.rb', line 8
def generate_completion(prompt, options = {})
return "Error: API key not configured" unless configured?
model = options[:model] || "claude-3-7-sonnet-latest"
max_tokens = options[:max_tokens] || 1000
temperature = options[:temperature] || 0.7
response = build_connection("https://api.anthropic.com").post do |req|
req.url "/v1/messages"
req.["Content-Type"] = "application/json"
req.["x-api-key"] = api_key
req.["anthropic-version"] = "2023-06-01"
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["content"][0]["text"].strip
else
"Error: #{response.status} - #{response.body}"
end
rescue => e
"Error: #{e.message}"
end
|