Class: Ragents::Providers::Test
- Inherits:
-
Ragents::Provider
- Object
- Ragents::Provider
- Ragents::Providers::Test
- Defined in:
- lib/ragents/providers/test.rb
Overview
Test provider for unit testing without real API calls. Allows scripting responses for predictable test scenarios.
Instance Attribute Summary collapse
-
#requests ⇒ Object
readonly
Returns the value of attribute requests.
Attributes inherited from Ragents::Provider
Instance Method Summary collapse
-
#default_response=(response) ⇒ Object
Set default response for when no stubs remain.
- #generate(messages:, tools: [], **options) ⇒ Object
-
#initialize(**config) ⇒ Test
constructor
A new instance of Test.
-
#last_request ⇒ Hash?
Get the last request made.
-
#request_count ⇒ Integer
Get the number of requests made.
-
#requests? ⇒ Boolean
Check if any requests were made.
-
#reset! ⇒ Object
Clear all stubs and requests.
- #stream(messages:, tools: [], **options, &block) ⇒ Object
-
#stub_response(content: nil, tool_calls: nil, finish_reason: "stop") ⇒ self
Stub the next response.
-
#stub_tool_call(name:, arguments: {}, id: nil, content: nil) ⇒ self
Stub a tool call response.
Methods inherited from Ragents::Provider
Constructor Details
Instance Attribute Details
#requests ⇒ Object (readonly)
Returns the value of attribute requests.
30 31 32 |
# File 'lib/ragents/providers/test.rb', line 30 def requests @requests end |
Instance Method Details
#default_response=(response) ⇒ Object
Set default response for when no stubs remain
71 72 73 |
# File 'lib/ragents/providers/test.rb', line 71 def default_response=(response) @default_response = response end |
#generate(messages:, tools: [], **options) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/ragents/providers/test.rb', line 75 def generate(messages:, tools: [], **) @requests << { messages: , tools: tools, options: } @responses.shift || @default_response end |
#last_request ⇒ Hash?
Get the last request made
106 107 108 |
# File 'lib/ragents/providers/test.rb', line 106 def last_request @requests.last end |
#request_count ⇒ Integer
Get the number of requests made
118 119 120 |
# File 'lib/ragents/providers/test.rb', line 118 def request_count @requests.size end |
#requests? ⇒ Boolean
Check if any requests were made
112 113 114 |
# File 'lib/ragents/providers/test.rb', line 112 def requests? !@requests.empty? end |
#reset! ⇒ Object
Clear all stubs and requests
99 100 101 102 |
# File 'lib/ragents/providers/test.rb', line 99 def reset! @responses.clear @requests.clear end |
#stream(messages:, tools: [], **options, &block) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ragents/providers/test.rb', line 85 def stream(messages:, tools: [], **, &block) response = generate(messages: , tools: tools, **) # Simulate streaming by yielding content character by character if block_given? && response.content response.content.each_char do |char| block.call(char) end end response end |
#stub_response(content: nil, tool_calls: nil, finish_reason: "stop") ⇒ self
Stub the next response
44 45 46 47 48 49 50 51 |
# File 'lib/ragents/providers/test.rb', line 44 def stub_response(content: nil, tool_calls: nil, finish_reason: "stop") @responses << Response.new( content: content, tool_calls: tool_calls || [], finish_reason: finish_reason ) self end |
#stub_tool_call(name:, arguments: {}, id: nil, content: nil) ⇒ self
Stub a tool call response
59 60 61 62 63 64 65 66 67 |
# File 'lib/ragents/providers/test.rb', line 59 def stub_tool_call(name:, arguments: {}, id: nil, content: nil) tool_call = ToolCall.new( id: id || "call_#{SecureRandom.hex(8)}", name: name, arguments: arguments ) stub_response(content: content, tool_calls: [tool_call], finish_reason: "tool_calls") self end |