Class: LLM::Anthropic
- Extended by:
- Utils
- Defined in:
- lib/llm/providers/anthropic.rb,
lib/llm/providers/anthropic/files.rb,
lib/llm/providers/anthropic/utils.rb,
lib/llm/providers/anthropic/models.rb,
lib/llm/providers/anthropic/error_handler.rb,
lib/llm/providers/anthropic/stream_parser.rb,
lib/llm/providers/anthropic/request_adapter.rb,
lib/llm/providers/anthropic/response_adapter.rb
Overview
The Anthropic class implements a provider for Anthropic.
Defined Under Namespace
Modules: Utils 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.
-
#name ⇒ Symbol
Returns the provider's name.
-
#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.
- #adapt_function(fn) ⇒ Hash
-
#tool_role ⇒ Symbol
Anthropic expects tool results to be sent as user messages containing
tool_resultcontent blocks rather than a distincttoolrole. -
#default_model ⇒ String
Returns the default model for chat completions.
- #server_tools ⇒ String => LLM::ServerTool
-
#initialize ⇒ Anthropic
constructor
A new instance of Anthropic.
Methods included from Utils
name_of, normalize_base_path, object_id, record?, resolve_option, timestamp, uuidv7?
Methods inherited from Provider
#assistant_role, #audio, #build_messages, #chat, #developer_role, #embed, #host, #images, #inspect, #interrupt!, #key?, #moderations, #ocr, #port, #registry, #request_owner, #respond, #responses, #retry_budget, #retry_errors, #schema, #server_tool, #system_role, #tracer, #tracer=, #user_role, #vector_stores, #with, #with_tracer
Constructor Details
Instance Method Details
#web_search(query:) ⇒ LLM::Response
A convenience method for performing a web search using the Anthropic web search tool.
130 131 132 |
# File 'lib/llm/providers/anthropic.rb', line 130 def web_search(query:) ResponseAdapter.adapt(complete(query, tools: [server_tools[:web_search]]), type: :web_search) end |
#name ⇒ Symbol
Returns the provider's name
38 39 40 |
# File 'lib/llm/providers/anthropic.rb', line 38 def name :anthropic end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
52 53 54 55 56 57 58 59 60 |
# File 'lib/llm/providers/anthropic.rb', line 52 def complete(prompt, params = {}) params, stream, tools, role = normalize_complete_params(params) req = build_complete_request(prompt, params, role) res, span, tracer, request_id = execute(request: req, stream: stream, operation: "chat", model: params[:model]) res = ResponseAdapter.adapt(res, type: :completion) .extend(Module.new { define_method(:__tools__) { tools } }) tracer.on_request_finish(operation: "chat", model: params[:model], res:, span:, request_id:) res 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 |
#files ⇒ LLM::Anthropic::Files
Provides an interface to Anthropic's files API
74 75 76 |
# File 'lib/llm/providers/anthropic.rb', line 74 def files LLM::Anthropic::Files.new(self) end |
#adapt_function(fn) ⇒ Hash
81 82 83 84 85 86 87 |
# File 'lib/llm/providers/anthropic.rb', line 81 def adapt_function(fn) { name: fn.name, description: fn.description, input_schema: fn.params.to_h }.compact end |
#tool_role ⇒ Symbol
Anthropic expects tool results to be sent as user messages
containing tool_result content blocks rather than a distinct
tool role.
94 95 96 |
# File 'lib/llm/providers/anthropic.rb', line 94 def tool_role :user end |
#default_model ⇒ String
Returns the default model for chat completions
102 103 104 |
# File 'lib/llm/providers/anthropic.rb', line 102 def default_model "claude-opus-4-8" end |
#server_tools ⇒ String => LLM::ServerTool
This method includes certain tools that require configuration through a set of options that are easier to set through the LLM::Provider#server_tool method.
113 114 115 116 117 118 119 |
# File 'lib/llm/providers/anthropic.rb', line 113 def server_tools { bash: server_tool(:bash, type: "bash_20250124"), web_search: server_tool(:web_search, type: "web_search_20250305", max_uses: 5), text_editor: server_tool(:str_replace_based_edit_tool, type: "text_editor_20250728", max_characters: 10_000) } end |