lc-rag 0.20.0

RAG (Retrieval-Augmented Generation) module for langchainrust — BM25, Hybrid Retrieval, GraphRAG, HyDE, Reranking, MultiQuery, Document Loaders
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// src/retrieval/multi_query.rs
//! MultiQueryRetriever implementation
//!
//! Uses an LLM to generate multiple query variants, improving retrieval recall.

use lc_core::language_models::BaseChatModel;
use lc_core::tools::ToolDefinition;
use lc_prompts::PromptTemplate;
use lc_providers::ProviderError;
use lc_schema::Message;
use lc_vector_stores::{Document, SearchResult};
use serde_json::json;

use crate::retriever::RetrieverTrait;
use crate::structured::chat_structured;
use std::collections::HashMap;
use std::sync::Arc;

/// Generate a stable document ID from content hash (M58).
///
/// P2-3: Replaces `DefaultHasher` with FNV-1a 64-bit (the std internal algorithm is not
/// guaranteed stable across processes; FNV-1a is a fully-specified deterministic hash).
fn doc_content_hash(content: &str) -> String {
    use std::hash::{Hash, Hasher};
    let mut hasher = fnv::FnvHasher::default();
    content.hash(&mut hasher);
    format!("{:016x}", hasher.finish())
}

/// MultiQueryRetriever error type
#[derive(Debug)]
#[non_exhaustive]
pub enum MultiQueryError {
    /// LLM error
    LLMError(String),

    /// Retriever error
    RetrieverError(String),

    /// Parse error
    ParseError(String),
}

impl std::fmt::Display for MultiQueryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MultiQueryError::LLMError(msg) => write!(f, "LLM error: {}", msg),
            MultiQueryError::RetrieverError(msg) => write!(f, "Retriever error: {}", msg),
            MultiQueryError::ParseError(msg) => write!(f, "Parse error: {}", msg),
        }
    }
}

impl std::error::Error for MultiQueryError {}

/// MultiQueryRetriever configuration
pub struct MultiQueryConfig {
    /// Number of generated queries
    pub num_queries: usize,

    /// Number of documents returned per query
    pub k_per_query: usize,

    /// Number of documents returned in the final result
    pub final_k: usize,

    /// The query-generation prompt
    pub prompt_template: String,
}

impl Default for MultiQueryConfig {
    fn default() -> Self {
        Self {
            num_queries: 3,
            k_per_query: 5,
            final_k: 10,
            prompt_template: DEFAULT_MULTI_QUERY_PROMPT.to_string(),
        }
    }
}

impl MultiQueryConfig {
    /// Creates a `MultiQueryConfig` with default configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the number of generated queries
    pub fn with_num_queries(mut self, n: usize) -> Self {
        self.num_queries = n;
        self
    }

    /// Sets the number of documents returned per query
    pub fn with_k_per_query(mut self, k: usize) -> Self {
        self.k_per_query = k;
        self
    }

    /// Sets the number of documents returned in the final result
    pub fn with_final_k(mut self, k: usize) -> Self {
        self.final_k = k;
        self
    }

    /// Sets the query-generation prompt
    pub fn with_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt_template = prompt.into();
        self
    }
}

const DEFAULT_MULTI_QUERY_PROMPT: &str = r#"You are an AI language model assistant. Your task is to generate 3 different versions of the given user question to retrieve relevant documents from a vector database.

By generating multiple perspectives on the user question, your goal is to help overcome some of the limitations of distance-based similarity search.

Provide these alternative questions separated by newlines.

Original question: {question}

Alternative questions:"#;

/// MultiQueryRetriever
///
/// Uses an LLM to generate multiple query variants, retrieves with the base retriever for
/// each one, then merges and dedups the results before returning.
pub struct MultiQueryRetriever {
    /// The LLM used to generate query variants
    ///
    /// P0-3: no longer hardcodes `OpenAIChat`; accepts any LLM implementing `BaseChatModel`.
    llm: Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>,

    /// The base retriever
    base_retriever: Arc<dyn RetrieverTrait>,

    /// Configuration
    config: MultiQueryConfig,
}

