Class: Ragnar::QueryProcessor
- Inherits:
-
Object
- Object
- Ragnar::QueryProcessor
- Defined in:
- lib/ragnar/query_processor.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#embedder ⇒ Object
readonly
Returns the value of attribute embedder.
-
#reranker ⇒ Object
readonly
Returns the value of attribute reranker.
-
#rewriter ⇒ Object
readonly
Returns the value of attribute rewriter.
Instance Method Summary collapse
-
#initialize(db_path: Ragnar::DEFAULT_DB_PATH) ⇒ QueryProcessor
constructor
A new instance of QueryProcessor.
- #query(user_query, top_k: 3, verbose: false, enable_rewriting: true, enable_reranking: false) ⇒ Object
Constructor Details
#initialize(db_path: Ragnar::DEFAULT_DB_PATH) ⇒ QueryProcessor
Returns a new instance of QueryProcessor.
10 11 12 13 14 15 16 17 |
# File 'lib/ragnar/query_processor.rb', line 10 def initialize(db_path: Ragnar::DEFAULT_DB_PATH) @database = Database.new(db_path) @embedder = Embedder.new @llm_manager = LLMManager.instance @umap_service = UmapTransformService.instance @rewriter = QueryRewriter.new(llm_manager: @llm_manager) @reranker = nil # Will initialize when needed end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
8 9 10 |
# File 'lib/ragnar/query_processor.rb', line 8 def database @database end |
#embedder ⇒ Object (readonly)
Returns the value of attribute embedder.
8 9 10 |
# File 'lib/ragnar/query_processor.rb', line 8 def @embedder end |
#reranker ⇒ Object (readonly)
Returns the value of attribute reranker.
8 9 10 |
# File 'lib/ragnar/query_processor.rb', line 8 def reranker @reranker end |
#rewriter ⇒ Object (readonly)
Returns the value of attribute rewriter.
8 9 10 |
# File 'lib/ragnar/query_processor.rb', line 8 def rewriter @rewriter end |
Instance Method Details
#query(user_query, top_k: 3, verbose: false, enable_rewriting: true, enable_reranking: false) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ragnar/query_processor.rb', line 19 def query(user_query, top_k: 3, verbose: false, enable_rewriting: true, enable_reranking: false) puts "Processing query: #{user_query}" if verbose # Step 1: Rewrite and analyze the query (if enabled) if enable_rewriting puts "\n#{'-'*60}" if verbose puts "STEP 1: Query Analysis & Rewriting" if verbose puts "-"*60 if verbose rewritten = @rewriter.rewrite(user_query) # Always include the original query in sub-queries to ensure direct matches # are found regardless of how the rewriter reformulates sub_queries = rewritten['sub_queries'] || [] unless sub_queries.include?(user_query) sub_queries.unshift(user_query) end rewritten['sub_queries'] = sub_queries if verbose puts "\nOriginal Query: #{user_query}" puts "\nRewritten Query Analysis:" puts " Clarified Intent: #{rewritten['clarified_intent']}" puts " Query Type: #{rewritten['query_type']}" puts " Context Needed: #{rewritten['context_needed']}" puts "\nGenerated Sub-queries (#{rewritten['sub_queries'].length}):" rewritten['sub_queries'].each_with_index do |sq, idx| puts " #{idx + 1}. #{sq}" end if rewritten['key_terms'] && !rewritten['key_terms'].empty? puts "\nKey Terms Identified:" puts " #{rewritten['key_terms'].join(', ')}" end end else # Skip rewriting - use original query directly rewritten = { 'clarified_intent' => user_query, 'query_type' => 'direct', 'context_needed' => 'general', 'sub_queries' => [user_query], 'key_terms' => [] } if verbose puts "\n#{'-'*60}" puts "STEP 1: Query Analysis (Rewriting Disabled)" puts "-"*60 puts "\nUsing original query directly" end end # Step 2: Retrieve candidates using RRF if verbose puts "\n#{'-'*60}" puts "STEP 2: Document Retrieval with RRF" puts "-"*60 end candidates = retrieve_with_rrf( rewritten['sub_queries'], k: 20, verbose: verbose ) if candidates.empty? return { query: user_query, clarified: rewritten['clarified_intent'], answer: "No relevant documents found in the database.", sources: [] } end if verbose puts "\nRetrieval Summary:" puts " Total candidates found: #{candidates.size}" puts " Unique sources: #{candidates.map { |c| c[:file_path] }.uniq.size}" end # Step 3: Rerank candidates if verbose puts "\n#{'-'*60}" puts "STEP 3: Document Reranking" puts "-"*60 end if enable_reranking reranked = rerank_documents( query: user_query, documents: candidates, top_k: top_k * 2 ) else # Use retrieval order (RRF scores) directly — often more reliable than # small cross-encoder rerankers on domain-specific corpora reranked = candidates end if verbose && reranked.any? puts "\nTop #{enable_reranking ? 'Reranked' : 'Retrieved'} Documents:" reranked[0..2].each_with_index do |doc, idx| full_text = (doc[:chunk_text] || doc[:text] || "").gsub(/\s+/, ' ') puts " #{idx + 1}. [#{File.basename(doc[:file_path] || 'unknown')}]" puts " Score: #{doc[:score]&.round(4) if doc[:score]}" puts " Distance: #{doc[:distance]&.round(4) if doc[:distance]}" puts " Full chunk (#{full_text.length} chars):" puts " \"#{full_text}\"" puts "" end end # Step 4: Prepare context with neighboring chunks if verbose puts "\n#{'-'*60}" puts "STEP 4: Context Preparation" puts "-"*60 end context_docs = prepare_context(reranked[0...top_k], rewritten['context_needed']) if verbose puts "\nContext Documents Selected: #{context_docs.length}" puts "Context strategy: #{rewritten['context_needed']}" end # Step 5: Repack context for optimal LLM consumption if verbose puts "\n#{'-'*60}" puts "STEP 5: Context Repacking" puts "-"*60 end repacked_context = ContextRepacker.repack( context_docs, rewritten['clarified_intent'] ) if verbose original_size = context_docs.sum { |d| (d[:chunk_text] || "").length } puts "\nContext Optimization:" puts " Original size: #{original_size} chars" puts " Repacked size: #{repacked_context.length} chars" puts " Compression ratio: #{(100.0 * repacked_context.length / original_size).round(1)}%" puts "\nFull Repacked Context:" puts "-" * 40 puts repacked_context puts "-" * 40 end # Step 6: Generate response if verbose puts "\n#{'-'*60}" puts "STEP 6: Response Generation" puts "-"*60 end response = generate_response( query: rewritten['clarified_intent'], repacked_context: repacked_context, query_type: rewritten['query_type'] ) if verbose puts "\nGenerated Response:" puts "-" * 40 puts response puts "-" * 40 end result = { query: user_query, clarified: rewritten['clarified_intent'], answer: response, sources: context_docs.map { |d| { source_file: d[:file_path] || d[:source_file] || d["file_path"], chunk_index: d[:chunk_index] || d["chunk_index"] } }.reject { |s| s[:source_file].nil? }, sub_queries: rewritten['sub_queries'], confidence: calculate_confidence(reranked[0...top_k]) } if verbose puts "\n#{'-'*60}" puts "FINAL RESULTS" puts "-"*60 puts "\nConfidence Score: #{result[:confidence]}%" puts "\nSources Used:" result[:sources].each_with_index do |source, idx| puts " #{idx + 1}. #{source[:source_file]} (chunk #{source[:chunk_index]})" end end result end |