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
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API.
-
#chat(prompt, params = {}) ⇒ LLM::Session
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::Gemini::Images
Returns an interface to the images API.
-
#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.
-
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API.
-
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API.
-
#persist! ⇒ LLM::Provider
This method configures a provider to use a persistent connection pool via the optional dependency [Net::HTTP::Persistent](github.com/drbrain/net-http-persistent).
-
#respond(prompt, params = {}) ⇒ LLM::Session
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 a thread-local tracer.
-
#tracer=(tracer) ⇒ void
Set a thread-local tracer.
- #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.
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
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
170 171 172 |
# File 'lib/llm/provider.rb', line 170 def assistant_role raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
134 135 136 |
# File 'lib/llm/provider.rb', line 134 def audio raise NotImplementedError end |
#chat(prompt, params = {}) ⇒ LLM::Session
Starts a new chat powered by the chat completions API
97 98 99 100 |
# File 'lib/llm/provider.rb', line 97 def chat(prompt, params = {}) role = params.delete(:role) LLM::Session.new(self, params).talk(prompt, role:) end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
88 89 90 |
# File 'lib/llm/provider.rb', line 88 def complete(prompt, params = {}) raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
177 178 179 |
# File 'lib/llm/provider.rb', line 177 def default_model raise NotImplementedError end |
#developer_role ⇒ Symbol
255 256 257 |
# File 'lib/llm/provider.rb', line 255 def developer_role :developer end |
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding
64 65 66 |
# File 'lib/llm/provider.rb', line 64 def (input, model: nil, **params) raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
141 142 143 |
# File 'lib/llm/provider.rb', line 141 def files raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Gemini::Images
Returns an interface to the images API
127 128 129 |
# File 'lib/llm/provider.rb', line 127 def images raise NotImplementedError 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 |
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API
148 149 150 |
# File 'lib/llm/provider.rb', line 148 def models raise NotImplementedError end |
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API
155 156 157 |
# File 'lib/llm/provider.rb', line 155 def moderations raise NotImplementedError end |
#persist! ⇒ LLM::Provider
This method configures a provider to use a persistent connection pool via the optional dependency [Net::HTTP::Persistent](github.com/drbrain/net-http-persistent)
305 306 307 308 309 310 |
# File 'lib/llm/provider.rb', line 305 def persist! client = persistent_client lock do tap { @client = client } end end |
#respond(prompt, params = {}) ⇒ LLM::Session
Starts a new chat powered by the responses API
108 109 110 111 |
# File 'lib/llm/provider.rb', line 108 def respond(prompt, params = {}) role = params.delete(:role) LLM::Session.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.
120 121 122 |
# File 'lib/llm/provider.rb', line 120 def responses raise NotImplementedError end |
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema
184 185 186 |
# File 'lib/llm/provider.rb', line 184 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.
227 228 229 |
# File 'lib/llm/provider.rb', line 227 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.
210 211 212 |
# File 'lib/llm/provider.rb', line 210 def server_tools {} end |
#system_role ⇒ Symbol
249 250 251 |
# File 'lib/llm/provider.rb', line 249 def system_role :system end |
#tool_role ⇒ Symbol
261 262 263 |
# File 'lib/llm/provider.rb', line 261 def tool_role :tool end |
#tracer ⇒ LLM::Tracer
Returns a thread-local tracer
268 269 270 |
# File 'lib/llm/provider.rb', line 268 def tracer weakmap[self] || LLM::Tracer::Null.new(self) end |
#tracer=(tracer) ⇒ void
This method returns an undefined value.
Set a thread-local tracer
286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/llm/provider.rb', line 286 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 |
#user_role ⇒ Symbol
243 244 245 |
# File 'lib/llm/provider.rb', line 243 def user_role :user end |
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API
162 163 164 |
# File 'lib/llm/provider.rb', line 162 def vector_stores raise NotImplementedError end |
#web_search(query:) ⇒ LLM::Response
Provides a web search capability
237 238 239 |
# File 'lib/llm/provider.rb', line 237 def web_search(query:) raise NotImplementedError end |
#with(headers:) ⇒ LLM::Provider
Add one or more headers to all requests
198 199 200 201 202 |
# File 'lib/llm/provider.rb', line 198 def with(headers:) lock do tap { @headers.merge!(headers) } end end |