multimodal

package
v0.5.13 Latest Latest
Warning

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

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

Documentation

Overview

Package multimodal 提供多模态 RAG 支持

本包实现多模态检索增强生成:

  • ImageProcessor: 图像处理器
  • AudioProcessor: 音频处理器
  • VideoProcessor: 视频处理器
  • MultimodalDocument: 多模态文档
  • MultimodalRetriever: 多模态检索器

支持的内容类型:

  • 图像: PNG, JPEG, GIF, WebP
  • 音频: MP3, WAV, OGG
  • 视频: MP4, WebM
  • 文档: PDF, DOCX (带嵌入图片)

设计参考:

  • GPT-4V, Claude Vision

Index

Constants

This section is empty.

Variables

View Source
var ErrCLIPNotImplemented = fmt.Errorf("CLIP embedding 未配置后端: 请用 WithCLIPBackend 注入真实 CLIP 后端(OpenAI CLIP / HF Inference / clip-as-service / 自建服务)")

ErrCLIPNotImplemented 表示未注入 CLIP 后端时调用嵌入方法返回的错误。

Functions

This section is empty.

Types

type AudioFormat

type AudioFormat string

AudioFormat 音频格式

const (
	AudioFormatMP3 AudioFormat = "mp3"
	AudioFormatWAV AudioFormat = "wav"
	AudioFormatOGG AudioFormat = "ogg"
)

type AudioProcessor

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

AudioProcessor 音频处理器

func NewAudioProcessor

func NewAudioProcessor(transcriber AudioTranscriber) *AudioProcessor

NewAudioProcessor 创建音频处理器

func (*AudioProcessor) Process

func (p *AudioProcessor) Process(ctx context.Context, content *Content) (*ProcessResult, error)

Process 处理音频

func (*AudioProcessor) SupportedTypes

func (p *AudioProcessor) SupportedTypes() []ContentType

SupportedTypes 支持的内容类型

type AudioTranscriber

type AudioTranscriber interface {
	// Transcribe 将音频转录为文本
	Transcribe(ctx context.Context, audio *Content) (string, error)
}

AudioTranscriber 音频转录接口

type CLIPBackend

type CLIPBackend interface {
	// EmbedText 向量化文本,返回与图像向量同空间、可比较的向量。
	EmbedText(ctx context.Context, texts []string) ([][]float32, error)
	// EmbedImage 向量化单张图像。
	EmbedImage(ctx context.Context, image *Content) ([]float32, error)
}

CLIPBackend 是 CLIP 向量化的外部后端接口。

框架不内置 CLIP 模型(需 GPU / 外部推理服务),而是通过本接口让用户注入真实后端:

  • OpenAI / Hugging Face Inference API
  • clip-as-service 或自建 CLIP gRPC/HTTP 服务

注入后 CLIPEmbedder 的方法委托给后端;未注入时返回 ErrCLIPNotImplemented。 这样框架在不内置重依赖、不伪造结果的前提下完整支持 CLIP 能力。

type CLIPEmbedder

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

CLIPEmbedder CLIP 模型向量化器:同时处理文本与图像,生成可跨模态比较的向量。

本身不含模型权重;实际计算由注入的 CLIPBackend 完成(见 WithCLIPBackend)。 未注入后端时所有嵌入方法返回 ErrCLIPNotImplemented(安全默认,不伪造向量)。

func NewCLIPEmbedder

func NewCLIPEmbedder(endpoint, apiKey string, opts ...CLIPOption) *CLIPEmbedder

NewCLIPEmbedder 创建 CLIP 向量化器。

endpoint/apiKey 供基于 HTTP 的后端实现按需取用;未通过 WithCLIPBackend 注入后端时, 嵌入方法返回 ErrCLIPNotImplemented。

func (*CLIPEmbedder) EmbedImage

func (e *CLIPEmbedder) EmbedImage(ctx context.Context, image *Content) ([]float32, error)

EmbedImage 向量化图像(委托后端;未注入后端返回 ErrCLIPNotImplemented)。

func (*CLIPEmbedder) EmbedImages

func (e *CLIPEmbedder) EmbedImages(ctx context.Context, images []*Content) ([][]float32, error)