impl MultiQueryRetriever {
    /// Creates a MultiQueryRetriever (accepting any LLM implementing `BaseChatModel`)
    pub fn new<L>(llm: L, base_retriever: Arc<dyn RetrieverTrait>) -> Self
    where
        L: BaseChatModel + Send + Sync + 'static,
        L::Error: Into<ProviderError>,
    {
        Self {
            llm: lc_providers::wrap_chat_model(llm),
            base_retriever,
            config: MultiQueryConfig::default(),
        }
    }

    /// P0-3: Builds from an already-wrapped `Arc<dyn BaseChatModel<Error = ProviderError>>`
    pub fn new_arc(
        llm: Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>,
        base_retriever: Arc<dyn RetrieverTrait>,
    ) -> Self {
        Self {
            llm,
            base_retriever,
            config: MultiQueryConfig::default(),
        }
    }

    /// Sets the MultiQuery configuration
    pub fn with_config(mut self, config: MultiQueryConfig) -> Self {
        self.config = config;
        self
    }

    /// Sets the number of generated queries
    pub fn with_num_queries(mut self, n: usize) -> Self {
        self.config.num_queries = n;
        self
    }

    /// Sets the number of documents returned per query
    pub fn with_k_per_query(mut self, k: usize) -> Self {
        self.config.k_per_query = k;
        self
    }

    /// Sets the number of documents returned in the final result
    pub fn with_final_k(mut self, k: usize) -> Self {
        self.config.final_k = k;
        self
    }

    async fn generate_queries(&self, original_query: &str) -> Result<Vec<String>, MultiQueryError> {
        let template = PromptTemplate::new(&self.config.prompt_template);
        let mut vars = HashMap::new();
        vars.insert("question", original_query);
        let prompt = template
            .format(&vars)
            .unwrap_or_else(|_| self.config.prompt_template.clone());

        // P2-1: Prefer the structured query list from tool_calls; on text-parse failure,
        // retry once with a hint.
        const MAX_RETRIES: usize = 1;
        let mut current_prompt = prompt;

        for attempt in 0..=MAX_RETRIES {
            let result = chat_structured(
                self.llm.as_ref(),
                Some(queries_tool()),
                vec![Message::human(&current_prompt)],
            )
            .await
            .map_err(|e| MultiQueryError::LLMError(e.to_string()))?;

            // Prefer tool_calls: the query string array
            if let Some(args) = &result.tool_args {
                if let Some(queries) = parse_queries(args) {
                    if !queries.is_empty() {
                        return Ok(queries);
                    }
                }
            }

            // Text fallback: split line by line, cleaning numbered/quote/bullet noise
            let queries = parse_query_lines(&result.content, self.config.num_queries);
            if !queries.is_empty() {
                return Ok(queries);
            }

            if attempt < MAX_RETRIES {
                current_prompt = format!(
                    "上次的输出不是有效的查询列表。请重新为原问题生成 {} 个不同的查询变体,\
                     每行一个,不要编号、不要项目符号、不要解释或多余文字。\n\n原问题:{}\n\n\
                     上次输出(无效):\n{}\n\n新的查询变体:",
                    self.config.num_queries, original_query, result.content
                );
            }
        }

        Err(MultiQueryError::ParseError(
            "LLM did not generate valid query variants".to_string(),
        ))
    }

    /// Generates multiple query variants, retrieves each one, and returns the merged deduped documents
    pub async fn retrieve_multi(&self, query: &str) -> Result<Vec<Document>, MultiQueryError> {
        let queries = self.generate_queries(query).await?;

        let all_queries: Vec<String> = std::iter::once(query.to_string()).chain(queries).collect();

        let mut doc_scores: HashMap<String, (Document, f32)> = HashMap::new();

        for q in &all_queries {
            let results = self
                .base_retriever
                .retrieve_with_scores(q, self.config.k_per_query)
                .await
                .map_err(|e| MultiQueryError::RetrieverError(e.to_string()))?;

            for result in results {
                let doc_id = result
                    .document
                    .id
                    .clone()
                    .unwrap_or_else(|| doc_content_hash(&result.document.content));

                doc_scores
                    .entry(doc_id)
                    .and_modify(|(_, score)| {
                        // M3: use addition instead of no-op .max()
                        *score += result.score;
                    })
                    .or_insert((result.document.clone(), result.score));
            }
        }

        let mut scored_docs: Vec<(Document, f32)> = doc_scores
            .values()
            .map(|(doc, score)| (doc.clone(), *score))
            .collect();

        scored_docs.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let final_docs: Vec<Document> = scored_docs
            .into_iter()
            .take(self.config.final_k)
            .map(|(doc, _)| doc)
            .collect();

        Ok(final_docs)
    }

