core

package
v1.4.6 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package core 定义 RAG 层的最小检索契约、核心模型和错误映射。

core 包不依赖 config,不依赖 agent/workflow/api/cmd。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSharedEvalMetrics

func BuildSharedEvalMetrics(m EvalMetrics) types.RAGEvalMetrics

BuildSharedEvalMetrics maps rag eval metrics into types-level eval contract.

func BuildSharedRetrievalRecords

func BuildSharedRetrievalRecords(results []RetrievalResult, trace types.RetrievalTrace) []types.RetrievalRecord

BuildSharedRetrievalRecords maps rag retrieval results into types-level minimal shared records.

Types

type Chunk

type Chunk struct {
	Content    string         `json:"content"`
	StartPos   int            `json:"start_pos"`
	EndPos     int            `json:"end_pos"`
	Metadata   map[string]any `json:"metadata"`
	TokenCount int            `json:"token_count"`
}

Chunk 文档块。

type Clearable

type Clearable interface {
	ClearAll(ctx context.Context) error
}

Clearable 可选接口,支持清空所有数据。

type ContextProvider

type ContextProvider interface {
	GenerateContext(ctx context.Context, doc Document, chunk string) (string, error)
}

ContextProvider 上下文提供器接口。

type CrossEncoderProvider

type CrossEncoderProvider interface {
	Score(ctx context.Context, pairs []QueryDocPair) ([]float64, error)
}

CrossEncoderProvider Cross-Encoder 提供器接口。

type Document

type Document struct {
	ID        string         `json:"id"`
	Content   string         `json:"content"`
	Metadata  map[string]any `json:"metadata,omitempty"`
	Embedding []float64      `json:"embedding,omitempty"`
}

Document 文档。

type DocumentLister

type DocumentLister interface {
	ListDocumentIDs(ctx context.Context, limit int, offset int) ([]string, error)
}

DocumentLister 可选接口,支持分页列出文档 ID。

type Edge

type Edge struct {
	ID         string         `json:"id"`
	Source     string         `json:"source"`
	Target     string         `json:"target"`
	Type       string         `json:"type"`
	Properties map[string]any `json:"properties,omitempty"`
	Weight     float64        `json:"weight"`
}

Edge 知识图边。

type EmbeddingProvider

type EmbeddingProvider interface {
	EmbedQuery(ctx context.Context, query string) ([]float64, error)
	EmbedDocuments(ctx context.Context, documents []string) ([][]float64, error)
	Name() string
}

EmbeddingProvider 嵌入提供者接口。

type EmbeddingProviderType

type EmbeddingProviderType string

EmbeddingProviderType 标识嵌入提供者。

type EvalMetrics

type EvalMetrics struct {
	ContextRelevance float64 `json:"context_relevance"`
	Faithfulness     float64 `json:"faithfulness"`
	AnswerRelevancy  float64 `json:"answer_relevancy"`
	RecallAtK        float64 `json:"recall_at_k"`
	MRR              float64 `json:"mrr"`
}

EvalMetrics RAG 评估指标定义。

type GraphEmbedder

type GraphEmbedder interface {
	Embed(ctx context.Context, text string) ([]float64, error)
}

GraphEmbedder 图嵌入生成器接口。

type GraphRetrievalResult

type GraphRetrievalResult struct {
	ID           string         `json:"id"`
	Content      string         `json:"content"`
	Score        float64        `json:"score"`
	GraphScore   float64        `json:"graph_score"`
	VectorScore  float64        `json:"vector_score"`
	Source       string         `json:"source"`
	Metadata     map[string]any `json:"metadata,omitempty"`
	RelatedNodes []*Node        `json:"related_nodes,omitempty"`
}

GraphRetrievalResult 图检索结果。

type LLMRerankerProvider

type LLMRerankerProvider interface {
	ScoreRelevance(ctx context.Context, query, document string) (float64, error)
}

LLMRerankerProvider LLM 重排序提供器。

type LowLevelSearchResult

type LowLevelSearchResult struct {
	ID       string         `json:"id"`
	Score    float64        `json:"score"`
	Metadata map[string]any `json:"metadata"`
}

LowLevelSearchResult 底层向量搜索结果。

type LowLevelVectorStore

type LowLevelVectorStore interface {
	Store(ctx context.Context, id string, vector []float64, metadata map[string]any) error
	Search(ctx context.Context, query []float64, topK int, filter map[string]any) ([]LowLevelSearchResult, error)
	Delete(ctx context.Context, id string) error
}

LowLevelVectorStore 底层向量存储接口。

type Node

type Node struct {
	ID         string         `json:"id"`
	Type       string         `json:"type"`
	Label      string         `json:"label"`
	Properties map[string]any `json:"properties,omitempty"`
	Embedding  []float64      `json:"embedding,omitempty"`
	CreatedAt  time.Time      `json:"created_at"`
}

