indexInternal 内部索引方法。
(ctx context.Context, docID string, text string, meta map[string]any, provenance *MemoryProvenance)
| 136 | |
| 137 | // indexInternal 内部索引方法。 |
| 138 | func (sm *SemanticMemory) indexInternal(ctx context.Context, docID string, text string, meta map[string]any, provenance *MemoryProvenance) error { |
| 139 | if docID == "" || text == "" { |
| 140 | return errors.New("docID and text are required") |
| 141 | } |
| 142 | |
| 143 | vecs, err := sm.cfg.Embedder.EmbedText(ctx, []string{text}) |
| 144 | if err != nil { |
| 145 | return fmt.Errorf("embed text: %w", err) |
| 146 | } |
| 147 | if len(vecs) == 0 { |
| 148 | return errors.New("embedder returned empty vectors") |
| 149 | } |
| 150 | |
| 151 | // 将文本复制到 metadata 中, 方便检索结果直接携带原文片段。 |
| 152 | metaCopy := make(map[string]any, len(meta)+2) |
| 153 | for k, v := range meta { |
| 154 | metaCopy[k] = v |
| 155 | } |
| 156 | metaCopy["text"] = text |
| 157 | |
| 158 | // 添加 Provenance 到 metadata |
| 159 | if provenance != nil { |
| 160 | provenanceMeta := provenance.ToMetadata() |
| 161 | for k, v := range provenanceMeta { |
| 162 | metaCopy[k] = v |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return sm.cfg.Store.Upsert(ctx, []vector.Document{ |
| 167 | { |
| 168 | ID: docID, |
| 169 | Text: text, |
| 170 | Embedding: vecs[0], |
| 171 | Metadata: metaCopy, |
| 172 | Namespace: sm.namespaceFromMeta(meta), |
| 173 | }, |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | // Search 在指定命名空间内执行向量检索。 |
| 178 | // query 为用户自然语言查询, meta 用于构造 namespace/过滤。 |
no test coverage detected