Class: Durable::Llm::Client
- Inherits:
-
Object
- Object
- Durable::Llm::Client
- Defined in:
- lib/durable/llm/client.rb
Overview
Unified interface for interacting with different LLM providers
The Client class provides a facade that delegates operations like completion, chat, embedding, and streaming to the appropriate provider instance while handling parameter processing, model configuration, and providing convenience methods for quick text completion. The client automatically resolves provider classes based on the provider name and manages default parameters including model selection.
Instance Attribute Summary collapse
-
#model ⇒ String?
The default model to use for requests.
-
#provider ⇒ Object
readonly
The underlying provider instance.
Instance Method Summary collapse
-
#chat(params = {}) ⇒ Object
Performs a chat completion request (alias for completion).
-
#completion(params = {}) ⇒ Object
Performs a completion request.
-
#default_params ⇒ Hash
Returns the default parameters to merge with request options.
-
#embed(params = {}) ⇒ Object
Performs an embedding request.
-
#initialize(provider_name, options = {}) ⇒ Client
constructor
Initializes a new LLM client for the specified provider.
-
#quick_complete(text, _opts = {}) ⇒ String
Performs a quick text completion with minimal configuration.
-
#stream(params = {}) {|Object| ... } ⇒ Object
Performs a streaming completion request.
-
#stream? ⇒ Boolean
Checks if the provider supports streaming.
Constructor Details
#initialize(provider_name, options = {}) ⇒ Client
Initializes a new LLM client for the specified provider
37 38 39 40 41 42 43 |
# File 'lib/durable/llm/client.rb', line 37 def initialize(provider_name, = {}) @model = .delete('model') || .delete(:model) if .key?('model') || .key?(:model) provider_class = Durable::Llm::Providers.provider_class_for(provider_name) @provider = provider_class.new(**) end |
Instance Attribute Details
#model ⇒ String?
Returns The default model to use for requests.
27 28 29 |
# File 'lib/durable/llm/client.rb', line 27 def model @model end |
#provider ⇒ Object (readonly)
Returns The underlying provider instance.
24 25 26 |
# File 'lib/durable/llm/client.rb', line 24 def provider @provider end |
Instance Method Details
#chat(params = {}) ⇒ Object
Performs a chat completion request (alias for completion)
89 90 91 |
# File 'lib/durable/llm/client.rb', line 89 def chat(params = {}) @provider.completion(process_params(params)) end |
#completion(params = {}) ⇒ Object
Performs a completion request
80 81 82 |
# File 'lib/durable/llm/client.rb', line 80 def completion(params = {}) @provider.completion(process_params(params)) end |
#default_params ⇒ Hash
Returns the default parameters to merge with request options
48 49 50 |
# File 'lib/durable/llm/client.rb', line 48 def default_params @model ? { model: @model } : {} end |
#embed(params = {}) ⇒ Object
Performs an embedding request
99 100 101 102 103 |
# File 'lib/durable/llm/client.rb', line 99 def (params = {}) @provider.(**process_params(params)) rescue NotImplementedError raise NotImplementedError, "#{@provider.class.name} does not support embeddings" end |
#quick_complete(text, _opts = {}) ⇒ String
Performs a quick text completion with minimal configuration
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/durable/llm/client.rb', line 60 def quick_complete(text, _opts = {}) response = completion(process_params(messages: [{ role: 'user', content: text }])) choice = response.choices.first raise IndexError, 'No completion choices returned' unless choice = choice. raise NoMethodError, 'Response choice has no message' unless content = .content raise NoMethodError, 'Response message has no content' unless content content end |
#stream(params = {}) {|Object| ... } ⇒ Object
Performs a streaming completion request
112 113 114 115 116 |
# File 'lib/durable/llm/client.rb', line 112 def stream(params = {}, &block) @provider.stream(process_params(params), &block) rescue NotImplementedError raise NotImplementedError, "#{@provider.class.name} does not support streaming" end |
#stream? ⇒ Boolean
Checks if the provider supports streaming
121 122 123 |
# File 'lib/durable/llm/client.rb', line 121 def stream? @provider.stream? end |