Class: LLM::Anthropic
- Defined in:
- lib/llm/providers/anthropic.rb,
lib/llm/providers/anthropic/files.rb,
lib/llm/providers/anthropic/format.rb,
lib/llm/providers/anthropic/models.rb,
lib/llm/providers/anthropic/error_handler.rb,
lib/llm/providers/anthropic/stream_parser.rb
Overview
The Anthropic class implements a provider for Anthropic.
Defined Under Namespace
Modules: Response Classes: Files, Models
Constant Summary collapse
- HOST =
"api.anthropic.com"
Instance Method Summary collapse
-
#web_search(query:) ⇒ LLM::Response
A convenience method for performing a web search using the Anthropic web search tool.
-
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API.
-
#models ⇒ LLM::Anthropic::Models
Provides an interface to Anthropic’s models API.
-
#files ⇒ LLM::Anthropic::Files
Provides an interface to Anthropic’s files API.
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#default_model ⇒ String
Returns the default model for chat completions.
-
#tools ⇒ String => LLM::Tool
-
#initialize ⇒ Anthropic
constructor
A new instance of Anthropic.
Methods inherited from Provider
#audio, #chat, #chat!, clients, #embed, #images, #inspect, #moderations, mutex, #respond, #respond!, #responses, #schema, #tool, #vector_stores, #with
Constructor Details
Instance Method Details
#web_search(query:) ⇒ LLM::Response
A convenience method for performing a web search using the Anthropic web search tool.
111 112 113 114 |
# File 'lib/llm/providers/anthropic.rb', line 111 def web_search(query:) complete(query, tools: [tools[:web_search]]) .extend(LLM::Anthropic::Response::WebSearch) end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/llm/providers/anthropic.rb', line 44 def complete(prompt, params = {}) params = {role: :user, model: default_model, max_tokens: 1024}.merge!(params) params = [params, format_tools(params)].inject({}, &:merge!).compact role, stream = params.delete(:role), params.delete(:stream) params[:stream] = true if stream.respond_to?(:<<) || stream == true 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 = execute(request: req, stream:) LLM::Response.new(res).extend(LLM::Anthropic::Response::Completion) end |
#models ⇒ LLM::Anthropic::Models
Provides an interface to Anthropic’s models API
61 62 63 |
# File 'lib/llm/providers/anthropic.rb', line 61 def models LLM::Anthropic::Models.new(self) end |
#files ⇒ LLM::Anthropic::Files
Provides an interface to Anthropic’s files API
69 70 71 |
# File 'lib/llm/providers/anthropic.rb', line 69 def files LLM::Anthropic::Files.new(self) end |
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
75 76 77 |
# File 'lib/llm/providers/anthropic.rb', line 75 def assistant_role "assistant" end |
#default_model ⇒ String
Returns the default model for chat completions
83 84 85 |
# File 'lib/llm/providers/anthropic.rb', line 83 def default_model "claude-sonnet-4-20250514" end |
#tools ⇒ String => LLM::Tool
This method includes certain tools that require configuration through a set of options that are easier to set through the LLM::Provider#tool method.
94 95 96 97 98 99 100 |
# File 'lib/llm/providers/anthropic.rb', line 94 def tools { bash: tool(:bash, type: "bash_20250124"), web_search: tool(:web_search, type: "web_search_20250305", max_uses: 5), text_editor: tool(:str_replace_based_edit_tool, type: "text_editor_20250728", max_characters: 10_000) } end |