Node 知识图节点。

type QueryDocPair

type QueryDocPair struct {
	Query    string
	Document string
}

QueryDocPair 查询-文档对。

type QueryLLMProvider

type QueryLLMProvider interface {
	Complete(ctx context.Context, prompt string) (string, error)
}

QueryLLMProvider 基于 LLM 的查询接口。

type RAGError

type RAGError struct {
	Code    types.ErrorCode
	Message string
	Cause   error
}

RAGError 统一 RAG 错误类型,映射到 types.ErrorCode。

func ErrConfig

func ErrConfig(message string, cause error) *RAGError

ErrConfig 配置错误。

func ErrInternal

func ErrInternal(message string, cause error) *RAGError

ErrInternal 内部错误。

func ErrTimeout

func ErrTimeout(message string, cause error) *RAGError

ErrTimeout 超时错误。

func ErrUpstream

func ErrUpstream(message string, cause error) *RAGError

ErrUpstream 上游服务错误(embedding/rerank provider 不可用)。

func NewRAGError

func NewRAGError(code types.ErrorCode, message string, cause error) *RAGError

NewRAGError 创建 RAG 错误。

func (*RAGError) Error

func (e *RAGError) Error() string

func (*RAGError) Unwrap

func (e *RAGError) Unwrap() error

type RerankProvider

type RerankProvider interface {
	RerankSimple(ctx context.Context, query string, documents []string, topN int) ([]rerank.RerankResult, error)
	Name() string
}

RerankProvider 重排提供者接口。

type RerankProviderType

type RerankProviderType string

RerankProviderType 标识重排提供者。

type Reranker

type Reranker interface {
	Rerank(ctx context.Context, query string, results []RetrievalResult) ([]RetrievalResult, error)
}

Reranker 重排序器接口。

type RetrievalMetrics

type RetrievalMetrics struct {
	TraceID          string        `json:"trace_id"`
	RunID            string        `json:"run_id"`
	SpanID           string        `json:"span_id,omitempty"`
	RetrievalLatency time.Duration `json:"retrieval_latency_ms"`
	RerankLatency    time.Duration `json:"rerank_latency_ms"`
	TopK             int           `json:"topk"`
	HitCount         int           `json:"hit_count"`
	ContextTokens    int           `json:"context_tokens"`
	Strategy         string        `json:"strategy"`
}

RetrievalMetrics 统一检索观测字段。

type RetrievalResult

type RetrievalResult struct {
	Document    Document `json:"document"`
	BM25Score   float64  `json:"bm25_score"`
	VectorScore float64  `json:"vector_score"`
	HybridScore float64  `json:"hybrid_score"`
	RerankScore float64  `json:"rerank_score,omitempty"`
	FinalScore  float64  `json:"final_score"`
}

RetrievalResult 检索结果。

type SearchResult

type SearchResult struct {
	ID       string
	Distance float64
	Score    float64
}

SearchResult 向量索引搜索结果。

type Tokenizer

type Tokenizer interface {
	CountTokens(text string) int
	Encode(text string) []int
}

Tokenizer RAG 分块专用分词器接口。

type VectorSearchResult

type VectorSearchResult struct {
	Document Document `json:"document"`
	Score    float64  `json:"score"`
	Distance float64  `json:"distance"`
}

VectorSearchResult 向量搜索结果。

type VectorStore

type VectorStore interface {
	AddDocuments(ctx context.Context, docs []Document) error
	Search(ctx context.Context, queryEmbedding []float64, topK int) ([]VectorSearchResult, error)
	DeleteDocuments(ctx context.Context, ids []string) error
	UpdateDocument(ctx context.Context, doc Document) error
	Count(ctx context.Context) (int, error)
}

VectorStore 向量数据库接口。

type VectorStoreType

type VectorStoreType string

VectorStoreType 标识向量存储后端。

const (
	VectorStoreMemory   VectorStoreType = "memory"
	VectorStoreQdrant   VectorStoreType = "qdrant"
	VectorStoreWeaviate VectorStoreType = "weaviate"
	VectorStoreMilvus   VectorStoreType = "milvus"
	VectorStorePinecone VectorStoreType = "pinecone"
)

type WebRetrievalResult

type WebRetrievalResult struct {
	URL     string  `json:"url"`
	Title   string  `json:"title"`
	Content string  `json:"content"`
	Score   float64 `json:"score"`
}

WebRetrievalResult 网络搜索结果。

type WebSearchFunc

type WebSearchFunc func(ctx context.Context, query string, maxResults int) ([]WebRetrievalResult, error)

WebSearchFunc 网络搜索函数签名。

Jump to

Keyboard shortcuts

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