EmbedImages 批量向量化图像(逐张委托后端;未注入后端返回 ErrCLIPNotImplemented)。

func (*CLIPEmbedder) EmbedText

func (e *CLIPEmbedder) EmbedText(ctx context.Context, texts []string) ([][]float32, error)

EmbedText 向量化文本(委托后端;未注入后端返回 ErrCLIPNotImplemented)。

func (*CLIPEmbedder) IsImplemented

func (e *CLIPEmbedder) IsImplemented() bool

IsImplemented 返回是否已注入可用的 CLIP 后端。

type CLIPOption

type CLIPOption func(*CLIPEmbedder)

CLIPOption 配置 CLIPEmbedder。

func WithCLIPBackend

func WithCLIPBackend(b CLIPBackend) CLIPOption

WithCLIPBackend 注入真实 CLIP 后端。注入后 IsImplemented() 返回 true。

type Content

type Content struct {
	// Type 内容类型
	Type ContentType `json:"type"`

	// Text 文本内容 (Type=text 时使用)
	Text string `json:"text,omitempty"`

	// Data 二进制数据 (图像/音频/视频)
	Data []byte `json:"data,omitempty"`

	// DataURL 数据URL (base64 编码)
	DataURL string `json:"data_url,omitempty"`

	// URL 外部URL
	URL string `json:"url,omitempty"`

	// Format 格式 (图像/音频/视频格式)
	Format string `json:"format,omitempty"`

	// Metadata 元数据
	Metadata map[string]any `json:"metadata,omitempty"`
}

Content 多模态内容

func NewAudioContent

func NewAudioContent(data []byte, format AudioFormat) *Content

NewAudioContent 从数据创建音频内容

func NewImageContent

func NewImageContent(data []byte, format ImageFormat) *Content

NewImageContent 从数据创建图像内容

func NewImageContentFromFile

func NewImageContentFromFile(path string) (*Content, error)

NewImageContentFromFile 从文件创建图像内容

func NewImageContentFromURL

func NewImageContentFromURL(url string) *Content

NewImageContentFromURL 从URL创建图像内容

func NewTextContent

func NewTextContent(text string) *Content

NewTextContent 创建文本内容

func NewVideoContent

func NewVideoContent(data []byte, format VideoFormat) *Content

NewVideoContent 从数据创建视频内容

func (*Content) IsAudio

func (c *Content) IsAudio() bool

IsAudio 是否为音频

func (*Content) IsImage

func (c *Content) IsImage() bool

IsImage 是否为图像

func (*Content) IsText

func (c *Content) IsText() bool

IsText 是否为文本

func (*Content) IsVideo

func (c *Content) IsVideo() bool

IsVideo 是否为视频

func (*Content) ToBase64

func (c *Content) ToBase64() string

ToBase64 将数据转换为 base64

type ContentProcessor

type ContentProcessor interface {
	// Process 处理内容
	Process(ctx context.Context, content *Content) (*ProcessResult, error)

	// SupportedTypes 支持的内容类型
	SupportedTypes() []ContentType
}

ContentProcessor 内容处理器接口

type ContentType

type ContentType string

ContentType 内容类型

const (
	ContentTypeText  ContentType = "text"
	ContentTypeImage ContentType = "image"
	ContentTypeAudio ContentType = "audio"
	ContentTypeVideo ContentType = "video"
)

type FrameExtractor

type FrameExtractor interface {
	// ExtractFrames 从视频中提取关键帧
	ExtractFrames(ctx context.Context, video *Content, interval time.Duration) ([]*Content, error)

	// ExtractAudio 从视频中提取音频
	ExtractAudio(ctx context.Context, video *Content) (*Content, error)
}

FrameExtractor 帧提取器接口

type ImageEmbedder

type ImageEmbedder interface {
	// EmbedImage 将图像转换为向量
	EmbedImage(ctx context.Context, image *Content) ([]float32, error)

	// EmbedImages 批量向量化
	EmbedImages(ctx context.Context, images []*Content) ([][]float32, error)
}

ImageEmbedder 图像向量化接口

type ImageFormat

type ImageFormat string

