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.
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API.
-
#build_messages(prompt, params, role, key: :messages) ⇒ Array<LLM::Message>
Builds the outgoing message array for a turn.
-
#chat(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the chat completions API.
-
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API.
-
#default_model ⇒ String
Returns the default model for chat completions.
- #developer_role ⇒ Symbol
-
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding.
-
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API.
-
#images ⇒ LLM::OpenAI::Images, LLM::Google::Images
Returns an interface to the images API.
-
#initialize(key:, host:, port: 443, timeout: 600, read_timeout: nil, connect_timeout: 5, ssl: true, base_path: "", persistent: false, transport: nil) ⇒ Provider
constructor
A new instance of Provider.
-
#inspect ⇒ String
Returns an inspection of the provider object.
-
#interrupt!(owner) ⇒ nil
(also: #cancel!)
Interrupt the active request, if any.
-
#key? ⇒ Boolean
Returns true when an API key is configured.
-
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API.
-
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API.
-
#name ⇒ Symbol
Returns the provider's name.
- #ocr ⇒ LLM::Response
-
#registry ⇒ LLM::Registry
Returns the provider's model registry.
-
#request_owner ⇒ Object
private
Returns the current request owner used by the transport.
-
#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.
-
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema.
-
#server_tool(name, options = {}) ⇒ LLM::ServerTool
Returns a tool provided by a provider.
-
#server_tools ⇒ String => LLM::ServerTool
Returns all known tools provided by a provider.
- #system_role ⇒ Symbol
- #tool_role ⇒ Symbol
-
#tracer ⇒ LLM::Tracer
Returns the current scoped tracer override or provider default tracer.
-
#tracer=(tracer) ⇒ void
Set the provider's default tracer This tracer is shared by the provider instance and becomes the fallback whenever no scoped override is active.
- #user_role ⇒ Symbol
-
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API.
-
#web_search(query:) ⇒ LLM::Response
Provides a web search capability.
-
#with(**headers) ⇒ LLM::Provider
Add one or more headers to all requests.
-
#with_tracer(tracer) { ... } ⇒ Object
Override the tracer for the current fiber while the block runs.
Constructor Details
#initialize(key:, host:, port: 443, timeout: 600, read_timeout: nil, connect_timeout: 5, ssl: true, base_path: "", persistent: false, transport: nil) ⇒ Provider
Returns a new instance of Provider.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/llm/provider.rb', line 35 def initialize(key:, host:, port: 443, timeout: 600, read_timeout: nil, connect_timeout: 5, ssl: true, base_path: "", persistent: false, transport: nil) @key = key @host = host @port = port @read_timeout = read_timeout || timeout @timeout = @read_timeout @connect_timeout = connect_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}"} @monitor = Monitor.new @transport = LLM::Transport::Utils.resolve_transport( host:, port:, timeout: @read_timeout, connect_timeout: @connect_timeout, ssl:, transport:, persistent: ) end |
Instance Method Details
#adapt_function(fn) ⇒ Hash
Adapt a Function to the provider-specific tool schema.
425 426 427 |
# File 'lib/llm/provider.rb', line 425 def adapt_function(fn) raise NotImplementedError end |
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually "assistant" or "model"
247 248 249 |
# File 'lib/llm/provider.rb', line 247 def assistant_role raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
211 212 213 |
# File 'lib/llm/provider.rb', line 211 def audio 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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/llm/provider.rb', line 77 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 |
#chat(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the chat completions API
174 175 176 177 |
# File 'lib/llm/provider.rb', line 174 def chat(prompt, params = {}) role = params.delete(:role) LLM::Context.new(self, params).talk(prompt, role:) end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API. Most users should use Context#talk or Agent#talk instead.
165 166 167 |
# File 'lib/llm/provider.rb', line 165 def complete(prompt, params = {}) raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
254 255 256 |
# File 'lib/llm/provider.rb', line 254 def default_model raise NotImplementedError end |
#developer_role ⇒ Symbol
338 339 340 |
# File 'lib/llm/provider.rb', line 338 def developer_role :developer end |
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding
129 130 131 |
# File 'lib/llm/provider.rb', line 129 def (input, model: nil, **params) raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
218 219 220 |
# File 'lib/llm/provider.rb', line 218 def files raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Google::Images
Returns an interface to the images API
204 205 206 |
# File 'lib/llm/provider.rb', line 204 def images raise NotImplementedError end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
98 99 100 |
# File 'lib/llm/provider.rb', line 98 def inspect "#<#{LLM::Utils.object_id(self)} @key=[REDACTED] @transport=#{transport.inspect} @tracer=#{tracer.inspect}>" end |
#interrupt!(owner) ⇒ nil Also known as: cancel!
Interrupt the active request, if any.
399 400 401 |
# File 'lib/llm/provider.rb', line 399 def interrupt!(owner) transport.interrupt!(owner) end |
#key? ⇒ Boolean
Returns true when an API key is configured
415 416 417 |
# File 'lib/llm/provider.rb', line 415 def key? @key != nil && @key.to_s.strip.size > 0 end |
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API
225 226 227 |
# File 'lib/llm/provider.rb', line 225 def models raise NotImplementedError end |
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API
232 233 234 |
# File 'lib/llm/provider.rb', line 232 def moderations raise NotImplementedError end |
#name ⇒ Symbol
Returns the provider's name
107 108 109 |
# File 'lib/llm/provider.rb', line 107 def name 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.
139 140 141 |
# File 'lib/llm/provider.rb', line 139 def ocr(...) raise NotImplementedError end |
#registry ⇒ LLM::Registry
Returns the provider's model registry.
114 115 116 |
# File 'lib/llm/provider.rb', line 114 def registry LLM.registry_for(self) 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.
408 409 410 |
# File 'lib/llm/provider.rb', line 408 def request_owner transport.request_owner end |
#respond(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the responses API
185 186 187 188 |
# File 'lib/llm/provider.rb', line 185 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.
197 198 199 |
# File 'lib/llm/provider.rb', line 197 def responses raise NotImplementedError end |
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema
261 262 263 |
# File 'lib/llm/provider.rb', line 261 def schema LLM::Schema.new 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.
310 311 312 |
# File 'lib/llm/provider.rb', line 310 def server_tool(name, = {}) LLM::ServerTool.new(name, , self) 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.
293 294 295 |
# File 'lib/llm/provider.rb', line 293 def server_tools {} end |
#system_role ⇒ Symbol
332 333 334 |
# File 'lib/llm/provider.rb', line 332 def system_role :system end |
#tool_role ⇒ Symbol
344 345 346 |
# File 'lib/llm/provider.rb', line 344 def tool_role :tool end |
#tracer ⇒ LLM::Tracer
Returns the current scoped tracer override or provider default tracer
351 352 353 |
# File 'lib/llm/provider.rb', line 351 def tracer weakmap[self] || @tracer || LLM::Tracer::Null.new(self) end |
#tracer=(tracer) ⇒ void
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.
365 366 367 |
# File 'lib/llm/provider.rb', line 365 def tracer=(tracer) @tracer = tracer || LLM::Tracer::Null.new(self) end |
#user_role ⇒ Symbol
326 327 328 |
# File 'lib/llm/provider.rb', line 326 def user_role :user end |
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API
239 240 241 |
# File 'lib/llm/provider.rb', line 239 def vector_stores raise NotImplementedError end |
#web_search(query:) ⇒ LLM::Response
Provides a web search capability
320 321 322 |
# File 'lib/llm/provider.rb', line 320 def web_search(query:) raise NotImplementedError end |
#with(**headers) ⇒ LLM::Provider
For backwards compatibility, headers can be
provided via the headers: keyword argument,
or provided directly as a Hash without the
headers: key namespace.
Add one or more headers to all requests
280 281 282 283 284 285 |
# File 'lib/llm/provider.rb', line 280 def with(**headers) headers = headers.merge(headers.delete(:headers) || {}) lock do tap { @headers.merge!(headers) } end 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.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/llm/provider.rb', line 380 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 |