Class: RSpec::LLM::Adapters::Fake

Inherits:
Base
  • Object
show all
Defined in:
lib/rspec/llm/adapters/fake.rb

Overview

In-memory programmable adapter. Use in unit-style specs to avoid hitting a real LLM while still exercising matcher and DSL behavior.

fake = RSpec::LLM::Adapters::Fake.new
fake.respond_to("Summarize: foo").with("foo summary")
fake.respond_to_pattern(/^Summarize/).with { |prompt| "summary of #{prompt}" }
fake.default("I don't know")
fake.embed_with { |text| [text.length.to_f, 0.0, 0.0] }

Defined Under Namespace

Classes: Stub, UnstubbedPromptError

Instance Attribute Summary collapse

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

wrap

Constructor Details

#initialize(client = nil) ⇒ Fake

Returns a new instance of Fake.



33
34
35
36
37
38
39
# File 'lib/rspec/llm/adapters/fake.rb', line 33

def initialize(client = nil)
  super
  @stubs = []
  @default = nil
  @embedder = nil
  @call_log = []
end

Instance Attribute Details

#call_logObject (readonly)

Returns the value of attribute call_log.



41
42
43
# File 'lib/rspec/llm/adapters/fake.rb', line 41

def call_log
  @call_log
end

Instance Method Details

#chat(messages) ⇒ Object



63
64
65
66
67
68
# File 'lib/rspec/llm/adapters/fake.rb', line 63

def chat(messages)
  prompt = last_user_message(normalize_messages(messages))
  @call_log << prompt
  response = lookup(prompt)
  response.is_a?(Proc) ? response.call(prompt) : response
end

#default(text = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
# File 'lib/rspec/llm/adapters/fake.rb', line 51

def default(text = nil, &block)
  raise ArgumentError, "pass text or a block" if text.nil? && block.nil?

  @default = text || block
  self
end

#embed(text) ⇒ Object

Raises:

  • (NotImplementedError)


70
71
72
73
74
# File 'lib/rspec/llm/adapters/fake.rb', line 70

def embed(text)
  raise NotImplementedError, "configure with #embed_with { |text| vector }" unless @embedder

  @embedder.call(text)
end

#embed_with(&block) ⇒ Object



58
59
60
61
# File 'lib/rspec/llm/adapters/fake.rb', line 58

def embed_with(&block)
  @embedder = block
  self
end

#reset!Object



76
77
78
79
80
81
82
# File 'lib/rspec/llm/adapters/fake.rb', line 76

def reset!
  @stubs.clear
  @default = nil
  @embedder = nil
  @call_log.clear
  self
end

#respond_to(prompt) ⇒ Object



43
44
45
# File 'lib/rspec/llm/adapters/fake.rb', line 43

def respond_to(prompt)
  Stub.new(self, prompt)
end

#respond_to_pattern(regex) ⇒ Object



47
48
49
# File 'lib/rspec/llm/adapters/fake.rb', line 47

def respond_to_pattern(regex)
  Stub.new(self, regex)
end