    /// Generates multiple query variants, retrieves each one, and returns the merged deduped results with scores
    pub async fn retrieve_multi_with_scores(
        &self,
        query: &str,
    ) -> Result<Vec<SearchResult>, MultiQueryError> {
        let queries = self.generate_queries(query).await?;

        let all_queries: Vec<String> = std::iter::once(query.to_string()).chain(queries).collect();

        let mut doc_scores: HashMap<String, (Document, f32, usize)> = HashMap::new();

        for q in &all_queries {
            let results = self
                .base_retriever
                .retrieve_with_scores(q, self.config.k_per_query)
                .await
                .map_err(|e| MultiQueryError::RetrieverError(e.to_string()))?;

            for result in results {
                let doc_id = result
                    .document
                    .id
                    .clone()
                    .unwrap_or_else(|| doc_content_hash(&result.document.content));

                doc_scores
                    .entry(doc_id)
                    .and_modify(|(_, score, count)| {
                        // M3: use addition instead of no-op .max()
                        *score += result.score;
                        *count += 1;
                    })
                    .or_insert((result.document.clone(), result.score, 1));
            }
        }

        let mut scored_docs: Vec<SearchResult> = doc_scores
            .values()
            .map(|(doc, score, count)| {
                let combined_score = score * (1.0 + 0.1 * *count as f32);
                SearchResult {
                    document: doc.clone(),
                    score: combined_score,
                }
            })
            .collect();

        scored_docs.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let final_results: Vec<SearchResult> =
            scored_docs.into_iter().take(self.config.final_k).collect();

        Ok(final_results)
    }

    /// Returns the LLM-generated query variants (without retrieving)
    pub async fn get_generated_queries(&self, query: &str) -> Result<Vec<String>, MultiQueryError> {
        self.generate_queries(query).await
    }
}

/// Query-variant tool definition (P2-1): forces the LLM to output a query string array.
fn queries_tool() -> ToolDefinition {
    ToolDefinition::new(
        "generate_queries",
        "为原问题生成多个不同的检索查询变体,返回查询字符串数组",
    )
    .with_parameters(json!({
        "type": "object",
        "properties": {
            "queries": {
                "type": "array",
                "items": { "type": "string" }
            }
        },
        "required": ["queries"]
    }))
}

/// Extracts the query array from tool_call arguments.
fn parse_queries(args: &serde_json::Value) -> Option<Vec<String>> {
    args.get("queries")?
        .as_array()?
        .iter()
        .map(|v| v.as_str().map(|s| s.trim().to_string()))
        .collect()
}

/// Text-line parsing: strips numbering ("1. xxx"), bullets, and surrounding quotes so dirty
/// text never becomes a query.
///
/// Takes only the first `limit` lines (usually `num_queries`): the LLM typically puts the
/// queries first, and trailing explanatory prose gets truncated instead of leaking in.
fn parse_query_lines(content: &str, limit: usize) -> Vec<String> {
    content
        .lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .map(|line| {
            let stripped = line.trim_start_matches(['-', '', '*', ' ']);
            let stripped = stripped.trim_start_matches(|c: char| {
                c.is_ascii_digit() || c == '.' || c == '' || c == ')' || c == ' '
            });
            stripped
                .trim_matches(['"', '\'', '', ''])
                .trim()
                .to_string()
        })
        .filter(|q| !q.is_empty())
        .take(limit)
        .collect()
}

/// A static query generator (no LLM dependency)
#[allow(clippy::type_complexity)]
pub struct StaticQueryGenerator {
    expansions: Vec<Box<dyn Fn(&str) -> Vec<String> + Send + Sync>>,
}

impl StaticQueryGenerator {
    /// Creates an empty static query generator
    pub fn new() -> Self {
        Self {
            expansions: Vec::new(),
        }
    }

