Skip to main content

lc_rag/loaders/
mod.rs

1// src/retrieval/loaders/mod.rs
2//! 文档加载器实现
3//!
4//! 提供从不同格式文件加载文档的功能,包括 PDF、CSV、Text、JSON、Markdown、HTML 等。
5//! v0.4.1 新增: WebScraper、Sitemap、Docx 加载器。
6
7mod csv;
8mod docx;
9mod html;
10mod json;
11mod markdown;
12mod pdf;
13mod sitemap;
14mod text;
15mod web_scraper;
16
17pub use csv::CSVLoader;
18pub use docx::DocxLoader;
19pub use html::HTMLLoader;
20pub use json::JSONLoader;
21pub use markdown::MarkdownLoader;
22pub use pdf::PDFLoader;
23pub use sitemap::SitemapLoader;
24pub use text::TextLoader;
25pub use web_scraper::WebScraperLoader;
26
27use async_trait::async_trait;
28use lc_vector_stores::Document;
29
30/// 文档加载器错误类型
31#[derive(Debug, thiserror::Error)]
32pub enum LoaderError {
33    /// IO 错误
34    #[error("IO 错误: {0}")]
35    IoError(#[from] std::io::Error),
36
37    /// CSV 解析错误
38    #[error("CSV 解析错误: {0}")]
39    CsvError(String),
40
41    /// PDF 解析错误
42    #[error("PDF 解析错误: {0}")]
43    PdfError(String),
44
45    /// JSON 解析错误
46    #[error("JSON 解析错误: {0}")]
47    JsonError(String),
48
49    /// 未知错误
50    #[error("未知错误: {0}")]
51    Other(String),
52}
53
54impl From<pdf_extract::Error> for LoaderError {
55    fn from(err: pdf_extract::Error) -> Self {
56        LoaderError::PdfError(err.to_string())
57    }
58}
59
60/// 文档加载器 trait
61///
62/// 定义从源加载文档的通用接口。
63#[async_trait]
64pub trait DocumentLoader: Send + Sync {
65    /// 从源加载文档
66    ///
67    /// # 返回
68    /// 加载的文档列表
69    async fn load(&self) -> Result<Vec<Document>, LoaderError>;
70}