Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tolerate failure to load some rag files #846

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/rag/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ use std::collections::HashMap;
pub const EXTENSION_METADATA: &str = "__extension__";
pub const PATH_METADATA: &str = "__path__";

pub async fn load_document(
loaders: &HashMap<String, String>,
path: &str,
has_error: &mut bool,
) -> (String, Vec<(String, RagMetadata)>) {
let mut maybe_error = None;
let mut files = vec![];
if is_url(path) {
if let Some(path) = path.strip_suffix("**") {
match load_recursive_url(loaders, path).await {
Ok(v) => files.extend(v),
Err(err) => maybe_error = Some(err),
}
} else {
match load_url(loaders, path).await {
Ok(v) => files.push(v),
Err(err) => maybe_error = Some(err),
}
}
} else {
match load_path(loaders, path, has_error).await {
Ok(v) => files.extend(v),
Err(err) => maybe_error = Some(err),
}
}
if let Some(err) = maybe_error {
*has_error = true;
println!("{}", warning_text(&format!("⚠️ {err:?}")));
}
(path.to_string(), files)
}

pub async fn load_recursive_url(
loaders: &HashMap<String, String>,
path: &str,
Expand Down Expand Up @@ -37,7 +69,9 @@ pub async fn load_recursive_url(
pub async fn load_path(
loaders: &HashMap<String, String>,
path: &str,
has_error: &mut bool,
) -> Result<Vec<(String, RagMetadata)>> {
let path = Path::new(path).absolutize()?.display().to_string();
let file_paths = expand_glob_paths(&[path]).await?;
let mut output = vec![];
let file_paths_len = file_paths.len();
Expand All @@ -46,10 +80,15 @@ pub async fn load_path(
1 => output.push(load_file(loaders, &file_paths[0]).await?),
_ => {
for path in file_paths {
println!("🚀 Loading file {path}");
output.push(load_file(loaders, &path).await?)
println!("Load {path}");
match load_file(loaders, &path).await {
Ok(v) => output.push(v),
Err(err) => {
*has_error = true;
println!("{}", warning_text(&format!("Error: {err:?}")));
}
}
}
println!("✨ Load directory completed");
}
}
Ok(output)
Expand Down
33 changes: 3 additions & 30 deletions src/rag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,9 @@ impl Rag {
for (index, path) in paths.iter().enumerate() {
let path = path.as_ref();
println!("Load {path} [{}/{paths_len}]", index + 1);
match load_document(&loaders, path).await {
Ok((path, document_files)) => {
files.extend(document_files);
document_paths.push(path);
}
Err(err) => {
has_error = true;
println!("{}", warning_text(&format!("Error: {err:?}")));
}
}
let (path, document_files) = load_document(&loaders, path, &mut has_error).await;
files.extend(document_files);
document_paths.push(path);
}

if has_error {
Expand Down Expand Up @@ -729,26 +722,6 @@ fn add_documents() -> Result<Vec<String>> {
Ok(paths)
}

async fn load_document(
loaders: &HashMap<String, String>,
path: &str,
) -> Result<(String, Vec<(String, RagMetadata)>)> {
let mut files = vec![];
if is_url(path) {
if let Some(path) = path.strip_suffix("**") {
files.extend(load_recursive_url(loaders, path).await?);
} else {
files.push(load_url(loaders, path).await?);
}
Ok((path.to_string(), files))
} else {
let path = Path::new(path);
let path = path.absolutize()?.display().to_string();
files.extend(load_path(loaders, &path).await?);
Ok((path.to_string(), files))
}
}

fn progress(spinner: &Option<Spinner>, message: String) {
if let Some(spinner) = spinner {
let _ = spinner.set_message(message);
Expand Down