Skip to content

Commit b11ec2a

Browse files
authored
Merge pull request #67 from Halo1236/main
feat: add tool_tavily_search
2 parents c3d73ff + bb9d973 commit b11ec2a

File tree

20 files changed

+452
-0
lines changed

20 files changed

+452
-0
lines changed
3.81 KB
Binary file not shown.

tools/tool_mem0_add/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Mem0 记忆添加工具
2+
3+
基于 mem0 API 的单入口工具,用于向指定用户写入一条记忆。
4+
5+
## 功能说明
6+
- 添加一条用户或助手消息到 Mem0
7+
- 支持 metadata
8+
- 支持可选 agent_id
9+
- 使用官方 `POST /v1/memories/` 接口
10+
- 注意:Mem0 默认异步处理,写入后可能需要等待 2-3 秒再检索
11+
12+
## 参数说明
13+
### 启动参数
14+
| 参数 | 组件类型 | 必填 | 说明 |
15+
| :--- | :--- | :--- | :--- |
16+
| `api_key` | 密码框 || Mem0 API Key |
17+
| `base_url` | 文本框 || API 地址,默认 `https://api.mem0.ai/v1` |
18+
19+
### 输入参数
20+
| 参数名 | 数据类型 | 必填 | 来源 | 说明 |
21+
| :--- | :--- | :--- | :--- | :--- |
22+
| `user_id` | string || 引用参数 | 用户唯一标识 |
23+
| `content` | string || 引用参数 | 要写入的记忆内容 |
24+
| `role` | string || 引用参数 | 角色,默认 `user` |
25+
| `agent_id` | string || 引用参数 | agent 唯一标识 |
26+
| `metadata` | string || 引用参数 | JSON 字符串格式的元数据 |

tools/tool_mem0_add/add_memory.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import json
2+
import requests
3+
4+
5+
def add_memory(user_id: str, content: str, role: str = "user", agent_id: str = "", metadata: str = "", api_key: str = "", base_url: str = "https://api.mem0.ai/v1"):
6+
try:
7+
if not user_id:
8+
return json.dumps({"success": False, "message": "user_id 不能为空", "error": "missing_user_id"}, ensure_ascii=False)
9+
if not content:
10+
return json.dumps({"success": False, "message": "content 不能为空", "error": "missing_content"}, ensure_ascii=False)
11+
if not api_key:
12+
return json.dumps({"success": False, "message": "api_key 不能为空", "error": "missing_api_key"}, ensure_ascii=False)
13+
14+
payload = {
15+
"messages": [{"role": role or "user", "content": content}],
16+
"user_id": user_id,
17+
"infer": True,
18+
"version": "v2"
19+
}
20+
if agent_id:
21+
payload["agent_id"] = agent_id
22+
if metadata:
23+
try:
24+
payload["metadata"] = json.loads(metadata)
25+
except Exception:
26+
return json.dumps({"success": False, "message": "metadata 必须是合法 JSON 字符串", "error": "invalid_metadata_json"}, ensure_ascii=False)
27+
28+
rep = requests.post(
29+
url=f"{base_url.rstrip('/')}/memories/",
30+
headers={
31+
"Content-Type": "application/json",
32+
"Authorization": f"Token {api_key}",
33+
"Accept": "application/json"
34+
},
35+
json=payload,
36+
timeout=30
37+
)
38+
rep.raise_for_status()
39+
data = rep.json()
40+
return json.dumps({"success": True, "message": "记忆添加成功", "data": data}, ensure_ascii=False)
41+
except requests.exceptions.HTTPError as e:
42+
detail = None
43+
try:
44+
detail = e.response.json()
45+
except Exception:
46+
detail = e.response.text if e.response is not None else str(e)
47+
return json.dumps({
48+
"success": False,
49+
"message": "添加记忆失败",
50+
"error": str(e),
51+
"status_code": e.response.status_code if e.response is not None else None,
52+
"details": detail
53+
}, ensure_ascii=False)
54+
except requests.exceptions.RequestException as e:
55+
return json.dumps({"success": False, "message": "添加记忆请求失败", "error": str(e)}, ensure_ascii=False)
56+
except Exception as e:
57+
return json.dumps({"success": False, "message": "处理添加记忆响应时发生错误", "error": str(e)}, ensure_ascii=False)

