LLMTape 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.env +0 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/LLMTape-0.1.0.gem +0 -0
- data/LLMTape-0.2.0.gem +0 -0
- data/README.md +101 -0
- data/Rakefile +8 -0
- data/lib/LLMTape/concerns/record_initializer.rb +31 -0
- data/lib/LLMTape/concerns/replay_initializer.rb +29 -0
- data/lib/LLMTape/services/record.rb +65 -0
- data/lib/LLMTape/services/replay.rb +16 -0
- data/lib/LLMTape/services/stale_buster.rb +44 -0
- data/lib/LLMTape/services/utilities.rb +34 -0
- data/lib/LLMTape/version.rb +5 -0
- data/lib/LLMTape.rb +82 -0
- data/logo.png +0 -0
- data/logo_white.png +0 -0
- data/sig/LLMTape.rbs +4 -0
- metadata +69 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 070ff888788202111d91ae3eff13c210d3c54fbcaa75f48f5aeb6638006f3d5a
|
|
4
|
+
data.tar.gz: 2448eb31c0514b95e3b8ee56a648ac956dd4d69b11c98eb2f5ebeea8cd1a4b33
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4416d4459d88b1b7570992949caa2cc77358bcc44412f75bcd00d98f5842b88e973d4c0b9678ccfb91480c760fbb9bf3aa6ae710719473460c8afd5dfacfe476
|
|
7
|
+
data.tar.gz: 68ec916b33e31c130ea59b857db371c2bce6324e82940f12b583546091174afb7119b720a028f22d70419427433512b9a084be084da7bf41781935338b8638f8
|
data/.env
ADDED
|
File without changes
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Amit Leshed
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/LLMTape-0.1.0.gem
ADDED
|
Binary file
|
data/LLMTape-0.2.0.gem
ADDED
|
Binary file
|
data/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="logo_white.png" alt="LLMTape Logo" width="250"/>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# 🎥 LLMTape
|
|
6
|
+
|
|
7
|
+
**LLMTape** is a lightweight Ruby gem for testing code that calls Large Language Models.
|
|
8
|
+
It works like a cassette tape: record an API call once, then replay it forever.
|
|
9
|
+
|
|
10
|
+
It wraps any LLM client with a tiny DSL. In test environement, it records “tapes” (YAML fixtures of real LLM calls) and replays them on subsequent runs; when a tape is stale, it re-records to keep tests current. Production stays clean and safe, while CI avoids hammering the API every run--yielding deterministic tests, faster pipelines, and fewer tokens spent.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 🌟 Features
|
|
15
|
+
|
|
16
|
+
**🎥 Record & Replay** – Capture any LLM API call once, then replay it in future test runs.
|
|
17
|
+
|
|
18
|
+
**🔒 Deterministic Tests** – Eliminate flaky outputs and unstable CI runs caused by live LLM calls.
|
|
19
|
+
|
|
20
|
+
**📼 Human-Readable Fixtures** – Responses are stored as YAML “tapes” you can inspect, diff, and commit.
|
|
21
|
+
|
|
22
|
+
**⚡ Fast & Cheap** – No repeated API calls in CI; only re-record when your prompt or logic changes.
|
|
23
|
+
|
|
24
|
+
**🧪 Client-Agnostic** – Works with any Ruby LLM client (e.g. ruby_llm), since you wrap the call yourself.
|
|
25
|
+
|
|
26
|
+
**⏳ Staleness Detection** – Marks a tape as stale if it’s too old or if the prompt has changed.
|
|
27
|
+
|
|
28
|
+
**🔧 Simple DSL** – Just wrap your LLM call in LLMTape.use("my_call") { ... } and it handles the rest.
|
|
29
|
+
|
|
30
|
+
**🚀 CI-Friendly** – Run in replay mode to avoid network calls and API keys entirely on CI.
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 📦 Installation
|
|
35
|
+
|
|
36
|
+
Add this gem to your application's `Gemfile`:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
gem "LLMTape", "~> 0.1.0" # in your gemfile
|
|
40
|
+
bundle add LLMTape # or through cli
|
|
41
|
+
bundle install
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## ⚙️ Configuration
|
|
45
|
+
```ruby
|
|
46
|
+
require "llm_tape"
|
|
47
|
+
|
|
48
|
+
LLMTape.configure(
|
|
49
|
+
fixtures_directory_path: "tmp/fixtures/llm",
|
|
50
|
+
mode: (ENV["LLM_TAPE"] || "auto").to_sym # :record, :replay, or :auto
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 📖 Usage
|
|
55
|
+
```ruby
|
|
56
|
+
class MyFancyAIController < ApplicationController
|
|
57
|
+
def create
|
|
58
|
+
prompt = params[:prompt].presence || "LLM Prompt"
|
|
59
|
+
LLMTape.use("my_fancy_ai#create", request: { prompt: prompt }) do
|
|
60
|
+
RubyLLM.chat.ask(prompt) # Say you use RubyLLM (it's great you should)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# LLMTape records/replays only in test; it enforces the env check internally.
|
|
65
|
+
# Dev/Prod behavior is unchanged (straight passthrough). No extra guards needed.
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class LLMTest < Minitest::Test
|
|
69
|
+
def test_three_word_greeting
|
|
70
|
+
prompt = "Say hello in exactly three words."
|
|
71
|
+
result = LLMTape.use("three_word_greeting", request: { prompt: prompt }) do
|
|
72
|
+
RubyLLM.chat.ask(prompt)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
refute_empty result.to_s.strip
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 🖥️ CLI
|
|
81
|
+
```bash
|
|
82
|
+
LLM_TAPE=record/replay/auto bundle exec rake test
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 🛠 Development
|
|
86
|
+
|
|
87
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
88
|
+
|
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
90
|
+
|
|
91
|
+
## 🤝 Contributing
|
|
92
|
+
|
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/LLMTape. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/LLMTape/blob/main/CODE_OF_CONDUCT.md).
|
|
94
|
+
|
|
95
|
+
## 📜 License
|
|
96
|
+
|
|
97
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
98
|
+
|
|
99
|
+
## 🧭 Code of Conduct
|
|
100
|
+
|
|
101
|
+
Everyone interacting in the LLMTape project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/LLMTape/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module LLMTape
|
|
2
|
+
module Backpack
|
|
3
|
+
module RecordInitializer
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.extend(ClassMethods)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
module ClassMethods
|
|
9
|
+
def call(**kwargs)
|
|
10
|
+
new(**defaults.merge(kwargs)).call
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def defaults
|
|
16
|
+
{ metadata: {}, path: LLMTape.fixtures_directory_path }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(description:, request:, response:, metadata: {}, path: LLMTape.fixtures_directory_path)
|
|
21
|
+
@description = description
|
|
22
|
+
@request = request
|
|
23
|
+
@response = response
|
|
24
|
+
@metadata = metadata
|
|
25
|
+
@path = path
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
attr_reader :description, :request, :response, :metadata, :path
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module LLMTape
|
|
2
|
+
module Backpack
|
|
3
|
+
module ReplayInitializer
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.extend(ClassMethods)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
module ClassMethods
|
|
9
|
+
def call(**kwargs)
|
|
10
|
+
new(**defaults.merge(kwargs)).call
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def defaults
|
|
16
|
+
{ path: LLMTape.fixtures_directory_path }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(description:, request:, path: File.join(LLMTape.fixtures_directory_path, "llm_tapes.yml"))
|
|
21
|
+
@description = description
|
|
22
|
+
@request = request
|
|
23
|
+
@fixture_path = path
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
attr_reader :description, :request, :fixture_path
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "fileutils"
|
|
3
|
+
require "yaml"
|
|
4
|
+
require "time"
|
|
5
|
+
require_relative "../concerns/record_initializer"
|
|
6
|
+
|
|
7
|
+
module LLMTape
|
|
8
|
+
module Services
|
|
9
|
+
class Record
|
|
10
|
+
attr_reader :description, :request, :response, :metadata
|
|
11
|
+
|
|
12
|
+
include LLMTape::Backpack::RecordInitializer
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
generate_fixture
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def generate_fixture
|
|
21
|
+
existing_created_at = @metadata["created_at"] || @metadata[:created_at]
|
|
22
|
+
fixture_data = build_fixture_data(@description, @request, @response, @metadata, existing_created_at)
|
|
23
|
+
file_exists = File.exist?(file_path)
|
|
24
|
+
fixtures = file_exists ? YAML.load_stream(File.read(file_path)) : []
|
|
25
|
+
matched_fixture = fixtures.find_index { |f| f["description"] == @description }
|
|
26
|
+
|
|
27
|
+
fixtures[matched_fixture] = fixture_data if matched_fixture
|
|
28
|
+
fixtures << fixture_data unless matched_fixture
|
|
29
|
+
|
|
30
|
+
record_dat_tape(file_path, fixtures, fixture_data)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def build_fixture_data(description, request, response, metadata, existing_created_at = nil)
|
|
34
|
+
{
|
|
35
|
+
"description" => description,
|
|
36
|
+
"data" => {
|
|
37
|
+
"id" => generate_hash(description),
|
|
38
|
+
"request" => request,
|
|
39
|
+
"response" => response,
|
|
40
|
+
"metadata" => metadata.merge({ :created_at => existing_created_at || Time.now.utc.iso8601 })
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def record_dat_tape(file_path, fixtures, fixture_data)
|
|
46
|
+
File.open(file_path, "w") do |file|
|
|
47
|
+
fixtures.each { |fixture| file.write(fixture.to_yaml) }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
fixture_data
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def generate_hash(description)
|
|
54
|
+
description.downcase.split.join("_") + "_" + Digest::SHA256.hexdigest(description)[0..7]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def file_path
|
|
58
|
+
fixture_file_path = File.join(
|
|
59
|
+
LLMTape.fixtures_directory_path,
|
|
60
|
+
"llm_tapes.yml"
|
|
61
|
+
)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require_relative "../concerns/replay_initializer"
|
|
2
|
+
|
|
3
|
+
module LLMTape
|
|
4
|
+
module Services
|
|
5
|
+
class Replay
|
|
6
|
+
attr_reader :description, :request
|
|
7
|
+
|
|
8
|
+
include LLMTape::Backpack::ReplayInitializer
|
|
9
|
+
|
|
10
|
+
def call
|
|
11
|
+
LLMTape::Backpack.find_tape(@fixture_path, @description) ||
|
|
12
|
+
raise(ArgumentError, "Tape not found for description: #{@description}")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "yaml"
|
|
2
|
+
require "debug"
|
|
3
|
+
|
|
4
|
+
module LLMTape
|
|
5
|
+
module Services
|
|
6
|
+
class StaleBuster
|
|
7
|
+
def self.call(description, current_prompt)
|
|
8
|
+
@current_prompt = current_prompt
|
|
9
|
+
@description = description
|
|
10
|
+
|
|
11
|
+
stale?(current_prompt: @current_prompt)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def self.stale?(current_prompt:)
|
|
17
|
+
return true unless file_exists?
|
|
18
|
+
fixture = LLMTape::Backpack.find_tape(fixture_path, @description)
|
|
19
|
+
return true unless fixture
|
|
20
|
+
created_at = fixture.dig("data", "metadata", :created_at)&.to_s
|
|
21
|
+
return true unless created_at
|
|
22
|
+
created_time = Time.parse(created_at) rescue nil
|
|
23
|
+
return true unless created_time
|
|
24
|
+
|
|
25
|
+
too_old = (Time.now - created_time) > 30*24*60*60
|
|
26
|
+
prompt_diff = fixture.dig("data", "request", :prompt).to_s != current_prompt.to_s
|
|
27
|
+
|
|
28
|
+
too_old || prompt_diff
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def self.fixture_path
|
|
33
|
+
File.join(
|
|
34
|
+
LLMTape.fixtures_directory_path,
|
|
35
|
+
"llm_tapes.yml"
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.file_exists?
|
|
40
|
+
File.exist?(fixture_path)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require "yaml"
|
|
2
|
+
require "digest"
|
|
3
|
+
|
|
4
|
+
module LLMTape
|
|
5
|
+
module Backpack
|
|
6
|
+
def self.generate_hash(description)
|
|
7
|
+
require "digest"
|
|
8
|
+
Digest::SHA256.hexdigest(description)[0..7]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.create_file
|
|
12
|
+
fixture_file_path = File.join(
|
|
13
|
+
LLMTape.fixtures_directory_path,
|
|
14
|
+
"llm_tapes.yml"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
FileUtils.mkdir_p(LLMTape.fixtures_directory_path) unless Dir.exist?(LLMTape.fixtures_directory_path)
|
|
18
|
+
FileUtils.touch(fixture_file_path) unless File.exist?(fixture_file_path)
|
|
19
|
+
fixture_file_path
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.find_tape(fixture_path, description)
|
|
23
|
+
return nil unless File.exist?(fixture_path)
|
|
24
|
+
|
|
25
|
+
fixture_path = File.join(LLMTape.fixtures_directory_path, "llm_tapes.yml")
|
|
26
|
+
create_file unless File.exist?(fixture_path)
|
|
27
|
+
docs = YAML.load_stream(File.read(fixture_path)).compact
|
|
28
|
+
docs = [YAML.safe_load(File.read(fixture_path))].compact if docs.empty?
|
|
29
|
+
target = description.to_s
|
|
30
|
+
|
|
31
|
+
docs.find { |doc| (doc["description"] || doc[:description]).to_s == target }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/LLMTape.rb
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "LLMTape/version"
|
|
4
|
+
require_relative "LLMTape/services/record"
|
|
5
|
+
require_relative "LLMTape/services/replay"
|
|
6
|
+
require_relative "LLMTape/services/stale_buster"
|
|
7
|
+
require_relative "LLMTape/services/utilities"
|
|
8
|
+
|
|
9
|
+
module LLMTape
|
|
10
|
+
Record = Services::Record
|
|
11
|
+
Replay = Services::Replay
|
|
12
|
+
StaleBuster = Services::StaleBuster
|
|
13
|
+
|
|
14
|
+
DEFAULT_FIXTURES_PATH = "test/fixtures/llm"
|
|
15
|
+
DEFAULT_MODE = (ENV["LLMTape"] || "auto").to_sym
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
attr_accessor :fixtures_directory_path, :mode
|
|
19
|
+
|
|
20
|
+
def use(description, record: false, request: nil, &block)
|
|
21
|
+
safety_first!(description, &block)
|
|
22
|
+
return block.call unless test_env?
|
|
23
|
+
|
|
24
|
+
configure if fixtures_directory_path.to_s.empty?
|
|
25
|
+
setup(description, record, request, &block)
|
|
26
|
+
use_tape(description)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def configure(fixtures_directory_path: DEFAULT_FIXTURES_PATH, mode: DEFAULT_MODE)
|
|
30
|
+
configure_fixtures_directory(fixtures_directory_path:, mode:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def test_env?
|
|
36
|
+
ENV["RAILS_ENV"] == "test" || ENV["RACK_ENV"] == "test" || ENV["ENV"] == "test"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def safety_first!(description, &block)
|
|
40
|
+
raise ArgumentError, "You must provide a block" unless block_given?
|
|
41
|
+
raise ArgumentError, "Description is required" if description.to_s.strip.empty?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def setup(description, record, request, &block)
|
|
45
|
+
@fixture_path = File.join(fixtures_directory_path, "#{description}.yml")
|
|
46
|
+
@current_request = request || {}
|
|
47
|
+
@current_response = block.call
|
|
48
|
+
@operation_mode = record ? :record : mode
|
|
49
|
+
@stale = StaleBuster.call(description, @current_request[:prompt])
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def configure_fixtures_directory(fixtures_directory_path: DEFAULT_FIXTURES_PATH, mode: DEFAULT_MODE)
|
|
53
|
+
self.fixtures_directory_path = fixtures_directory_path.to_s
|
|
54
|
+
FileUtils.mkdir_p(self.fixtures_directory_path)
|
|
55
|
+
self.mode = mode
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def use_tape(description)
|
|
59
|
+
if should_record?
|
|
60
|
+
Record.call(
|
|
61
|
+
description: description,
|
|
62
|
+
request: @current_request,
|
|
63
|
+
response: @current_response,
|
|
64
|
+
metadata: { fixture_path: @fixture_path, mode: @operation_mode }
|
|
65
|
+
)
|
|
66
|
+
elsif should_replay?
|
|
67
|
+
Replay.call(
|
|
68
|
+
description: description,
|
|
69
|
+
request: @current_request
|
|
70
|
+
)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def should_replay?
|
|
75
|
+
(@operation_mode == :replay || @operation_mode == :auto) && !@stale
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def should_record?
|
|
79
|
+
@operation_mode == :record || @stale
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/logo.png
ADDED
|
Binary file
|
data/logo_white.png
ADDED
|
Binary file
|
data/sig/LLMTape.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: LLMTape
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.3.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Amit Leshed
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-09-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: It wraps any LLM client with a tiny DSL. In test environement, it records
|
|
14
|
+
“tapes” (YAML fixtures of real LLM calls) and replays them on subsequent runs; when
|
|
15
|
+
a tape is stale, it re-records to keep tests current. Production stays clean and
|
|
16
|
+
safe, while CI avoids hammering the API every run--yielding deterministic tests,
|
|
17
|
+
faster pipelines, and fewer tokens spent.
|
|
18
|
+
email:
|
|
19
|
+
- amitleshed@icloud.com
|
|
20
|
+
executables: []
|
|
21
|
+
extensions: []
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
files:
|
|
24
|
+
- ".env"
|
|
25
|
+
- CODE_OF_CONDUCT.md
|
|
26
|
+
- LICENSE.txt
|
|
27
|
+
- LLMTape-0.1.0.gem
|
|
28
|
+
- LLMTape-0.2.0.gem
|
|
29
|
+
- README.md
|
|
30
|
+
- Rakefile
|
|
31
|
+
- lib/LLMTape.rb
|
|
32
|
+
- lib/LLMTape/concerns/record_initializer.rb
|
|
33
|
+
- lib/LLMTape/concerns/replay_initializer.rb
|
|
34
|
+
- lib/LLMTape/services/record.rb
|
|
35
|
+
- lib/LLMTape/services/replay.rb
|
|
36
|
+
- lib/LLMTape/services/stale_buster.rb
|
|
37
|
+
- lib/LLMTape/services/utilities.rb
|
|
38
|
+
- lib/LLMTape/version.rb
|
|
39
|
+
- logo.png
|
|
40
|
+
- logo_white.png
|
|
41
|
+
- sig/LLMTape.rbs
|
|
42
|
+
homepage: https://www.amitleshed.com
|
|
43
|
+
licenses:
|
|
44
|
+
- MIT
|
|
45
|
+
metadata:
|
|
46
|
+
allowed_push_host: https://rubygems.org
|
|
47
|
+
homepage_uri: https://www.amitleshed.com
|
|
48
|
+
source_code_uri: https://www.amitleshed.com
|
|
49
|
+
post_install_message:
|
|
50
|
+
rdoc_options: []
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: 3.2.0
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubygems_version: 3.4.1
|
|
65
|
+
signing_key:
|
|
66
|
+
specification_version: 4
|
|
67
|
+
summary: 'LLMTape is a lightweight Ruby gem for testing code that calls Large Language
|
|
68
|
+
Models. It works like a cassette tape: record an API call once, then replay it forever.'
|
|
69
|
+
test_files: []
|