Class: CompletionKit::AnthropicClient

Inherits:
LlmClient
  • Object
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

Constants inherited from LlmClient

LlmClient::DEFAULT_TEMPERATURE, LlmClient::REFUSAL_PHRASE, LlmClient::TEMPERATURE_REFUSAL

Instance Method Summary collapse

Methods inherited from LlmClient

for_model, for_provider, #initialize, #temperature_dropped?

Constructor Details

This class inherits a constructor from CompletionKit::LlmClient

Instance Method Details

#available_modelsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/completion_kit/anthropic_client.rb', line 47

def available_models
  return STATIC_MODELS unless configured?

  response = build_connection("https://api.anthropic.com").get("/v1/models?limit=100") do |req|
    req.headers["x-api-key"] = api_key
    req.headers["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_errorsObject



68
69
70
71
72
# File 'app/services/completion_kit/anthropic_client.rb', line 68

def configuration_errors
  errors = []
  errors << "Anthropic API key is not configured" unless api_key.present?
  errors
end

#configured?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/services/completion_kit/anthropic_client.rb', line 64

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
# File 'app/services/completion_kit/anthropic_client.rb', line 9

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 = resolve_temperature(options)

  response = post_messages(model: model, prompt: prompt, max_tokens: max_tokens, temperature: temperature)

  if response.status == 400 && !temperature.nil? && 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