Class: ActiveHarness::Providers::Anthropic

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

Overview

Anthropic Claude — native Messages API (not OpenAI-compatible). docs.anthropic.com/en/api/messages

Constant Summary collapse

API_URL =
URI("https://api.anthropic.com/v1/messages")
ANTHROPIC_VERSION =
"2023-06-01"
DEFAULT_MAX_TOKENS =
1024

Constants inherited from Base

Base::HTTP, Base::STREAMING_HTTP

Instance Method Summary collapse

Instance Method Details

#call(model:, messages:, temperature: 0.7) ⇒ 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
# File 'lib/active_harness/providers/anthropic.rb', line 12

def call(model:, messages:, temperature: 0.7)
  system_msg, chat_messages = extract_system(messages)

  body = {
    model:      model,
    max_tokens: DEFAULT_MAX_TOKENS,
    temperature: temperature,
    messages:   chat_messages
  }
  body[:system] = system_msg if system_msg

  raw  = post_json(API_URL,
    headers: {
      "Content-Type"      => "application/json",
      "x-api-key"         => api_key,
      "anthropic-version" => ANTHROPIC_VERSION
    },
    body: body
  )
  data = parse!(raw)
  handle_error!(data)

  {
    content:  data.dig("content", 0, "text").to_s.strip,
    provider: :anthropic,
    model:    data["model"] || model,
    usage:    extract_usage_anthropic(data)
  }
end