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.
-
#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 an LLM tracer.
-
#tracer=(tracer)
Set the 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 44 |
# 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 @tracer = LLM::Tracer::Null.new(self) @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"
171 172 173 |
# File 'lib/llm/provider.rb', line 171 def assistant_role raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
135 136 137 |
# File 'lib/llm/provider.rb', line 135 def audio raise NotImplementedError end |
#chat(prompt, params = {}) ⇒ LLM::Session
Starts a new chat powered by the chat completions API
98 99 100 101 |
# File 'lib/llm/provider.rb', line 98 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
89 90 91 |
# File 'lib/llm/provider.rb', line 89 def complete(prompt, params = {}) raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
178 179 180 |
# File 'lib/llm/provider.rb', line 178 def default_model raise NotImplementedError end |
#developer_role ⇒ Symbol
256 257 258 |
# File 'lib/llm/provider.rb', line 256 def developer_role :developer end |
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding
65 66 67 |
# File 'lib/llm/provider.rb', line 65 def (input, model: nil, **params) raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
142 143 144 |
# File 'lib/llm/provider.rb', line 142 def files raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Gemini::Images
Returns an interface to the images API
128 129 130 |
# File 'lib/llm/provider.rb', line 128 def images raise NotImplementedError end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
50 51 52 |
# File 'lib/llm/provider.rb', line 50 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
149 150 151 |
# File 'lib/llm/provider.rb', line 149 def models raise NotImplementedError end |
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API
156 157 158 |
# File 'lib/llm/provider.rb', line 156 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
299 300 301 302 303 304 |
# File 'lib/llm/provider.rb', line 299 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
109 110 111 112 |
# File 'lib/llm/provider.rb', line 109 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.
121 122 123 |
# File 'lib/llm/provider.rb', line 121 def responses raise NotImplementedError end |
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema
185 186 187 |
# File 'lib/llm/provider.rb', line 185 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.
228 229 230 |
# File 'lib/llm/provider.rb', line 228 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.
211 212 213 |
# File 'lib/llm/provider.rb', line 211 def server_tools {} end |
#system_role ⇒ Symbol
250 251 252 |
# File 'lib/llm/provider.rb', line 250 def system_role :system end |
#tool_role ⇒ Symbol
262 263 264 |
# File 'lib/llm/provider.rb', line 262 def tool_role :tool end |
#tracer ⇒ LLM::Tracer
Returns an LLM tracer
269 270 271 |
# File 'lib/llm/provider.rb', line 269 def tracer @tracer end |
#tracer=(tracer)
This method returns an undefined value.
Set the tracer
282 283 284 285 286 287 288 289 290 |
# File 'lib/llm/provider.rb', line 282 def tracer=(tracer) lock do @tracer = if tracer.nil? LLM::Tracer::Null.new(self) else tracer end end end |
#user_role ⇒ Symbol
244 245 246 |
# File 'lib/llm/provider.rb', line 244 def user_role :user end |
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API
163 164 165 |
# File 'lib/llm/provider.rb', line 163 def vector_stores raise NotImplementedError end |
#web_search(query:) ⇒ LLM::Response
Provides a web search capability
238 239 240 |
# File 'lib/llm/provider.rb', line 238 def web_search(query:) raise NotImplementedError end |
#with(headers:) ⇒ LLM::Provider
Add one or more headers to all requests
199 200 201 202 203 |
# File 'lib/llm/provider.rb', line 199 def with(headers:) lock do tap { @headers.merge!(headers) } end end |