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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'app/services/completion_kit/anthropic_client.rb', line 50
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
71
72
73
74
75
|
# File 'app/services/completion_kit/anthropic_client.rb', line 71
def configuration_errors
errors = []
errors << "Anthropic API key is not configured" unless api_key.present?
errors
end
|
67
68
69
|
# File 'app/services/completion_kit/anthropic_client.rb', line 67
def configured?
api_key.present?
end
|
#generate_completion(prompt, options = {}) ⇒ Object
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 'app/services/completion_kit/anthropic_client.rb', line 12
def generate_completion(prompt, options = {})
@temperature_dropped = false
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 = post_messages(model: model, prompt: prompt, max_tokens: max_tokens, temperature: temperature)
if response.status == 400 && temperature_unsupported?(response.body)
@temperature_dropped = true
response = post_messages(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: "anthropic",
status: 429,
retry_after: nil
)
end
if response.success?
data = JSON.parse(response.body)
data["content"][0]["text"].strip
else
"Error: #{response.status} - #{response.body}"
end
rescue CompletionKit::RateLimitError
raise
rescue Faraday::Error
raise
rescue => e
"Error: #{e.message}"
end
|
#temperature_dropped? ⇒ Boolean
8
9
10
|
# File 'app/services/completion_kit/anthropic_client.rb', line 8
def temperature_dropped?
@temperature_dropped == true
end
|