ImageFormat 图像格式

const (
	ImageFormatPNG  ImageFormat = "png"
	ImageFormatJPEG ImageFormat = "jpeg"
	ImageFormatGIF  ImageFormat = "gif"
	ImageFormatWebP ImageFormat = "webp"
)

type ImageProcessor

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

ImageProcessor 图像处理器

func NewImageProcessor

func NewImageProcessor(provider llm.Provider, opts ...ImageProcessorOption) *ImageProcessor

NewImageProcessor 创建图像处理器

func (*ImageProcessor) Process

func (p *ImageProcessor) Process(ctx context.Context, content *Content) (*ProcessResult, error)

Process 处理图像

func (*ImageProcessor) SupportedTypes

func (p *ImageProcessor) SupportedTypes() []ContentType

SupportedTypes 支持的内容类型

type ImageProcessorOption

type ImageProcessorOption func(*ImageProcessor)

ImageProcessorOption ImageProcessor 选项

func WithImageEmbedder

func WithImageEmbedder(embedder ImageEmbedder) ImageProcessorOption

WithImageEmbedder 设置图像向量化器

func WithImageModel

func WithImageModel(model string) ImageProcessorOption

WithImageModel 设置图像理解模型

func WithImagePrompt

func WithImagePrompt(prompt string) ImageProcessorOption

WithImagePrompt 设置图像描述提示词

type MultimodalDocument

type MultimodalDocument struct {
	// ID 文档唯一标识
	ID string `json:"id"`

	// Contents 内容列表 (可包含多种模态)
	Contents []*Content `json:"contents"`

	// Metadata 文档元数据
	Metadata map[string]any `json:"metadata,omitempty"`

	// Embeddings 各模态的向量表示
	Embeddings map[ContentType][]float32 `json:"embeddings,omitempty"`

	// TextDescription 文本描述 (用于存储图像描述等)
	TextDescription string `json:"text_description,omitempty"`

	// CreatedAt 创建时间
	CreatedAt time.Time `json:"created_at"`
}

MultimodalDocument 多模态文档

func NewMultimodalDocument

func NewMultimodalDocument(contents ...*Content) *MultimodalDocument

NewMultimodalDocument 创建多模态文档

func (*MultimodalDocument) AddContent

func (d *MultimodalDocument) AddContent(content *Content)

AddContent 添加内容

func (*MultimodalDocument) GetImages

func (d *MultimodalDocument) GetImages() []*Content

GetImages 获取所有图像内容

func (*MultimodalDocument) GetText

func (d *MultimodalDocument) GetText() string

GetText 获取所有文本内容

func (*MultimodalDocument) HasAudio

func (d *MultimodalDocument) HasAudio() bool

HasAudio 是否包含音频

func (*MultimodalDocument) HasImages

func (d *MultimodalDocument) HasImages() bool

HasImages 是否包含图像

func (*MultimodalDocument) HasVideo

func (d *MultimodalDocument) HasVideo() bool

HasVideo 是否包含视频

func (*MultimodalDocument) ToRAGDocument

func (d *MultimodalDocument) ToRAGDocument() rag.Document

ToRAGDocument 转换为 RAG 文档

type MultimodalIndexer

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

MultimodalIndexer 多模态索引器

func NewMultimodalIndexer

func NewMultimodalIndexer(store vector.Store, embedder vector.Embedder, opts ...MultimodalIndexerOption) *MultimodalIndexer

NewMultimodalIndexer 创建多模态索引器

func (*MultimodalIndexer) Clear

func (i *MultimodalIndexer) Clear(ctx context.Context) error

Clear 清空索引

func (*MultimodalIndexer) Count

func (i *MultimodalIndexer) Count(ctx context.Context) (int, error)

Count 返回文档数量

func (*MultimodalIndexer) Delete

func (i *MultimodalIndexer) Delete(ctx context.Context, ids []string) error

Delete 删除文档

func (*MultimodalIndexer) Index

func (i *MultimodalIndexer) Index(ctx context.Context, docs []rag.Document) error

Index 实现 rag.Indexer 接口

func (*MultimodalIndexer) IndexDocuments

