Class: LLM::Anthropic

Inherits:
Provider show all
Defined in:
lib/llm/providers/anthropic.rb,
lib/llm/providers/anthropic/format.rb,
lib/llm/providers/anthropic/error_handler.rb,
lib/llm/providers/anthropic/response_parser.rb

Overview

The Anthropic class implements a provider for Anthropic

Constant Summary collapse

HOST =
"api.anthropic.com"

Instance Method Summary collapse

Methods inherited from Provider

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

Constructor Details

#initialize(secret) ⇒ Anthropic

Returns a new instance of Anthropic.

Parameters:

  • secret (String)

    The secret key for authentication



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

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

Instance Method Details

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

Provides an embedding via VoyageAI per Anthropic’s recommendation

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:



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

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

#complete(prompt, role = :user, model: "claude-3-5-sonnet-20240620", 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: "claude-3-5-sonnet-20240620")

    The model to use for the completion

  • params (Hash)

    Other completion parameters

Returns:

Raises:

See Also:



49
50
51
52
53
54
55
56
# File 'lib/llm/providers/anthropic.rb', line 49

def complete(prompt, role = :user, model: "claude-3-5-sonnet-20240620", 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)]
  req.body = JSON.dump({messages: format(messages)}.merge!(params))
  res      = request(@http, req)
  Response::Completion.new(res).extend(response_parser)
end

#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”



60
61
62
# File 'lib/llm/providers/anthropic.rb', line 60

def assistant_role
  "assistant"
end

#modelsHash<String, LLM::Model>

Returns a hash of available models

Returns:

  • (Hash<String, LLM::Model>)

    Returns a hash of available models



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

def models
  @models ||= load_models!("anthropic")
end