Class: RubyLlmMesh::Providers::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm_mesh/providers/anthropic.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#complete(prompt:, system: nil, model: nil, **options) ⇒ 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
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_llm_mesh/providers/anthropic.rb', line 10

def complete(prompt:, system: nil, model: nil, **options)
  api_key = config.anthropic_api_key
  raise AuthenticationError.new("ANTHROPIC_API_KEY is not configured", provider: name) if api_key.to_s.empty?

  model ||= config.anthropic_model
  body = {
    model: model,
    max_tokens: options.fetch(:max_tokens, 1024),
    messages: [{ role: "user", content: prompt }]
  }
  body[:system] = system if system
  body[:temperature] = options[:temperature] if options.key?(:temperature)
  body[:tools] = options[:tools] if options[:tools]

  response, latency_ms = http_post(
    "#{config.anthropic_base_url.chomp('/')}/v1/messages",
    headers: {
      "x-api-key" => api_key,
      "anthropic-version" => "2023-06-01",
      "Content-Type" => "application/json"
    },
    body: body
  )

  raise_for_status!(response)
  data = parse_json(response)
  content = Array(data["content"]).map { |block| block["text"] }.compact.join

  Response.new(
    content: content,
    provider: name,
    model: data["model"] || model,
    usage: data["usage"] || {},
    raw: data,
    latency_ms: latency_ms
  )
end

#nameObject



6
7
8
# File 'lib/ruby_llm_mesh/providers/anthropic.rb', line 6

def name
  :anthropic
end