tools/tool_mem0_add/data.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: Mem0记忆添加
2+
tags:
3+
- 模型记忆
4+
title: 添加内容到 Mem0 记忆库
5+
description: 添加内容到 Mem0 记忆库,支持附带 metadata 和 agent_id

tools/tool_mem0_add/logo.png

75.1 KB
Loading
3.68 KB
Binary file not shown.

tools/tool_mem0_get/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Mem0 记忆获取工具
2+
3+
基于 mem0 API 的单入口工具,用于获取指定范围内的记忆列表。
4+
5+
## 功能说明
6+
- 获取记忆列表
7+
- 支持 user_id / agent_id / app_id / run_id 作用域过滤
8+
- 至少需要提供一个范围参数
9+
- 当提供多个范围参数时,内部使用 `OR` 过滤
10+
- 使用官方 `POST /v2/memories/` 接口
11+
12+
## 参数说明
13+
### 启动参数
14+
| 参数 | 组件类型 | 必填 | 说明 |
15+
| :--- | :--- | :--- | :--- |
16+
| `api_key` | 密码框 || Mem0 API Key |
17+
| `base_url` | 文本框 || API 地址,默认 `https://api.mem0.ai` |
18+
19+
### 输入参数
20+
| 参数名 | 数据类型 | 必填 | 来源 | 说明 |
21+
| :--- | :--- | :--- | :--- | :--- |
22+
| `user_id` | string || 引用参数 | 用户唯一标识 |
23+
| `agent_id` | string || 引用参数 | agent 唯一标识 |
24+
| `app_id` | string || 引用参数 | 应用唯一标识 |
25+
| `run_id` | string || 引用参数 | 运行/会话唯一标识 |

tools/tool_mem0_get/data.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: Mem0记忆获取
2+
tags:
3+
- 模型记忆
4+
title: 获取用户在 Mem0 中的记忆列表
5+
description: 获取指定用户在 Mem0 中的记忆列表,可按关键词过滤
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import json
2+
import requests
3+
4+
5+
def get_memories(user_id: str = "", agent_id: str = "", app_id: str = "", run_id: str = "", api_key: str = "", base_url: str = "https://api.mem0.ai"):
6+
try:
7+
if not api_key:
8+
return json.dumps({"success": False, "message": "api_key 不能为空", "error": "missing_api_key"}, ensure_ascii=False)
9+
if not any([user_id, agent_id, app_id, run_id]):
10+
return json.dumps({
11+
"success": False,
12+
"message": "user_id、agent_id、app_id、run_id 至少需要提供一个",
13+
"error": "missing_scope_filter"
14+
}, ensure_ascii=False)
15+
16+
filters = []
17+
if user_id:
18+
filters.append({"user_id": user_id})
19+
if agent_id:
20+
filters.append({"agent_id": agent_id})
21+
if app_id:
22+
filters.append({"app_id": app_id})
23+
if run_id:
24+
filters.append({"run_id": run_id})
25+
26+
payload = {
27+
"version": "v2"
28+
}
29+
if len(filters) == 1:
30+
payload["filters"] = filters[0]
31+
else:
32+
payload["filters"] = {"OR": filters}
33+
34+
rep = requests.post(
35+
url=f"{base_url.rstrip('/')}/v2/memories/",
36+
headers={
37+
"Content-Type": "application/json",
38+
"Authorization": f"Token {api_key}",
39+
"Accept": "application/json"
40+
},
41+
json=payload,
42+
timeout=30
43+
)
44+
rep.raise_for_status()
45+
data = rep.json()
46+
return json.dumps({"success": True, "message": "记忆获取成功", "data": data}, ensure_ascii=False)
47+
except requests.exceptions.HTTPError as e:
48+
detail = None
49+
try:
50+
detail = e.response.json()
51+
except Exception:
52+
detail = e.response.text if e.response is not None else str(e)
53+
return json.dumps({
54+
"success": False,
55+
"message": "获取记忆失败",
56+
"error": str(e),
57+
"status_code": e.response.status_code if e.response is not None else None,
58+
"details": detail
59+
}, ensure_ascii=False)
60+
except requests.exceptions.RequestException as e:
61+
return json.dumps({"success": False, "message": "获取记忆请求失败", "error": str(e)}, ensure_ascii=False)
62+
except Exception as e:
63+
return json.dumps({"success": False, "message": "处理获取记忆响应时发生错误", "error": str(e)}, ensure_ascii=False)

tools/tool_mem0_get/logo.png

75.1 KB
Loading

0 commit comments

Comments
 (0)