Files
rag-local/kb/model/domain/retrieval.go
T
2026-08-05 10:28:44 +08:00

67 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package domain
import (
"strconv"
"strings"
"rag-local/kb/model/entity"
)
// VecHit 向量 KNN 命中(vec0 默认 L2 距离)
type VecHit struct {
ChunkId int64
Distance float64
}
// FtsHit 全文检索命中(bm25 负分,越小越相关)
type FtsHit struct {
ChunkId int64
Score float64
}
// RetrievedChunk 混合检索融合后的结果
type RetrievedChunk struct {
Chunk *entity.Chunk
Score float64
// Sources 命中来源:"vector" / "fts" / "hybrid"
Sources []string
}
// HybridResult 混合检索整体结果(chunk + 知识图谱三元组,M5 扩展)
type HybridResult struct {
Chunks []*RetrievedChunk
}
// Citation 回答引用来源(与回答文本中 [1][2] 编号对应)
type Citation struct {
Index int `json:"index"`
DocumentId int64 `json:"document_id"`
ChunkId int64 `json:"chunk_id"`
Content string `json:"content"`
Score float64 `json:"score"`
Sources []string `json:"sources"`
}
// VecJson 向量 JSON 序列化([0.1,0.2,...]
func VecJson(vec []float32) string {
return vecJsonFloat(vec)
}
// VecJsonF64 float64 向量 JSON 序列化(embedding 接口返回 float64
func VecJsonF64(vec []float64) string {
return vecJsonFloat(vec)
}
func vecJsonFloat[T float32 | float64](vec []T) string {
var sb strings.Builder
sb.WriteByte('[')
for i, v := range vec {
if i > 0 {
sb.WriteByte(',')
}
sb.WriteString(strconv.FormatFloat(float64(v), 'f', -1, 32))
}
sb.WriteByte(']')
return sb.String()
}