rspec-llm 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba3f15006a8aa1820dd8741a4788ad7cfe94f4b213bcabb1cc1ee75458a1b8d0
4
+ data.tar.gz: b27e3aeefc80e89d3e8209672a66ae8e021d95af73867a4260936af916153459
5
+ SHA512:
6
+ metadata.gz: 2ae5b22b4148e8b04c4607d49d0b3a088b77405f8cac92a6db1b223ecc8aefd3b84471e5d8d42db86a1527f06f8d9330f4a3ec29f8abe29732d3e2a2c60c95a5
7
+ data.tar.gz: 352bc6061a08c5e8561286782b51c4690d79ef9939ed93498e657c37aa78623c4c35a927ad6a7abdacef82e3b6b925bd81881b4c30971b700d0ea3872791602d
data/.idea/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
@@ -0,0 +1,5 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ </profile>
5
+ </component>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptLibraryMappings">
4
+ <includedPredefinedLibrary name="Node.js Core" />
5
+ </component>
6
+ </project>
data/.idea/misc.xml ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="JsFlowSettings">
7
+ <service-enabled>true</service-enabled>
8
+ <exe-path />
9
+ <annotation-enable>false</annotation-enable>
10
+ <other-services-enabled>true</other-services-enabled>
11
+ <auto-save>true</auto-save>
12
+ </component>
13
+ <component name="ProjectDictionaryState">
14
+ <dictionary name="washu" />
15
+ </component>
16
+ <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 2.4.2" project-jdk-type="RUBY_SDK" />
17
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/rspec-llm.iml" filepath="$PROJECT_DIR$/.idea/rspec-llm.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,66 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="jdk" jdkName="rbenv: 3.4.9" jdkType="RUBY_SDK" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="addressable (v2.9.0, rbenv: 3.4.9) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.3, rbenv: 3.4.9) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="baran (v0.1.12, rbenv: 3.4.9) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="base64 (v0.3.0, rbenv: 3.4.9) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="bigdecimal (v4.1.2, rbenv: 3.4.9) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.5.23, rbenv: 3.4.9) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="crack (v1.0.1, rbenv: 3.4.9) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="csv (v3.3.5, rbenv: 3.4.9) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.6.2, rbenv: 3.4.9) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="event_stream_parser (v1.0.0, rbenv: 3.4.9) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="faraday (v2.14.2, rbenv: 3.4.9) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="faraday-multipart (v1.2.0, rbenv: 3.4.9) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="faraday-net_http (v3.4.4, rbenv: 3.4.9) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="faraday-retry (v2.4.0, rbenv: 3.4.9) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="hashdiff (v1.2.1, rbenv: 3.4.9) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="json (v2.19.8, rbenv: 3.4.9) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="json-schema (v4.3.1, rbenv: 3.4.9) [gem]" level="application" />
31
+ <orderEntry type="library" scope="TEST" name="langchainrb (v0.19.5, rbenv: 3.4.9) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="language_server-protocol (v3.17.0.5, rbenv: 3.4.9) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="lint_roller (v1.1.0, rbenv: 3.4.9) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="logger (v1.7.0, rbenv: 3.4.9) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="marcel (v1.2.1, rbenv: 3.4.9) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="matrix (v0.4.3, rbenv: 3.4.9) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="multipart-post (v2.4.1, rbenv: 3.4.9) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="net-http (v0.9.1, rbenv: 3.4.9) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v2.1.0, rbenv: 3.4.9) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="parser (v3.3.11.1, rbenv: 3.4.9) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="pragmatic_segmenter (v0.3.24, rbenv: 3.4.9) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="prism (v1.9.0, rbenv: 3.4.9) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="public_suffix (v7.0.5, rbenv: 3.4.9) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="racc (v1.8.1, rbenv: 3.4.9) [gem]" level="application" />
45
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, rbenv: 3.4.9) [gem]" level="application" />
46
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.4.2, rbenv: 3.4.9) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.12.0, rbenv: 3.4.9) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.4.4, rbenv: 3.4.9) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.13.2, rbenv: 3.4.9) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.13.6, rbenv: 3.4.9) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.13.5, rbenv: 3.4.9) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.13.8, rbenv: 3.4.9) [gem]" level="application" />
53
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.13.7, rbenv: 3.4.9) [gem]" level="application" />
54
+ <orderEntry type="library" scope="TEST" name="rubocop (v1.87.0, rbenv: 3.4.9) [gem]" level="application" />
55
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.49.1, rbenv: 3.4.9) [gem]" level="application" />
56
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, rbenv: 3.4.9) [gem]" level="application" />
57
+ <orderEntry type="library" scope="TEST" name="ruby_llm (v1.15.0, rbenv: 3.4.9) [gem]" level="application" />
58
+ <orderEntry type="library" scope="PROVIDED" name="ruby_llm-schema (v0.4.0, rbenv: 3.4.9) [gem]" level="application" />
59
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v3.2.0, rbenv: 3.4.9) [gem]" level="application" />
60
+ <orderEntry type="library" scope="PROVIDED" name="unicode-emoji (v4.2.0, rbenv: 3.4.9) [gem]" level="application" />
61
+ <orderEntry type="library" scope="PROVIDED" name="uri (v1.1.1, rbenv: 3.4.9) [gem]" level="application" />
62
+ <orderEntry type="library" scope="TEST" name="vcr (v6.4.0, rbenv: 3.4.9) [gem]" level="application" />
63
+ <orderEntry type="library" scope="TEST" name="webmock (v3.26.2, rbenv: 3.4.9) [gem]" level="application" />
64
+ <orderEntry type="library" scope="PROVIDED" name="zeitwerk (v2.8.2, rbenv: 3.4.9) [gem]" level="application" />
65
+ </component>
66
+ </module>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,33 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: disable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ EnforcedStyle: double_quotes
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - "spec/**/*"
18
+ - "*.gemspec"
19
+
20
+ Metrics/MethodLength:
21
+ Max: 20
22
+
23
+ Metrics/AbcSize:
24
+ Max: 25
25
+
26
+ Metrics/CyclomaticComplexity:
27
+ Max: 10
28
+
29
+ Metrics/PerceivedComplexity:
30
+ Max: 10
31
+
32
+ Layout/LineLength:
33
+ Max: 130
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-06-04
4
+
5
+ ### Added
6
+ - Initial release.
7
+ - `RSpec::LLM.configure` for setting a default client, judge model, and embedder.
8
+ - Adapters for the `ruby_llm` and `langchainrb` gems, plus a programmable in-memory `Fake` adapter for hermetic specs.
9
+ - Matchers: `pass_llm_judge`, `match_llm_intent`, `match_json_schema`, `be_semantically_similar_to`.
10
+ - `describe_llm` / `evaluate` DSL for batch evaluations.
11
+ - Helpers `llm`, `stub_llm`, `stub_llm_judge` mixed into all example groups.
@@ -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) 2026 Sal Scotto
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/README.md ADDED
@@ -0,0 +1,194 @@
1
+ # rspec-llm
2
+
3
+ [![CI](https://github.com/salscotto/rspec-llm/actions/workflows/ci.yml/badge.svg)](https://github.com/salscotto/rspec-llm/actions/workflows/ci.yml)
4
+
5
+ RSpec matchers, helpers, and a thin DSL for testing LLM-backed code in Ruby.
6
+
7
+ `rspec-llm` provides:
8
+
9
+ - **Custom matchers** for asserting on LLM outputs:
10
+ - `pass_llm_judge("criterion")` — LLM-as-judge boolean evaluation
11
+ - `match_llm_intent("the response is a polite apology")` — judge framed as intent matching
12
+ - `match_json_schema(schema)` — JSON Schema validation
13
+ - `be_semantically_similar_to("reference text")` — cosine similarity over embeddings
14
+ - **Adapters** for the [`ruby_llm`](https://github.com/crmne/ruby_llm) and [`langchainrb`](https://github.com/patterns-ai-core/langchainrb) gems, plus a programmable in-memory **fake** for hermetic unit specs.
15
+ - A `describe_llm` / `evaluate` **DSL** for running batches of prompt → expectation pairs.
16
+ - VCR-friendly: deterministic playback is delegated to your existing VCR config (see [VCR pattern](#vcr-pattern)).
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ bundle add rspec-llm
22
+ ```
23
+
24
+ You only need to install one of `ruby_llm` or `langchainrb` (or both) depending on which adapter you intend to use. `rspec-llm` doesn't pull either in as a hard dependency.
25
+
26
+ ```bash
27
+ bundle add ruby_llm # if you use ruby_llm
28
+ bundle add langchainrb # if you use langchainrb
29
+ ```
30
+
31
+ ## Configuration
32
+
33
+ Configure once in `spec/spec_helper.rb`:
34
+
35
+ ```ruby
36
+ require "rspec/llm"
37
+ require "ruby_llm"
38
+
39
+ RSpec::LLM.configure do |c|
40
+ c.client = RubyLLM.chat(model: "claude-sonnet-4-6")
41
+ c.judge = RubyLLM.chat(model: "claude-haiku-4-5")
42
+ c.embedder = ->(text) { RubyLLM.embed(text).vectors }
43
+ c.similarity_threshold = 0.8
44
+ end
45
+ ```
46
+
47
+ With `langchainrb` it looks like:
48
+
49
+ ```ruby
50
+ require "rspec/llm"
51
+ require "langchain"
52
+
53
+ llm = Langchain::LLM::OpenAI.new(api_key: ENV.fetch("OPENAI_API_KEY"))
54
+
55
+ RSpec::LLM.configure do |c|
56
+ c.client = llm
57
+ c.judge = llm
58
+ c.embedder = ->(text) { llm.embed(text: text).embedding }
59
+ end
60
+ ```
61
+
62
+ ## Matchers
63
+
64
+ ```ruby
65
+ RSpec.describe "Summarizer" do
66
+ let(:response) { MyApp.summarize(article) }
67
+
68
+ it { expect(response).to pass_llm_judge("contains a single-sentence summary") }
69
+ it { expect(response).to match_llm_intent("a summary of the article") }
70
+ it { expect(response).to match_json_schema(MyApp::SUMMARY_SCHEMA) }
71
+ it { expect(response).to be_semantically_similar_to(expected_gist).within(0.85) }
72
+ end
73
+ ```
74
+
75
+ ### `pass_llm_judge`
76
+
77
+ Sends the response and a criterion to the configured judge model and parses a YES/NO verdict from the first token of the reply. The judge's reasoning is surfaced in the failure message.
78
+
79
+ ```ruby
80
+ expect(reply).to pass_llm_judge("is polite and apologetic")
81
+ ```
82
+
83
+ To use a different judge for a single matcher, chain `.using(some_client)`.
84
+
85
+ ### `match_llm_intent`
86
+
87
+ Same machinery as `pass_llm_judge`, framed as intent matching — useful when the "criterion" is naturally a description of what the response should say.
88
+
89
+ ```ruby
90
+ expect(reply).to match_llm_intent("a refund confirmation for order #12345")
91
+ ```
92
+
93
+ ### `match_json_schema`
94
+
95
+ Parses the actual value as JSON (or accepts a Hash/Array directly) and validates against the given JSON Schema via the `json-schema` gem.
96
+
97
+ ```ruby
98
+ schema = {
99
+ "type" => "object",
100
+ "required" => ["summary"],
101
+ "properties" => { "summary" => { "type" => "string" } }
102
+ }
103
+ expect(response).to match_json_schema(schema)
104
+ ```
105
+
106
+ ### `be_semantically_similar_to`
107
+
108
+ Embeds both sides via the configured `embedder`, computes cosine similarity, and compares to the threshold. Override the threshold per-matcher with `.within(0.9)`.
109
+
110
+ ```ruby
111
+ expect(response).to be_semantically_similar_to("the cat sat on the mat").within(0.9)
112
+ ```
113
+
114
+ ## Fake adapter
115
+
116
+ For fast, hermetic unit tests, stub the client with the built-in fake:
117
+
118
+ ```ruby
119
+ RSpec.describe "Greeter" do
120
+ it "returns the canned greeting" do
121
+ stub_llm do |fake|
122
+ fake.respond_to("Say hi to Alice").with("Hi, Alice!")
123
+ fake.respond_to_pattern(/^Say hi to/).with { |prompt| "Hi! (from #{prompt})" }
124
+ fake.default("…")
125
+ end
126
+
127
+ expect(MyApp.greet("Alice")).to eq("Hi, Alice!")
128
+ end
129
+ end
130
+ ```
131
+
132
+ Use `stub_llm_judge` to stub the judge model separately — handy when testing your own code that wraps `pass_llm_judge`:
133
+
134
+ ```ruby
135
+ stub_llm_judge do |fake|
136
+ fake.default("YES\nLooks good to me.")
137
+ end
138
+ ```
139
+
140
+ ## DSL
141
+
142
+ `describe_llm` is a thin wrapper around `RSpec.describe` that adds an `evaluate` group helper:
143
+
144
+ ```ruby
145
+ RSpec.describe_llm "Summarizer evals" do
146
+ evaluate "single sentence",
147
+ prompt: "Summarize: #{ARTICLE}",
148
+ expect: [
149
+ pass_llm_judge("is one sentence"),
150
+ match_llm_intent("a summary of the article")
151
+ ]
152
+ end
153
+ ```
154
+
155
+ Each `evaluate` call defines one RSpec example: it calls the configured client with the prompt and applies every matcher in `expect:`.
156
+
157
+ ## VCR pattern
158
+
159
+ For integration tests against real APIs, record once and replay deterministically using [VCR](https://github.com/vcr/vcr) + [WebMock](https://github.com/bblimke/webmock):
160
+
161
+ ```ruby
162
+ # spec/spec_helper.rb
163
+ require "vcr"
164
+
165
+ VCR.configure do |c|
166
+ c.cassette_library_dir = "spec/cassettes"
167
+ c.hook_into :webmock
168
+ c.filter_sensitive_data("<OPENAI_API_KEY>") { ENV["OPENAI_API_KEY"] }
169
+ c.configure_rspec_metadata!
170
+ end
171
+
172
+ RSpec.describe "Summarizer", vcr: true do
173
+ it "summarizes" do
174
+ expect(MyApp.summarize(article)).to pass_llm_judge("is concise")
175
+ end
176
+ end
177
+ ```
178
+
179
+ ## Development
180
+
181
+ ```bash
182
+ bin/setup # install deps
183
+ bundle exec rspec # run tests
184
+ bundle exec rubocop # lint
185
+ bin/console # interactive prompt
186
+ ```
187
+
188
+ ## Contributing
189
+
190
+ Bug reports and pull requests welcome on GitHub at https://github.com/salscotto/rspec-llm.
191
+
192
+ ## License
193
+
194
+ MIT — see [LICENSE.txt](LICENSE.txt).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/build_release.sh ADDED
@@ -0,0 +1,13 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ # Extract version from version.rb
5
+ VERSION=$(ruby -r ./lib/rspec/llm/version.rb -e "puts RSpec::LLM::VERSION")
6
+
7
+ echo "Building gem version ${VERSION}..."
8
+ gem build rspec-llm.gemspec
9
+
10
+ echo "Pushing to RubyGems..."
11
+ gem push rspec-llm-${VERSION}.gem
12
+
13
+ echo "Done!"
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module LLM
5
+ module Adapters
6
+ # Abstract adapter. Subclasses normalize ruby_llm / langchainrb / fake
7
+ # clients to a common minimal surface: #chat(messages) -> String,
8
+ # and optionally #embed(text) -> Array<Float>.
9
+ class Base
10
+ class UnsupportedClientError < ArgumentError; end
11
+
12
+ # Wrap an arbitrary client object in the right adapter. Uses class-name
13
+ # string matching so that ruby_llm / langchainrb aren't hard required.
14
+ def self.wrap(obj)
15
+ return obj if obj.is_a?(Base)
16
+
17
+ class_name = obj.class.name.to_s
18
+ case class_name
19
+ when /\ARubyLLM::Chat/, "RubyLLM"
20
+ RubyLLM.new(obj)
21
+ when /\ALangchain::LLM::/
22
+ Langchain.new(obj)
23
+ else
24
+ raise UnsupportedClientError,
25
+ "Unsupported LLM client: #{class_name.empty? ? obj.inspect : class_name}. " \
26
+ "Wrap it in a subclass of RSpec::LLM::Adapters::Base."
27
+ end
28
+ end
29
+
30
+ def initialize(client)
31
+ @client = client
32
+ end
33
+
34
+ attr_reader :client
35
+
36
+ # Send a chat. `messages` may be a String (treated as a single user
37
+ # message) or an Array of { role:, content: } hashes. Returns the
38
+ # assistant's text content as a String.
39
+ def chat(_messages)
40
+ raise NotImplementedError
41
+ end
42
+
43
+ # Embed text. Returns Array<Float>. Optional — adapters may raise
44
+ # NotImplementedError if the underlying client doesn't support it.
45
+ def embed(_text)
46
+ raise NotImplementedError
47
+ end
48
+
49
+ protected
50
+
51
+ def normalize_messages(messages)
52
+ case messages
53
+ when String
54
+ [{ role: "user", content: messages }]
55
+ when Array
56
+ messages.map { |m| { role: m[:role] || m["role"] || "user", content: m[:content] || m["content"] } }
57
+ else
58
+ raise ArgumentError, "messages must be a String or Array of hashes, got #{messages.class}"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end