Class: Spacy::AnthropicClient

Inherits:
LLMClientBase show all
Defined in:
lib/ruby-spacy/anthropic_client.rb

Overview

A lightweight Anthropic Messages API client without external dependencies.

Constant Summary collapse

API_ENDPOINT =

Default Anthropic API endpoint

"https://api.anthropic.com/v1"
ANTHROPIC_VERSION =

Messages API version header value

"2023-06-01"
DEFAULT_MODEL =

Default model for chat requests

"claude-sonnet-5"
APIError =

Raised when an LLM API request fails after retries; carries the HTTP status code and the parsed response body when available.

LLMClientBase::APIError

Constants inherited from LLMClientBase

LLMClientBase::BASE_RETRY_DELAY, LLMClientBase::DEFAULT_TIMEOUT, LLMClientBase::MAX_RETRIES

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, timeout: DEFAULT_TIMEOUT, base_url: API_ENDPOINT) ⇒ AnthropicClient

Returns a new instance of AnthropicClient.

Parameters:

  • access_token (String)

    Anthropic API key

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    request timeout in seconds

  • base_url (String) (defaults to: API_ENDPOINT)

    API endpoint override



20
21
22
23
# File 'lib/ruby-spacy/anthropic_client.rb', line 20

def initialize(access_token:, timeout: DEFAULT_TIMEOUT, base_url: API_ENDPOINT)
  super(base_url: base_url, timeout: timeout)
  @access_token = access_token
end

Instance Method Details

#messages(model:, messages:, system: nil, max_tokens: 1000, temperature: nil, output_config: nil) ⇒ Hash

Sends a Messages API request.

The temperature parameter is sent only when explicitly given. Current Claude models (Opus 4.7+) reject it; the request is then retried once without it.

Parameters:

  • model (String)

    The model to use (e.g., "claude-sonnet-5")

  • messages (Array<Hash>)

    The conversation messages

  • system (String, nil) (defaults to: nil)

    System prompt (top-level parameter)

  • max_tokens (Integer) (defaults to: 1000)

    Maximum tokens in the response (required by the API)

  • temperature (Float, nil) (defaults to: nil)

    Sampling temperature (omitted when nil)

  • output_config (Hash, nil) (defaults to: nil)

    Output configuration, e.g. { format: { type: "json_schema", schema: ... } } for structured outputs

Returns:

  • (Hash)

    The API response



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby-spacy/anthropic_client.rb', line 39

def messages(model:, messages:, system: nil, max_tokens: 1000, temperature: nil, output_config: nil)
  body = {
    model: model,
    max_tokens: max_tokens,
    messages: messages
  }
  body[:system] = system if system
  body[:temperature] = temperature unless temperature.nil?
  body[:output_config] = output_config if output_config

  post("/messages", body)
rescue APIError => e
  raise unless body.key?(:temperature) && e.status_code == 400 && e.message.match?(/temperature/i)

  warn "Warning: model does not support the temperature parameter; retrying without it"
  post("/messages", body.reject { |key, _| key == :temperature })
end