dspy-deep_search 1.0.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: 6e560a465f74100b46c31a029666764a5c3ba5bc7916813dff0502ac9486c673
4
+ data.tar.gz: a6c488d9c459347ec6b6f34d809b70ca8c59188f835b96c62d1e9fa067e03972
5
+ SHA512:
6
+ metadata.gz: 4a9329395aefefe5db74191efbf9cee68a4a25266617403b8f9629898b8093ef3251eea470ac1bab55d9787db407c2981abbb7410eff90b4b8b8c05d3fa937ef
7
+ data.tar.gz: 24c77161c79f28bc8bdf99e4555f3a075466df16f2b533288dfd6634d4a37e08281057e27bd2084fafe5965b09e49e204e10eb2d19689fbbd1965641753642c0
data/LICENSE ADDED
@@ -0,0 +1,45 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Vicente Services SL
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
23
+ This project is a Ruby port of the original Python [DSPy library](https://github.com/stanfordnlp/dspy), which is licensed under the MIT License:
24
+
25
+ MIT License
26
+
27
+ Copyright (c) 2023 Stanford Future Data Systems
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy
30
+ of this software and associated documentation files (the "Software"), to deal
31
+ in the Software without restriction, including without limitation the rights
32
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33
+ copies of the Software, and to permit persons to whom the Software is
34
+ furnished to do so, subject to the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be included in all
37
+ copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,313 @@
1
+ # DSPy.rb
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/dspy)](https://rubygems.org/gems/dspy)
4
+ [![Total Downloads](https://img.shields.io/gem/dt/dspy)](https://rubygems.org/gems/dspy)
5
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/vicentereig/dspy.rb/ruby.yml?branch=main&label=build)](https://github.com/vicentereig/dspy.rb/actions/workflows/ruby.yml)
6
+ [![Documentation](https://img.shields.io/badge/docs-vicentereig.github.io%2Fdspy.rb-blue)](https://vicentereig.github.io/dspy.rb/)
7
+
8
+ > [!NOTE]
9
+ > The core Prompt Engineering Framework is production-ready with
10
+ > comprehensive documentation. I am focusing now on educational content on systematic Prompt Optimization and Context Engineering.
11
+ > Your feedback is invaluable. if you encounter issues, please open an [issue](https://github.com/vicentereig/dspy.rb/issues). If you have suggestions, open a [new thread](https://github.com/vicentereig/dspy.rb/discussions).
12
+ >
13
+ > If you want to contribute, feel free to reach out to me to coordinate efforts: hey at vicente.services
14
+ >
15
+ > And, yes, this is 100% a legit project. :)
16
+
17
+
18
+ **Build reliable LLM applications in idiomatic Ruby using composable, type-safe modules.**
19
+
20
+ The Ruby framework for programming with large language models. DSPy.rb brings structured LLM programming to Ruby developers, programmatic Prompt Engineering and Context Engineering.
21
+ Instead of wrestling with prompt strings and parsing responses, you define typed signatures using idiomatic Ruby to compose and decompose AI Worklows and AI Agents.
22
+
23
+ **Prompts are the just Functions.** Traditional prompting is like writing code with string concatenation: it works until it doesn't. DSPy.rb brings you
24
+ the programming approach pioneered by [dspy.ai](https://dspy.ai/): instead of crafting fragile prompts, you define modular
25
+ signatures and let the framework handle the messy details.
26
+
27
+ DSPy.rb is an idiomatic Ruby surgical port of Stanford's [DSPy framework](https://github.com/stanfordnlp/dspy). While implementing
28
+ the core concepts of signatures, predictors, and the main optimization algorithms from the original Python library, DSPy.rb embraces Ruby
29
+ conventions and adds Ruby-specific innovations like Sorbet-base Typed system, ReAct loops, and production-ready integrations like non-blocking Open Telemetry Instrumentation.
30
+
31
+ **What you get?** Ruby LLM applications that actually scale and don't break when you sneeze.
32
+
33
+ Check the [examples](examples/) and take them for a spin!
34
+
35
+ ## Your First DSPy Program
36
+ ### Installation
37
+
38
+ Add to your Gemfile:
39
+
40
+ ```ruby
41
+ gem 'dspy'
42
+ ```
43
+
44
+ and
45
+
46
+ ```bash
47
+ bundle install
48
+ ```
49
+
50
+ ### Your First Reliable Predictor
51
+
52
+ ```ruby
53
+
54
+ # Configure DSPy globablly to use your fave LLM - you can override this on an instance levle.
55
+ DSPy.configure do |c|
56
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
57
+ api_key: ENV['OPENAI_API_KEY'],
58
+ structured_outputs: true) # Enable OpenAI's native JSON mode
59
+ end
60
+
61
+ # Define a signature for sentiment classification - instead of writing a full prompt!
62
+ class Classify < DSPy::Signature
63
+ description "Classify sentiment of a given sentence." # sets the goal of the underlying prompt
64
+
65
+ class Sentiment < T::Enum
66
+ enums do
67
+ Positive = new('positive')
68
+ Negative = new('negative')
69
+ Neutral = new('neutral')
70
+ end
71
+ end
72
+
73
+ # Structured Inputs: makes sure you are sending only valid prompt inputs to your model
74
+ input do
75
+ const :sentence, String, description: 'The sentence to analyze'
76
+ end
77
+
78
+ # Structured Outputs: your predictor will validate the output of the model too.
79
+ output do
80
+ const :sentiment, Sentiment, description: 'The sentiment of the sentence'
81
+ const :confidence, Float, description: 'A number between 0.0 and 1.0'
82
+ end
83
+ end
84
+
85
+ # Wire it to the simplest prompting technique - a Predictn.
86
+ classify = DSPy::Predict.new(Classify)
87
+ # it may raise an error if you mess the inputs or your LLM messes the outputs.
88
+ result = classify.call(sentence: "This book was super fun to read!")
89
+
90
+ puts result.sentiment # => #<Sentiment::Positive>
91
+ puts result.confidence # => 0.85
92
+ ```
93
+
94
+ ### Sibling Gems
95
+
96
+ DSPy.rb ships multiple gems from this monorepo so you can opt into features with heavier dependency trees (e.g., datasets pull in Polars/Arrow, MIPROv2 requires `numo-*` BLAS bindings) only when you need them. Add these alongside `dspy`:
97
+
98
+ | Gem | Description | Status |
99
+ | --- | --- | --- |
100
+ | `dspy-schema` | Exposes `DSPy::TypeSystem::SorbetJsonSchema` for downstream reuse. (Still required by the core `dspy` gem; extraction lets other projects depend on it directly.) | **Stable** (v1.0.0) |
101
+ | `dspy-code_act` | Think-Code-Observe agents that synthesize and execute Ruby safely. (Add the gem or set `DSPY_WITH_CODE_ACT=1` before requiring `dspy/code_act`.) | **Stable** (v1.0.0) |
102
+ | `dspy-datasets` | Dataset helpers plus Parquet/Polars tooling for richer evaluation corpora. (Toggle via `DSPY_WITH_DATASETS`.) | **Stable** (v1.0.0) |
103
+ | `dspy-evals` | High-throughput evaluation harness with metrics, callbacks, and regression fixtures. (Toggle via `DSPY_WITH_EVALS`.) | **Stable** (v1.0.0) |
104
+ | `dspy-miprov2` | Bayesian optimization + Gaussian Process backend for the MIPROv2 teleprompter. (Install or export `DSPY_WITH_MIPROV2=1` before requiring the teleprompter.) | **Stable** (v1.0.0) |
105
+ | `dspy-gepa` | `DSPy::Teleprompt::GEPA`, reflection loops, experiment tracking, telemetry adapters. (Install or set `DSPY_WITH_GEPA=1`.) | **Stable** (v1.0.0) |
106
+ | `gepa` | GEPA optimizer core (Pareto engine, telemetry, reflective proposer). | **Stable** (v1.0.0) |
107
+ | `dspy-o11y` | Core observability APIs: `DSPy::Observability`, async span processor, observation types. (Install or set `DSPY_WITH_O11Y=1`.) | **Stable** (v1.0.0) |
108
+ | `dspy-o11y-langfuse` | Auto-configures DSPy observability to stream spans to Langfuse via OTLP. (Install or set `DSPY_WITH_O11Y_LANGFUSE=1`.) | **Stable** (v1.0.0) |
109
+ | `dspy-deep_search` | Production DeepSearch loop with Exa-backed search/read, token budgeting, and instrumentation (Issue #163). | **Beta** (v1.0.0) |
110
+ | `dspy-deep_research` | Planner/QA orchestration atop DeepSearch plus the memory supervisor used by the CLI example. | **Beta** (v1.0.0) |
111
+
112
+ Set the matching `DSPY_WITH_*` environment variables (see `Gemfile`) to include or exclude each sibling gem when running Bundler locally (for example `DSPY_WITH_GEPA=1` or `DSPY_WITH_O11Y_LANGFUSE=1`). Refer to `adr/013-dependency-tree.md` for the full dependency map and roadmap.
113
+ ### Your First Reliable Predictor
114
+
115
+ ```ruby
116
+
117
+ # Configure DSPy globablly to use your fave LLM - you can override this on an instance levle.
118
+ DSPy.configure do |c|
119
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
120
+ api_key: ENV['OPENAI_API_KEY'],
121
+ structured_outputs: true) # Enable OpenAI's native JSON mode
122
+ end
123
+
124
+ # Define a signature for sentiment classification - instead of writing a full prompt!
125
+ class Classify < DSPy::Signature
126
+ description "Classify sentiment of a given sentence." # sets the goal of the underlying prompt
127
+
128
+ class Sentiment < T::Enum
129
+ enums do
130
+ Positive = new('positive')
131
+ Negative = new('negative')
132
+ Neutral = new('neutral')
133
+ end
134
+ end
135
+
136
+ # Structured Inputs: makes sure you are sending only valid prompt inputs to your model
137
+ input do
138
+ const :sentence, String, description: 'The sentence to analyze'
139
+ end
140
+
141
+ # Structured Outputs: your predictor will validate the output of the model too.
142
+ output do
143
+ const :sentiment, Sentiment, description: 'The sentiment of the sentence'
144
+ const :confidence, Float, description: 'A number between 0.0 and 1.0'
145
+ end
146
+ end
147
+
148
+ # Wire it to the simplest prompting technique - a Predictn.
149
+ classify = DSPy::Predict.new(Classify)
150
+ # it may raise an error if you mess the inputs or your LLM messes the outputs.
151
+ result = classify.call(sentence: "This book was super fun to read!")
152
+
153
+ puts result.sentiment # => #<Sentiment::Positive>
154
+ puts result.confidence # => 0.85
155
+ ```
156
+
157
+ ### Access to 200+ Models Across 5 Providers
158
+
159
+ DSPy.rb provides unified access to major LLM providers with provider-specific optimizations:
160
+
161
+ ```ruby
162
+ # OpenAI (GPT-4, GPT-4o, GPT-4o-mini, GPT-5, etc.)
163
+ DSPy.configure do |c|
164
+ c.lm = DSPy::LM.new('openai/gpt-4o-mini',
165
+ api_key: ENV['OPENAI_API_KEY'],
166
+ structured_outputs: true) # Native JSON mode
167
+ end
168
+
169
+ # Google Gemini (Gemini 1.5 Pro, Flash, Gemini 2.0, etc.)
170
+ DSPy.configure do |c|
171
+ c.lm = DSPy::LM.new('gemini/gemini-2.5-flash',
172
+ api_key: ENV['GEMINI_API_KEY'],
173
+ structured_outputs: true) # Native structured outputs
174
+ end
175
+
176
+ # Anthropic Claude (Claude 3.5, Claude 4, etc.)
177
+ DSPy.configure do |c|
178
+ c.lm = DSPy::LM.new('anthropic/claude-sonnet-4-5-20250929',
179
+ api_key: ENV['ANTHROPIC_API_KEY'],
180
+ structured_outputs: true) # Tool-based extraction (default)
181
+ end
182
+
183
+ # Ollama - Run any local model (Llama, Mistral, Gemma, etc.)
184
+ DSPy.configure do |c|
185
+ c.lm = DSPy::LM.new('ollama/llama3.2') # Free, runs locally, no API key needed
186
+ end
187
+
188
+ # OpenRouter - Access to 200+ models from multiple providers
189
+ DSPy.configure do |c|
190
+ c.lm = DSPy::LM.new('openrouter/deepseek/deepseek-chat-v3.1:free',
191
+ api_key: ENV['OPENROUTER_API_KEY'])
192
+ end
193
+ ```
194
+
195
+ ## What You Get
196
+
197
+ **Developer Experience:**
198
+ - LLM provider support using official Ruby clients:
199
+ - [OpenAI Ruby](https://github.com/openai/openai-ruby) with vision model support
200
+ - [Anthropic Ruby SDK](https://github.com/anthropics/anthropic-sdk-ruby) with multimodal capabilities
201
+ - [Google Gemini API](https://ai.google.dev/) with native structured outputs
202
+ - [Ollama](https://ollama.com/) via OpenAI compatibility layer for local models
203
+ - **Multimodal Support** - Complete image analysis with DSPy::Image, type-safe bounding boxes, vision-capable models
204
+ - Runtime type checking with [Sorbet](https://sorbet.org/) including T::Enum and union types
205
+ - Type-safe tool definitions for ReAct agents
206
+ - Comprehensive instrumentation and observability
207
+
208
+ **Core Building Blocks:**
209
+ - **Signatures** - Define input/output schemas using Sorbet types with T::Enum and union type support
210
+ - **Predict** - LLM completion with structured data extraction and multimodal support
211
+ - **Chain of Thought** - Step-by-step reasoning for complex problems with automatic prompt optimization
212
+ - **ReAct** - Tool-using agents with type-safe tool definitions and error recovery
213
+ - **Module Composition** - Combine multiple LLM calls into production-ready workflows
214
+
215
+ **Optimization & Evaluation:**
216
+ - **Prompt Objects** - Manipulate prompts as first-class objects instead of strings
217
+ - **Typed Examples** - Type-safe training data with automatic validation
218
+ - **Evaluation Framework** - Advanced metrics beyond simple accuracy with error-resilient pipelines
219
+ - **MIPROv2 Optimization** - Advanced Bayesian optimization with Gaussian Processes, multiple optimization strategies, auto-config presets, and storage persistence
220
+
221
+ **Production Features:**
222
+ - **Reliable JSON Extraction** - Native structured outputs for OpenAI and Gemini, Anthropic tool-based extraction, and automatic strategy selection with fallback
223
+ - **Type-Safe Configuration** - Strategy enums with automatic provider optimization (Strict/Compatible modes)
224
+ - **Smart Retry Logic** - Progressive fallback with exponential backoff for handling transient failures
225
+ - **Zero-Config Langfuse Integration** - Set env vars and get automatic OpenTelemetry traces in Langfuse
226
+ - **Performance Caching** - Schema and capability caching for faster repeated operations
227
+ - **File-based Storage** - Optimization result persistence with versioning
228
+ - **Structured Logging** - JSON and key=value formats with span tracking
229
+
230
+ ## Recent Achievements
231
+
232
+ DSPy.rb has rapidly evolved from experimental to production-ready:
233
+
234
+ ### Foundation
235
+ - ✅ **JSON Parsing Reliability** - Native OpenAI structured outputs with adaptive retry logic and schema-aware fallbacks
236
+ - ✅ **Type-Safe Strategy Configuration** - Provider-optimized strategy selection and enum-backed optimizer presets
237
+ - ✅ **Core Module System** - Predict, ChainOfThought, ReAct with type safety (add `dspy-code_act` for Think-Code-Observe agents)
238
+ - ✅ **Production Observability** - OpenTelemetry, New Relic, and Langfuse integration
239
+ - ✅ **Advanced Optimization** - MIPROv2 with Bayesian optimization, Gaussian Processes, and multi-mode search
240
+
241
+ ### Recent Advances
242
+ - ✅ **MIPROv2 ADE Integrity (v0.29.1)** - Stratified train/val/test splits, honest precision accounting, and enum-driven `--auto` presets with integration coverage
243
+ - ✅ **Instruction Deduplication (v0.29.1)** - Candidate generation now filters repeated programs so optimization logs highlight unique strategies
244
+ - ✅ **GEPA Teleprompter (v0.29.0)** - Genetic-Pareto reflective prompt evolution with merge proposer scheduling, reflective mutation, and ADE demo parity
245
+ - ✅ **Optimizer Utilities Parity (v0.29.0)** - Bootstrap strategies, dataset summaries, and Layer 3 utilities unlock multi-predictor programs on Ruby
246
+ - ✅ **Observability Hardening (v0.29.0)** - OTLP exporter runs on a single-thread executor preventing frozen SSL contexts without blocking spans
247
+ - ✅ **Documentation Refresh (v0.29.x)** - New GEPA guide plus ADE optimization docs covering presets, stratified splits, and error-handling defaults
248
+
249
+ **Current Focus Areas:**
250
+
251
+ ### Production Readiness
252
+ - 🚧 **Production Patterns** - Real-world usage validation and performance optimization
253
+ - 🚧 **Ruby Ecosystem Integration** - Rails integration, Sidekiq compatibility, deployment patterns
254
+
255
+ ### Community & Adoption
256
+ - 🚧 **Community Examples** - Real-world applications and case studies
257
+ - 🚧 **Contributor Experience** - Making it easier to contribute and extend
258
+ - 🚧 **Performance Benchmarks** - Comparative analysis vs other frameworks
259
+
260
+ **v1.0 Philosophy:**
261
+ v1.0 will be released after extensive production battle-testing, not after checking off features.
262
+ The API is already stable - v1.0 represents confidence in production reliability backed by real-world validation.
263
+
264
+
265
+ ## Documentation
266
+
267
+ 📖 **[Complete Documentation Website](https://vicentereig.github.io/dspy.rb/)**
268
+
269
+ ### LLM-Friendly Documentation
270
+
271
+ For LLMs and AI assistants working with DSPy.rb:
272
+ - **[llms.txt](https://vicentereig.github.io/dspy.rb/llms.txt)** - Concise reference optimized for LLMs
273
+ - **[llms-full.txt](https://vicentereig.github.io/dspy.rb/llms-full.txt)** - Comprehensive API documentation
274
+
275
+ ### Getting Started
276
+ - **[Installation & Setup](docs/src/getting-started/installation.md)** - Detailed installation and configuration
277
+ - **[Quick Start Guide](docs/src/getting-started/quick-start.md)** - Your first DSPy programs
278
+ - **[Core Concepts](docs/src/getting-started/core-concepts.md)** - Understanding signatures, predictors, and modules
279
+
280
+ ### Prompt Engineering
281
+ - **[Signatures & Types](docs/src/core-concepts/signatures.md)** - Define typed interfaces for LLM operations
282
+ - **[Predictors](docs/src/core-concepts/predictors.md)** - Predict, ChainOfThought, ReAct, and more
283
+ - **[Modules & Pipelines](docs/src/core-concepts/modules.md)** - Compose complex multi-stage workflows
284
+ - **[Multimodal Support](docs/src/core-concepts/multimodal.md)** - Image analysis with vision-capable models
285
+ - **[Examples & Validation](docs/src/core-concepts/examples.md)** - Type-safe training data
286
+ - **[Rich Types](docs/src/advanced/complex-types.md)** - Sorbet type integration with automatic coercion for structs, enums, and arrays
287
+ - **[Composable Pipelines](docs/src/advanced/pipelines.md)** - Manual module composition patterns
288
+
289
+ ### Prompt Optimization
290
+ - **[Evaluation Framework](docs/src/optimization/evaluation.md)** - Advanced metrics beyond simple accuracy
291
+ - **[Prompt Optimization](docs/src/optimization/prompt-optimization.md)** - Manipulate prompts as objects
292
+ - **[MIPROv2 Optimizer](docs/src/optimization/miprov2.md)** - Advanced Bayesian optimization with Gaussian Processes
293
+ - **[GEPA Optimizer](docs/src/optimization/gepa.md)** *(beta)* - Reflective mutation with optional reflection LMs
294
+
295
+ ### Context Engineering
296
+ - **[Tools](docs/src/core-concepts/toolsets.md)** - Tool wieldint agents.
297
+ - **[Agentic Memory](docs/src/core-concepts/memory.md)** - Memory Tools & Agentic Loops
298
+ - **[RAG Patterns](docs/src/advanced/rag.md)** - Manual RAG implementation with external services
299
+
300
+ ### Production Features
301
+ - **[Observability](docs/src/production/observability.md)** - Zero-config Langfuse integration with a dedicated export worker that never blocks your LLMs
302
+ - **[Storage System](docs/src/production/storage.md)** - Persistence and optimization result storage
303
+ - **[Custom Metrics](docs/src/advanced/custom-metrics.md)** - Proc-based evaluation logic
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+ ## License
313
+ This project is licensed under the MIT License.
@@ -0,0 +1,5 @@
1
+ # DSPy::DeepSearch
2
+
3
+ Foundational building blocks for DeepSearch-style DSPy agents. Tracks the search→read→reason loop, token budgets, and adapters for search providers.
4
+
5
+ See ADR 015 for the implementation roadmap.
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module DeepSearch
5
+ module Clients
6
+ class ExaClient
7
+ extend T::Sig
8
+
9
+ class Error < StandardError; end
10
+ class ConfigurationError < Error; end
11
+ class ApiError < Error; end
12
+
13
+ class Result < T::Struct
14
+ const :url, String
15
+ const :title, T.nilable(String)
16
+ const :summary, T.nilable(String)
17
+ const :highlights, T::Array[String]
18
+ const :score, T.nilable(Float)
19
+ end
20
+
21
+ class Content < T::Struct
22
+ const :url, String
23
+ const :text, T.nilable(String)
24
+ const :summary, T.nilable(String)
25
+ const :highlights, T::Array[String]
26
+ end
27
+
28
+ sig { params(client: T.nilable(::Exa::Client)).void }
29
+ def initialize(client: nil)
30
+ @client = T.let(client || build_client, ::Exa::Client)
31
+ end
32
+
33
+ sig do
34
+ params(
35
+ query: String,
36
+ num_results: Integer,
37
+ autoprompt: T::Boolean
38
+ ).returns(T::Array[Result])
39
+ end
40
+ def search(query:, num_results: 5, autoprompt: true)
41
+ response = with_api_errors do
42
+ client.search.search(
43
+ query: query,
44
+ num_results: num_results,
45
+ use_autoprompt: autoprompt,
46
+ summary: true
47
+ )
48
+ end
49
+
50
+ response.results.filter_map do |result|
51
+ next if result.url.nil?
52
+
53
+ Result.new(
54
+ url: result.url,
55
+ title: result.title,
56
+ summary: result.summary,
57
+ highlights: normalize_highlights(result.highlights),
58
+ score: result.score
59
+ )
60
+ end
61
+ end
62
+
63
+ sig do
64
+ params(
65
+ urls: T::Array[String],
66
+ options: T::Hash[Symbol, T.untyped]
67
+ ).returns(T::Array[Content])
68
+ end
69
+ def contents(urls:, **options)
70
+ raise ArgumentError, "urls must not be empty" if urls.empty?
71
+
72
+ defaults = {
73
+ text: true,
74
+ summary: true,
75
+ highlights: true,
76
+ filter_empty_results: true
77
+ }
78
+
79
+ payload = Exa::Types::ContentsRequest.new(**defaults.merge(options).merge(urls: urls)).to_payload
80
+
81
+ raw_response = with_api_errors do
82
+ client.request(
83
+ method: :post,
84
+ path: "contents",
85
+ body: payload,
86
+ response_model: nil
87
+ )
88
+ end
89
+
90
+ symbolized = symbolize_keys(raw_response)
91
+
92
+ check_content_statuses!(symbolized)
93
+
94
+ Array(symbolized[:results]).each_with_index.filter_map do |result, index|
95
+ result = symbolize_keys(result)
96
+ url = result[:url] || urls[index]
97
+ next if url.nil?
98
+
99
+ Content.new(
100
+ url: url,
101
+ text: result[:text],
102
+ summary: result[:summary],
103
+ highlights: normalize_highlights(result[:highlights])
104
+ )
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ sig { returns(::Exa::Client) }
111
+ def build_client
112
+ ::Exa::Client.new
113
+ rescue ::Exa::Errors::ConfigurationError => e
114
+ raise ConfigurationError, e.message
115
+ end
116
+
117
+ sig { params(response: T::Hash[Symbol, T.untyped]).void }
118
+ def check_content_statuses!(response)
119
+ statuses = response[:statuses]
120
+ return if statuses.nil?
121
+
122
+ failure = Array(statuses).map { |status| symbolize_keys(status) }.find { |status| status[:status] != "success" }
123
+ return if failure.nil?
124
+
125
+ error_details = failure[:error] ? failure[:error].inspect : nil
126
+ message = [
127
+ "Exa contents request failed for #{failure[:id]}",
128
+ failure[:status],
129
+ error_details
130
+ ].compact.join(" - ")
131
+
132
+ raise ApiError, message
133
+ end
134
+
135
+ sig { params(hash: T.untyped).returns(T::Hash[Symbol, T.untyped]) }
136
+ def symbolize_keys(hash)
137
+ case hash
138
+ when Hash
139
+ hash.each_with_object({}) do |(key, value), acc|
140
+ acc[(key.is_a?(String) ? key.to_sym : key)] = value
141
+ end
142
+ else
143
+ {}
144
+ end
145
+ end
146
+
147
+ sig { params(highlights: T.nilable(T::Array[T.nilable(String)])).returns(T::Array[String]) }
148
+ def normalize_highlights(highlights)
149
+ Array(highlights).compact.map(&:to_s)
150
+ end
151
+
152
+ sig { params(block: T.proc.returns(T.untyped)).returns(T.untyped) }
153
+ def with_api_errors(&block)
154
+ block.call
155
+ rescue ::Exa::Errors::ConfigurationError => e
156
+ raise ConfigurationError, e.message
157
+ rescue ::Exa::Errors::APIError => e
158
+ raise ApiError, e.message
159
+ rescue ::Exa::Error => e
160
+ raise ApiError, e.message
161
+ end
162
+
163
+ sig { returns(::Exa::Client) }
164
+ attr_reader :client
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module DSPy
6
+ module DeepSearch
7
+ class GapQueue
8
+ extend T::Sig
9
+
10
+ class Empty < StandardError; end
11
+
12
+ sig { void }
13
+ def initialize
14
+ @queue = T.let([], T::Array[T.untyped])
15
+ @seen = T.let(Set.new, T::Set[T.untyped])
16
+ end
17
+
18
+ sig { params(item: T.untyped).void }
19
+ def enqueue(item)
20
+ return if @seen.include?(item)
21
+
22
+ @queue << item
23
+ @seen << item
24
+ end
25
+
26
+ sig { returns(T.untyped) }
27
+ def dequeue
28
+ raise Empty, "No items remaining in gap queue" if @queue.empty?
29
+
30
+ item = @queue.shift
31
+ @seen.delete(item)
32
+ item
33
+ end
34
+
35
+ sig { returns(Integer) }
36
+ def size
37
+ @queue.length
38
+ end
39
+
40
+ sig { returns(T::Boolean) }
41
+ def empty?
42
+ @queue.empty?
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,463 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module DeepSearch
5
+ class Module < DSPy::Module
6
+ extend T::Sig
7
+
8
+ class Result < T::Struct
9
+ const :answer, String
10
+ const :notes, T::Array[String]
11
+ const :citations, T::Array[String]
12
+ const :budget_exhausted, T::Boolean, default: false
13
+ const :warning, T.nilable(String), default: nil
14
+ end
15
+
16
+ class TokenBudgetExceeded < DSPy::Error; end
17
+
18
+ DEFAULT_SEARCH_RESULTS = 5
19
+
20
+ MODEL_ENV_KEYS = {
21
+ seed: 'DSPY_DEEP_SEARCH_SEED_MODEL',
22
+ reader: 'DSPY_DEEP_SEARCH_READER_MODEL',
23
+ reason: 'DSPY_DEEP_SEARCH_REASON_MODEL'
24
+ }.freeze
25
+
26
+ MODEL_PRIORITY = {
27
+ seed: [
28
+ 'gemini/gemini-2.5-flash-lite',
29
+ 'anthropic/claude-haiku-4-5'
30
+ ],
31
+ reader: [
32
+ 'anthropic/claude-sonnet-4-5',
33
+ 'openai/gpt-4.1'
34
+ ],
35
+ reason: [
36
+ 'gemini/gemini-2.5-pro',
37
+ 'openai/o4-mini',
38
+ 'anthropic/claude-4.1-opus'
39
+ ]
40
+ }.freeze
41
+
42
+ subscribe 'lm.tokens', :meter_tokens
43
+
44
+ sig do
45
+ params(
46
+ token_budget: DSPy::DeepSearch::TokenBudget,
47
+ seed_predictor: T.untyped,
48
+ search_predictor: T.nilable(T.untyped),
49
+ reader_predictor: T.untyped,
50
+ reason_predictor: T.untyped,
51
+ search_client: DSPy::DeepSearch::Clients::ExaClient
52
+ ).void
53
+ end
54
+ def initialize(
55
+ token_budget: DSPy::DeepSearch::TokenBudget.new(limit: 20_000),
56
+ seed_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::SeedQuery),
57
+ search_predictor: nil,
58
+ reader_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::ReadSource),
59
+ reason_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::ReasonStep),
60
+ search_client: DSPy::DeepSearch::Clients::ExaClient.new
61
+ )
62
+ super()
63
+
64
+ @token_budget = token_budget
65
+ @token_budget_limit = token_budget.limit
66
+ @seed_predictor = seed_predictor
67
+ @search_predictor = search_predictor
68
+ @reader_predictor = reader_predictor
69
+ @reason_predictor = reason_predictor
70
+ @search_client = search_client
71
+ @gap_queue = DSPy::DeepSearch::GapQueue.new
72
+
73
+ configure_default_predictor_models
74
+ end
75
+
76
+ def forward_untyped(**input_values)
77
+ question = input_values[:question]
78
+ unless question.is_a?(String)
79
+ raise ArgumentError, "DeepSearch expects keyword argument :question"
80
+ end
81
+
82
+ reset_state!
83
+ process_question(question)
84
+ rescue DSPy::DeepSearch::TokenBudget::Exceeded => e
85
+ build_budget_exhausted_result(question, e)
86
+ end
87
+
88
+ sig { override.returns(T::Array[[String, DSPy::Module]]) }
89
+ def named_predictors
90
+ pairs = []
91
+ pairs << ["seed_predictor", @seed_predictor] if @seed_predictor
92
+ pairs << ["search_predictor", T.must(@search_predictor)] if @search_predictor
93
+ pairs << ["reader_predictor", @reader_predictor] if @reader_predictor
94
+ pairs << ["reason_predictor", @reason_predictor] if @reason_predictor
95
+ pairs
96
+ end
97
+
98
+ sig { override.returns(T::Array[DSPy::Module]) }
99
+ def predictors
100
+ named_predictors.map { |(_, predictor)| predictor }
101
+ end
102
+
103
+ sig { params(instruction: String).returns(Module) }
104
+ def with_instruction(instruction)
105
+ clone_with(
106
+ seed_predictor: apply_instruction(@seed_predictor, instruction),
107
+ search_predictor: apply_instruction(@search_predictor, instruction),
108
+ reader_predictor: apply_instruction(@reader_predictor, instruction),
109
+ reason_predictor: apply_instruction(@reason_predictor, instruction),
110
+ token_budget_limit: @token_budget_limit
111
+ )
112
+ end
113
+
114
+ sig { params(examples: T::Array[DSPy::FewShotExample]).returns(Module) }
115
+ def with_examples(examples)
116
+ examples_copy = examples.map { |example| example }
117
+ clone_with(
118
+ seed_predictor: apply_examples(@seed_predictor, examples_copy),
119
+ search_predictor: apply_examples(@search_predictor, examples_copy),
120
+ reader_predictor: apply_examples(@reader_predictor, examples_copy),
121
+ reason_predictor: apply_examples(@reason_predictor, examples_copy),
122
+ token_budget_limit: @token_budget_limit
123
+ )
124
+ end
125
+ sig { params(limit: Integer).returns(Module) }
126
+ def with_token_budget(limit)
127
+ clone_with(
128
+ seed_predictor: @seed_predictor,
129
+ search_predictor: @search_predictor,
130
+ reader_predictor: @reader_predictor,
131
+ reason_predictor: @reason_predictor,
132
+ token_budget_limit: limit
133
+ )
134
+ end
135
+
136
+ private
137
+
138
+ sig { params(question: String).returns(Result) }
139
+ def process_question(question)
140
+ query = @seed_predictor.call(question: question).query
141
+ loop do
142
+ emit_loop_started(question, query)
143
+
144
+ urls = fetch_search_urls(query)
145
+ break if urls.empty?
146
+
147
+ urls.each { |url| enqueue_url(url) }
148
+ collect_notes
149
+
150
+ decision = @reason_predictor.call(question: question, insights: @notes)
151
+ emit_reason_decision(question, decision)
152
+
153
+ case decision.decision
154
+ when DSPy::DeepSearch::Signatures::ReasonStep::Decision::Answer
155
+ answer_text = decision.draft_answer || synthesize_answer
156
+ return Result.new(answer: answer_text, notes: @notes.dup, citations: @citations.dup)
157
+ when DSPy::DeepSearch::Signatures::ReasonStep::Decision::ContinueSearch
158
+ query = decision.refined_query || query
159
+ next
160
+ when DSPy::DeepSearch::Signatures::ReasonStep::Decision::ReadMore
161
+ collect_notes if pending_urls?
162
+ next
163
+ end
164
+ end
165
+
166
+ Result.new(answer: synthesize_answer, notes: @notes.dup, citations: @citations.dup)
167
+ end
168
+
169
+ sig { params(url: String).void }
170
+ def enqueue_url(url)
171
+ @gap_queue.enqueue(url)
172
+ end
173
+
174
+ sig { returns(T::Boolean) }
175
+ def pending_urls?
176
+ !@gap_queue.empty?
177
+ end
178
+
179
+ sig { void }
180
+ def collect_notes
181
+ until @gap_queue.empty?
182
+ url = @gap_queue.dequeue
183
+ fetch_and_extract(url)
184
+ end
185
+ end
186
+
187
+ sig { params(url: String).void }
188
+ def fetch_and_extract(url)
189
+ DSPy.event(
190
+ "deep_search.fetch.started",
191
+ url: url
192
+ )
193
+
194
+ notes_before = @notes.length
195
+ citations_before = @citations.length
196
+
197
+ contents = @search_client.contents(urls: [url])
198
+ record_notes(url, contents)
199
+ reader_notes = @reader_predictor.call(url: url).notes
200
+ @notes.concat(Array(reader_notes))
201
+
202
+ DSPy.event(
203
+ "deep_search.fetch.completed",
204
+ url: url,
205
+ notes_added: @notes.length - notes_before,
206
+ citations_added: @citations.length - citations_before,
207
+ token_budget_remaining: token_budget_remaining
208
+ )
209
+ rescue DSPy::DeepSearch::Clients::ExaClient::ApiError => e
210
+ DSPy.event(
211
+ "deep_search.fetch.failed",
212
+ url: url,
213
+ error: e.message
214
+ )
215
+ DSPy.logger&.warn("DeepSearch fetch failed", url: url, error: e.message)
216
+ end
217
+
218
+ sig { params(url: String, contents: T::Array[DSPy::DeepSearch::Clients::ExaClient::Content]).void }
219
+ def record_notes(url, contents)
220
+ contents.each do |content|
221
+ if content.summary
222
+ @notes << content.summary
223
+ end
224
+ content.highlights.each do |highlight|
225
+ @notes << highlight
226
+ end
227
+ @citations << url unless @citations.include?(url)
228
+ end
229
+ end
230
+
231
+ sig { returns(String) }
232
+ def synthesize_answer
233
+ return "" if @notes.empty?
234
+
235
+ @notes.first(5).join("\n")
236
+ end
237
+
238
+ sig { void }
239
+ def reset_state!
240
+ @notes = []
241
+ @citations = []
242
+ @gap_queue = DSPy::DeepSearch::GapQueue.new
243
+ end
244
+
245
+ sig { params(query: String).returns(T::Array[String]) }
246
+ def fetch_search_urls(query)
247
+ if @search_predictor
248
+ Array(@search_predictor.call(query: query).urls).compact
249
+ else
250
+ results = Array(
251
+ @search_client.search(
252
+ query: query,
253
+ num_results: DEFAULT_SEARCH_RESULTS,
254
+ autoprompt: true
255
+ )
256
+ )
257
+ results.map do |result|
258
+ if result.respond_to?(:url)
259
+ result.url
260
+ elsif result.is_a?(Hash)
261
+ result[:url] || result['url']
262
+ else
263
+ nil
264
+ end
265
+ end.compact.uniq
266
+ end
267
+ end
268
+
269
+ sig { params(_event_name: String, attrs: T::Hash[Symbol, T.untyped]).void }
270
+ def meter_tokens(_event_name, attrs)
271
+ @token_budget.track!(
272
+ prompt_tokens: attrs[:input_tokens].to_i,
273
+ completion_tokens: attrs[:output_tokens].to_i
274
+ )
275
+ end
276
+
277
+ sig do
278
+ params(
279
+ seed_predictor: T.untyped,
280
+ search_predictor: T.nilable(T.untyped),
281
+ reader_predictor: T.untyped,
282
+ reason_predictor: T.untyped,
283
+ token_budget_limit: Integer
284
+ ).returns(Module)
285
+ end
286
+ def clone_with(seed_predictor:, search_predictor:, reader_predictor:, reason_predictor:, token_budget_limit: @token_budget_limit)
287
+ self.class.new(
288
+ token_budget: DSPy::DeepSearch::TokenBudget.new(limit: token_budget_limit),
289
+ seed_predictor: seed_predictor,
290
+ search_predictor: search_predictor,
291
+ reader_predictor: reader_predictor,
292
+ reason_predictor: reason_predictor,
293
+ search_client: @search_client
294
+ )
295
+ end
296
+
297
+ sig { params(predictor: T.nilable(T.untyped), instruction: String).returns(T.nilable(T.untyped)) }
298
+ def apply_instruction(predictor, instruction)
299
+ return nil if predictor.nil?
300
+ return predictor.with_instruction(instruction) if predictor.respond_to?(:with_instruction)
301
+ predictor
302
+ end
303
+
304
+ sig { params(predictor: T.nilable(T.untyped), examples: T::Array[DSPy::FewShotExample]).returns(T.nilable(T.untyped)) }
305
+ def apply_examples(predictor, examples)
306
+ return nil if predictor.nil?
307
+ return predictor.with_examples(examples) if predictor.respond_to?(:with_examples)
308
+ predictor
309
+ end
310
+
311
+ sig { params(question: String, query: String).void }
312
+ def emit_loop_started(question, query)
313
+ DSPy.event(
314
+ "deep_search.loop.started",
315
+ question: question,
316
+ query: query,
317
+ token_budget_remaining: token_budget_remaining
318
+ )
319
+ end
320
+
321
+ sig { params(question: String, decision: T.untyped).void }
322
+ def emit_reason_decision(question, decision)
323
+ decision_enum = decision.respond_to?(:decision) ? decision.decision : nil
324
+ serialized_decision =
325
+ if decision_enum.respond_to?(:serialize)
326
+ decision_enum.serialize
327
+ elsif decision_enum.respond_to?(:to_s)
328
+ decision_enum.to_s
329
+ else
330
+ nil
331
+ end
332
+
333
+ DSPy.event(
334
+ "deep_search.reason.decision",
335
+ question: question,
336
+ decision: serialized_decision,
337
+ notes_count: @notes.length,
338
+ citations_count: @citations.length,
339
+ refined_query: decision.respond_to?(:refined_query) ? decision.refined_query : nil,
340
+ draft_answer: decision.respond_to?(:draft_answer) ? decision.draft_answer : nil,
341
+ token_budget_remaining: token_budget_remaining
342
+ )
343
+ end
344
+
345
+ sig { returns(Integer) }
346
+ def token_budget_remaining
347
+ remaining = @token_budget_limit - @token_budget.total_tokens
348
+ remaining.negative? ? 0 : remaining
349
+ end
350
+
351
+ def configure_default_predictor_models
352
+ @lm_cache = {}
353
+ assign_model(@seed_predictor, :seed)
354
+ assign_model(@reader_predictor, :reader)
355
+ assign_model(@reason_predictor, :reason)
356
+ end
357
+
358
+ def env_model(role)
359
+ key = MODEL_ENV_KEYS[role]
360
+ value = key ? ENV[key] : nil
361
+ return nil if value.nil?
362
+
363
+ trimmed = value.strip
364
+ trimmed.empty? ? nil : trimmed
365
+ end
366
+
367
+ def assign_model(predictor, role)
368
+ return unless predictor
369
+ return if predictor.respond_to?(:config) && predictor.config.lm
370
+
371
+ candidates = []
372
+ env_override = env_model(role)
373
+ candidates << env_override if env_override
374
+ candidates.concat(Array(MODEL_PRIORITY[role]))
375
+
376
+ candidates.each do |model_id|
377
+ next unless model_id
378
+ lm = build_lm(model_id)
379
+ next unless lm
380
+
381
+ begin
382
+ predictor.configure { |config| config.lm = lm }
383
+ return
384
+ rescue StandardError => e
385
+ DSPy.logger&.warn(
386
+ "DeepSearch predictor LM assignment error",
387
+ role: role,
388
+ model: model_id,
389
+ error: e.message
390
+ )
391
+ end
392
+ end
393
+
394
+ DSPy.logger&.warn(
395
+ "DeepSearch predictor LM assignment skipped (no viable model)",
396
+ role: role
397
+ )
398
+ end
399
+
400
+ def build_lm(model_id)
401
+ @lm_cache ||= {}
402
+ return @lm_cache[model_id] if @lm_cache.key?(model_id)
403
+
404
+ provider = model_id.split('/', 2).first
405
+ api_key = api_key_for(provider)
406
+ unless api_key && !api_key.strip.empty?
407
+ DSPy.logger&.warn(
408
+ "DeepSearch skipped LM assignment due to missing API key",
409
+ model: model_id,
410
+ provider: provider
411
+ )
412
+ return nil
413
+ end
414
+
415
+ @lm_cache[model_id] = DSPy::LM.new(model_id, api_key: api_key)
416
+ rescue StandardError => e
417
+ DSPy.logger&.warn(
418
+ "DeepSearch failed to initialize LM",
419
+ model: model_id,
420
+ error: e.message
421
+ )
422
+ nil
423
+ end
424
+
425
+ def api_key_for(provider)
426
+ case provider
427
+ when 'openai'
428
+ ENV['OPENAI_API_KEY']
429
+ when 'anthropic'
430
+ ENV['ANTHROPIC_API_KEY']
431
+ when 'gemini'
432
+ ENV['GEMINI_API_KEY']
433
+ when 'google'
434
+ ENV['GEMINI_API_KEY'] || ENV['GOOGLE_API_KEY']
435
+ else
436
+ nil
437
+ end
438
+ end
439
+
440
+ sig { params(question: String, error: DSPy::DeepSearch::TokenBudget::Exceeded).returns(Result) }
441
+ def build_budget_exhausted_result(question, error)
442
+ warning = error.message
443
+ DSPy.event(
444
+ "deep_search.budget.exhausted",
445
+ question: question,
446
+ notes_count: @notes.length,
447
+ citations_count: @citations.length,
448
+ token_budget_limit: @token_budget_limit,
449
+ total_tokens: @token_budget.total_tokens,
450
+ warning: warning
451
+ )
452
+
453
+ Result.new(
454
+ answer: synthesize_answer,
455
+ notes: @notes.dup,
456
+ citations: @citations.dup,
457
+ budget_exhausted: true,
458
+ warning: warning
459
+ )
460
+ end
461
+ end
462
+ end
463
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module DeepSearch
5
+ module Signatures
6
+ extend T::Sig
7
+
8
+ class SeedQuery < DSPy::Signature
9
+ description "Seed the first search query for DeepSearch"
10
+
11
+ input do
12
+ const :question, String, description: "User research question"
13
+ end
14
+
15
+ output do
16
+ const :query, String, description: "Initial search query"
17
+ end
18
+ end
19
+
20
+ class SearchSources < DSPy::Signature
21
+ description "Call the search provider and return candidate URLs"
22
+
23
+ input do
24
+ const :query, String, description: "Search engine query"
25
+ end
26
+
27
+ output do
28
+ const :urls, T::Array[String], description: "Ranked URLs to read next"
29
+ end
30
+ end
31
+
32
+ class ReadSource < DSPy::Signature
33
+ description "Summarize a single source into bullet notes"
34
+
35
+ input do
36
+ const :url, String, description: "URL selected by the search step"
37
+ end
38
+
39
+ output do
40
+ const :notes, T::Array[String], description: "Key takeaways from the page"
41
+ end
42
+ end
43
+
44
+ class ReasonStep < DSPy::Signature
45
+ description "Decide whether to keep searching, read more, or answer"
46
+
47
+ class Decision < T::Enum
48
+ enums do
49
+ ContinueSearch = new("continue_search")
50
+ ReadMore = new("read_more")
51
+ Answer = new("answer")
52
+ end
53
+ end
54
+
55
+ input do
56
+ const :question, String, description: "Original user question"
57
+ const :insights, T::Array[String], description: "Accumulated notes"
58
+ end
59
+
60
+ output do
61
+ const :decision, Decision, description: "Next action for the loop"
62
+ const :refined_query, T.nilable(String), description: "Follow-up search query"
63
+ const :draft_answer, T.nilable(String), description: "Candidate answer"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module DeepSearch
5
+ class TokenBudget
6
+ extend T::Sig
7
+
8
+ class Exceeded < StandardError; end
9
+
10
+ sig { returns(Integer) }
11
+ attr_reader :limit
12
+
13
+ sig { returns(Integer) }
14
+ attr_reader :total_tokens
15
+
16
+ sig { params(limit: Integer).void }
17
+ def initialize(limit:)
18
+ @limit = limit
19
+ @total_tokens = T.let(0, Integer)
20
+ end
21
+
22
+ sig do
23
+ params(
24
+ prompt_tokens: Integer,
25
+ completion_tokens: Integer
26
+ ).void
27
+ end
28
+ def track!(prompt_tokens:, completion_tokens:)
29
+ prompt = T.must(prompt_tokens)
30
+ completion = T.must(completion_tokens)
31
+
32
+ increment = prompt + completion
33
+ new_total = @total_tokens + increment
34
+
35
+ if new_total >= limit
36
+ raise Exceeded, "Token budget exceeded: #{new_total}/#{limit}"
37
+ end
38
+
39
+ @total_tokens = new_total
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPy
4
+ module DeepSearch
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sorbet-runtime"
4
+ require "exa"
5
+
6
+ require_relative "deep_search/version"
7
+ require_relative "deep_search/token_budget"
8
+ require_relative "deep_search/gap_queue"
9
+ require_relative "deep_search/clients/exa_client"
10
+ require_relative "deep_search/signatures"
11
+ require_relative "deep_search/module"
12
+
13
+ module DSPy
14
+ module DeepSearch
15
+ extend T::Sig
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dspy-deep_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vicente Reig Rincón de Arellano
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: dspy
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.30'
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 0.30.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '0.30'
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.30.1
32
+ - !ruby/object:Gem::Dependency
33
+ name: exa-ai-ruby
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '1.0'
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 1.0.0
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.0'
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 1.0.0
52
+ - !ruby/object:Gem::Dependency
53
+ name: sorbet-runtime
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '0.5'
59
+ type: :runtime
60
+ prerelease: false
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - "~>"
64
+ - !ruby/object:Gem::Version
65
+ version: '0.5'
66
+ description: DeepSearch loop utilities and modules for DSPy agents.
67
+ email:
68
+ - oss@vicente.services
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files: []
72
+ files:
73
+ - LICENSE
74
+ - README.md
75
+ - lib/dspy/deep_search.rb
76
+ - lib/dspy/deep_search/README.md
77
+ - lib/dspy/deep_search/clients/exa_client.rb
78
+ - lib/dspy/deep_search/gap_queue.rb
79
+ - lib/dspy/deep_search/module.rb
80
+ - lib/dspy/deep_search/signatures.rb
81
+ - lib/dspy/deep_search/token_budget.rb
82
+ - lib/dspy/deep_search/version.rb
83
+ homepage: https://vicentereig.github.io/dspy.rb/
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ homepage_uri: https://vicentereig.github.io/dspy.rb/
88
+ source_code_uri: https://github.com/vicentereig/dspy.rb
89
+ changelog_uri: https://github.com/vicentereig/dspy.rb/blob/main/CHANGELOG.md
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '3.1'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.6.9
105
+ specification_version: 4
106
+ summary: DeepSearch primitives for DSPy
107
+ test_files: []