Skip to content

Commit 6c2b131

Browse files
authored
Added Feedback in SDK (#2393)
1 parent 2ffe992 commit 6c2b131

File tree

7 files changed

+112
-2
lines changed

7 files changed

+112
-2
lines changed

docs/docs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"features/direct-import",
5656
"features/async-client",
5757
"features/memory-export",
58-
"features/webhooks"
58+
"features/webhooks",
59+
"features/feedback-mechanism"
5960
]
6061
}
6162
]

docs/features/feedback-mechanism.mdx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: Feedback Mechanism
3+
icon: "thumbs-up"
4+
iconType: "solid"
5+
---
6+
7+
Mem0's **Feedback Mechanism** allows you to provide feedback on the memories generated by your application. This feedback is used to improve the accuracy of the memories and the search results.
8+
9+
## How it works
10+
11+
The feedback mechanism is a simple API that allows you to provide feedback on the memories generated by your application. The feedback is stored in the database and is used to improve the accuracy of the memories and the search results. Over time, Mem0 continuously learns from this feedback, refining its memory generation and search capabilities for better performance.
12+
13+
## Give Feedback
14+
15+
You can give feedback on a memory by calling the `feedback` method on the Mem0 client.
16+
17+
<CodeGroup>
18+
19+
```python Python
20+
from mem0 import Mem0
21+
22+
client = Mem0(api_key="your_api_key")
23+
24+
client.feedback(memory_id="your-memory-id", feedback="NEGATIVE", feedback_reason="I don't like this memory because it is not relevant.")
25+
```
26+
27+
```javascript JavaScript
28+
import MemoryClient from 'mem0ai';
29+
30+
const client = new MemoryClient({ apiKey: 'your-api-key'});
31+
32+
client.feedback({
33+
memory_id: "your-memory-id",
34+
feedback: "NEGATIVE",
35+
feedback_reason: "I don't like this memory because it is not relevant."
36+
})
37+
```
38+
39+
</CodeGroup>
40+
41+
## Feedback Types
42+
43+
The `feedback` parameter can be one of the following values:
44+
45+
- `POSITIVE`: The memory is useful.
46+
- `NEGATIVE`: The memory is not useful.
47+
- `VERY_NEGATIVE`: The memory is not useful at all.
48+
49+
## Parameters
50+
51+
The `feedback` method takes the following parameters:
52+
53+
- `memory_id`: The ID of the memory to give feedback on.
54+
- `feedback`: The feedback to give on the memory. (Optional)
55+
- `feedback_reason`: The reason for the feedback. (Optional)
56+
57+
The `feedback_reason` parameter is optional and can be used to provide a reason for the feedback.
58+
59+
<Note>
60+
You can pass `None` or `null` to the `feedback` and `feedback_reason` parameters to remove the feedback for a memory.
61+
</Note>
62+

mem0-ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mem0ai",
3-
"version": "2.1.5",
3+
"version": "2.1.8",
44
"description": "The Memory Layer For Your AI Apps",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

mem0-ts/src/client/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export type {
2323
Message,
2424
AllUsers,
2525
User,
26+
FeedbackPayload,
27+
Feedback,
2628
} from "./mem0.types";
2729

2830
// Export telemetry types

mem0-ts/src/client/mem0.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Webhook,
1313
WebhookPayload,
1414
Message,
15+
FeedbackPayload,
1516
} from "./mem0.types";
1617
import { captureClientEvent, generateHash } from "./telemetry";
1718

@@ -560,6 +561,18 @@ export default class MemoryClient {
560561
);
561562
return response;
562563
}
564+
565+
async feedback(data: FeedbackPayload): Promise<{ message: string }> {
566+
const response = await this._fetchWithErrorHandling(
567+
`${this.host}/v1/feedback/`,
568+
{
569+
method: "POST",
570+
headers: this.headers,
571+
body: JSON.stringify(data),
572+
},
573+
);
574+
return response;
575+
}
563576
}
564577

565578
export { MemoryClient };

mem0-ts/src/client/mem0.types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export enum API_VERSION {
3131
V2 = "v2",
3232
}
3333

34+
export enum Feedback {
35+
POSITIVE = "POSITIVE",
36+
NEGATIVE = "NEGATIVE",
37+
VERY_NEGATIVE = "VERY_NEGATIVE",
38+
}
39+
3440
export interface MultiModalMessages {
3541
type: "image_url";
3642
image_url: {
@@ -164,3 +170,9 @@ export interface WebhookPayload {
164170
name: string;
165171
url: string;
166172
}
173+
174+
export interface FeedbackPayload {
175+
memory_id: string;
176+
feedback?: Feedback | null;
177+
feedback_reason?: string | null;
178+
}

mem0/client/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44
from functools import wraps
55
from typing import Any, Dict, List, Optional, Union
6+
from enum import Enum
67

78
import httpx
89

@@ -998,3 +999,22 @@ async def delete_webhook(self, webhook_id: int) -> Dict[str, str]:
998999
response.raise_for_status()
9991000
capture_client_event("async_client.delete_webhook", self.sync_client, {"webhook_id": webhook_id})
10001001
return response.json()
1002+
1003+
@api_error_handler
1004+
async def feedback(self, memory_id: str, feedback: Optional[str] = None, feedback_reason: Optional[str] = None) -> Dict[str, str]:
1005+
VALID_FEEDBACK_VALUES = {"POSITIVE", "NEGATIVE", "VERY_NEGATIVE"}
1006+
1007+
feedback = feedback.upper() if feedback else None
1008+
if feedback is not None and feedback not in VALID_FEEDBACK_VALUES:
1009+
raise ValueError(f'feedback must be one of {", ".join(VALID_FEEDBACK_VALUES)} or None')
1010+
1011+
data = {
1012+
"memory_id": memory_id,
1013+
"feedback": feedback,
1014+
"feedback_reason": feedback_reason
1015+
}
1016+
1017+
response = await self.async_client.post("/v1/feedback/", json=data)
1018+
response.raise_for_status()
1019+
capture_client_event("async_client.feedback", self.sync_client, data)
1020+
return response.json()

0 commit comments

Comments
 (0)