Class: LLM::Anthropic
- 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
Defined Under Namespace
Classes: Models
Constant Summary collapse
- HOST =
"api.anthropic.com"
Instance Method Summary collapse
-
#initialize ⇒ Anthropic
constructor
A new instance of Anthropic.
-
#embed(input, key:, model: "voyage-2", **params) ⇒ LLM::Response::Embedding
Provides an embedding via VoyageAI per Anthropic’s recommendation.
-
#complete(prompt, params = {}) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API.
-
#models ⇒ LLM::Anthropic::Models
Provides an interface to Anthropic’s models API.
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#default_model ⇒ String
Returns the default model for chat completions.
Methods inherited from Provider
#audio, #chat, #chat!, #files, #images, #inspect, #respond, #respond!, #responses, #schema, #with
Constructor Details
Instance Method Details
#embed(input, key:, model: "voyage-2", **params) ⇒ LLM::Response::Embedding
Provides an embedding via VoyageAI per Anthropic’s recommendation
35 36 37 38 |
# File 'lib/llm/providers/anthropic.rb', line 35 def (input, key:, model: "voyage-2", **params) llm = LLM.voyageai(key:) llm.(input, **params.merge(model:)) end |
#complete(prompt, params = {}) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/llm/providers/anthropic.rb', line 50 def complete(prompt, params = {}) params = {role: :user, model: default_model, max_tokens: 1024}.merge!(params) params = [params, format_tools(params)].inject({}, &:merge!).compact role = params.delete(:role) req = Net::HTTP::Post.new("/v1/messages", headers) = [*(params.delete(:messages) || []), Message.new(role, prompt)] body = JSON.dump({messages: [format()].flatten}.merge!(params)) set_body_stream(req, StringIO.new(body)) res = request(@http, req) Response::Completion.new(res).extend(response_parser) end |
#models ⇒ LLM::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 |
#assistant_role ⇒ 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 |
#default_model ⇒ String
Returns the default model for chat completions
80 81 82 |
# File 'lib/llm/providers/anthropic.rb', line 80 def default_model "claude-3-5-sonnet-20240620" end |