Class: LLM::Anthropic
- 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"
Instance Method Summary collapse
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
- #complete(prompt, role = :user, **params) ⇒ LLM::Response::Completion
-
#embed(input, token:, **params) ⇒ LLM::Response::Embedding
Provides an embedding via VoyageAI per [Anthropic’s recommendation](docs.anthropic.com/en/docs/build-with-claude/embeddings).
-
#initialize(secret) ⇒ Anthropic
constructor
A new instance of Anthropic.
-
#models ⇒ Hash<String, LLM::Model>
Returns a hash of available models.
Methods included from Format
Methods inherited from Provider
Methods included from HTTPClient
Constructor Details
Instance Method Details
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
51 52 53 |
# File 'lib/llm/providers/anthropic.rb', line 51 def assistant_role "assistant" end |
#complete(prompt, role = :user, **params) ⇒ LLM::Response::Completion
40 41 42 43 44 45 46 47 |
# File 'lib/llm/providers/anthropic.rb', line 40 def complete(prompt, role = :user, **params) params = {max_tokens: 1024, model: "claude-3-5-sonnet-20240620"}.merge!(params) req = Net::HTTP::Post.new("/v1/messages", headers) = [*(params.delete(:messages) || []), Message.new(role, prompt)] req.body = JSON.dump({messages: format()}.merge!(params)) res = request(@http, req) Response::Completion.new(res).extend(response_parser) end |
#embed(input, token:, **params) ⇒ LLM::Response::Embedding
Provides an embedding via VoyageAI per [Anthropic’s recommendation](docs.anthropic.com/en/docs/build-with-claude/embeddings)
30 31 32 33 |
# File 'lib/llm/providers/anthropic.rb', line 30 def (input, token:, **params) llm = LLM.voyageai(token) llm.(input, **params) end |
#models ⇒ Hash<String, LLM::Model>
Returns a hash of available models
57 58 59 |
# File 'lib/llm/providers/anthropic.rb', line 57 def models @models ||= load_models!("anthropic") end |