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/response_parser.rb

Overview

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

Defined Under Namespace

Modules: Format, ResponseParser Classes: ErrorHandler, Models

Constant Summary collapse

HOST =
"api.anthropic.com"

Instance Method Summary collapse

Methods included from Format

#format

Methods inherited from Provider

#audio, #chat, #chat!, #files, #images, #inspect, #respond, #respond!, #responses, #schema

Constructor Details

#initialize(secret) ⇒ Anthropic

Returns a new instance of Anthropic.

Parameters:

  • secret (String)

    The secret key for authentication



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

def initialize(secret, **)
  super(secret, 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”



72
73
74
# File 'lib/llm/providers/anthropic.rb', line 72

def assistant_role
  "assistant"
end

#complete(prompt, role = :user, model: default_model, max_tokens: 1024, **params) ⇒ LLM::Response::Completion

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"},
  {role: "system", content: "Your answers should be short and concise"},
]
res = llm.complete("Hello. What is the answer to 5 + 2 ?", :user, messages:)
print "[#{res.choices[0].role}]", res.choices[0].content, "\n"

Parameters:

  • max_tokens (defaults to: 1024)

    The maximum number of tokens to generate

  • prompt (String)

    The input prompt to be completed

  • role (Symbol) (defaults to: :user)

    The role of the prompt (e.g. :user, :system)

  • model (String) (defaults to: default_model)

    The model to use for the completion

  • params (Hash)

    Other completion parameters

Returns:

Raises:

See Also:



52
53
54
55
56
57
58
59
60
# File 'lib/llm/providers/anthropic.rb', line 52

def complete(prompt, role = :user, model: default_model, max_tokens: 1024, **params)
  params = {max_tokens:, model:}.merge!(params)
  req = Net::HTTP::Post.new("/v1/messages", headers)
  messages = [*(params.delete(:messages) || []), Message.new(role, prompt)]
  body = JSON.dump({messages: format(messages)}.merge!(params))
  set_body_stream(req, StringIO.new(body))
  res = request(@http, req)
  Response::Completion.new(res).extend(response_parser)
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



80
81
82
# File 'lib/llm/providers/anthropic.rb', line 80

def default_model
  "claude-3-5-sonnet-20240620"
end

#embed(input, token:, model: "voyage-2", **params) ⇒ LLM::Response::Embedding

Provides an embedding via VoyageAI per [Anthropic’s recommendation](docs.anthropic.com/en/docs/build-with-claude/embeddings)

Parameters:

  • token (String)

    Valid token for the VoyageAI API

  • model (String) (defaults to: "voyage-2")

    The embedding model to use

  • params (Hash)

    Other embedding parameters

  • input (String, Array<String>)

    The input to embed

Returns:

Raises:



34
35
36
37
# File 'lib/llm/providers/anthropic.rb', line 34

def embed(input, token:, model: "voyage-2", **params)
  llm = LLM.voyageai(token)
  llm.embed(input, **params.merge(model:))
end

#modelsLLM::Anthropic::Models

Provides an interface to Anthropic’s models API



66
67
68
# File 'lib/llm/providers/anthropic.rb', line 66

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