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

Instance Method Summary collapse

Methods inherited from LlmClient

for_model, for_provider, #initialize

Constructor Details

This class inherits a constructor from CompletionKit::LlmClient

Instance Method Details

#available_modelsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/completion_kit/anthropic_client.rb', line 49

def available_models
  return STATIC_MODELS unless configured?

  require "faraday"
  require "faraday/retry"
  require "json"

  response = Faraday.get("https://api.anthropic.com/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



74
75
76
77
78
# File 'app/services/completion_kit/anthropic_client.rb', line 74

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

#configured?Boolean

Returns:

  • (Boolean)


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

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
39
40
41
42
43
44
45
46
47
# File 'app/services/completion_kit/anthropic_client.rb', line 8

def generate_completion(prompt, options = {})
  return "Error: API key not configured" unless configured?
  
  require "faraday"
  require "faraday/retry"
  require "json"
  
  model = options[:model] || "claude-3-7-sonnet-latest"
  max_tokens = options[:max_tokens] || 1000
  temperature = options[:temperature] || 0.7
  
  conn = Faraday.new(url: "https://api.anthropic.com") do |f|
    f.request :retry, max: 2, interval: 0.5
    f.adapter Faraday.default_adapter
  end
  
  response = conn.post do |req|
    req.url "/v1/messages"
    req.headers["Content-Type"] = "application/json"
    req.headers["x-api-key"] = api_key
    req.headers["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