Search 在指定命名空间内执行向量检索。 query 为用户自然语言查询, meta 用于构造 namespace/过滤。
(ctx context.Context, query string, meta map[string]any, topK int)
| 177 | // Search 在指定命名空间内执行向量检索。 |
| 178 | // query 为用户自然语言查询, meta 用于构造 namespace/过滤。 |
| 179 | func (sm *SemanticMemory) Search(ctx context.Context, query string, meta map[string]any, topK int) ([]vector.Hit, error) { |
| 180 | if sm == nil || sm.cfg.Store == nil || sm.cfg.Embedder == nil { |
| 181 | return nil, nil |
| 182 | } |
| 183 | if query == "" { |
| 184 | return nil, nil |
| 185 | } |
| 186 | if topK <= 0 { |
| 187 | topK = sm.cfg.TopK |
| 188 | } |
| 189 | |
| 190 | vecs, err := sm.cfg.Embedder.EmbedText(ctx, []string{query}) |
| 191 | if err != nil { |
| 192 | return nil, fmt.Errorf("embed query: %w", err) |
| 193 | } |
| 194 | if len(vecs) == 0 { |
| 195 | return nil, errors.New("embedder returned empty vectors") |
| 196 | } |
| 197 | |
| 198 | return sm.cfg.Store.Query(ctx, vector.Query{ |
| 199 | Vector: vecs[0], |
| 200 | TopK: topK, |
| 201 | Namespace: sm.namespaceFromMeta(meta), |
| 202 | Filter: meta, |
| 203 | }) |
| 204 | } |
| 205 | |
| 206 | func (sm *SemanticMemory) namespaceFromMeta(meta map[string]any) string { |
| 207 | if meta == nil { |
no test coverage detected