Class: RubyLLM::Test::TestProvider

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby_llm/test/test_provider.rb

Overview

This class serves as a test double for the provider used in the ‘RubyLLM::Test` class. It captures all calls to

the `complete` method, allowing tests to assert that the correct parameters were passed and to simulate
responses from the provider.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, test_harness = RubyLLM::Test) ⇒ TestProvider

Returns a new instance of TestProvider.



16
17
18
19
20
# File 'lib/ruby_llm/test/test_provider.rb', line 16

def initialize(provider, test_harness = RubyLLM::Test)
  super(provider)
  @test_harness = test_harness
  @complete_calls = []
end

Instance Attribute Details

#complete_callsObject (readonly)

Returns the value of attribute complete_calls.



11
12
13
# File 'lib/ruby_llm/test/test_provider.rb', line 11

def complete_calls
  @complete_calls
end

Instance Method Details

#complete(messages, tools:, temperature:, model:, params: {}, headers: {}, schema: nil, thinking: nil, tool_prefs: nil, &block) ⇒ Object

rubocop:disable Metrics/ParameterLists



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/test/test_provider.rb', line 22

def complete(messages, tools:, temperature:, model:, params: {}, # rubocop:disable Metrics/ParameterLists
             headers: {}, schema: nil, thinking: nil, tool_prefs: nil, &block)
  @complete_calls << CompleteParameters.new(messages:, tools:, temperature:, model:, params:, headers:, schema:,
                                            thinking:, tool_prefs:, block:)
  raise Errors::NoResponseProvidedError, messages if @test_harness.responses_empty?

  response = @test_harness.next_response
  return response if response.is_a?(Message)

  Message.new(
    role: :assistant,
    content: response.is_a?(Hash) ? response.to_json : response
  )
end

#last_callObject



37
38
39
# File 'lib/ruby_llm/test/test_provider.rb', line 37

def last_call
  @complete_calls.last
end