Class: Prescient::Client

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

client = Prescient::Client.new(:openai)
response = client.generate_response("Hello, world!")
embedding = client.generate_embedding("Text to embed")

Using default provider

client = Prescient::Client.new  # Uses configured default
puts client.provider_name       # => :ollama (or configured default)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_name = nil, enable_fallback: true, provider_options: {}) ⇒ Client

Initialize a new client with the specified provider

Parameters:

  • provider_name (Symbol, nil) (defaults to: nil)

    Name of provider to use, or nil for default

  • enable_fallback (Boolean) (defaults to: true)

    Whether to enable automatic fallback to other providers

  • provider_options (Hash) (defaults to: {})

    Temporary options for the selected provider

Raises:



32
33
34
35
36
37
38
# File 'lib/prescient/client.rb', line 32

def initialize(provider_name = nil, enable_fallback: true, provider_options: {})
  @provider_name = provider_name || Prescient.configuration.default_provider
  @provider = provider_with_options(@provider_name, provider_options)
  @enable_fallback = enable_fallback

  raise Prescient::Error, "Provider not configured: #{@provider_name}" unless @provider
end

Instance Attribute Details

#providerPrescient::Base (readonly)

Returns The underlying provider instance.

Returns:



24
25
26
# File 'lib/prescient/client.rb', line 24

def provider
  @provider
end

#provider_nameSymbol (readonly)

Returns The name of the provider being used.

Returns:

  • (Symbol)

    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

Returns:

  • (Boolean)

    true if the provider currently passes its availability check



94
95
96
# File 'lib/prescient/client.rb', line 94

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.

Parameters:

  • text (String)

    The text to generate embeddings for

  • options (Hash)

    Provider-specific options

Returns:

  • (Array<Float>)

    Array of embedding values

Raises:



50
51
52
53
54
55
56
57
58
# File 'lib/prescient/client.rb', line 50

def generate_embedding(text, **options)
  if @enable_fallback
    with_fallback_handling(:generate_embedding, text, **options)
  else
    with_error_handling do
      @provider.generate_embedding(text, **options)
    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.

Parameters:

  • prompt (String)

    The prompt to generate a response for

  • context_items (Array<Hash, String>) (defaults to: [])

    Optional context items

  • options (Hash)

    Provider-specific generation options

Options Hash (**options):

  • :temperature (Float)

    Sampling temperature (0.0-2.0)

  • :max_tokens (Integer)

    Maximum tokens to generate

  • :top_p (Float)

    Nucleus sampling parameter

Returns:

  • (Hash)

    Response hash with :response, :model, :provider keys

Raises:



74
75
76
77
78
79
80
81
82
# File 'lib/prescient/client.rb', line 74

def generate_response(prompt, context_items = [], **options)
  if @enable_fallback
    with_fallback_handling(:generate_response, prompt, context_items, **options)
  else
    with_error_handling do
      @provider.generate_response(prompt, context_items, **options)
    end
  end
end

#health_checkHash

Check the health status of the provider

Returns:

  • (Hash)

    Health status information from the selected provider



87
88
89
# File 'lib/prescient/client.rb', line 87

def health_check
  @provider.health_check
end

#provider_infoHash

Get comprehensive information about the provider

Returns details about the provider including its availability and configuration options (with sensitive data removed).

Returns:

  • (Hash)

    Provider information including :name, :class, :available, and recursively sanitized :options



105
106
107
108
109
110
111
112
# File 'lib/prescient/client.rb', line 105

def provider_info
  {
    name:      @provider_name,
    class:     @provider.class.name.split('::').last,
    available: available?,
    options:   sanitize_options(@provider.options),
  }
end