Documentation
¶
Overview ¶
Package core 定义 RAG 层的最小检索契约、核心模型和错误映射。
core 包不依赖 config,不依赖 agent/workflow/api/cmd。
Index ¶
- func BuildSharedEvalMetrics(m EvalMetrics) types.RAGEvalMetrics
- func BuildSharedRetrievalRecords(results []RetrievalResult, trace types.RetrievalTrace) []types.RetrievalRecord
- type Chunk
- type Clearable
- type ContextProvider
- type CrossEncoderProvider
- type Document
- type DocumentLister
- type Edge
- type EmbeddingProvider
- type EmbeddingProviderType
- type EvalMetrics
- type GraphEmbedder
- type GraphRetrievalResult
- type LLMRerankerProvider
- type LowLevelSearchResult
- type LowLevelVectorStore
- type Node
- type QueryDocPair
- type QueryLLMProvider
- type RAGError
- type RerankProvider
- type RerankProviderType
- type Reranker
- type RetrievalMetrics
- type RetrievalResult
- type SearchResult
- type Tokenizer
- type VectorSearchResult
- type VectorStore
- type VectorStoreType
- type WebRetrievalResult
- type WebSearchFunc
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 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 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 ¶
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 QueryLLMProvider ¶
QueryLLMProvider 基于 LLM 的查询接口。
type RAGError ¶
RAGError 统一 RAG 错误类型,映射到 types.ErrorCode。
func ErrUpstream ¶
ErrUpstream 上游服务错误(embedding/rerank provider 不可用)。
func NewRAGError ¶
NewRAGError 创建 RAG 错误。
type RerankProvider ¶
type RerankProvider interface {
RerankSimple(ctx context.Context, query string, documents []string, topN int) ([]rerank.RerankResult, error)
Name() string
}
RerankProvider 重排提供者接口。
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 ¶
SearchResult 向量索引搜索结果。
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 网络搜索函数签名。