func (i *MultimodalIndexer) IndexDocuments(ctx context.Context, docs []*MultimodalDocument) error

IndexDocuments 索引多模态文档

type MultimodalIndexerOption

type MultimodalIndexerOption func(*MultimodalIndexer)

MultimodalIndexerOption 选项

func WithMultimodalBatchSize

func WithMultimodalBatchSize(size int) MultimodalIndexerOption

WithMultimodalBatchSize 设置批量大小

func WithProcessor

func WithProcessor(processor ContentProcessor) MultimodalIndexerOption

WithProcessor 添加内容处理器

type MultimodalLoader

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

MultimodalLoader 多模态加载器

func NewMultimodalLoader

func NewMultimodalLoader() *MultimodalLoader

NewMultimodalLoader 创建多模态加载器

func (*MultimodalLoader) LoadDirectory

func (l *MultimodalLoader) LoadDirectory(ctx context.Context, dir string) ([]*MultimodalDocument, error)

LoadDirectory 加载目录中的所有文件

func (*MultimodalLoader) LoadFile

func (l *MultimodalLoader) LoadFile(ctx context.Context, path string) (*MultimodalDocument, error)

LoadFile 加载单个文件

func (*MultimodalLoader) LoadFromReader

func (l *MultimodalLoader) LoadFromReader(ctx context.Context, r io.Reader, contentType ContentType, format string) (*MultimodalDocument, error)

LoadFromReader 从 Reader 加载

type MultimodalRetriever

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

MultimodalRetriever 多模态检索器

func NewMultimodalRetriever

func NewMultimodalRetriever(store vector.Store, embedder vector.Embedder, opts ...MultimodalRetrieverOption) *MultimodalRetriever

NewMultimodalRetriever 创建多模态检索器

func (*MultimodalRetriever) Retrieve

func (r *MultimodalRetriever) Retrieve(ctx context.Context, query string, topK int) ([]rag.Document, error)

Retrieve 实现 rag.Retriever 接口

func (*MultimodalRetriever) RetrieveByImage

func (r *MultimodalRetriever) RetrieveByImage(ctx context.Context, image *Content) ([]rag.Document, error)

RetrieveByImage 基于图像查询检索

func (*MultimodalRetriever) RetrieveByText

func (r *MultimodalRetriever) RetrieveByText(ctx context.Context, query string) ([]rag.Document, error)

RetrieveByText 基于文本查询检索

type MultimodalRetrieverOption

type MultimodalRetrieverOption func(*MultimodalRetriever)

MultimodalRetrieverOption 选项

func WithMultimodalImageEmbedder

func WithMultimodalImageEmbedder(embedder ImageEmbedder) MultimodalRetrieverOption

WithMultimodalImageEmbedder 设置图像向量化器

func WithMultimodalImageProcessor

func WithMultimodalImageProcessor(processor *ImageProcessor) MultimodalRetrieverOption

WithMultimodalImageProcessor 设置图像处理器

func WithMultimodalTopK

func WithMultimodalTopK(k int) MultimodalRetrieverOption

WithMultimodalTopK 设置返回数量

type ProcessResult

type ProcessResult struct {
	// TextDescription 文本描述
	TextDescription string

	// Embedding 向量表示
	Embedding []float32

	// Metadata 提取的元数据
	Metadata map[string]any
}

ProcessResult 处理结果

type VideoFormat

type VideoFormat string

VideoFormat 视频格式

const (
	VideoFormatMP4  VideoFormat = "mp4"
	VideoFormatWebM VideoFormat = "webm"
)

type VideoProcessor

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

VideoProcessor 视频处理器

func NewVideoProcessor

func NewVideoProcessor(imageProcessor *ImageProcessor, audioProcessor *AudioProcessor, frameExtractor FrameExtractor) *VideoProcessor

NewVideoProcessor 创建视频处理器

func (*VideoProcessor) Process

func (p *VideoProcessor) Process(ctx context.Context, content *Content) (*ProcessResult, error)

Process 处理视频

func (*VideoProcessor) SupportedTypes

func (p *VideoProcessor) SupportedTypes() []ContentType

SupportedTypes 支持的内容类型

Jump to

Keyboard shortcuts

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