Class: RLM::Lm::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/rlm/lm/mock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responses:, cost_cents: 0) ⇒ Mock

Returns a new instance of Mock.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
# File 'lib/rlm/lm/mock.rb', line 8

def initialize(responses:, cost_cents: 0)
  @responses = Array(responses).dup.freeze
  raise ArgumentError, "responses must not be empty" if @responses.empty?

  @cost_cents_per_call = cost_cents
  @cost_cents = 0
  @prompts = []
  @index = 0
end

Instance Attribute Details

#cost_centsObject (readonly)

Returns the value of attribute cost_cents.



6
7
8
# File 'lib/rlm/lm/mock.rb', line 6

def cost_cents
  @cost_cents
end

#promptsObject (readonly)

Returns the value of attribute prompts.



6
7
8
# File 'lib/rlm/lm/mock.rb', line 6

def prompts
  @prompts
end

Instance Method Details

#call(prompt:) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rlm/lm/mock.rb', line 18

def call(prompt:, **)
  raise ProviderError, "prompt must be a String" unless prompt.is_a?(String)
  raise ProviderError, "mock LM responses exhausted" if exhausted?

  prompts << prompt
  @cost_cents += @cost_cents_per_call

  response = @responses.fetch(@index)
  @index += 1
  response
end

#call_countObject



30
31
32
# File 'lib/rlm/lm/mock.rb', line 30

def call_count
  prompts.length
end

#last_promptObject



34
35
36
# File 'lib/rlm/lm/mock.rb', line 34

def last_prompt
  prompts.last
end