Class: DSPy::DeepSearch::Module

Inherits:
Module
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/deep_search/module.rb

Defined Under Namespace

Classes: Result, TokenBudgetExceeded

Constant Summary collapse

DEFAULT_SEARCH_RESULTS =
5
MODEL_ENV_KEYS =
{
  seed: 'DSPY_DEEP_SEARCH_SEED_MODEL',
  reader: 'DSPY_DEEP_SEARCH_READER_MODEL',
  reason: 'DSPY_DEEP_SEARCH_REASON_MODEL'
}.freeze
MODEL_PRIORITY =
{
  seed: [
    'gemini/gemini-2.5-flash-lite',
    'anthropic/claude-haiku-4-5'
  ],
  reader: [
    'anthropic/claude-sonnet-4-5',
    'openai/gpt-4.1'
  ],
  reason: [
    'gemini/gemini-2.5-pro',
    'openai/o4-mini',
    'anthropic/claude-4.1-opus'
  ]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(token_budget: DSPy::DeepSearch::TokenBudget.new(limit: 20_000), seed_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::SeedQuery), search_predictor: nil, reader_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::ReadSource), reason_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::ReasonStep), search_client: DSPy::DeepSearch::Clients::ExaClient.new) ⇒ Module

Returns a new instance of Module.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dspy/deep_search/module.rb', line 54

def initialize(
  token_budget: DSPy::DeepSearch::TokenBudget.new(limit: 20_000),
  seed_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::SeedQuery),
  search_predictor: nil,
  reader_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::ReadSource),
  reason_predictor: DSPy::Predict.new(DSPy::DeepSearch::Signatures::ReasonStep),
  search_client: DSPy::DeepSearch::Clients::ExaClient.new
)
  super()

  @token_budget = token_budget
  @token_budget_limit = token_budget.limit
  @seed_predictor = seed_predictor
  @search_predictor = search_predictor
  @reader_predictor = reader_predictor
  @reason_predictor = reason_predictor
  @search_client = search_client
  @gap_queue = DSPy::DeepSearch::GapQueue.new

  configure_default_predictor_models
end

Instance Method Details

#forward_untyped(**input_values) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dspy/deep_search/module.rb', line 76

def forward_untyped(**input_values)
  question = input_values[:question]
  unless question.is_a?(String)
    raise ArgumentError, "DeepSearch expects keyword argument :question"
  end

  reset_state!
  process_question(question)
rescue DSPy::DeepSearch::TokenBudget::Exceeded => e
  build_budget_exhausted_result(question, e)
end

#named_predictorsObject



89
90
91
92
93
94
95
96
# File 'lib/dspy/deep_search/module.rb', line 89

def named_predictors
  pairs = []
  pairs << ["seed_predictor", @seed_predictor] if @seed_predictor
  pairs << ["search_predictor", T.must(@search_predictor)] if @search_predictor
  pairs << ["reader_predictor", @reader_predictor] if @reader_predictor
  pairs << ["reason_predictor", @reason_predictor] if @reason_predictor
  pairs
end

#predictorsObject



99
100
101
# File 'lib/dspy/deep_search/module.rb', line 99

def predictors
  named_predictors.map { |(_, predictor)| predictor }
end

#with_examples(examples) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/dspy/deep_search/module.rb', line 115

def with_examples(examples)
  examples_copy = examples.map { |example| example }
  clone_with(
    seed_predictor: apply_examples(@seed_predictor, examples_copy),
    search_predictor: apply_examples(@search_predictor, examples_copy),
    reader_predictor: apply_examples(@reader_predictor, examples_copy),
    reason_predictor: apply_examples(@reason_predictor, examples_copy),
    token_budget_limit: @token_budget_limit
  )
end

#with_instruction(instruction) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/dspy/deep_search/module.rb', line 104

def with_instruction(instruction)
  clone_with(
    seed_predictor: apply_instruction(@seed_predictor, instruction),
    search_predictor: apply_instruction(@search_predictor, instruction),
    reader_predictor: apply_instruction(@reader_predictor, instruction),
    reason_predictor: apply_instruction(@reason_predictor, instruction),
    token_budget_limit: @token_budget_limit
  )
end

#with_token_budget(limit) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/dspy/deep_search/module.rb', line 126

def with_token_budget(limit)
  clone_with(
    seed_predictor: @seed_predictor,
    search_predictor: @search_predictor,
    reader_predictor: @reader_predictor,
    reason_predictor: @reason_predictor,
    token_budget_limit: limit
  )
end