Class: LLM::Provider Abstract
- Inherits:
-
Object
- Object
- LLM::Provider
- Defined in:
- lib/llm/provider.rb
Overview
The Provider class represents an abstract class for LLM (Language Model) providers.
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, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
Starts a new lazy chat powered by the chat completions API.
-
#chat!(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
Starts a new chat powered by the chat completions API.
-
#complete(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Response::Completion
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::Embedding
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(secret, host:, port: 443, timeout: 60, ssl: true) ⇒ 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.
-
#respond(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
Starts a new lazy chat powered by the responses API.
-
#respond!(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
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 ⇒ JSON::Schema
Returns an object that can generate a JSON schema.
Constructor Details
#initialize(secret, host:, port: 443, timeout: 60, ssl: true) ⇒ Provider
Returns a new instance of Provider.
20 21 22 23 24 25 26 |
# File 'lib/llm/provider.rb', line 20 def initialize(secret, host:, port: 443, timeout: 60, ssl: true) @secret = secret @http = Net::HTTP.new(host, port).tap do |http| http.use_ssl = ssl http.read_timeout = timeout end end |
Instance Method Details
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
189 190 191 |
# File 'lib/llm/provider.rb', line 189 def assistant_role raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
167 168 169 |
# File 'lib/llm/provider.rb', line 167 def audio raise NotImplementedError end |
#chat(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
This method creates a lazy version of a LLM::Chat object.
Starts a new lazy chat powered by the chat completions API
91 92 93 |
# File 'lib/llm/provider.rb', line 91 def chat(prompt, role = :user, model: default_model, schema: nil, **params) LLM::Chat.new(self, **params.merge(model:, schema:)).lazy.chat(prompt, role) end |
#chat!(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
This method creates a non-lazy version of a LLM::Chat object.
Starts a new chat powered by the chat completions API
108 109 110 |
# File 'lib/llm/provider.rb', line 108 def chat!(prompt, role = :user, model: default_model, schema: nil, **params) LLM::Chat.new(self, **params.merge(model:, schema:)).chat(prompt, role) end |
#complete(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API
74 75 76 |
# File 'lib/llm/provider.rb', line 74 def complete(prompt, role = :user, model: default_model, schema: nil, **params) raise NotImplementedError end |
#default_model ⇒ String
Returns the default model for chat completions
196 197 198 |
# File 'lib/llm/provider.rb', line 196 def default_model raise NotImplementedError end |
#embed(input, model: nil, **params) ⇒ LLM::Response::Embedding
Provides an embedding
47 48 49 |
# File 'lib/llm/provider.rb', line 47 def (input, model: nil, **params) raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
174 175 176 |
# File 'lib/llm/provider.rb', line 174 def files raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Gemini::Images
Returns an interface to the images API
160 161 162 |
# File 'lib/llm/provider.rb', line 160 def images raise NotImplementedError end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
32 33 34 |
# File 'lib/llm/provider.rb', line 32 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} @secret=[REDACTED] @http=#{@http.inspect}>" end |
#models ⇒ LLM::OpenAI::Models
Returns an interface to the models API
181 182 183 |
# File 'lib/llm/provider.rb', line 181 def models raise NotImplementedError end |
#respond(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
This method creates a lazy variant of a LLM::Chat object.
Starts a new lazy chat powered by the responses API
125 126 127 |
# File 'lib/llm/provider.rb', line 125 def respond(prompt, role = :user, model: default_model, schema: nil, **params) LLM::Chat.new(self, **params.merge(model:, schema:)).lazy.respond(prompt, role) end |
#respond!(prompt, role = :user, model: default_model, schema: nil, **params) ⇒ LLM::Chat
This method creates a non-lazy variant of a LLM::Chat object.
Starts a new chat powered by the responses API
142 143 144 |
# File 'lib/llm/provider.rb', line 142 def respond!(prompt, role = :user, model: default_model, schema: nil, **params) LLM::Chat.new(self, **params.merge(model:, schema:)).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.
153 154 155 |
# File 'lib/llm/provider.rb', line 153 def responses raise NotImplementedError end |
#schema ⇒ JSON::Schema
Returns an object that can generate a JSON schema
203 204 205 206 207 208 |
# File 'lib/llm/provider.rb', line 203 def schema @schema ||= begin require_relative "../json/schema" JSON::Schema.new end end |