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::Bot
Starts a new lazy chat powered by the chat completions API.
-
#chat!(prompt, params = {}) ⇒ LLM::Bot
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.
-
#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.
-
#respond(prompt, params = {}) ⇒ LLM::Bot
Starts a new lazy chat powered by the responses API.
-
#respond!(prompt, params = {}) ⇒ LLM::Bot
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.
-
#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 |
# 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 : transient_client @base_uri = URI("#{ssl ? "https" : "http"}://#{host}:#{port}/") 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”
202 203 204 |
# File 'lib/llm/provider.rb', line 202 def assistant_role raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
166 167 168 |
# File 'lib/llm/provider.rb', line 166 def audio raise NotImplementedError end |
#chat(prompt, params = {}) ⇒ LLM::Bot
This method creates a lazy version of a LLM::Bot object.
Starts a new lazy 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::Bot.new(self, params).chat(prompt, role:) end |
#chat!(prompt, params = {}) ⇒ LLM::Bot
This method creates a non-lazy version of a LLM::Bot object.
Starts a new chat powered by the chat completions API
112 113 114 115 |
# File 'lib/llm/provider.rb', line 112 def chat!(prompt, params = {}) role = params.delete(:role) LLM::Bot.new(self, params).chat(prompt, role:) end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
86 87 88 |
# File 'lib/llm/provider.rb', line 86 def complete(prompt, params = {}) raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
209 210 211 |
# File 'lib/llm/provider.rb', line 209 def default_model raise NotImplementedError end |
#embed(input, model: nil, **params) ⇒ LLM::Response
Provides an embedding
62 63 64 |
# File 'lib/llm/provider.rb', line 62 def (input, model: nil, **params) raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
173 174 175 |
# File 'lib/llm/provider.rb', line 173 def files raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Gemini::Images
Returns an interface to the images API
159 160 161 |
# File 'lib/llm/provider.rb', line 159 def images raise NotImplementedError end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
47 48 49 |
# File 'lib/llm/provider.rb', line 47 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} @key=[REDACTED] @http=#{@http.inspect}>" end |
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API
180 181 182 |
# File 'lib/llm/provider.rb', line 180 def models raise NotImplementedError end |
#moderations ⇒ LLM::OpenAI::Moderations
Returns an interface to the moderations API
187 188 189 |
# File 'lib/llm/provider.rb', line 187 def moderations raise NotImplementedError end |
#respond(prompt, params = {}) ⇒ LLM::Bot
This method creates a lazy variant of a LLM::Bot object.
Starts a new lazy chat powered by the responses API
126 127 128 129 |
# File 'lib/llm/provider.rb', line 126 def respond(prompt, params = {}) role = params.delete(:role) LLM::Bot.new(self, params).respond(prompt, role:) end |
#respond!(prompt, params = {}) ⇒ LLM::Bot
This method creates a non-lazy variant of a LLM::Bot object.
Starts a new chat powered by the responses API
140 141 142 143 |
# File 'lib/llm/provider.rb', line 140 def respond!(prompt, params = {}) role = params.delete(:role) LLM::Bot.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.
152 153 154 |
# File 'lib/llm/provider.rb', line 152 def responses raise NotImplementedError end |
#schema ⇒ LLM::Schema
Returns an object that can generate a JSON schema
216 217 218 |
# File 'lib/llm/provider.rb', line 216 def schema @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.
257 258 259 |
# File 'lib/llm/provider.rb', line 257 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.
240 241 242 |
# File 'lib/llm/provider.rb', line 240 def server_tools {} end |
#vector_stores ⇒ LLM::OpenAI::VectorStore
Returns an interface to the vector stores API
194 195 196 |
# File 'lib/llm/provider.rb', line 194 def vector_stores raise NotImplementedError end |
#web_search(query:) ⇒ LLM::Response
Provides a web search capability
267 268 269 |
# File 'lib/llm/provider.rb', line 267 def web_search(query:) raise NotImplementedError end |
#with(headers:) ⇒ LLM::Provider
Add one or more headers to all requests
230 231 232 |
# File 'lib/llm/provider.rb', line 230 def with(headers:) tap { (@headers ||= {}).merge!(headers) } end |