Skip to content

Commit 731f0e2

Browse files
committed
feat: add comprehensive API reference documentation
1 parent 40ddf03 commit 731f0e2

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed

API-Reference.md

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
# Mem4j API Reference
2+
3+
基于当前代码实现的完整 API 文档。
4+
5+
## Base URL
6+
7+
所有 API 端点都基于以下 URL:
8+
9+
```
10+
http://localhost:8080/api/v1/memory
11+
```
12+
13+
## API 端点
14+
15+
### 1. 添加记忆
16+
17+
**POST** `/memory/add`
18+
19+
从对话中添加记忆。
20+
21+
#### 请求体示例:
22+
23+
```json
24+
{
25+
"messages": [
26+
{
27+
"role": "user",
28+
"content": "我喜欢吃苹果"
29+
},
30+
{
31+
"role": "assistant",
32+
"content": "好的,我记住了你喜欢吃苹果"
33+
}
34+
],
35+
"userId": "user123",
36+
"metadata": {
37+
"source": "chat",
38+
"timestamp": "2024-01-01T10:00:00Z"
39+
},
40+
"infer": true,
41+
"memoryType": "factual"
42+
}
43+
```
44+
45+
#### 参数说明:
46+
47+
- `messages`: 对话消息列表(必需)
48+
- `userId`: 用户 ID(必需)
49+
- `metadata`: 附加元数据(可选)
50+
- `infer`: 是否推理提取记忆(默认 true)
51+
- `memoryType`: 记忆类型,可选值:`factual`, `episodic`, `semantic`, `procedural`, `working`
52+
53+
#### 响应示例:
54+
55+
```json
56+
{
57+
"status": "success",
58+
"message": "Memories added successfully"
59+
}
60+
```
61+
62+
### 2. 搜索记忆
63+
64+
**GET** `/memory/search`
65+
66+
搜索相关记忆。
67+
68+
#### 查询参数:
69+
70+
- `query`: 搜索查询(必需)
71+
- `userId`: 用户 ID(必需)
72+
- `limit`: 返回结果数量限制(默认 10)
73+
- `threshold`: 相似度阈值(可选)
74+
- 其他过滤参数可以作为查询参数传递
75+
76+
#### 示例:
77+
78+
```
79+
GET /memory/search?query=苹果&userId=user123&limit=5
80+
```
81+
82+
#### 响应示例:
83+
84+
```json
85+
{
86+
"status": "success",
87+
"results": [
88+
{
89+
"id": "memory_id_1",
90+
"content": "用户喜欢吃苹果",
91+
"type": "factual",
92+
"score": 0.85,
93+
"metadata": {...}
94+
}
95+
],
96+
"count": 1
97+
}
98+
```
99+
100+
### 3. 获取所有记忆
101+
102+
**GET** `/memory/all`
103+
104+
获取用户的所有记忆。
105+
106+
#### 查询参数:
107+
108+
- `userId`: 用户 ID(必需)
109+
- `limit`: 返回结果数量限制(默认 100)
110+
- 其他过滤参数
111+
112+
#### 示例:
113+
114+
```
115+
GET /memory/all?userId=user123&limit=50
116+
```
117+
118+
#### 响应格式同搜索记忆
119+
120+
### 4. 获取特定记忆
121+
122+
**GET** `/memory/{memoryId}`
123+
124+
根据记忆 ID 获取特定记忆。
125+
126+
#### 路径参数:
127+
128+
- `memoryId`: 记忆 ID
129+
130+
#### 响应示例:
131+
132+
```json
133+
{
134+
"status": "success",
135+
"memory": {
136+
"id": "memory_id_1",
137+
"content": "用户喜欢吃苹果",
138+
"type": "factual",
139+
"metadata": {...}
140+
}
141+
}
142+
```
143+
144+
### 5. 更新记忆
145+
146+
**PUT** `/memory/{memoryId}`
147+
148+
更新指定记忆。
149+
150+
#### 路径参数:
151+
152+
- `memoryId`: 记忆 ID
153+
154+
#### 请求体示例:
155+
156+
```json
157+
{
158+
"content": "更新后的记忆内容",
159+
"metadata": {
160+
"updated": "2024-01-01T11:00:00Z"
161+
}
162+
}
163+
```
164+
165+
#### 响应示例:
166+
167+
```json
168+
{
169+
"status": "success",
170+
"message": "Memory updated successfully"
171+
}
172+
```
173+
174+
### 6. 删除记忆
175+
176+
**DELETE** `/memory/{memoryId}`
177+
178+
删除指定记忆。
179+
180+
#### 路径参数:
181+
182+
- `memoryId`: 记忆 ID
183+
184+
#### 响应示例:
185+
186+
```json
187+
{
188+
"status": "success",
189+
"message": "Memory deleted successfully"
190+
}
191+
```
192+
193+
### 7. 删除用户所有记忆
194+
195+
**DELETE** `/memory/user/{userId}`
196+
197+
删除指定用户的所有记忆。
198+
199+
#### 路径参数:
200+
201+
- `userId`: 用户 ID
202+
203+
#### 响应示例:
204+
205+
```json
206+
{
207+
"status": "success",
208+
"message": "All memories deleted successfully"
209+
}
210+
```
211+
212+
### 8. 重置所有记忆
213+
214+
**POST** `/memory/reset`
215+
216+
重置所有记忆(主要用于测试)。
217+
218+
#### 响应示例:
219+
220+
```json
221+
{
222+
"status": "success",
223+
"message": "All memories reset successfully"
224+
}
225+
```
226+
227+
## 记忆类型
228+
229+
支持的记忆类型:
230+
231+
- **factual**: 事实性记忆 - 存储事实和信息
232+
- **episodic**: 情景记忆 - 存储事件和经历
233+
- **semantic**: 语义记忆 - 存储概念和关系
234+
- **procedural**: 程序性记忆 - 存储操作方法信息
235+
- **working**: 工作记忆 - 当前任务的临时信息
236+
237+
## 错误处理
238+
239+
所有 API 在发生错误时都会返回以下格式:
240+
241+
```json
242+
{
243+
"status": "error",
244+
"message": "错误描述信息"
245+
}
246+
```
247+
248+
常见的 HTTP 状态码:
249+
250+
- `200`: 成功
251+
- `400`: 请求参数错误
252+
- `404`: 资源未找到
253+
- `500`: 服务器内部错误
254+
255+
## cURL 示例
256+
257+
### 添加记忆
258+
259+
```bash
260+
curl -X POST http://localhost:8080/api/v1/memory/add \
261+
-H "Content-Type: application/json" \
262+
-d '{
263+
"messages": [{"role": "user", "content": "我喜欢吃苹果"}],
264+
"userId": "user123",
265+
"memoryType": "factual"
266+
}'
267+
```
268+
269+
### 搜索记忆
270+
271+
```bash
272+
curl "http://localhost:8080/api/v1/memory/search?query=苹果&userId=user123&limit=5"
273+
```
274+
275+
### 获取所有记忆
276+
277+
```bash
278+
curl "http://localhost:8080/api/v1/memory/all?userId=user123"
279+
```
280+
281+
## 注意事项
282+
283+
1. 应用当前使用的上下文路径是 `/api/v1`,所以完整的端点路径是 `/api/v1/memory/*`
284+
2. 应用依赖 Mem4j 核心库,需要正确配置 DashScope 或 OpenAI API Key
285+
3. 默认使用 H2 内存数据库,生产环境建议使用 PostgreSQL
286+
4. 向量存储默认为内存模式,生产环境建议使用 Qdrant 或其他向量数据库

0 commit comments

Comments
 (0)