Class: Prescient::Client
- Inherits:
-
Object
- Object
- Prescient::Client
- Defined in:
- lib/prescient/client.rb
Overview
Client class for interacting with AI providers
The Client provides a high-level interface for working with AI providers, handling error recovery, retries, and method delegation. It acts as a facade over the configured providers.
Instance Attribute Summary collapse
-
#provider ⇒ Prescient::Base
readonly
The underlying provider instance.
-
#provider_name ⇒ Symbol
readonly
The name of the provider being used.
Instance Method Summary collapse
-
#available? ⇒ Boolean
Check if the provider is currently available.
-
#generate_embedding(text, **options) ⇒ Array<Float>
Generate embeddings for the given text.
-
#generate_response(prompt, context_items = [], **options) ⇒ Hash
Generate text response for the given prompt.
-
#health_check ⇒ Hash
Check the health status of the provider.
-
#initialize(provider_name = nil, enable_fallback: true) ⇒ Client
constructor
Initialize a new client with the specified provider.
-
#provider_info ⇒ Hash
Get comprehensive information about the provider.
Constructor Details
#initialize(provider_name = nil, enable_fallback: true) ⇒ Client
Initialize a new client with the specified provider
31 32 33 34 35 36 37 |
# File 'lib/prescient/client.rb', line 31 def initialize(provider_name = nil, enable_fallback: true) @provider_name = provider_name || Prescient.configuration.default_provider @provider = Prescient.configuration.provider(@provider_name) @enable_fallback = enable_fallback raise Prescient::Error, "Provider not configured: #{@provider_name}" unless @provider end |
Instance Attribute Details
#provider ⇒ Prescient::Base (readonly)
Returns The underlying provider instance.
24 25 26 |
# File 'lib/prescient/client.rb', line 24 def provider @provider end |
#provider_name ⇒ Symbol (readonly)
Returns The name of the provider being used.
21 22 23 |
# File 'lib/prescient/client.rb', line 21 def provider_name @provider_name end |
Instance Method Details
#available? ⇒ Boolean
Check if the provider is currently available
93 94 95 |
# File 'lib/prescient/client.rb', line 93 def available? @provider.available? end |
#generate_embedding(text, **options) ⇒ Array<Float>
Generate embeddings for the given text
Delegates to the underlying provider with automatic retry logic for transient failures. If fallback is enabled, tries other providers on persistent failures.
49 50 51 52 53 54 55 56 57 |
# File 'lib/prescient/client.rb', line 49 def (text, **) if @enable_fallback with_fallback_handling(:generate_embedding, text, **) else with_error_handling do @provider.(text, **) end end end |
#generate_response(prompt, context_items = [], **options) ⇒ Hash
Generate text response for the given prompt
Delegates to the underlying provider with automatic retry logic for transient failures. Supports optional context items for RAG. If fallback is enabled, tries other providers on persistent failures.
73 74 75 76 77 78 79 80 81 |
# File 'lib/prescient/client.rb', line 73 def generate_response(prompt, context_items = [], **) if @enable_fallback with_fallback_handling(:generate_response, prompt, context_items, **) else with_error_handling do @provider.generate_response(prompt, context_items, **) end end end |
#health_check ⇒ Hash
Check the health status of the provider
86 87 88 |
# File 'lib/prescient/client.rb', line 86 def health_check @provider.health_check end |
#provider_info ⇒ Hash
Get comprehensive information about the provider
Returns details about the provider including its availability and configuration options (with sensitive data removed).
104 105 106 107 108 109 110 111 |
# File 'lib/prescient/client.rb', line 104 def provider_info { name: @provider_name, class: @provider.class.name.split('::').last, available: available?, options: (@provider.), } end |