summaryrefslogtreecommitdiffstats
path: root/src/rag/mod.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-11-06 19:57:47 +0800
committerGitHub <noreply@github.com>2024-11-06 19:57:47 +0800
commitcc74b36740833b69f527d0b395331b74257644b6 (patch)
treeb9b8782fbf66f69886fe011a64638e62c23a44fa /src/rag/mod.rs
parent3ffa87689169b50cf3b551a7dcd31358ddafe32e (diff)
downloadaichat-cc74b36740833b69f527d0b395331b74257644b6.tar.gz
refactor: `.sources rag` show document ids (#975)
Diffstat (limited to 'src/rag/mod.rs')
-rw-r--r--src/rag/mod.rs26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/rag/mod.rs b/src/rag/mod.rs
index 1fd0376..4ddb0da 100644
--- a/src/rag/mod.rs
+++ b/src/rag/mod.rs
@@ -214,18 +214,26 @@ impl Rag {
}
pub fn set_last_sources(&self, ids: &[DocumentId]) {
- let sources: IndexSet<_> = ids
- .iter()
- .filter_map(|id| {
- let (file_index, _) = id.split();
- let file = self.data.files.get(&file_index)?;
- Some(file.path.clone())
- })
- .collect();
+ let mut sources: IndexMap<String, Vec<String>> = IndexMap::new();
+ for id in ids {
+ let (file_index, _) = id.split();
+ if let Some(file) = self.data.files.get(&file_index) {
+ sources
+ .entry(file.path.clone())
+ .or_default()
+ .push(format!("{id:?}"));
+ }
+ }
let sources = if sources.is_empty() {
None
} else {
- Some(sources.into_iter().collect::<Vec<_>>().join("\n"))
+ Some(
+ sources
+ .into_iter()
+ .map(|(path, ids)| format!("{path} ({})", ids.join(",")))
+ .collect::<Vec<_>>()
+ .join("\n"),
+ )
};
*self.last_sources.write() = sources;
}