Class: LLM::Provider Abstract
Overview
The Provider class represents an abstract class for LLM (Language Model) providers.
Constant Summary collapse
- @@clients =
-
{}
Class Method Summary collapse
- .clients ⇒ Object private
Instance Method Summary collapse
-
#persist! ⇒
LLM::Provider
This method configures a provider to use a persistent connection pool via the optional dependency Net::HTTP::Persistent.
-
#initialize(key:,
host:, port: 443, timeout: 60, ssl: true, persistent: false) ⇒
Provider constructor
A new instance of Provider.
-
#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.
-
#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 a fiber-local tracer.
-
#tracer=(tracer)
⇒ void
Set a fiber-local tracer.
Constructor Details
#initialize(key:, host:, port: 443, timeout: 60, ssl: true, persistent: false) ⇒ Provider
Returns a new instance of Provider.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/llm/provider.rb', line 33 def initialize(key:, host:, port: 443, timeout: 60, ssl: true, persistent: false) @key = key @host = host @port = port @timeout = timeout @ssl = ssl @client = persistent ? persistent_client : nil @base_uri = URI("#{ssl ? "https" : "http"}://#{host}:#{port}/") @headers = {"User-Agent" => "llm.rb v#{LLM::VERSION}"} @monitor = Monitor.new end |
Class Method Details
.clients ⇒ 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.
17 |
# File 'lib/llm/provider.rb', line 17 def self.clients = @@clients |
Instance Method Details
#persist! ⇒ LLM::Provider
This method configures a provider to use a persistent connection pool via the optional dependency Net::HTTP::Persistent
314 315 316 317 318 319 |
# File 'lib/llm/provider.rb', line 314 def persist! client = persistent_client lock do tap { @client = client } end end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
49 50 51 |
# File 'lib/llm/provider.rb', line 49 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} @key=[REDACTED] @client=#{@client.inspect} @tracer=#{tracer.inspect}>" end |
#name ⇒ Symbol
Returns the provider's name
58 59 60 |
# File 'lib/llm/provider.rb', line 58 def name raise NotImplementedError end |
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding
73 74 75 |
# File 'lib/llm/provider.rb', line 73 def (input, model: nil, **params) raise NotImplementedError end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
97 98 99 |
# File 'lib/llm/provider.rb', line 97 def complete(prompt, params = {}) raise NotImplementedError end |
#chat(prompt, params = {}) ⇒ LLM::Context
Starts a new chat powered by the chat completions API
106 107 108 109 |
# File 'lib/llm/provider.rb', line 106 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
117 118 119 120 |
# File 'lib/llm/provider.rb', line 117 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.
129 130 131 |
# File 'lib/llm/provider.rb', line 129 def responses raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Google::Images
Returns an interface to the images API
136 137 138 |
# File 'lib/llm/provider.rb', line 136 def images raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
143 144 145 |
# File 'lib/llm/provider.rb', line 143 def audio raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
150 151 152 |
# File 'lib/llm/provider.rb', line 150 def files raise NotImplementedError end |
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API
157 158 159 |
# File 'lib/llm/provider.rb', line 157 def models raise NotImplementedError end |
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API
164 165 166 |
# File 'lib/llm/provider.rb', line 164 def moderations raise NotImplementedError end |
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API
171 172 173 |
# File 'lib/llm/provider.rb', line 171 def vector_stores raise NotImplementedError end |
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually "assistant" or "model"
179 180 181 |
# File 'lib/llm/provider.rb', line 179 def assistant_role raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
186 187 188 |
# File 'lib/llm/provider.rb', line 186 def default_model raise NotImplementedError end |
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema
193 194 195 |
# File 'lib/llm/provider.rb', line 193 def schema LLM::Schema.new end |
#with(headers:) ⇒ LLM::Provider
Add one or more headers to all requests
207 208 209 210 211 |
# File 'lib/llm/provider.rb', line 207 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.
219 220 221 |
# File 'lib/llm/provider.rb', line 219 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.
236 237 238 |
# File 'lib/llm/provider.rb', line 236 def server_tool(name, = {}) LLM::ServerTool.new(name, , self) end |
#web_search(query:) ⇒ LLM::Response
Provides a web search capability
246 247 248 |
# File 'lib/llm/provider.rb', line 246 def web_search(query:) raise NotImplementedError end |
#user_role ⇒ Symbol
252 253 254 |
# File 'lib/llm/provider.rb', line 252 def user_role :user end |
#system_role ⇒ Symbol
258 259 260 |
# File 'lib/llm/provider.rb', line 258 def system_role :system end |
#developer_role ⇒ Symbol
264 265 266 |
# File 'lib/llm/provider.rb', line 264 def developer_role :developer end |
#tool_role ⇒ Symbol
270 271 272 |
# File 'lib/llm/provider.rb', line 270 def tool_role :tool end |
#tracer ⇒ LLM::Tracer
Returns a fiber-local tracer
277 278 279 |
# File 'lib/llm/provider.rb', line 277 def tracer weakmap[self] || LLM::Tracer::Null.new(self) end |
#tracer=(tracer) ⇒ void
This method returns an undefined value.
Set a fiber-local tracer
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/llm/provider.rb', line 295 def tracer=(tracer) if tracer.nil? if weakmap.respond_to?(:delete) weakmap.delete(self) else weakmap[self] = nil end else weakmap[self] = tracer end end |