|
| 1 | +# MemOS 记忆检索工具 |
| 2 | + |
| 3 | +基于 MemOS 记忆存储系统 API 封装的记忆检索工具,根据查询文本检索指定用户的记忆,返回与查询内容最相关的事实记忆、偏好记忆片段供 Agent 使用。 |
| 4 | + |
| 5 | +## 一、功能说明 |
| 6 | + |
| 7 | +- 根据查询文本在记忆库中搜索最相关的记忆片段 |
| 8 | +- 支持召回事实记忆、偏好记忆两类 |
| 9 | +- 可自定义返回的记忆项数量 |
| 10 | + |
| 11 | +## 二、参数说明 |
| 12 | + |
| 13 | +### 2.1 启动参数 |
| 14 | + |
| 15 | +| 参数 | 组件类型 | 必填 | 说明 | |
| 16 | +| :--- | :--- | :--- | :--- | |
| 17 | +| `access_key` | 密码框 | 是 | MemOS API Key,在 API 控制台 > 接口密钥中获取 | |
| 18 | + |
| 19 | +### 2.2 输入参数 |
| 20 | + |
| 21 | +| 参数名 | 数据类型 | 必填 | 来源 | 说明 | |
| 22 | +| :--- | :--- | :--- | :--- | :--- | |
| 23 | +| `user_id` | string | 是 | 引用参数 | 用户唯一标识符 | |
| 24 | +| `query` | string | 是 | 引用参数 | 查询文本内容(token 上限 20k) | |
| 25 | +| `conversation_id` | string | 否 | 引用参数 | 会话唯一标识符 | |
| 26 | +| `memory_limit_number` | int | 否 | 引用参数 | 事实记忆返回条数(默认 6,最大 25) | |
| 27 | + |
| 28 | +> **参数获取说明**: |
| 29 | +> - `user_id`:MaxKB 中使用系统变量 `{{user_id}}`,建议添加前缀如 `maxkb_{{user_id}}` |
| 30 | +> - `conversation_id`:MaxKB 中使用系统变量 `{{chat_id}}` |
| 31 | +
|
| 32 | +--- |
| 33 | + |
| 34 | +## 三、工具内容(Python) |
| 35 | + |
| 36 | +```python |
| 37 | +import requests |
| 38 | + |
| 39 | + |
| 40 | +def search_memory(user_id: str, query: str, conversation_id: str = "", memory_limit_number: int = 6, access_key: str = ""): |
| 41 | + """ |
| 42 | + MemOS 记忆检索 |
| 43 | +
|
| 44 | + 参数类型(根据官方API文档): |
| 45 | + - user_id: string, 必填, 用户唯一标识符 |
| 46 | + - query: string, 必填, 查询文本内容(token上限20k) |
| 47 | + - conversation_id: string, 可选, 会话唯一标识符 |
| 48 | + - memory_limit_number: number, 可选, 事实记忆返回条数(默认6,最大25) |
| 49 | + - access_key: string, 必填, API密钥 |
| 50 | + """ |
| 51 | + http_session = requests.Session() |
| 52 | + try: |
| 53 | + data = { |
| 54 | + "user_id": user_id, |
| 55 | + "query": query, |
| 56 | + "include_preference": True, |
| 57 | + "memory_limit_number": memory_limit_number |
| 58 | + } |
| 59 | + if conversation_id: |
| 60 | + data["conversation_id"] = conversation_id |
| 61 | + |
| 62 | + rep = http_session.post( |
| 63 | + url="https://memos.memtensor.cn/api/openmem/v1/search/memory", |
| 64 | + headers={ |
| 65 | + "Content-Type": "application/json", |
| 66 | + "Authorization": f"Token {access_key}" |
| 67 | + }, |
| 68 | + json=data |
| 69 | + ).json() |
| 70 | + |
| 71 | + if rep.get("code") == 0: |
| 72 | + result_parts = [] |
| 73 | + memory_list = rep.get("data", {}).get("memory_detail_list", []) |
| 74 | + preference_list = rep.get("data", {}).get("preference_detail_list", []) |
| 75 | + |
| 76 | + if memory_list: |
| 77 | + result_parts.append("【事实记忆】") |
| 78 | + for mem in memory_list: |
| 79 | + result_parts.append(f"- {mem.get('memory_key', '')}: {mem.get('memory_value', '')}") |
| 80 | + |
| 81 | + if preference_list: |
| 82 | + result_parts.append("【偏好记忆】") |
| 83 | + for pref in preference_list: |
| 84 | + result_parts.append(f"- {pref.get('preference', '')}") |
| 85 | + |
| 86 | + if result_parts: |
| 87 | + return "\n".join(result_parts) |
| 88 | + else: |
| 89 | + return "未找到相关记忆" |
| 90 | + else: |
| 91 | + return f"错误:记忆检索失败:{rep.get('message', '未知错误')}" |
| 92 | + except requests.exceptions.RequestException as e: |
| 93 | + return f"错误:记忆检索时发生网络错误: {e}" |
| 94 | + except Exception as e: |
| 95 | + return f"错误:处理记忆检索响应时发生错误: {e}" |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
0 commit comments