diff options
| author | sigoden <sigoden@gmail.com> | 2024-06-28 06:24:20 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-28 06:24:20 +0800 |
| commit | 4fbbbd2d991b37ac04b77151ef862de9649bbfec (patch) | |
| tree | 7e4343fb19b8d105b39ad6137d23944d8bffce60 /src/config | |
| parent | 10bd71297db11c163f95625080d956469a1d8689 (diff) | |
| download | aichat-4fbbbd2d991b37ac04b77151ef862de9649bbfec.tar.gz | |
feat: `.file`/`--file` support URLs (#665)
Diffstat (limited to 'src/config')
| -rw-r--r-- | src/config/input.rs | 23 | ||||
| -rw-r--r-- | src/config/mod.rs | 10 |
2 files changed, 23 insertions, 10 deletions
diff --git a/src/config/input.rs b/src/config/input.rs index 3b15d1f..bfc3ea9 100644 --- a/src/config/input.rs +++ b/src/config/input.rs @@ -59,20 +59,25 @@ impl Input { } } - pub fn new( + pub async fn from_files( config: &GlobalConfig, text: &str, files: Vec<String>, role: Option<Role>, ) -> Result<Self> { - let mut texts = vec![text.to_string()]; + let mut texts = vec![]; + if !text.is_empty() { + texts.push(text.to_string()); + }; let mut medias = vec![]; let mut data_urls = HashMap::new(); let files: Vec<_> = files .iter() .map(|f| (f, is_image_ext(Path::new(f)))) .collect(); - let include_filepath = files.iter().filter(|(_, is_image)| !*is_image).count() > 1; + let multi_files = files.iter().filter(|(_, is_image)| !*is_image).count() > 1; + let loaders = config.read().document_loaders.clone(); + let spinner = create_spinner("Loading files").await; for (file_item, is_image) in files { match resolve_local_file(file_item) { Some(file_path) => { @@ -84,7 +89,7 @@ impl Input { } else { let text = read_file(&file_path) .with_context(|| format!("Unable to read file '{file_item}'"))?; - if include_filepath { + if multi_files { texts.push(format!("`{file_item}`:\n~~~~~~\n{text}\n~~~~~~")); } else { texts.push(text); @@ -95,11 +100,19 @@ impl Input { if is_image { medias.push(file_item.to_string()) } else { - bail!("Unable to use remote file '{file_item}"); + let (text, _) = fetch(&loaders, file_item) + .await + .with_context(|| format!("Failed to load '{file_item}'"))?; + if multi_files { + texts.push(format!("`{file_item}`:\n~~~~~~\n{text}\n~~~~~~")); + } else { + texts.push(text); + } } } } } + spinner.stop(); let (role, with_session, with_agent) = resolve_role(&config.read(), role); Ok(Self { diff --git a/src/config/mod.rs b/src/config/mod.rs index 338d7bd..879f511 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -114,7 +114,7 @@ pub struct Config { pub rag_min_score_keyword_search: f32, pub rag_min_score_rerank: f32, #[serde(default)] - pub rag_document_loaders: HashMap<String, String>, + pub document_loaders: HashMap<String, String>, pub rag_template: Option<String>, pub highlight: bool, @@ -174,7 +174,7 @@ impl Default for Config { rag_min_score_vector_search: 0.0, rag_min_score_keyword_search: 0.0, rag_min_score_rerank: 0.0, - rag_document_loaders: Default::default(), + document_loaders: Default::default(), rag_template: None, save_session: None, @@ -230,7 +230,7 @@ impl Config { config.setup_model()?; config.setup_highlight(); config.setup_light_theme()?; - config.setup_rag_document_loaders(); + config.setup_document_loaders(); Ok(config) } @@ -1433,12 +1433,12 @@ impl Config { Ok(()) } - fn setup_rag_document_loaders(&mut self) { + fn setup_document_loaders(&mut self) { [("pdf", "pdftotext $1 -"), ("docx", "pandoc --to plain $1")] .into_iter() .for_each(|(k, v)| { let (k, v) = (k.to_string(), v.to_string()); - self.rag_document_loaders.entry(k).or_insert(v); + self.document_loaders.entry(k).or_insert(v); }); } } |
