Class: LLM::Provider Abstract
- Inherits:
-
Object
- Object
- LLM::Provider
- Includes:
- Transport::Execution
- Defined in:
- lib/llm/provider.rb
Overview
Instance Method Summary collapse
-
#adapt_function(fn) ⇒ Hash
abstract
Adapt a Function to the provider-specific tool schema.
-
#build_messages(prompt, params, role, key: :messages) ⇒ Array<LLM::Message>
Builds the outgoing message array for a turn.
-
#inspect ⇒ String
Returns an inspection of the provider object.
-
#name ⇒ Symbol
Returns the provider's name.
-
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding.
- #ocr ⇒ LLM::Response
-
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API.
-
#chat(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the chat completions API.
-
#respond(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the responses API.
-
#responses ⇒ LLM::OpenAI::Responses
Compared to the chat completions API, the responses API can require less bandwidth on each turn, maintain state server-side, and produce faster responses.
-
#images ⇒ LLM::OpenAI::Images, LLM::Google::Images
Returns an interface to the images API.
-
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API.
-
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API.
-
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API.
-
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API.
-
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API.
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#default_model ⇒ String
Returns the default model for chat completions.
-
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema.
-
#with(headers:) ⇒ LLM::Provider
Add one or more headers to all requests.
-
#server_tools ⇒ String => LLM::ServerTool
Returns all known tools provided by a provider.
-
#server_tool(name, options = {}) ⇒ LLM::ServerTool
Returns a tool provided by a provider.
-
#web_search(query:) ⇒ LLM::Response
Provides a web search capability.
- #user_role ⇒ Symbol
- #system_role ⇒ Symbol
- #developer_role ⇒ Symbol
- #tool_role ⇒ Symbol
-
#tracer ⇒ LLM::Tracer
Returns the current scoped tracer override or provider default tracer.
-
#tracer=(tracer)
Set the provider's default tracer This tracer is shared by the provider instance and becomes the fallback whenever no scoped override is active.
-
#with_tracer(tracer) { ... } ⇒ Object
Override the tracer for the current fiber while the block runs.
-
#interrupt!(owner) ⇒ nil
(also: #cancel!)
Interrupt the active request, if any.
-
#request_owner ⇒ Object
private
Returns the current request owner used by the transport.
-
#key? ⇒ Boolean
Returns true when an API key is configured.
-
#initialize(key:, host:, port: 443, timeout: 900, ssl: true, base_path: "", persistent: false, transport: nil) ⇒ Provider
constructor
A new instance of Provider.
Constructor Details
#initialize(key:, host:, port: 443, timeout: 900, ssl: true, base_path: "", persistent: false, transport: nil) ⇒ Provider
Returns a new instance of Provider.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/llm/provider.rb', line 30 def initialize(key:, host:, port: 443, timeout: 900, ssl: true, base_path: "", persistent: false, transport: nil) @key = key @host = host @port = port @timeout = timeout @ssl = ssl @base_path = LLM::Utils.normalize_base_path(base_path) @base_uri = URI("#{ssl ? "https" : "http"}://#{host}:#{port}/") @headers = {"User-Agent" => "llm.rb v#{LLM::VERSION}"} @transport = LLM::Transport::Utils.resolve_transport(host:, port:, timeout:, ssl:, transport:, persistent:) @monitor = Monitor.new end |
Instance Method Details
#adapt_function(fn) ⇒ Hash
Adapt a Function to the provider-specific tool schema.
397 398 399 |
# File 'lib/llm/provider.rb', line 397 def adapt_function(fn) raise NotImplementedError end |
#build_messages(prompt, params, role, key: :messages) ⇒ Array<LLM::Message>
Builds the outgoing message array for a turn. Normalizes the prompt into one or more Message objects and prepends the existing history.
The method is idempotent. If the prompt is already an Message or an array of Messages (ie it was built by a previous call and possibly transformed), it is returned as-is without rebuilding.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/llm/provider.rb', line 62 def (prompt, params, role, key: :messages) case prompt when LLM::Message [prompt] when Array if prompt.all? { LLM::Message === _1 } prompt else [*(params.delete(key) || []), LLM::Message.new(role, prompt)] end when LLM::Prompt [*(params.delete(key) || []), *prompt.to_a] else [*(params.delete(key) || []), LLM::Message.new(role, prompt)] end end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
83 84 85 |
# File 'lib/llm/provider.rb', line 83 def inspect "#<#{LLM::Utils.object_id(self)} @key=[REDACTED] @transport=#{transport.inspect} @tracer=#{tracer.inspect}>" end |
#name ⇒ Symbol
Returns the provider's name
92 93 94 |
# File 'lib/llm/provider.rb', line 92 def name raise NotImplementedError end |
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding
107 108 109 |
# File 'lib/llm/provider.rb', line 107 def (input, model: nil, **params) raise NotImplementedError end |
#ocr ⇒ LLM::Response
This feature is not implemented by all providers, and it will raise NotImplementedError for providers that do not support it.
117 118 119 |
# File 'lib/llm/provider.rb', line 117 def ocr(...) raise NotImplementedError end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API. Most users should use Context#talk or Agent#talk instead.
143 144 145 |
# File 'lib/llm/provider.rb', line 143 def complete(prompt, params = {}) raise NotImplementedError end |
#chat(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the chat completions API
152 153 154 155 |
# File 'lib/llm/provider.rb', line 152 def chat(prompt, params = {}) role = params.delete(:role) LLM::Context.new(self, params).talk(prompt, role:) end |
#respond(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the responses API
163 164 165 166 |
# File 'lib/llm/provider.rb', line 163 def respond(prompt, params = {}) role = params.delete(:role) LLM::Context.new(self, params).respond(prompt, role:) end |
#responses ⇒ LLM::OpenAI::Responses
Compared to the chat completions API, the responses API can require less bandwidth on each turn, maintain state server-side, and produce faster responses.
175 176 177 |
# File 'lib/llm/provider.rb', line 175 def responses raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Google::Images
Returns an interface to the images API
182 183 184 |
# File 'lib/llm/provider.rb', line 182 def images raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
189 190 191 |
# File 'lib/llm/provider.rb', line 189 def audio raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
196 197 198 |
# File 'lib/llm/provider.rb', line 196 def files raise NotImplementedError end |
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API
203 204 205 |
# File 'lib/llm/provider.rb', line 203 def models raise NotImplementedError end |
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API
210 211 212 |
# File 'lib/llm/provider.rb', line 210 def moderations raise NotImplementedError end |
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API
217 218 219 |
# File 'lib/llm/provider.rb', line 217 def vector_stores raise NotImplementedError end |
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually "assistant" or "model"
225 226 227 |
# File 'lib/llm/provider.rb', line 225 def assistant_role raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
232 233 234 |
# File 'lib/llm/provider.rb', line 232 def default_model raise NotImplementedError end |
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema
239 240 241 |
# File 'lib/llm/provider.rb', line 239 def schema LLM::Schema.new end |
#with(headers:) ⇒ LLM::Provider
Add one or more headers to all requests
253 254 255 256 257 |
# File 'lib/llm/provider.rb', line 253 def with(headers:) lock do tap { @headers.merge!(headers) } end end |
#server_tools ⇒ String => LLM::ServerTool
This method might be outdated, and the LLM::Provider#server_tool method can be used if a tool is not found here.
Returns all known tools provided by a provider.
265 266 267 |
# File 'lib/llm/provider.rb', line 265 def server_tools {} end |
#server_tool(name, options = {}) ⇒ LLM::ServerTool
OpenAI, Anthropic, and Gemini provide platform-tools for things like web search, and more.
Returns a tool provided by a provider.
282 283 284 |
# File 'lib/llm/provider.rb', line 282 def server_tool(name, = {}) LLM::ServerTool.new(name, , self) end |
#web_search(query:) ⇒ LLM::Response
Provides a web search capability
292 293 294 |
# File 'lib/llm/provider.rb', line 292 def web_search(query:) raise NotImplementedError end |
#user_role ⇒ Symbol
298 299 300 |
# File 'lib/llm/provider.rb', line 298 def user_role :user end |
#system_role ⇒ Symbol
304 305 306 |
# File 'lib/llm/provider.rb', line 304 def system_role :system end |
#developer_role ⇒ Symbol
310 311 312 |
# File 'lib/llm/provider.rb', line 310 def developer_role :developer end |
#tool_role ⇒ Symbol
316 317 318 |
# File 'lib/llm/provider.rb', line 316 def tool_role :tool end |
#tracer ⇒ LLM::Tracer
Returns the current scoped tracer override or provider default tracer
323 324 325 |
# File 'lib/llm/provider.rb', line 323 def tracer weakmap[self] || @tracer || LLM::Tracer::Null.new(self) end |
#tracer=(tracer)
This method returns an undefined value.
Set the provider's default tracer This tracer is shared by the provider instance and becomes the fallback whenever no scoped override is active.
337 338 339 |
# File 'lib/llm/provider.rb', line 337 def tracer=(tracer) @tracer = tracer || LLM::Tracer::Null.new(self) end |
#with_tracer(tracer) { ... } ⇒ Object
Override the tracer for the current fiber while the block runs. This is useful when you want per-request or per-turn tracing without replacing the provider's default tracer.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/llm/provider.rb', line 352 def with_tracer(tracer) had_override = weakmap.key?(self) previous = weakmap[self] weakmap[self] = tracer || LLM::Tracer::Null.new(self) yield ensure if had_override weakmap[self] = previous elsif weakmap.respond_to?(:delete) weakmap.delete(self) else weakmap[self] = nil end end |
#interrupt!(owner) ⇒ nil Also known as: cancel!
Interrupt the active request, if any.
371 372 373 |
# File 'lib/llm/provider.rb', line 371 def interrupt!(owner) transport.interrupt!(owner) end |
#request_owner ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the current request owner used by the transport.
380 381 382 |
# File 'lib/llm/provider.rb', line 380 def request_owner transport.request_owner end |
#key? ⇒ Boolean
Returns true when an API key is configured
387 388 389 |
# File 'lib/llm/provider.rb', line 387 def key? @key != nil && @key.to_s.strip.size > 0 end |