diff --git a/docs/docs.json b/docs/docs.json index 7ff25ad7..75b73aae 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -55,7 +55,8 @@ "features/direct-import", "features/async-client", "features/memory-export", - "features/webhooks" + "features/webhooks", + "features/feedback-mechanism" ] } ] diff --git a/docs/features/feedback-mechanism.mdx b/docs/features/feedback-mechanism.mdx new file mode 100644 index 00000000..dcd40efd --- /dev/null +++ b/docs/features/feedback-mechanism.mdx @@ -0,0 +1,62 @@ +--- +title: Feedback Mechanism +icon: "thumbs-up" +iconType: "solid" +--- + +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. + +## How it works + +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. + +## Give Feedback + +You can give feedback on a memory by calling the `feedback` method on the Mem0 client. + + + +```python Python +from mem0 import Mem0 + +client = Mem0(api_key="your_api_key") + +client.feedback(memory_id="your-memory-id", feedback="NEGATIVE", feedback_reason="I don't like this memory because it is not relevant.") +``` + +```javascript JavaScript +import MemoryClient from 'mem0ai'; + +const client = new MemoryClient({ apiKey: 'your-api-key'}); + +client.feedback({ + memory_id: "your-memory-id", + feedback: "NEGATIVE", + feedback_reason: "I don't like this memory because it is not relevant." +}) +``` + + + +## Feedback Types + +The `feedback` parameter can be one of the following values: + +- `POSITIVE`: The memory is useful. +- `NEGATIVE`: The memory is not useful. +- `VERY_NEGATIVE`: The memory is not useful at all. + +## Parameters + +The `feedback` method takes the following parameters: + +- `memory_id`: The ID of the memory to give feedback on. +- `feedback`: The feedback to give on the memory. (Optional) +- `feedback_reason`: The reason for the feedback. (Optional) + +The `feedback_reason` parameter is optional and can be used to provide a reason for the feedback. + + +You can pass `None` or `null` to the `feedback` and `feedback_reason` parameters to remove the feedback for a memory. + + diff --git a/mem0-ts/package.json b/mem0-ts/package.json index 4cc1ffc6..9eff2085 100644 --- a/mem0-ts/package.json +++ b/mem0-ts/package.json @@ -1,6 +1,6 @@ { "name": "mem0ai", - "version": "2.1.5", + "version": "2.1.8", "description": "The Memory Layer For Your AI Apps", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/mem0-ts/src/client/index.ts b/mem0-ts/src/client/index.ts index 3ed4ef99..9a7217ee 100644 --- a/mem0-ts/src/client/index.ts +++ b/mem0-ts/src/client/index.ts @@ -23,6 +23,8 @@ export type { Message, AllUsers, User, + FeedbackPayload, + Feedback, } from "./mem0.types"; // Export telemetry types diff --git a/mem0-ts/src/client/mem0.ts b/mem0-ts/src/client/mem0.ts index 760b15cf..8ddabe22 100644 --- a/mem0-ts/src/client/mem0.ts +++ b/mem0-ts/src/client/mem0.ts @@ -12,6 +12,7 @@ import { Webhook, WebhookPayload, Message, + FeedbackPayload, } from "./mem0.types"; import { captureClientEvent, generateHash } from "./telemetry"; @@ -560,6 +561,18 @@ export default class MemoryClient { ); return response; } + + async feedback(data: FeedbackPayload): Promise<{ message: string }> { + const response = await this._fetchWithErrorHandling( + `${this.host}/v1/feedback/`, + { + method: "POST", + headers: this.headers, + body: JSON.stringify(data), + }, + ); + return response; + } } export { MemoryClient }; diff --git a/mem0-ts/src/client/mem0.types.ts b/mem0-ts/src/client/mem0.types.ts index 50ae316c..26ee13b4 100644 --- a/mem0-ts/src/client/mem0.types.ts +++ b/mem0-ts/src/client/mem0.types.ts @@ -31,6 +31,12 @@ export enum API_VERSION { V2 = "v2", } +export enum Feedback { + POSITIVE = "POSITIVE", + NEGATIVE = "NEGATIVE", + VERY_NEGATIVE = "VERY_NEGATIVE", +} + export interface MultiModalMessages { type: "image_url"; image_url: { @@ -164,3 +170,9 @@ export interface WebhookPayload { name: string; url: string; } + +export interface FeedbackPayload { + memory_id: string; + feedback?: Feedback | null; + feedback_reason?: string | null; +} diff --git a/mem0/client/main.py b/mem0/client/main.py index 95024d69..e89871b3 100644 --- a/mem0/client/main.py +++ b/mem0/client/main.py @@ -3,6 +3,7 @@ import os import warnings from functools import wraps from typing import Any, Dict, List, Optional, Union +from enum import Enum import httpx @@ -998,3 +999,22 @@ class AsyncMemoryClient: response.raise_for_status() capture_client_event("async_client.delete_webhook", self.sync_client, {"webhook_id": webhook_id}) return response.json() + + @api_error_handler + async def feedback(self, memory_id: str, feedback: Optional[str] = None, feedback_reason: Optional[str] = None) -> Dict[str, str]: + VALID_FEEDBACK_VALUES = {"POSITIVE", "NEGATIVE", "VERY_NEGATIVE"} + + feedback = feedback.upper() if feedback else None + if feedback is not None and feedback not in VALID_FEEDBACK_VALUES: + raise ValueError(f'feedback must be one of {", ".join(VALID_FEEDBACK_VALUES)} or None') + + data = { + "memory_id": memory_id, + "feedback": feedback, + "feedback_reason": feedback_reason + } + + response = await self.async_client.post("/v1/feedback/", json=data) + response.raise_for_status() + capture_client_event("async_client.feedback", self.sync_client, data) + return response.json() \ No newline at end of file