Class: Contai::Providers::Claude

Inherits:
Base
  • Object
show all
Includes:
HTTParty
Defined in:
lib/contai/providers/claude.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Contai::Providers::Base

Instance Method Details

#generate(prompt) ⇒ Object



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
# File 'lib/contai/providers/claude.rb', line 10

def generate(prompt)
  response = self.class.post("/messages",
    headers: {
      "x-api-key" => @options[:api_key],
      "Content-Type" => "application/json",
      "anthropic-version" => "2023-06-01"
    },
    body: {
      model: @options[:model] || "claude-3-sonnet-20240229",
      max_tokens: @options[:max_tokens] || 1000,
      messages: [
        { role: "user", content: prompt }
      ]
    }.to_json,
    timeout: @options[:timeout] || Contai.configuration.timeout
  )

  if response.success?
    content = response.parsed_response.dig("content", 0, "text")
    success_result(content)
  else
    error_result("Claude API error: #{response.code} #{response.message}")
  end
rescue => e
  error_result(e.message)
end