WrapModelCall 包装模型调用,注入 Logic Memory 到 system prompt
(ctx context.Context, req *ModelRequest, handler ModelCallHandler)
| 178 | |
| 179 | // WrapModelCall 包装模型调用,注入 Logic Memory 到 system prompt |
| 180 | func (m *LogicMemoryMiddleware) WrapModelCall(ctx context.Context, req *ModelRequest, handler ModelCallHandler) (*ModelResponse, error) { |
| 181 | // 如果不启用注入,直接调用下一层 |
| 182 | if !m.config.EnableInjection { |
| 183 | return handler(ctx, req) |
| 184 | } |
| 185 | |
| 186 | // 获取 namespace |
| 187 | namespace := m.namespaceExtractor(req) |
| 188 | if namespace == "" { |
| 189 | // 没有 namespace,跳过注入 |
| 190 | return handler(ctx, req) |
| 191 | } |
| 192 | |
| 193 | // 检索相关 Memory |
| 194 | memories, err := m.manager.RetrieveMemories(ctx, namespace, |
| 195 | logic.WithTopK(m.config.MaxMemories), |
| 196 | logic.WithMinConfidence(m.config.MinConfidence), |
| 197 | logic.WithOrderBy(logic.OrderByConfidence), |
| 198 | ) |
| 199 | if err != nil { |
| 200 | lmLog.Error(ctx, "failed to retrieve memories", map[string]any{"error": err.Error()}) |
| 201 | // 继续执行,不因为 Memory 检索失败而中断 |
| 202 | return handler(ctx, req) |
| 203 | } |
| 204 | |
| 205 | // 如果没有相关 Memory,直接调用 |
| 206 | if len(memories) == 0 { |
| 207 | return handler(ctx, req) |
| 208 | } |
| 209 | |
| 210 | // 保存原始 system prompt |
| 211 | originalSystemPrompt := req.SystemPrompt |
| 212 | |
| 213 | // 构建 Memory 注入文本 |
| 214 | memorySection := m.buildMemorySection(memories) |
| 215 | |
| 216 | // 根据配置的注入点注入 |
| 217 | switch m.config.InjectionPoint { |
| 218 | case "system_prompt_start": |
| 219 | if originalSystemPrompt != "" { |
| 220 | req.SystemPrompt = memorySection + "\n\n" + originalSystemPrompt |
| 221 | } else { |
| 222 | req.SystemPrompt = memorySection |
| 223 | } |
| 224 | case "system_prompt_end": |
| 225 | if originalSystemPrompt != "" { |
| 226 | req.SystemPrompt = originalSystemPrompt + "\n\n" + memorySection |
| 227 | } else { |
| 228 | req.SystemPrompt = memorySection |
| 229 | } |
| 230 | case "both": |
| 231 | if originalSystemPrompt != "" { |
| 232 | req.SystemPrompt = memorySection + "\n\n" + originalSystemPrompt + "\n\n" + memorySection |
| 233 | } else { |
| 234 | req.SystemPrompt = memorySection |
| 235 | } |
| 236 | default: |
| 237 | // 默认追加到末尾 |