    /// Adds a synonym-expansion rule
    pub fn with_synonym_expansion(mut self, synonyms: HashMap<String, Vec<String>>) -> Self {
        self.expansions.push(Box::new(move |query: &str| {
            let mut expanded = Vec::new();
            for (word, syns) in &synonyms {
                if query.contains(word) {
                    for syn in syns {
                        expanded.push(query.replace(word, syn));
                    }
                }
            }
            expanded
        }));
        self
    }

    /// Adds a prefix-expansion rule
    pub fn with_prefix_expansion(mut self, prefixes: Vec<String>) -> Self {
        self.expansions.push(Box::new(move |query: &str| {
            prefixes
                .iter()
                .map(|p| format!("{} {}", p, query))
                .collect()
        }));
        self
    }

    /// Applies all expansion rules to generate query variants
    pub fn generate(&self, query: &str) -> Vec<String> {
        self.expansions
            .iter()
            .flat_map(|exp| exp(query))
            .filter(|q| q != query)
            .collect()
    }
}

impl Default for StaticQueryGenerator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_static_query_generator_synonym() {
        let synonyms: HashMap<String, Vec<String>> = HashMap::from([(
            "数据库".to_string(),
            vec!["DB".to_string(), "存储".to_string()],
        )]);

        let generator = StaticQueryGenerator::new().with_synonym_expansion(synonyms);

        let queries = generator.generate("数据库连接失败");

        assert!(queries.contains(&"DB连接失败".to_string()));
        assert!(queries.contains(&"存储连接失败".to_string()));
    }

    #[test]
    fn test_static_query_generator_prefix() {
        let generator = StaticQueryGenerator::new()
            .with_prefix_expansion(vec!["如何".to_string(), "怎么".to_string()]);

        let queries = generator.generate("处理错误");

        assert!(queries.contains(&"如何 处理错误".to_string()));
        assert!(queries.contains(&"怎么 处理错误".to_string()));
    }

    #[test]
    fn test_multi_query_config() {
        let config = MultiQueryConfig::new()
            .with_num_queries(5)
            .with_k_per_query(10)
            .with_final_k(20);

        assert_eq!(config.num_queries, 5);
        assert_eq!(config.k_per_query, 10);
        assert_eq!(config.final_k, 20);
    }

    #[test]
    fn test_multi_query_config_default() {
        let config = MultiQueryConfig::default();

        assert_eq!(config.num_queries, 3);
        assert_eq!(config.k_per_query, 5);
        assert_eq!(config.final_k, 10);
    }

    /// P2-1: The query tool definition carries a queries-array schema.
    #[test]
    fn test_queries_tool_schema() {
        let tool = queries_tool();
        assert_eq!(tool.function.name, "generate_queries");
        let params = tool.function.parameters.expect("parameters should exist");
        assert_eq!(params["properties"]["queries"]["type"], "array");
    }

    /// P2-1: The tool_call arguments parse into a query array.
    #[test]
    fn test_parse_queries() {
        let args = json!({ "queries": ["数据库连接失败怎么办", "DB 连接错误排查"] });
        let queries = parse_queries(&args).expect("should parse successfully");
        assert_eq!(queries.len(), 2);
        assert_eq!(queries[0], "数据库连接失败怎么办");
    }

    /// P2-1: A missing `queries` key → None.
    #[test]
    fn test_parse_queries_missing_key() {
        let args = json!({ "other": 1 });
        assert!(parse_queries(&args).is_none());
    }

    /// P2-1: Text-line parsing strips numbering/bullets/quotes and caps trailing prose by limit.
    #[test]
    fn test_parse_query_lines_cleanup() {
        let content = "1. 数据库连接失败\n- 如何排查 DB 错误\n\"连接超时怎么办\"\n\n补充解释";
        let queries = parse_query_lines(content, 3);
        assert_eq!(
            queries,
            vec![
                "数据库连接失败".to_string(),
                "如何排查 DB 错误".to_string(),
                "连接超时怎么办".to_string(),
            ]
        );
    }

    /// P2-1: Empty text → empty array.
    #[test]
    fn test_parse_query_lines_empty() {
        assert!(parse_query_lines("  \n\n", 3).is_empty());
    }

    /// P2-1: Extra lines beyond the limit are truncated.
    #[test]
    fn test_parse_query_lines_capped() {
        let content = "a\nb\nc\nd";
        assert_eq!(
            parse_query_lines(content, 2),
            vec!["a".to_string(), "b".to_string()]
        );
    }
}