Class: LastLLM::Client

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

Overview

Client for interacting with LLM providers This is the main interface for the LastLLM library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, options = {}) ⇒ Client

Initialize a new client

Parameters:

  • config (Configuration, nil) (defaults to: nil)

    The configuration to use

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

    Additional options

Options Hash (options):

  • :provider (Symbol)

    The provider to use



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/last_llm/client.rb', line 22

def initialize(config = nil, options = {})
  @configuration = case config
                   when Configuration
                     config
                   when Hash
                     Configuration.new(config)
                   else
                     # When no config provided, default to test mode in test environment
                     # Force test_mode to true when running in RSpec
                     raise ConfigurationError, 'No configuration provided' unless defined?(RSpec)
                     Configuration.new(test_mode: defined?(RSpec))
                   end

  provider_name = options[:provider] || @configuration.default_provider
  @provider = create_provider(provider_name)
end

Instance Attribute Details

#configurationObject (readonly)

Client configuration



13
14
15
# File 'lib/last_llm/client.rb', line 13

def configuration
  @configuration
end

#providerObject

Current provider instance



16
17
18
# File 'lib/last_llm/client.rb', line 16

def provider
  @provider
end

Instance Method Details

#generate_object(prompt, schema, options = {}) ⇒ Hash

Generate a structured object from a prompt

Parameters:

  • prompt (String)

    The prompt to generate the object from

  • schema (Dry::Schema::JSON)

    The schema to validate against

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

    Generation options

Options Hash (options):

  • :model (String)

    The model to use

  • :temperature (Float) — default: 0.2

    The temperature to use

Returns:

  • (Hash)

    The generated object

Raises:



62
63
64
65
# File 'lib/last_llm/client.rb', line 62

def generate_object(prompt, schema, options = {})
  structured_output = LastLLM::StructuredOutput.new(self)
  structured_output.generate(prompt, schema, options)
end

#generate_text(prompt, options = {}) ⇒ String

Generate text in a single call

Parameters:

  • prompt (String)

    The input text

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

    Options to control generation

Returns:

  • (String)

    The generated text



50
51
52
# File 'lib/last_llm/client.rb', line 50

def generate_text(prompt, options = {})
  @provider.generate_text(prompt, options)
end