loader

package
v0.5.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package loader 提供 RAG 文档加载功能

本文件实现数据连接器:

  • GitHub: 加载 GitHub 仓库、Issues、PR
  • Notion: 加载 Notion 页面和数据库
  • Slack: 加载 Slack 消息和频道
  • Database: 加载 SQL 数据库内容

设计借鉴:

  • LlamaIndex: Data Connectors
  • LangChain: Document Loaders

Package loader 提供 RAG 系统的文档加载器

csv.go 实现 CSV 文件加载器和 Excel/PPTX 加载器:

  • CSVLoader: 标准 CSV 格式解析,支持自定义分隔符、内容列指定
  • ExcelLoader: Excel (.xlsx) 文件加载
  • PPTXLoader: PowerPoint (.pptx) 文件加载

使用示例:

loader := NewCSVLoader("data.csv",
    WithCSVSeparator(';'),
    WithCSVContentColumn("description"),
)
docs, err := loader.Load(ctx)

Package loader 提供 RAG 系统的文档加载器

Loader 用于从各种来源加载文档:

  • TextLoader: 纯文本文件
  • MarkdownLoader: Markdown 文件
  • DirectoryLoader: 目录批量加载
  • URLLoader: 从 URL 加载

Package loader 提供 RAG 系统的文档加载器

本文件实现额外的文档加载器:

  • GitHubLoader: GitHub 仓库加载器
  • YAMLLoader: YAML 文件加载器
  • CompositeLoader: 组合加载器
  • S3Loader: AWS S3 加载器
  • DatabaseLoader: 数据库加载器
  • NotionLoader: Notion 文档加载器
  • SlackLoader: Slack 消息加载器

Package loader 提供 RAG 系统的文档加载器

ocr.go 实现高级文档解析 (OCR) 能力:

  • OCRLoader: 通用 OCR 加载器,支持图片和扫描 PDF
  • OCREngine: OCR 引擎接口,可对接 Tesseract、PaddleOCR 等
  • TesseractEngine: Tesseract OCR 引擎实现
  • VisionLLMEngine: 基于多模态 LLM 的 OCR(如 GPT-4V)

对标 LlamaIndex 的 LlamaParse 高级文档解析能力。

使用示例:

// 方式 1: 使用 Tesseract
engine := NewTesseractEngine(WithTesseractLang("chi_sim+eng"))
loader := NewOCRLoader("scan.pdf", engine)
docs, err := loader.Load(ctx)

// 方式 2: 使用多模态 LLM
engine := NewVisionLLMEngine(llmProvider, "gpt-4-vision-preview")
loader := NewOCRLoader("photo.png", engine)
docs, err := loader.Load(ctx)

Package loader 提供 Hexagon RAG 的文档加载与解析能力

本文件定义文档解析层抽象:把原始字节解析为结构化 Document。

解析层与加载层(Loader)正交:Loader 负责从来源取数据(文件/URL/数据库等), Parser 负责把取到的字节解析成结构化内容。解析引擎可插拔——既能注册本地解析器, 也能注册外部解析服务(如独立的多模态文档理解 gRPC 服务)的适配器,只要其实现 Parser 接口即可接入。多模态:图片经可注入的 VLM 描述后端转为文本描述 Document。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnectorFailed 连接器失败
	ErrConnectorFailed = errors.New("connector failed")

	// ErrAuthFailed 认证失败
	ErrAuthFailed = errors.New("authentication failed")

	// ErrRateLimited 被限流
	ErrRateLimited = errors.New("rate limited")

	// ErrNotFound 未找到
	ErrNotFound = errors.New("not found")
)
View Source
var ErrNoImageDescriber = fmt.Errorf("rag/parser: 未注入 ImageDescriber(图片 VLM 描述后端)")

ErrNoImageDescriber 未注入图片描述后端时返回。

Functions

This section is empty.

Types

type CSVLoader

type CSVLoader struct {
	// contains filtered or unexported fields
}

CSVLoader CSV 文件加载器 将 CSV 文件中的每行数据转换为一个文档

func NewCSVLoader

func NewCSVLoader(path string, opts ...CSVOption) *CSVLoader

NewCSVLoader 创建 CSV 加载器

func (*CSVLoader) Load

