Class: LLM::Anthropic

Inherits:
Provider show all
Includes:
Format
Defined in:
lib/llm/providers/anthropic.rb,
lib/llm/providers/anthropic/format.rb,
lib/llm/providers/anthropic/models.rb,
lib/llm/providers/anthropic/error_handler.rb,
lib/llm/providers/anthropic/stream_parser.rb

Overview

The Anthropic class implements a provider for [Anthropic](www.anthropic.com)

Defined Under Namespace

Modules: Format, Response Classes: ErrorHandler, Models, StreamParser

Constant Summary collapse

HOST =
"api.anthropic.com"

Instance Method Summary collapse

Methods included from Format

#format

Methods inherited from Provider

#audio, #chat, #chat!, #embed, #files, #images, #inspect, #moderations, #respond, #respond!, #responses, #schema, #vector_stores, #with

Constructor Details

#initializeAnthropic

Returns a new instance of Anthropic.

Parameters:

  • key (String, nil)

    The secret key for authentication



19
20
21
# File 'lib/llm/providers/anthropic.rb', line 19

def initialize(**)
  super(host: HOST, **)
end

Instance Method Details

#assistant_roleString

Returns the role of the assistant in the conversation. Usually “assistant” or “model”

Returns:

  • (String)

    Returns the role of the assistant in the conversation. Usually “assistant” or “model”



56
57
58
# File 'lib/llm/providers/anthropic.rb', line 56

def assistant_role
  "assistant"
end

#complete(prompt, params = {}) ⇒ LLM::Response

Provides an interface to the chat completions API

Examples:

llm = LLM.openai(ENV["KEY"])
messages = [{role: "system", content: "Your task is to answer all of my questions"}]
res = llm.complete("5 + 2 ?", messages:)
print "[#{res.choices[0].role}]", res.choices[0].content, "\n"

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Returns:

Raises:

See Also:



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/llm/providers/anthropic.rb', line 33

def complete(prompt, params = {})
  params = {role: :user, model: default_model, max_tokens: 1024}.merge!(params)
  params = [params, format_tools(params)].inject({}, &:merge!).compact
  role, stream = params.delete(:role), params.delete(:stream)
  params[:stream] = true if stream.respond_to?(:<<) || stream == true
  req = Net::HTTP::Post.new("/v1/messages", headers)
  messages = [*(params.delete(:messages) || []), Message.new(role, prompt)]
  body = JSON.dump({messages: [format(messages)].flatten}.merge!(params))
  set_body_stream(req, StringIO.new(body))
  res = execute(request: req, stream:)
  LLM::Response.new(res).extend(LLM::Anthropic::Response::Completion)
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



64
65
66
# File 'lib/llm/providers/anthropic.rb', line 64

def default_model
  "claude-sonnet-4-20250514"
end

#modelsLLM::Anthropic::Models

Provides an interface to Anthropic’s models API



50
51
52
# File 'lib/llm/providers/anthropic.rb', line 50

def models
  LLM::Anthropic::Models.new(self)
end