Class: LLM::Provider Abstract
- Inherits:
-
Object
- Object
- LLM::Provider
- Defined in:
- lib/llm/provider.rb
Overview
This class is not meant to be instantiated directly. Instead, use one of the subclasses that implement the methods defined here.
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: nil, **params) ⇒ LLM::Chat
Starts a new lazy chat powered by the chat completions API.
-
#chat!(prompt, role = :user, model: nil, **params) ⇒ LLM::Chat
Starts a new chat powered by the chat completions API.
-
#complete(prompt, role = :user, model:, **params) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API.
-
#embed(input, model:, **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 ⇒ Hash<String, LLM::Model>
Returns a hash of available models.
-
#respond(prompt, role = :user, model: nil, **params) ⇒ LLM::Chat
Starts a new lazy chat powered by the responses API.
-
#respond!(prompt, role = :user, model: 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.
Constructor Details
#initialize(secret, host:, port: 443, timeout: 60, ssl: true) ⇒ Provider
Returns a new instance of Provider.
29 30 31 32 33 34 35 |
# File 'lib/llm/provider.rb', line 29 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”
185 186 187 |
# File 'lib/llm/provider.rb', line 185 def assistant_role raise NotImplementedError end |
#audio ⇒ LLM::OpenAI::Audio
Returns an interface to the audio API
170 171 172 |
# File 'lib/llm/provider.rb', line 170 def audio raise NotImplementedError end |
#chat(prompt, role = :user, model: 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
97 98 99 |
# File 'lib/llm/provider.rb', line 97 def chat(prompt, role = :user, model: nil, **params) LLM::Chat.new(self, params).lazy.chat(prompt, role) end |
#chat!(prompt, role = :user, model: 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
113 114 115 |
# File 'lib/llm/provider.rb', line 113 def chat!(prompt, role = :user, model: nil, **params) LLM::Chat.new(self, params).chat(prompt, role) end |
#complete(prompt, role = :user, model:, **params) ⇒ LLM::Response::Completion
Provides an interface to the chat completions API
81 82 83 |
# File 'lib/llm/provider.rb', line 81 def complete(prompt, role = :user, model:, **params) raise NotImplementedError end |
#embed(input, model:, **params) ⇒ LLM::Response::Embedding
Provides an embedding
56 57 58 |
# File 'lib/llm/provider.rb', line 56 def (input, model:, **params) raise NotImplementedError end |
#files ⇒ LLM::OpenAI::Files
Returns an interface to the files API
177 178 179 |
# File 'lib/llm/provider.rb', line 177 def files raise NotImplementedError end |
#images ⇒ LLM::OpenAI::Images, LLM::Gemini::Images
Returns an interface to the images API
163 164 165 |
# File 'lib/llm/provider.rb', line 163 def images raise NotImplementedError end |
#inspect ⇒ String
The secret key is redacted in inspect for security reasons
Returns an inspection of the provider object
41 42 43 |
# File 'lib/llm/provider.rb', line 41 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} @secret=[REDACTED] @http=#{@http.inspect}>" end |
#models ⇒ Hash<String, LLM::Model>
Returns a hash of available models
192 193 194 |
# File 'lib/llm/provider.rb', line 192 def models raise NotImplementedError end |
#respond(prompt, role = :user, model: 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
129 130 131 |
# File 'lib/llm/provider.rb', line 129 def respond(prompt, role = :user, model: nil, **params) LLM::Chat.new(self, params).lazy.respond(prompt, role) end |
#respond!(prompt, role = :user, model: 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
145 146 147 |
# File 'lib/llm/provider.rb', line 145 def respond!(prompt, role = :user, model: nil, **params) LLM::Chat.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.
156 157 158 |
# File 'lib/llm/provider.rb', line 156 def responses raise NotImplementedError end |