func (l *CSVLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 CSV 文件

func (*CSVLoader) Name

func (l *CSVLoader) Name() string

Name 返回加载器名称

type CSVOption

type CSVOption func(*CSVLoader)

CSVOption CSV 加载器选项

func WithCSVContentColumn

func WithCSVContentColumn(column string) CSVOption

WithCSVContentColumn 设置内容列名

func WithCSVContentColumns

func WithCSVContentColumns(cols ...string) CSVOption

WithCSVContentColumns 设置多个内容列名

func WithCSVDelimiter

func WithCSVDelimiter(delim rune) CSVOption

WithCSVDelimiter 设置 CSV 分隔符(WithCSVSeparator 的别名)

func WithCSVHeader

func WithCSVHeader(hasHeader bool) CSVOption

WithCSVHeader 设置是否有表头

func WithCSVMetadataColumns

func WithCSVMetadataColumns(columns []string) CSVOption

WithCSVMetadataColumns 设置元数据列

func WithCSVNoHeader

func WithCSVNoHeader() CSVOption

WithCSVNoHeader 表示 CSV 无表头行

func WithCSVRowsPerDoc

func WithCSVRowsPerDoc(rows int) CSVOption

WithCSVRowsPerDoc 设置每个文档包含的行数

func WithCSVSeparator

func WithCSVSeparator(sep rune) CSVOption

WithCSVSeparator 设置 CSV 分隔符

type CompositeLoader

type CompositeLoader struct {
	// contains filtered or unexported fields
}

CompositeLoader 组合加载器 可以组合多个加载器

func NewCompositeLoader

func NewCompositeLoader(loaders ...rag.Loader) *CompositeLoader

NewCompositeLoader 创建组合加载器

func (*CompositeLoader) AddLoader

func (l *CompositeLoader) AddLoader(loader rag.Loader)

AddLoader 添加加载器

func (*CompositeLoader) Load

func (l *CompositeLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载所有加载器的文档

func (*CompositeLoader) Name

func (l *CompositeLoader) Name() string

Name 返回加载器名称

type Connector

type Connector interface {
	// Name 连接器名称
	Name() string

	// Load 加载文档
	Load(ctx context.Context) ([]*Document, error)
}

Connector 数据连接器接口

type DOCXLoader

type DOCXLoader struct {
	// contains filtered or unexported fields
}

DOCXLoader Word 文档加载器

支持从 DOCX 文件中提取文本内容。 DOCX 是基于 Open XML 格式的文档,本质是一个 ZIP 文件。

func NewDOCXLoader

func NewDOCXLoader(path string, opts ...DOCXOption) *DOCXLoader

NewDOCXLoader 创建 DOCX 加载器

func NewDOCXLoaderFromReader

func NewDOCXLoaderFromReader(r io.ReaderAt, size int64, opts ...DOCXOption) *DOCXLoader

NewDOCXLoaderFromReader 从 ReaderAt 创建 DOCX 加载器

func (*DOCXLoader) Load

func (l *DOCXLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 DOCX 文档

func (*DOCXLoader) Name

func (l *DOCXLoader) Name() string

Name 返回加载器名称

type DOCXOption

type DOCXOption func(*DOCXLoader)

DOCXOption DOCX 加载器选项

func WithDOCXExtractMetadata

func WithDOCXExtractMetadata(extract bool) DOCXOption

WithDOCXExtractMetadata 提取元数据

func WithDOCXPreserveParagraphs

func WithDOCXPreserveParagraphs(preserve bool) DOCXOption

WithDOCXPreserveParagraphs 保留段落结构

func WithDOCXSplitByHeading

func WithDOCXSplitByHeading(split bool) DOCXOption

WithDOCXSplitByHeading 按标题分割

type DatabaseConfig

type DatabaseConfig struct {
	// DB 数据库连接
	DB *sql.DB

	// Query SQL 查询
	Query string

	// Columns 要提取的列
	Columns []string

	// Template 文档模板(使用 {{.ColumnName}} 语法)
	Template string
}

DatabaseConfig 数据库连接器配置

type DatabaseConnector

type DatabaseConnector struct {
	// contains filtered or unexported fields
}

DatabaseConnector SQL 数据库连接器

func NewDatabaseConnector

func NewDatabaseConnector(config *DatabaseConfig) *DatabaseConnector

NewDatabaseConnector 创建数据库连接器

func (*DatabaseConnector) Load

func (dc *DatabaseConnector) Load(ctx context.Context) ([]*Document, error)

Load 加载数据库内容

func (*DatabaseConnector) Name

func (dc *DatabaseConnector) Name() string

Name 返回连接器名称

type DatabaseLoader

type DatabaseLoader struct {
	// contains filtered or unexported fields
}

DatabaseLoader 数据库加载器

func NewDatabaseLoader

func NewDatabaseLoader(driver, dsn string, opts ...DatabaseOption) *DatabaseLoader

NewDatabaseLoader 创建数据库加载器

func (*DatabaseLoader) Load

func (l *DatabaseLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 从数据库加载文档 注意:这是一个占位实现,实际使用需要注入数据库驱动

func (*DatabaseLoader) Name

func (l *DatabaseLoader) Name() string

Name 返回加载器名称

type DatabaseOption

type DatabaseOption func(*DatabaseLoader)

DatabaseOption 数据库加载器选项

func WithDBContentColumn

func WithDBContentColumn(col string) DatabaseOption

WithDBContentColumn 设置内容列

func WithDBMetadataColumns

func WithDBMetadataColumns(cols []string) DatabaseOption

WithDBMetadataColumns 设置元数据列

func WithDBQuery

func WithDBQuery(query string) DatabaseOption

WithDBQuery 设置查询语句

type DirectoryLoader

type DirectoryLoader struct {
	// contains filtered or unexported fields
}

DirectoryLoader 目录批量加载器

func NewDirectoryLoader

func NewDirectoryLoader(path string, opts ...DirectoryOption) *DirectoryLoader

NewDirectoryLoader 创建目录加载器

func (*DirectoryLoader) Load

func (l *DirectoryLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载目录中的所有文件

func (*DirectoryLoader) Name

func (l *DirectoryLoader) Name() string

Name 返回加载器名称

type DirectoryOption

type DirectoryOption func(*DirectoryLoader)

DirectoryOption 目录加载器选项

func WithLoaderFunc

func WithLoaderFunc(fn func(path string) rag.Loader) DirectoryOption

WithLoaderFunc 设置自定义加载器工厂

func WithPattern

func WithPattern(pattern string) DirectoryOption

WithPattern 设置文件匹配模式

func WithRecursive

func WithRecursive(recursive bool) DirectoryOption

WithRecursive 设置是否递归

type Document

type Document = rag.Document

Document 是 rag.Document 的别名

type EnhancedPDFParser

type EnhancedPDFParser struct{}

EnhancedPDFParser 增强型 PDF 解析器

支持现代 PDF 的核心特性:xref 表、压缩流、文本操作符解析、元数据提取。 适用于大部分标准 PDF 文件的文本提取场景。

限制:

  • 不支持加密 PDF
  • 不支持 CMap 字体映射(CID 字体可能输出为乱码)
  • 不支持 XRef Stream(PDF 1.5+)的交叉引用流
  • 不处理图片、表格等非文本内容

func (*EnhancedPDFParser) Parse

Parse 解析 PDF 并返回结构化文档

解析流程:

  1. 读取全部数据并验证 PDF 签名
  2. 解析 xref 交叉引用表定位对象
  3. 提取 /Info 字典中的元数据
  4. 遍历页面树获取每页内容流
  5. 解压并解析内容流中的文本操作符

如果增强解析失败,自动降级到简单文本提取。

type ExcelLoader

type ExcelLoader struct {
	// contains filtered or unexported fields
}

ExcelLoader Excel 文件加载器(.xlsx 格式) 将 .xlsx 文件中的每行数据转换为一个文档

实现原理:.xlsx 文件本质是 ZIP 包,内含 XML 描述的工作表数据。 当前为基础实现,如需完整的 Excel 功能建议使用 excelize 等第三方库。

func NewExcelLoader

func NewExcelLoader(path string, opts ...ExcelOption) *ExcelLoader

NewExcelLoader 创建 Excel 加载器

func (*ExcelLoader) Load

func (l *ExcelLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 Excel 文件 将 xlsx 文件每行转换为一个 Document

func (*ExcelLoader) Name

func (l *ExcelLoader) Name() string

Name 返回加载器名称

type ExcelOption

type ExcelOption func(*ExcelLoader)

ExcelOption Excel 加载器配置选项

func WithExcelContentColumns

func WithExcelContentColumns(cols ...string) ExcelOption

WithExcelContentColumns 设置用作内容的列名

func WithExcelMetadataColumns

func WithExcelMetadataColumns(cols ...string) ExcelOption

WithExcelMetadataColumns 设置用作元数据的列名

func WithExcelSheet

func WithExcelSheet(name string) ExcelOption

WithExcelSheet 设置工作表名称(空表示第一个工作表)

type GitHubConfig

type GitHubConfig struct {
	// Token GitHub 访问令牌
	Token string

	// Owner 仓库所有者
	Owner string

	// Repo 仓库名称
	Repo string

	// Branch 分支名称
	Branch string

	// Path 文件路径(可选)
	Path string

	// LoadType 加载类型
	LoadType GitHubLoadType

	// FileExtensions 文件扩展名过滤(仅 LoadFiles)
	FileExtensions []string

	// IssueState Issue 状态过滤
	IssueState string // "open", "closed", "all"

	// MaxItems 最大项数
	MaxItems int
}

GitHubConfig GitHub 连接器配置

type GitHubConnector

type GitHubConnector struct {
	// contains filtered or unexported fields
}

GitHubConnector GitHub 数据连接器

func NewGitHubConnector

func NewGitHubConnector(config *GitHubConfig) *GitHubConnector

NewGitHubConnector 创建 GitHub 连接器

func (*GitHubConnector) Load

func (gc *GitHubConnector) Load(ctx context.Context) ([]*Document, error)

Load 加载 GitHub 内容

func (*GitHubConnector) Name

func (gc *GitHubConnector) Name() string

Name 返回连接器名称

type GitHubLoadType

type GitHubLoadType int

GitHubLoadType GitHub 加载类型

const (
	// GitHubLoadFiles 加载文件
	GitHubLoadFiles GitHubLoadType = iota
	// GitHubLoadIssues 加载 Issues
	GitHubLoadIssues
	// GitHubLoadPRs 加载 Pull Requests
	GitHubLoadPRs
	// GitHubLoadDiscussions 加载 Discussions
	GitHubLoadDiscussions
)

type GitHubLoader

type GitHubLoader struct {
	// contains filtered or unexported fields
}

GitHubLoader GitHub 仓库加载器

func NewGitHubLoader

func NewGitHubLoader(owner, repo string, opts ...GitHubOption) *GitHubLoader

NewGitHubLoader 创建 GitHub 加载器

func (*GitHubLoader) Load

func (l *GitHubLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 GitHub 仓库内容

func (*GitHubLoader) Name

func (l *GitHubLoader) Name() string

Name 返回加载器名称

type GitHubOption

type GitHubOption func(*GitHubLoader)

GitHubOption GitHub 加载器选项

func WithGitHubBranch

func WithGitHubBranch(branch string) GitHubOption

WithGitHubBranch 设置分支

func WithGitHubExtensions

func WithGitHubExtensions(exts []string) GitHubOption

WithGitHubExtensions 设置文件扩展名过滤

func WithGitHubPath

func WithGitHubPath(path string) GitHubOption

WithGitHubPath 设置仓库内路径

func WithGitHubToken

func WithGitHubToken(token string) GitHubOption

WithGitHubToken 设置 GitHub token

type HTMLLoader

type HTMLLoader struct {
	// contains filtered or unexported fields
}

HTMLLoader HTML 文档加载器

func NewHTMLLoader

func NewHTMLLoader(path string, opts ...HTMLOption) *HTMLLoader

NewHTMLLoader 创建 HTML 加载器

func NewHTMLLoaderFromReader

func NewHTMLLoaderFromReader(r io.Reader, url string, opts ...HTMLOption) *HTMLLoader

NewHTMLLoaderFromReader 从 Reader 创建 HTML 加载器

func (*HTMLLoader) Load

func (l *HTMLLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 HTML 文档

func (*HTMLLoader) Name

func (l *HTMLLoader) Name() string

Name 返回加载器名称

type HTMLOption

type HTMLOption func(*HTMLLoader)

HTMLOption HTML 加载器选项

func WithHTMLExtractTitle

func WithHTMLExtractTitle(extract bool) HTMLOption

WithHTMLExtractTitle 提取标题

func WithHTMLRemoveScripts

func WithHTMLRemoveScripts(remove bool) HTMLOption

WithHTMLRemoveScripts 移除脚本

func WithHTMLRemoveStyles

func WithHTMLRemoveStyles(remove bool) HTMLOption

WithHTMLRemoveStyles 移除样式

type ImageDescriber

type ImageDescriber interface {
	// Describe 返回图片的文本描述。
	Describe(ctx context.Context, image []byte, mime string) (string, error)
}

ImageDescriber 是图片描述后端(VLM)的接口约定(seam)。

框架定义接口,具体后端(多模态 LLM 客户端)由调用方注入,不内置/不伪造, 避免在解析层硬依赖某家 VLM。

type ImageParser

type ImageParser struct {
	// contains filtered or unexported fields
}

ImageParser 把图片解析为其文本描述 Document(多模态文档理解)。

func NewImageParser

func NewImageParser(describer ImageDescriber) *ImageParser

NewImageParser 创建图片解析器;describer 为图片描述后端(nil 时 Parse 返回错误)。

func (*ImageParser) CanParse

func (p *ImageParser) CanParse(mime string) bool

CanParse 支持 image/* 类型。

func (*ImageParser) Parse

func (p *ImageParser) Parse(ctx context.Context, in ParseInput) ([]Document, error)

Parse 调用 VLM 描述后端把图片转为文本描述 Document;未注入后端则返回 ErrNoImageDescriber。

type JSONLoader

type JSONLoader struct {
	// contains filtered or unexported fields
}

JSONLoader JSON 文件加载器

func NewJSONLoader

func NewJSONLoader(path string, opts ...JSONOption) *JSONLoader

NewJSONLoader 创建 JSON 加载器

func (*JSONLoader) Load

func (l *JSONLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 JSON 文件

func (*JSONLoader) Name

func (l *JSONLoader) Name() string

Name 返回加载器名称

type JSONOption

type JSONOption func(*JSONLoader)

JSONOption JSON 加载器选项

func WithJSONContentKey

func WithJSONContentKey(key string) JSONOption

WithJSONContentKey 设置内容键

func WithJSONMetadataKeys

func WithJSONMetadataKeys(keys []string) JSONOption

WithJSONMetadataKeys 设置元数据键

type MarkdownLoader

type MarkdownLoader struct {
	// contains filtered or unexported fields
}

MarkdownLoader Markdown 文件加载器

func NewMarkdownLoader

func NewMarkdownLoader(path string, opts ...MarkdownOption) *MarkdownLoader

NewMarkdownLoader 创建 Markdown 加载器

func (*MarkdownLoader) Load

func (l *MarkdownLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 Markdown 文件

func (*MarkdownLoader) Name

func (l *MarkdownLoader) Name() string

Name 返回加载器名称

type MarkdownOption

type MarkdownOption func(*MarkdownLoader)

MarkdownOption Markdown 加载器选项

func WithExtractMetadata

func WithExtractMetadata(extract bool) MarkdownOption

WithExtractMetadata 提取 front matter 元数据

func WithRemoveImages

func WithRemoveImages(remove bool) MarkdownOption

WithRemoveImages 移除图片

func WithRemoveLinks(remove bool) MarkdownOption

WithRemoveLinks 移除链接

type NotionConfig

type NotionConfig struct {
	// Token Notion 集成令牌
	Token string

	// PageID 页面 ID(可选)
	PageID string

	// DatabaseID 数据库 ID(可选)
	DatabaseID string
}

NotionConfig Notion 连接器配置

type NotionConnector

type NotionConnector struct {
	// contains filtered or unexported fields
}

NotionConnector Notion 数据连接器

func NewNotionConnector

func NewNotionConnector(config *NotionConfig) *NotionConnector

NewNotionConnector 创建 Notion 连接器

func (*NotionConnector) Load

func (nc *NotionConnector) Load(ctx context.Context) ([]*Document, error)

Load 加载 Notion 内容

func (*NotionConnector) Name

func (nc *NotionConnector) Name() string

Name 返回连接器名称

type NotionLoader

type NotionLoader struct {
	// contains filtered or unexported fields
}

NotionLoader Notion 文档加载器

func NewNotionLoader

func NewNotionLoader(apiKey string, opts ...NotionOption) *NotionLoader

NewNotionLoader 创建 Notion 加载器

func (*NotionLoader) Load

func (l *NotionLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 从 Notion 加载文档

func (*NotionLoader) Name

func (l *NotionLoader) Name() string

Name 返回加载器名称

type NotionOption

type NotionOption func(*NotionLoader)

NotionOption Notion 加载器选项

func WithNotionDatabaseID

func WithNotionDatabaseID(id string) NotionOption

WithNotionDatabaseID 设置 Notion 数据库 ID

func WithNotionPageID

func WithNotionPageID(id string) NotionOption

WithNotionPageID 设置 Notion 页面 ID

type OCREngine

type OCREngine interface {
	// ExtractText 从文件中提取文字
	// filePath: 图片或 PDF 文件路径
	// 返回提取的文字内容和可能的元数据
	ExtractText(ctx context.Context, filePath string) (*OCRResult, error)

	// Name 返回引擎名称
	Name() string

	// SupportedFormats 返回支持的文件格式
	SupportedFormats() []string
}

OCREngine OCR 引擎接口 实现此接口以对接不同的 OCR 后端

type OCRLoader

type OCRLoader struct {
	// contains filtered or unexported fields
}

OCRLoader OCR 文档加载器 使用 OCR 引擎从图片或扫描文档中提取文字

func NewOCRLoader

func NewOCRLoader(filePath string, engine OCREngine, opts ...OCRLoaderOption) *OCRLoader

NewOCRLoader 创建 OCR 加载器

func (*OCRLoader) Load

func (l *OCRLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 使用 OCR 加载文档

func (*OCRLoader) Name

func (l *OCRLoader) Name() string

Name 返回加载器名称

type OCRLoaderOption

type OCRLoaderOption func(*OCRLoader)

OCRLoaderOption OCR 加载器选项

func WithOCRMetadata

func WithOCRMetadata(key string, value any) OCRLoaderOption

WithOCRMetadata 设置加载元数据

type OCRPage

type OCRPage struct {
	// PageNum 页码(从 1 开始)
	PageNum int

	// Text 该页文本
	Text string

	// Confidence 该页置信度
	Confidence float64
}

OCRPage 单页 OCR 结果

type OCRResult

type OCRResult struct {
	// Text 提取的完整文本
	Text string

	// Pages 分页结果(PDF 场景)
	Pages []OCRPage

	// Language 检测到的语言
	Language string

	// Confidence 整体置信度 (0-1)
	Confidence float64

	// Metadata 额外元数据
	Metadata map[string]any
}

OCRResult OCR 提取结果

type PDFDocument

type PDFDocument struct {
	// Pages 页面内容列表
	Pages []string

	// Metadata PDF 元数据
	Metadata PDFMetadata
}

PDFDocument 解析后的 PDF 文档

type PDFLoader

type PDFLoader struct {
	// contains filtered or unexported fields
}

PDFLoader PDF 文档加载器

支持从 PDF 文件中提取文本内容。 注意:此实现为基础版本,使用简单的文本提取。 对于复杂的 PDF(包含图片、表格等),建议使用专业的 PDF 库。

func NewPDFLoader

func NewPDFLoader(path string, opts ...PDFOption) *PDFLoader

NewPDFLoader 创建 PDF 加载器

默认使用 EnhancedPDFParser 进行解析,支持压缩流、xref 表、 hex 字符串等现代 PDF 特性。如需降级到简单解析器,可使用 WithPDFParser(&SimplePDFParser{}) 选项。

func NewPDFLoaderFromReader

func NewPDFLoaderFromReader(r io.Reader, opts ...PDFOption) *PDFLoader

NewPDFLoaderFromReader 从 Reader 创建 PDF 加载器

默认使用 EnhancedPDFParser 进行解析。

func (*PDFLoader) Load

func (l *PDFLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 PDF 文档

func (*PDFLoader) Name

func (l *PDFLoader) Name() string

Name 返回加载器名称

type PDFMetadata

type PDFMetadata struct {
	// Title 标题
	Title string `json:"title,omitempty"`

	// Author 作者
	Author string `json:"author,omitempty"`

	// Subject 主题
	Subject string `json:"subject,omitempty"`

	// Creator 创建者
	Creator string `json:"creator,omitempty"`

	// Producer 生产者
	Producer string `json:"producer,omitempty"`

	// CreationDate 创建日期
	CreationDate time.Time `json:"creation_date,omitempty"`

	// ModDate 修改日期
	ModDate time.Time `json:"mod_date,omitempty"`

	// PageCount 页数
	PageCount int `json:"page_count"`
}

PDFMetadata PDF 元数据

type PDFOption

type PDFOption func(*PDFLoader)

PDFOption PDF 加载器选项

func WithPDFExtractMetadata

func WithPDFExtractMetadata(extract bool) PDFOption

WithPDFExtractMetadata 设置是否提取元数据

func WithPDFPageRange

func WithPDFPageRange(start, end int) PDFOption

WithPDFPageRange 设置页面范围

func WithPDFParser

func WithPDFParser(parser PDFParser) PDFOption

WithPDFParser 设置自定义 PDF 解析器

func WithPDFPassword

func WithPDFPassword(password string) PDFOption

WithPDFPassword 设置密码

func WithPDFSplitPages

func WithPDFSplitPages(split bool) PDFOption

WithPDFSplitPages 按页面分割

type PDFParser

type PDFParser interface {
	// Parse 解析 PDF 并返回页面内容
	Parse(ctx context.Context, r io.Reader) (*PDFDocument, error)
}

PDFParser PDF 解析器接口

定义 PDF 解析的抽象接口,允许使用不同的 PDF 库实现。 默认提供基础的文本提取实现。

type PPTXLoader

type PPTXLoader struct {
	// contains filtered or unexported fields
}

PPTXLoader PowerPoint 文件加载器(.pptx 格式) 提取每张幻灯片的文本内容,每张幻灯片生成一个文档

func NewPPTXLoader

func NewPPTXLoader(path string, opts ...PPTXOption) *PPTXLoader

NewPPTXLoader 创建 PPTX 加载器

func (*PPTXLoader) Load

func (l *PPTXLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 PPTX 文件

func (*PPTXLoader) Name

func (l *PPTXLoader) Name() string

Name 返回加载器名称

type PPTXOption

type PPTXOption func(*PPTXLoader)

PPTXOption PPTX 加载器配置选项

func WithSlidePerDoc

func WithSlidePerDoc(enabled bool) PPTXOption

WithSlidePerDoc 每张幻灯片生成一个文档

type ParseEngine

type ParseEngine struct {
	// contains filtered or unexported fields
}

ParseEngine 是可插拔的解析引擎:按 MIME 把输入路由到匹配的 Parser。

未匹配到任何解析器时使用 fallback(若设置),否则返回错误。线程安全。

func NewParseEngine

func NewParseEngine(parsers ...Parser) *ParseEngine

NewParseEngine 创建解析引擎并注册初始解析器。

func (*ParseEngine) Parse

func (e *ParseEngine) Parse(ctx context.Context, in ParseInput) ([]Document, error)

Parse 按 MIME 选择支持的解析器解析输入。

func (*ParseEngine) Register

func (e *ParseEngine) Register(p Parser)

Register 追加注册一个解析器。

func (*ParseEngine) SetFallback

func (e *ParseEngine) SetFallback(p Parser)

SetFallback 设置兜底解析器(所有已注册解析器都不支持该 MIME 时使用)。

type ParseInput

type ParseInput struct {
	// Content 待解析的原始字节
	Content []byte
	// MIME 内容类型(如 "application/pdf" / "image/png" / "text/markdown")
	MIME string
	// Filename 可选来源文件名,用于按扩展名辅助推断格式
	Filename string
	// Metadata 透传到产出 Document 的元数据
	Metadata map[string]any
}

ParseInput 解析输入:原始字节 + 内容类型/来源提示。

type Parser

type Parser interface {
	// Parse 解析输入为结构化文档。
	Parse(ctx context.Context, in ParseInput) ([]Document, error)
	// CanParse 报告本解析器是否支持给定 MIME 类型。
	CanParse(mime string) bool
}

Parser 是文档解析层抽象:把原始字节解析为一个或多个结构化 Document。

type ReaderLoader

type ReaderLoader struct {
	// contains filtered or unexported fields
}

ReaderLoader 从 io.Reader 加载

func NewReaderLoader

func NewReaderLoader(r io.Reader, source string) *ReaderLoader

NewReaderLoader 创建 Reader 加载器

func (*ReaderLoader) Load

func (l *ReaderLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 从 Reader 加载

func (*ReaderLoader) Name

func (l *ReaderLoader) Name() string

Name 返回加载器名称

type S3Loader

type S3Loader struct {
	// contains filtered or unexported fields
}

S3Loader AWS S3 加载器

func NewS3Loader

func NewS3Loader(bucket string, opts ...S3Option) *S3Loader

NewS3Loader 创建 S3 加载器

func (*S3Loader) Load

func (l *S3Loader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 S3 对象 注意:这是一个占位实现,实际使用需要注入 AWS SDK

func (*S3Loader) Name

func (l *S3Loader) Name() string

Name 返回加载器名称

type S3Option

type S3Option func(*S3Loader)

S3Option S3 加载器选项

func WithS3Extensions

func WithS3Extensions(exts []string) S3Option

WithS3Extensions 设置文件扩展名过滤

func WithS3Prefix

func WithS3Prefix(prefix string) S3Option

WithS3Prefix 设置前缀

func WithS3Region

func WithS3Region(region string) S3Option

WithS3Region 设置区域

type SimplePDFParser

type SimplePDFParser struct{}

SimplePDFParser 简单的 PDF 解析器

这是一个基础实现,从 PDF 二进制流中提取文本。 对于复杂的 PDF 文档,建议使用专业的 PDF 库(如 pdfcpu、unidoc 等)。

func (*SimplePDFParser) Parse

func (p *SimplePDFParser) Parse(ctx context.Context, r io.Reader) (*PDFDocument, error)

Parse 解析 PDF

注意:这是一个简化的实现,主要用于演示。 实际使用中建议集成专业的 PDF 解析库。

type SlackConfig

type SlackConfig struct {
	// Token Slack Bot Token
	Token string

	// ChannelID 频道 ID
	ChannelID string

	// Limit 消息数量限制
	Limit int
}

SlackConfig Slack 连接器配置

type SlackConnector

type SlackConnector struct {
	// contains filtered or unexported fields
}

SlackConnector Slack 数据连接器

func NewSlackConnector

func NewSlackConnector(config *SlackConfig) *SlackConnector

NewSlackConnector 创建 Slack 连接器

func (*SlackConnector) Load

func (sc *SlackConnector) Load(ctx context.Context) ([]*Document, error)

Load 加载 Slack 消息

func (*SlackConnector) Name

func (sc *SlackConnector) Name() string

Name 返回连接器名称

type SlackLoader

type SlackLoader struct {
	// contains filtered or unexported fields
}

SlackLoader Slack 消息加载器

func NewSlackLoader

func NewSlackLoader(token string, opts ...SlackOption) *SlackLoader

NewSlackLoader 创建 Slack 加载器

func (*SlackLoader) Load

func (l *SlackLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 从 Slack 加载消息

func (*SlackLoader) Name

func (l *SlackLoader) Name() string

Name 返回加载器名称

type SlackOption

type SlackOption func(*SlackLoader)

SlackOption Slack 加载器选项

func WithSlackChannelID

func WithSlackChannelID(id string) SlackOption

WithSlackChannelID 设置频道 ID

func WithSlackLimit

func WithSlackLimit(limit int) SlackOption

WithSlackLimit 设置消息数量限制

type StringLoader

type StringLoader struct {
	// contains filtered or unexported fields
}

StringLoader 从字符串加载

func NewStringLoader

func NewStringLoader(content, source string) *StringLoader

NewStringLoader 创建字符串加载器

func (*StringLoader) Load

func (l *StringLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 从字符串加载

func (*StringLoader) Name

func (l *StringLoader) Name() string

Name 返回加载器名称

type TesseractEngine

type TesseractEngine struct {
	// contains filtered or unexported fields
}

TesseractEngine Tesseract OCR 引擎 需要系统安装 tesseract 命令行工具

func NewTesseractEngine

func NewTesseractEngine(opts ...TesseractOption) *TesseractEngine

NewTesseractEngine 创建 Tesseract OCR 引擎

func (*TesseractEngine) ExtractText

func (e *TesseractEngine) ExtractText(ctx context.Context, filePath string) (*OCRResult, error)

ExtractText 使用 Tesseract 提取文字

func (*TesseractEngine) Name

func (e *TesseractEngine) Name() string

func (*TesseractEngine) SupportedFormats

func (e *TesseractEngine) SupportedFormats() []string

type TesseractOption

type TesseractOption func(*TesseractEngine)

TesseractOption Tesseract 选项

func WithTesseractLang

func WithTesseractLang(lang string) TesseractOption

WithTesseractLang 设置识别语言

func WithTesseractPSM

func WithTesseractPSM(psm string) TesseractOption

WithTesseractPSM 设置页面分割模式

func WithTesseractPath

func WithTesseractPath(path string) TesseractOption

WithTesseractPath 设置 tesseract 路径

type TextLoader

type TextLoader struct {
	// contains filtered or unexported fields
}

TextLoader 纯文本文件加载器

func NewTextLoader

func NewTextLoader(path string) *TextLoader

NewTextLoader 创建文本加载器

func (*TextLoader) Load

func (l *TextLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载文本文件

func (*TextLoader) Name

func (l *TextLoader) Name() string

Name 返回加载器名称

type TextParser

type TextParser struct{}

TextParser 解析纯文本/Markdown 等文本类内容,直接把字节作为文档内容。

func NewTextParser

func NewTextParser() *TextParser

NewTextParser 创建文本解析器。

func (*TextParser) CanParse

func (p *TextParser) CanParse(mime string) bool

CanParse 支持 text/* 与常见结构化文本类型。

func (*TextParser) Parse

func (p *TextParser) Parse(_ context.Context, in ParseInput) ([]Document, error)

Parse 把字节包装为单个 Document。

type URLLoader

type URLLoader struct {
	// contains filtered or unexported fields
}

URLLoader URL 加载器

func NewURLLoader

func NewURLLoader(url string, opts ...URLOption) *URLLoader

NewURLLoader 创建 URL 加载器

func (*URLLoader) Load

func (l *URLLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 从 URL 加载内容

func (*URLLoader) Name

func (l *URLLoader) Name() string

Name 返回加载器名称

type URLOption

type URLOption func(*URLLoader)

URLOption URL 加载器选项

func WithHTTPClient

func WithHTTPClient(client *http.Client) URLOption

WithHTTPClient 设置 HTTP 客户端

func WithHeaders

func WithHeaders(headers map[string]string) URLOption

WithHeaders 设置请求头

func WithUserAgent

func WithUserAgent(ua string) URLOption

WithUserAgent 设置 User-Agent

type VisionLLMEngine

type VisionLLMEngine struct {
	// contains filtered or unexported fields
}

VisionLLMEngine 基于多模态 LLM 的 OCR 引擎 使用 GPT-4V、Gemini Vision 等多模态模型提取文字

func NewVisionLLMEngine

func NewVisionLLMEngine(provider llm.Provider, opts ...VisionLLMOption) *VisionLLMEngine

NewVisionLLMEngine 创建 Vision LLM OCR 引擎

func (*VisionLLMEngine) ExtractText

func (e *VisionLLMEngine) ExtractText(ctx context.Context, filePath string) (*OCRResult, error)

ExtractText 使用多模态 LLM 提取文字 将图片文件读取后 base64 编码,通过 MultiContent 消息发送给 LLM

func (*VisionLLMEngine) Name

func (e *VisionLLMEngine) Name() string

func (*VisionLLMEngine) SupportedFormats

func (e *VisionLLMEngine) SupportedFormats() []string

type VisionLLMOption

type VisionLLMOption func(*VisionLLMEngine)

VisionLLMOption Vision LLM 选项

func WithVisionModel

func WithVisionModel(model string) VisionLLMOption

WithVisionModel 设置视觉模型

func WithVisionSystemPrompt

func WithVisionSystemPrompt(prompt string) VisionLLMOption

WithVisionSystemPrompt 设置系统提示词

type WebAPIConfig

type WebAPIConfig struct {
	// URL API 端点
	URL string

	// Method HTTP 方法
	Method string

	// Headers 请求头
	Headers map[string]string

	// Body 请求体
	Body string

	// JSONPath JSON 路径(提取数组)
	JSONPath string
}

WebAPIConfig Web API 连接器配置

type WebAPIConnector

type WebAPIConnector struct {
	// contains filtered or unexported fields
}

WebAPIConnector Web API 连接器

func NewWebAPIConnector

func NewWebAPIConnector(config *WebAPIConfig) *WebAPIConnector

NewWebAPIConnector 创建 Web API 连接器

func (*WebAPIConnector) Load

func (wc *WebAPIConnector) Load(ctx context.Context) ([]*Document, error)

Load 加载 API 数据

func (*WebAPIConnector) Name

func (wc *WebAPIConnector) Name() string

Name 返回连接器名称

type YAMLLoader

type YAMLLoader struct {
	// contains filtered or unexported fields
}

YAMLLoader YAML 文件加载器

func NewYAMLLoader

func NewYAMLLoader(path string, opts ...YAMLOption) *YAMLLoader

NewYAMLLoader 创建 YAML 加载器

func (*YAMLLoader) Load

func (l *YAMLLoader) Load(ctx context.Context) ([]rag.Document, error)

Load 加载 YAML 文件

func (*YAMLLoader) Name

func (l *YAMLLoader) Name() string

Name 返回加载器名称

type YAMLOption

type YAMLOption func(*YAMLLoader)

YAMLOption YAML 加载器选项

func WithMultiDoc

func WithMultiDoc(multi bool) YAMLOption

WithMultiDoc 设置为多文档模式

func WithYAMLContentKey

func WithYAMLContentKey(key string) YAMLOption

WithYAMLContentKey 设置内容键

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL