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/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

Constant Summary collapse

HOST =
"api.anthropic.com"
DEFAULT_PARAMS =
{max_tokens: 1024, model: "claude-3-5-sonnet-20240620"}.freeze

Instance Method Summary collapse

Methods included from Format

#format

Methods inherited from Provider

#chat, #chat!, #inspect

Methods included from HTTPClient

#request

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

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

Parameters:

  • prompt (String)

    The input prompt to be completed

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

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

Returns:

See Also:



38
39
40
41
42
43
44
45
46
# File 'lib/llm/providers/anthropic.rb', line 38

def complete(prompt, role = :user, **params)
  req = Net::HTTP::Post.new ["/v1", "messages"].join("/")
  messages = [*(params.delete(:messages) || []), Message.new(role, prompt)]
  params = DEFAULT_PARAMS.merge(params)
  body = {messages: format(messages)}.merge!(params)
  req = preflight(req, body)
  res = request(@http, req)
  Response::Completion.new(res).extend(response_parser)
end

#embed(input, **params) ⇒ LLM::Response::Embedding

Parameters:

  • input (String)

    The input to embed

Returns:



25
26
27
28
29
30
31
# File 'lib/llm/providers/anthropic.rb', line 25

def embed(input, **params)
  req = Net::HTTP::Post.new ["api.voyageai.com/v1", "embeddings"].join("/")
  body = {input:, model: "voyage-2"}.merge!(params)
  req = preflight(req, body)
  res = request(@http, req)
  Response::Embedding.new(res).extend(response_parser)
end