Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# RubyLLM::Test

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby_llm/test`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem
This gem provides testing utilities for RubyLLM, a Ruby library for working with large language models (LLMs). It enables calls to LLM's to be stubbed so that the surrounding application logic can be tested without making actual calls to the LLM. This is particularly useful for testing code that interacts with LLMs, as it allows developers to simulate responses from the LLM without incurring the cost, latency, or randomness of real API calls.

## Installation

Expand All @@ -22,7 +20,57 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
Add the following lines to your `spec/spec_helper.rb` or `test/test_helper.rb`:

```ruby
require 'ruby_llm/test'

RubyLLM::Models.singleton_class.prepend(RubyLLM::Test::ResolveWithTestProvider)
```

Then, in your tests, you can use the `stub_response` method to stub responses from the LLM. For example:

```ruby
it 'returns a stubbed response' do
RubyLLM::Test.stub_response('Hello, world!')

response = MyLLMClient.call('Hello?')
expect(response).to eq('Hello, world!')
end
```

If you make multiple calls to the LLM, you can call `stub_response` more than once or use method `stub_responses` to stub multiple responses at once. For example:

```ruby
it 'returns multiple stubbed responses' do
RubyLLM::Test.stub_responses(['Hello, world!', 'How are you?'])
response1 = MyLLMClient.call('Hello?')
response2 = MyLLMClient.call('How are you?')

expect(response1).to eq('Hello, world!')
expect(response2).to eq('How are you?')
end
```

### Resetting Stubs

Make sure to reset stubs after each test to avoid interference before or between tests.

```ruby
RubyLLM::Test.reset
```

### Stubbing with a Block

You can also stub responses in a block, which handles the setup and teardown of stubs automatically. For example:

```ruby

RubyLLM::Test.stub_response('Hello, world!') do
response = MyLLMClient.call('Hello?')
expect(response).to eq('Hello, world!')
end
```

## Development

Expand All @@ -32,7 +80,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_llm-test.
Bug reports and pull requests are welcome on GitHub at https://github.com/RockSolt/ruby_llm-test.

## License

Expand Down
2 changes: 0 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
Loading