feat: embedding router

This commit is contained in:
Ran
2026-01-26 05:10:53 +07:00
parent c332ce7749
commit 3ff0e2c4dd
22 changed files with 2572 additions and 392 deletions
-102
View File
@@ -1,102 +0,0 @@
package memory
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type Embedder interface {
Embed(ctx context.Context, input string) ([]float32, error)
Dimensions() int
}
type OpenAIEmbedder struct {
apiKey string
baseURL string
model string
dims int
http *http.Client
}
type openAIEmbeddingRequest struct {
Input string `json:"input"`
Model string `json:"model"`
}
type openAIEmbeddingResponse struct {
Data []struct {
Embedding []float32 `json:"embedding"`
} `json:"data"`
}
func NewOpenAIEmbedder(apiKey, baseURL, model string, dims int, timeout time.Duration) *OpenAIEmbedder {
if baseURL == "" {
baseURL = "https://api.openai.com"
}
if model == "" {
model = "text-embedding-3-small"
}
if dims <= 0 {
dims = 1536
}
if timeout <= 0 {
timeout = 10 * time.Second
}
return &OpenAIEmbedder{
apiKey: apiKey,
baseURL: strings.TrimRight(baseURL, "/"),
model: model,
dims: dims,
http: &http.Client{
Timeout: timeout,
},
}
}
func (e *OpenAIEmbedder) Dimensions() int {
return e.dims
}
func (e *OpenAIEmbedder) Embed(ctx context.Context, input string) ([]float32, error) {
payload, err := json.Marshal(openAIEmbeddingRequest{
Input: input,
Model: e.model,
})
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, e.baseURL+"/v1/embeddings", bytes.NewReader(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if e.apiKey != "" {
req.Header.Set("Authorization", "Bearer "+e.apiKey)
}
resp, err := e.http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("openai embeddings error: %s", strings.TrimSpace(string(body)))
}
var parsed openAIEmbeddingResponse
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
return nil, err
}
if len(parsed.Data) == 0 {
return nil, fmt.Errorf("openai embeddings empty response")
}
return parsed.Data[0].Embedding, nil
}
+154 -8
View File
@@ -15,12 +15,18 @@ type QdrantStore struct {
client *qdrant.Client
collection string
dimension int
baseURL string
apiKey string
timeout time.Duration
vectorNames map[string]int
usesNamedVectors bool
}
type qdrantPoint struct {
ID string `json:"id"`
Vector []float32 `json:"vector"`
Payload map[string]interface{} `json:"payload,omitempty"`
ID string `json:"id"`
Vector []float32 `json:"vector"`
VectorName string `json:"vector_name,omitempty"`
Payload map[string]interface{} `json:"payload,omitempty"`
}
func NewQdrantStore(baseURL, apiKey, collection string, dimension int, timeout time.Duration) (*QdrantStore, error) {
@@ -50,11 +56,59 @@ func NewQdrantStore(baseURL, apiKey, collection string, dimension int, timeout t
client: client,
collection: collection,
dimension: dimension,
baseURL: baseURL,
apiKey: apiKey,
timeout: timeoutOrDefault(timeout),
}
ctx, cancel := context.WithTimeout(context.Background(), timeoutOrDefault(timeout))
defer cancel()
if err := store.ensureCollection(ctx); err != nil {
if err := store.ensureCollection(ctx, nil); err != nil {
return nil, err
}
return store, nil
}
func (s *QdrantStore) NewSibling(collection string, dimension int) (*QdrantStore, error) {
return NewQdrantStore(s.baseURL, s.apiKey, collection, dimension, s.timeout)
}
func NewQdrantStoreWithVectors(baseURL, apiKey, collection string, vectors map[string]int, timeout time.Duration) (*QdrantStore, error) {
host, port, useTLS, err := parseQdrantEndpoint(baseURL)
if err != nil {
return nil, err
}
if collection == "" {
collection = "memory"
}
if len(vectors) == 0 {
return nil, fmt.Errorf("vectors map is required")
}
cfg := &qdrant.Config{
Host: host,
Port: port,
APIKey: apiKey,
UseTLS: useTLS,
}
client, err := qdrant.NewClient(cfg)
if err != nil {
return nil, err
}
store := &QdrantStore{
client: client,
collection: collection,
baseURL: baseURL,
apiKey: apiKey,
timeout: timeoutOrDefault(timeout),
vectorNames: vectors,
usesNamedVectors: true,
}
ctx, cancel := context.WithTimeout(context.Background(), timeoutOrDefault(timeout))
defer cancel()
if err := store.ensureCollection(ctx, vectors); err != nil {
return nil, err
}
return store, nil
@@ -70,9 +124,17 @@ func (s *QdrantStore) Upsert(ctx context.Context, points []qdrantPoint) error {
if err != nil {
return err
}
var vectors *qdrant.Vectors
if point.VectorName != "" && s.usesNamedVectors {
vectors = qdrant.NewVectorsMap(map[string]*qdrant.Vector{
point.VectorName: qdrant.NewVectorDense(point.Vector),
})
} else {
vectors = qdrant.NewVectorsDense(point.Vector)
}
qPoints = append(qPoints, &qdrant.PointStruct{
Id: qdrant.NewIDUUID(point.ID),
Vectors: qdrant.NewVectorsDense(point.Vector),
Vectors: vectors,
Payload: payload,
})
}
@@ -84,14 +146,19 @@ func (s *QdrantStore) Upsert(ctx context.Context, points []qdrantPoint) error {
return err
}
func (s *QdrantStore) Search(ctx context.Context, vector []float32, limit int, filters map[string]interface{}) ([]qdrantPoint, []float64, error) {
func (s *QdrantStore) Search(ctx context.Context, vector []float32, limit int, filters map[string]interface{}, vectorName string) ([]qdrantPoint, []float64, error) {
if limit <= 0 {
limit = 10
}
filter := buildQdrantFilter(filters)
var using *string
if vectorName != "" && s.usesNamedVectors {
using = qdrant.PtrOf(vectorName)
}
results, err := s.client.Query(ctx, &qdrant.QueryPoints{
CollectionName: s.collection,
Query: qdrant.NewQueryDense(vector),
Using: using,
Limit: qdrant.PtrOf(uint64(limit)),
Filter: filter,
WithPayload: qdrant.NewWithPayload(true),
@@ -112,6 +179,27 @@ func (s *QdrantStore) Search(ctx context.Context, vector []float32, limit int, f
return points, scores, nil
}
func (s *QdrantStore) SearchBySources(ctx context.Context, vector []float32, limit int, filters map[string]interface{}, sources []string, vectorName string) (map[string][]qdrantPoint, map[string][]float64, error) {
pointsBySource := make(map[string][]qdrantPoint, len(sources))
scoresBySource := make(map[string][]float64, len(sources))
if len(sources) == 0 {
return pointsBySource, scoresBySource, nil
}
for _, source := range sources {
merged := cloneFilters(filters)
if source != "" {
merged["source"] = source
}
points, scores, err := s.Search(ctx, vector, limit, merged, vectorName)
if err != nil {
return nil, nil, err
}
pointsBySource[source] = points
scoresBySource[source] = scores
}
return pointsBySource, scoresBySource, nil
}
func (s *QdrantStore) Get(ctx context.Context, id string) (*qdrantPoint, error) {
result, err := s.client.Get(ctx, &qdrant.GetPoints{
CollectionName: s.collection,
@@ -178,13 +266,26 @@ func (s *QdrantStore) DeleteAll(ctx context.Context, filters map[string]interfac
return err
}
func (s *QdrantStore) ensureCollection(ctx context.Context) error {
func (s *QdrantStore) ensureCollection(ctx context.Context, vectors map[string]int) error {
exists, err := s.client.CollectionExists(ctx, s.collection)
if err != nil {
return err
}
if exists {
return nil
return s.refreshCollectionSchema(ctx, vectors)
}
if len(vectors) > 0 {
params := make(map[string]*qdrant.VectorParams, len(vectors))
for name, dim := range vectors {
params[name] = &qdrant.VectorParams{
Size: uint64(dim),
Distance: qdrant.Distance_Cosine,
}
}
return s.client.CreateCollection(ctx, &qdrant.CreateCollection{
CollectionName: s.collection,
VectorsConfig: qdrant.NewVectorsConfigMap(params),
})
}
return s.client.CreateCollection(ctx, &qdrant.CreateCollection{
CollectionName: s.collection,
@@ -195,6 +296,40 @@ func (s *QdrantStore) ensureCollection(ctx context.Context) error {
})
}
func (s *QdrantStore) refreshCollectionSchema(ctx context.Context, vectors map[string]int) error {
info, err := s.client.GetCollectionInfo(ctx, s.collection)
if err != nil {
return err
}
config := info.GetConfig()
if config == nil || config.GetParams() == nil || config.GetParams().GetVectorsConfig() == nil {
return nil
}
vectorsConfig := config.GetParams().GetVectorsConfig()
if vectorsConfig.GetParamsMap() != nil {
s.usesNamedVectors = true
s.vectorNames = map[string]int{}
for name, vec := range vectorsConfig.GetParamsMap().GetMap() {
if vec != nil {
s.vectorNames[name] = int(vec.GetSize())
}
}
if len(vectors) == 0 {
return nil
}
for name, dim := range vectors {
if existing, ok := s.vectorNames[name]; ok && existing == dim {
continue
}
return fmt.Errorf("collection missing vector %s (dim %d); migration required", name, dim)
}
return nil
}
s.usesNamedVectors = false
s.vectorNames = nil
return nil
}
func parseQdrantEndpoint(endpoint string) (string, int, bool, error) {
if endpoint == "" {
return "127.0.0.1", 6334, false, nil
@@ -247,6 +382,17 @@ func buildQdrantFilter(filters map[string]interface{}) *qdrant.Filter {
}
}
func cloneFilters(filters map[string]interface{}) map[string]interface{} {
if len(filters) == 0 {
return map[string]interface{}{}
}
clone := make(map[string]interface{}, len(filters))
for key, value := range filters {
clone[key] = value
}
return clone
}
func buildQdrantCondition(key string, value interface{}) *qdrant.Condition {
switch typed := value.(type) {
case string:
+308 -22
View File
@@ -5,23 +5,33 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"math"
"sort"
"strings"
"time"
"github.com/google/uuid"
"github.com/memohai/memoh/internal/embeddings"
)
type Service struct {
llm *LLMClient
embedder Embedder
store *QdrantStore
llm *LLMClient
embedder embeddings.Embedder
store *QdrantStore
resolver *embeddings.Resolver
defaultTextModelID string
defaultMultimodalModelID string
}
func NewService(llm *LLMClient, embedder Embedder, store *QdrantStore) *Service {
func NewService(llm *LLMClient, embedder embeddings.Embedder, store *QdrantStore, resolver *embeddings.Resolver, defaultTextModelID, defaultMultimodalModelID string) *Service {
return &Service{
llm: llm,
embedder: embedder,
store: store,
llm: llm,
embedder: embedder,
store: store,
resolver: resolver,
defaultTextModelID: defaultTextModelID,
defaultMultimodalModelID: defaultMultimodalModelID,
}
}
@@ -122,27 +132,128 @@ func (s *Service) Search(ctx context.Context, req SearchRequest) (SearchResponse
return SearchResponse{}, fmt.Errorf("query is required")
}
filters := buildSearchFilters(req)
vector, err := s.embedder.Embed(ctx, req.Query)
if err != nil {
return SearchResponse{}, err
modality := ""
if raw, ok := filters["modality"].(string); ok {
modality = strings.ToLower(strings.TrimSpace(raw))
}
points, scores, err := s.store.Search(ctx, vector, req.Limit, filters)
if err != nil {
return SearchResponse{}, err
}
results := make([]MemoryItem, 0, len(points))
for idx, point := range points {
item := payloadToMemoryItem(point.ID, point.Payload)
if idx < len(scores) {
item.Score = scores[idx]
var (
vector []float32
store *QdrantStore
vectorName string
err error
)
if modality == embeddings.TypeMultimodal {
if s.resolver == nil {
return SearchResponse{}, fmt.Errorf("embeddings resolver not configured")
}
results = append(results, item)
result, err := s.resolver.Embed(ctx, embeddings.Request{
Type: embeddings.TypeMultimodal,
Input: embeddings.Input{
Text: req.Query,
},
})
if err != nil {
return SearchResponse{}, err
}
vector = result.Embedding
store = s.store
vectorName = s.vectorNameForMultimodal()
} else {
vector, err = s.embedder.Embed(ctx, req.Query)
if err != nil {
return SearchResponse{}, err
}
store = s.store
vectorName = s.vectorNameForText()
}
if len(req.Sources) == 0 {
points, scores, err := store.Search(ctx, vector, req.Limit, filters, vectorName)
if err != nil {
return SearchResponse{}, err
}
results := make([]MemoryItem, 0, len(points))
for idx, point := range points {
item := payloadToMemoryItem(point.ID, point.Payload)
if idx < len(scores) {
item.Score = scores[idx]
}
results = append(results, item)
}
return SearchResponse{Results: results}, nil
}
pointsBySource, scoresBySource, err := store.SearchBySources(ctx, vector, req.Limit, filters, req.Sources, vectorName)
if err != nil {
return SearchResponse{}, err
}
results := fuseByRankFusion(pointsBySource, scoresBySource)
return SearchResponse{Results: results}, nil
}
func (s *Service) EmbedUpsert(ctx context.Context, req EmbedUpsertRequest) (EmbedUpsertResponse, error) {
if s.resolver == nil {
return EmbedUpsertResponse{}, fmt.Errorf("embeddings resolver not configured")
}
if req.UserID == "" && req.AgentID == "" && req.RunID == "" {
return EmbedUpsertResponse{}, fmt.Errorf("user_id, agent_id or run_id is required")
}
req.Type = strings.TrimSpace(req.Type)
req.Provider = strings.TrimSpace(req.Provider)
req.Model = strings.TrimSpace(req.Model)
req.Input.Text = strings.TrimSpace(req.Input.Text)
req.Input.ImageURL = strings.TrimSpace(req.Input.ImageURL)
req.Input.VideoURL = strings.TrimSpace(req.Input.VideoURL)
result, err := s.resolver.Embed(ctx, embeddings.Request{
Type: req.Type,
Provider: req.Provider,
Model: req.Model,
Input: embeddings.Input{
Text: req.Input.Text,
ImageURL: req.Input.ImageURL,
VideoURL: req.Input.VideoURL,
},
})
if err != nil {
return EmbedUpsertResponse{}, err
}
if s.store == nil {
return EmbedUpsertResponse{}, fmt.Errorf("qdrant store not configured")
}
vectorName := ""
if s.store != nil && s.store.usesNamedVectors {
vectorName = result.Model
}
id := uuid.NewString()
filters := buildEmbedFilters(req)
payload := buildEmbeddingPayload(req, filters)
if metadata, ok := payload["metadata"].(map[string]interface{}); ok && result.Model != "" {
metadata["model_id"] = result.Model
}
if err := s.store.Upsert(ctx, []qdrantPoint{{
ID: id,
Vector: result.Embedding,
VectorName: vectorName,
Payload: payload,
}}); err != nil {
return EmbedUpsertResponse{}, err
}
item := payloadToMemoryItem(id, payload)
return EmbedUpsertResponse{
Item: item,
Provider: result.Provider,
Model: result.Model,
Dimensions: result.Dimensions,
}, nil
}
func (s *Service) Update(ctx context.Context, req UpdateRequest) (MemoryItem, error) {
if strings.TrimSpace(req.MemoryID) == "" {
return MemoryItem{}, fmt.Errorf("memory_id is required")
@@ -171,6 +282,7 @@ func (s *Service) Update(ctx context.Context, req UpdateRequest) (MemoryItem, er
if err := s.store.Upsert(ctx, []qdrantPoint{{
ID: req.MemoryID,
Vector: vector,
VectorName: s.vectorNameForText(),
Payload: payload,
}}); err != nil {
return MemoryItem{}, err
@@ -270,7 +382,7 @@ func (s *Service) collectCandidates(ctx context.Context, facts []string, filters
if err != nil {
return nil, err
}
points, _, err := s.store.Search(ctx, vector, 5, filters)
points, _, err := s.store.Search(ctx, vector, 5, filters, s.vectorNameForText())
if err != nil {
return nil, err
}
@@ -301,6 +413,7 @@ func (s *Service) applyAdd(ctx context.Context, text string, filters map[string]
if err := s.store.Upsert(ctx, []qdrantPoint{{
ID: id,
Vector: vector,
VectorName: s.vectorNameForText(),
Payload: payload,
}}); err != nil {
return MemoryItem{}, err
@@ -337,6 +450,7 @@ func (s *Service) applyUpdate(ctx context.Context, id, text string, filters map[
if err := s.store.Upsert(ctx, []qdrantPoint{{
ID: id,
Vector: vector,
VectorName: s.vectorNameForText(),
Payload: payload,
}}); err != nil {
return MemoryItem{}, err
@@ -403,6 +517,68 @@ func buildSearchFilters(req SearchRequest) map[string]interface{} {
return filters
}
func buildEmbedFilters(req EmbedUpsertRequest) map[string]interface{} {
filters := map[string]interface{}{}
for key, value := range req.Filters {
filters[key] = value
}
if req.UserID != "" {
filters["userId"] = req.UserID
}
if req.AgentID != "" {
filters["agentId"] = req.AgentID
}
if req.RunID != "" {
filters["runId"] = req.RunID
}
return filters
}
func buildEmbeddingPayload(req EmbedUpsertRequest, filters map[string]interface{}) map[string]interface{} {
text := req.Input.Text
payload := buildPayload(text, filters, req.Metadata, "")
payload["hash"] = hashEmbeddingInput(req.Input.Text, req.Input.ImageURL, req.Input.VideoURL)
if req.Source != "" {
payload["source"] = req.Source
}
modality := "text"
if req.Type != "" {
modality = strings.ToLower(req.Type)
}
payload["modality"] = modality
if payload["metadata"] == nil {
payload["metadata"] = map[string]interface{}{}
}
if metadata, ok := payload["metadata"].(map[string]interface{}); ok {
if req.Source != "" {
metadata["source"] = req.Source
}
metadata["modality"] = modality
if req.Input.ImageURL != "" {
metadata["image_url"] = req.Input.ImageURL
}
if req.Input.VideoURL != "" {
metadata["video_url"] = req.Input.VideoURL
}
}
return payload
}
func (s *Service) vectorNameForText() string {
if s.store == nil || !s.store.usesNamedVectors {
return ""
}
return strings.TrimSpace(s.defaultTextModelID)
}
func (s *Service) vectorNameForMultimodal() string {
if s.store == nil || !s.store.usesNamedVectors {
return ""
}
return strings.TrimSpace(s.defaultMultimodalModelID)
}
func buildPayload(text string, filters map[string]interface{}, metadata map[string]interface{}, createdAt string) map[string]interface{} {
if createdAt == "" {
createdAt = time.Now().UTC().Format(time.RFC3339)
@@ -450,6 +626,16 @@ func payloadToMemoryItem(id string, payload map[string]interface{}) MemoryItem {
}
if meta, ok := payload["metadata"].(map[string]interface{}); ok {
item.Metadata = meta
} else if payload["metadata"] == nil {
item.Metadata = map[string]interface{}{}
}
if item.Metadata != nil {
if source, ok := payload["source"].(string); ok && source != "" {
item.Metadata["source"] = source
}
if modality, ok := payload["modality"].(string); ok && modality != "" {
item.Metadata["modality"] = modality
}
}
return item
}
@@ -459,6 +645,16 @@ func hashMemory(text string) string {
return hex.EncodeToString(sum[:])
}
func hashEmbeddingInput(text, imageURL, videoURL string) string {
combined := strings.Join([]string{
strings.TrimSpace(text),
strings.TrimSpace(imageURL),
strings.TrimSpace(videoURL),
}, "|")
sum := md5.Sum([]byte(combined))
return hex.EncodeToString(sum[:])
}
func mergeMetadata(base interface{}, extra map[string]interface{}) map[string]interface{} {
merged := map[string]interface{}{}
if baseMap, ok := base.(map[string]interface{}); ok {
@@ -471,3 +667,93 @@ func mergeMetadata(base interface{}, extra map[string]interface{}) map[string]in
}
return merged
}
type rerankCandidate struct {
ID string
Payload map[string]interface{}
Score float64
Source string
Rank int
}
const (
fusionModeRRF = "rrf"
fusionModeCombMNZ = "combmnz"
fusionMode = fusionModeRRF
rrfK = 60.0
)
func fuseByRankFusion(pointsBySource map[string][]qdrantPoint, scoresBySource map[string][]float64) []MemoryItem {
candidates := map[string]*rerankCandidate{}
rrfScores := map[string]float64{}
combScores := map[string]float64{}
combCounts := map[string]int{}
for source, points := range pointsBySource {
scores := scoresBySource[source]
minScore := math.MaxFloat64
maxScore := -math.MaxFloat64
for idx, point := range points {
if idx >= len(scores) {
continue
}
score := scores[idx]
if score < minScore {
minScore = score
}
if score > maxScore {
maxScore = score
}
if _, ok := candidates[point.ID]; !ok {
candidates[point.ID] = &rerankCandidate{
ID: point.ID,
Payload: point.Payload,
}
}
}
if minScore == math.MaxFloat64 {
minScore = 0
}
if maxScore == -math.MaxFloat64 {
maxScore = minScore
}
for idx, point := range points {
if idx >= len(scores) {
continue
}
score := scores[idx]
rank := float64(idx + 1)
rrfScores[point.ID] += 1.0 / (rrfK + rank)
scoreNorm := normalizeScore(score, minScore, maxScore)
combScores[point.ID] += scoreNorm
combCounts[point.ID]++
}
}
items := make([]MemoryItem, 0, len(candidates))
for id, candidate := range candidates {
item := payloadToMemoryItem(candidate.ID, candidate.Payload)
switch fusionMode {
case fusionModeCombMNZ:
item.Score = combScores[id] * float64(combCounts[id])
default:
item.Score = rrfScores[id]
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool {
return items[i].Score > items[j].Score
})
return items
}
func normalizeScore(score, minScore, maxScore float64) float64 {
if maxScore <= minScore {
return 1
}
return (score - minScore) / (maxScore - minScore)
}
+27
View File
@@ -23,6 +23,7 @@ type SearchRequest struct {
RunID string `json:"run_id,omitempty"`
Limit int `json:"limit,omitempty"`
Filters map[string]interface{} `json:"filters,omitempty"`
Sources []string `json:"sources,omitempty"`
}
type UpdateRequest struct {
@@ -43,6 +44,32 @@ type DeleteAllRequest struct {
RunID string `json:"run_id,omitempty"`
}
type EmbedInput struct {
Text string `json:"text,omitempty"`
ImageURL string `json:"image_url,omitempty"`
VideoURL string `json:"video_url,omitempty"`
}
type EmbedUpsertRequest struct {
Type string `json:"type"`
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
Input EmbedInput `json:"input"`
Source string `json:"source,omitempty"`
UserID string `json:"user_id,omitempty"`
AgentID string `json:"agent_id,omitempty"`
RunID string `json:"run_id,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Filters map[string]interface{} `json:"filters,omitempty"`
}
type EmbedUpsertResponse struct {
Item MemoryItem `json:"item"`
Provider string `json:"provider"`
Model string `json:"model"`
Dimensions int `json:"dimensions"`
}
type MemoryItem struct {
ID string `json:"id"`
Memory string `json:"memory"`