Class: Prescient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/prescient/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_name = nil) ⇒ Client

Returns a new instance of Client.

Raises:



8
9
10
11
12
13
# File 'lib/prescient/client.rb', line 8

def initialize(provider_name = nil)
  @provider_name = provider_name || Prescient.configuration.default_provider
  @provider = Prescient.configuration.provider(@provider_name)

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



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

def method_missing(method_name, ...)
  if @provider.respond_to?(method_name)
    @provider.send(method_name, ...)
  else
    super
  end
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/prescient/client.rb', line 6

def provider
  @provider
end

#provider_nameObject (readonly)

Returns the value of attribute provider_name.



5
6
7
# File 'lib/prescient/client.rb', line 5

def provider_name
  @provider_name
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/prescient/client.rb', line 39

def available?
  @provider.available?
end

#generate_embedding(text, **options) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/prescient/client.rb', line 15

def generate_embedding(text, **options)
  with_error_handling do
    if options.any?
      @provider.generate_embedding(text, **options)
    else
      @provider.generate_embedding(text)
    end
  end
end

#generate_response(prompt, context_items = [], **options) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/prescient/client.rb', line 25

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

#health_checkObject



35
36
37
# File 'lib/prescient/client.rb', line 35

def health_check
  @provider.health_check
end

#provider_infoObject



43
44
45
46
47
48
49
50
# File 'lib/prescient/client.rb', line 43

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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/prescient/client.rb', line 60

def respond_to_missing?(method_name, include_private = false)
  @provider.respond_to?(method_name, include_private) || super
end