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

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, stream: nil) ⇒ Object



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
# File 'lib/active_harness/providers/anthropic.rb', line 11

def call(model:, messages:, temperature: 0.7, stream: nil)
  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

  headers = {
    "Content-Type"      => "application/json",
    "x-api-key"         => api_key,
    "anthropic-version" => ANTHROPIC_VERSION
  }

  return call_streaming(url: config.anthropic_api_url, headers: headers, body: body, stream: stream, provider: :anthropic, model: model) if stream

  raw  = post_json(URI(config.anthropic_api_url), headers: headers, 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