From 5b9b65c3958dc0bf4ad55406bb9c39580ba7fb33 Mon Sep 17 00:00:00 2001 From: Prateek Chhikara <46902268+prateekchhikara@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:06:22 -0700 Subject: [PATCH] Doc Updates (#1843) --- docs/components/llms/models/azure_openai.mdx | 31 ++- docs/components/llms/overview.mdx | 26 +- docs/platform/quickstart.mdx | 272 +++++++++++++++++-- mem0/llms/ollama.py | 3 +- 4 files changed, 301 insertions(+), 31 deletions(-) diff --git a/docs/components/llms/models/azure_openai.mdx b/docs/components/llms/models/azure_openai.mdx index 002d4618eb..ddb99322b8 100644 --- a/docs/components/llms/models/azure_openai.mdx +++ b/docs/components/llms/models/azure_openai.mdx @@ -36,6 +36,35 @@ m = Memory.from_config(config) m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"}) ``` +We also support the new [OpenAI structured-outputs](https://platform.openai.com/docs/guides/structured-outputs/introduction) model. + +```python +import os +from mem0 import Memory + +os.environ["LLM_AZURE_OPENAI_API_KEY"] = "your-api-key" +os.environ["LLM_AZURE_DEPLOYMENT"] = "your-deployment-name" +os.environ["LLM_AZURE_ENDPOINT"] = "your-api-base-url" +os.environ["LLM_AZURE_API_VERSION"] = "version-to-use" + +config = { + "llm": { + "provider": "azure_openai_structured", + "config": { + "model": "your-deployment-name", + "temperature": 0.1, + "max_tokens": 2000, + "azure_kwargs" : { + "azure_deployment" : "", + "api_version" : "", + "azure_endpoint" : "", + "api_key" : "" + } + } + } +} +``` + ## Config -All available parameters for the `azure_openai` config are present in [Master List of All Params in Config](../config). \ No newline at end of file +All available parameters for the `azure_openai` config are present in [Master List of All Params in Config](../config). diff --git a/docs/components/llms/overview.mdx b/docs/components/llms/overview.mdx index 362ca3dd06..d0fce6a2ec 100644 --- a/docs/components/llms/overview.mdx +++ b/docs/components/llms/overview.mdx @@ -23,4 +23,28 @@ To view all supported llms, visit the [Supported LLMs](./models). - \ No newline at end of file + + +## Structured vs Unstructured Outputs + +Mem0 supports two types of OpenAI LLM formats, each with its own strengths and use cases: + +### Structured Outputs + +Structured outputs are LLMs that align with OpenAI's structured outputs model: + +- **Optimized for:** Returning structured responses (e.g., JSON objects) +- **Benefits:** Precise, easily parseable data +- **Ideal for:** Data extraction, form filling, API responses +- **Learn more:** [OpenAI Structured Outputs Guide](https://platform.openai.com/docs/guides/structured-outputs/introduction) + +### Unstructured Outputs + +Unstructured outputs correspond to OpenAI's standard, free-form text model: + +- **Flexibility:** Returns open-ended, natural language responses +- **Customization:** Use the `response_format` parameter to guide output +- **Trade-off:** Less efficient than structured outputs for specific data needs +- **Best for:** Creative writing, explanations, general conversation + +Choose the format that best suits your application's requirements for optimal performance and usability. diff --git a/docs/platform/quickstart.mdx b/docs/platform/quickstart.mdx index b307e23f49..95d34605af 100644 --- a/docs/platform/quickstart.mdx +++ b/docs/platform/quickstart.mdx @@ -55,7 +55,12 @@ messages = [ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."} ] -client.add(messages, user_id="alex") + +# The default output_format is v1.0 +client.add(messages, user_id="alex", output_format="v1.0") + +# To use the latest output_format, set the output_format parameter to "v1.1" +client.add(messages, user_id="alex", output_format="v1.1") ``` ```javascript JavaScript @@ -63,7 +68,7 @@ const messages = [ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."} ]; -client.add(messages, { user_id: "alex" }) +client.add(messages, { user_id: "alex", output_format: "v1.1" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` @@ -77,15 +82,39 @@ curl -X POST "https://api.mem0.ai/v1/memories/" \ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."} ], - "user_id": "alex" + "user_id": "alex", + "output_format": "v1.1" }' ``` -```json Output +```json Output (v1.0) {'message': 'ok'} ``` +```json Output (v1.1) +{ + "results": [ + { + "memory": "Name is Alex", + "event": "ADD" + }, + { + "memory": "Is a vegetarian", + "event": "ADD" + }, + { + "memory": "Is allergic to nuts", + "event": "ADD" + } + ] +} +``` + + + The `add` method supports two output formats: `v1.0` (default) and `v1.1`. To use the latest format, which provides more detailed information about each memory operation, set the `output_format` parameter to `v1.1`: + + #### Short-term memory for a user session These memory instances persist only for the duration of a user session. Ideal for non-repetitive interactions and managing context windows efficiently. @@ -99,7 +128,12 @@ messages = [ {"role": "user", "content": "Yes, please! Especially in Tokyo."}, {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."} ] -client.add(messages, user_id="alex123", session_id="trip-planning-2024") + +# The default output_format is v1.0 +client.add(messages, user_id="alex123", session_id="trip-planning-2024", output_format="v1.0") + +# To use the latest output_format, set the output_format parameter to "v1.1" +client.add(messages, user_id="alex123", session_id="trip-planning-2024", output_format="v1.1") ``` ```javascript JavaScript @@ -109,7 +143,7 @@ const messages = [ {"role": "user", "content": "Yes, please! Especially in Tokyo."}, {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."} ]; -client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024" }) +client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024", output_format: "v1.1" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` @@ -126,15 +160,35 @@ curl -X POST "https://api.mem0.ai/v1/memories/" \ {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."} ], "user_id": "alex123", - "session_id": "trip-planning-2024" + "session_id": "trip-planning-2024", + "output_format": "v1.1" }' ``` -```json Output +```json Output (v1.0) {'message': 'ok'} ``` + +```json Output (v1.1) +{ + "results": [ + { + "memory": "Planning a trip to Japan next month", + "event": "ADD" + }, + { + "memory": "Interested in vegetarian restaurants in Tokyo", + "event": "ADD" + } + ] +} +``` + + + + #### Long-term memory for agents Add a memory layer for the assistants and agents so that their responses remain consistent across sessions. @@ -145,7 +199,12 @@ messages = [ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."} ] -client.add(messages, agent_id="ai-tutor") + +# The default output_format is v1.0 +client.add(messages, agent_id="ai-tutor", output_format="v1.0") + +# To use the latest output_format, set the output_format parameter to "v1.1" +client.add(messages, agent_id="ai-tutor", output_format="v1.1") ``` ```javascript JavaScript @@ -153,7 +212,7 @@ const messages = [ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."} ]; -client.add(messages, { agent_id: "ai-tutor" }) +client.add(messages, { agent_id: "ai-tutor", output_format: "v1.1" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` @@ -167,15 +226,36 @@ curl -X POST "https://api.mem0.ai/v1/memories/" \ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."} ], - "agent_id": "ai-tutor" + "agent_id": "ai-tutor", + "output_format": "v1.1" }' ``` -```json Output +```json Output (v1.0) {'message': 'ok'} ``` + +```json Output (v1.1) +{ + "results": [ + { + "memory": "Name is Alex", + "event": "ADD" + }, + { + "memory": "Is a vegetarian", + "event": "ADD" + }, + { + "memory": "Is allergic to nuts", + "event": "ADD" + } + ] +} +``` + #### Monitor Memories You can monitor memory operations on the platform dashboard: @@ -188,16 +268,23 @@ You can monitor memory operations on the platform dashboard: Pass user messages, interactions, and queries into our search method to retrieve relevant memories. + The `search` method supports two output formats: `v1.0` (default) and `v1.1`. To use the latest format, which provides more detailed information about each memory operation, set the `output_format` parameter to `v1.1`: + ```python Python query = "What should I cook for dinner today?" -client.search(query, user_id="alex") + +# The default output_format is v1.0 +client.search(query, user_id="alex", output_format="v1.0") + +# To use the latest output_format, set the output_format parameter to "v1.1" +client.search(query, user_id="alex", output_format="v1.1") ``` ```javascript JavaScript const query = "What should I cook for dinner today?"; -client.search(query, { user_id: "alex" }) +client.search(query, { user_id: "alex", output_format: "v1.1" }) .then(results => console.log(results)) .catch(error => console.error(error)); ``` @@ -208,11 +295,12 @@ curl -X POST "https://api.mem0.ai/v1/memories/search/" \ -H "Content-Type: application/json" \ -d '{ "query": "What should I cook for dinner today?", - "user_id": "alex" + "user_id": "alex", + "output_format": "v1.1" }' ``` -```json Output +```json Output (v1.0) [ { "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5", @@ -235,8 +323,35 @@ curl -X POST "https://api.mem0.ai/v1/memories/search/" \ } ] ``` + +```json Output (v1.1) +{ + "results": [ + { + "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5", + "memory": "Vegetarian. Allergic to nuts.", + "input": [ + { + "role": "user", + "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." + }, + { + "role": "assistant", + "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions." + } + ], + "user_id": "alex", + "hash": "9ee7e1455e84d1dab700ed8749aed75a", + "metadata": null, + "created_at": "2024-07-20T01:30:36.275141-07:00", + "updated_at": "2024-07-20T01:30:36.275172-07:00" + } + ] +} +``` + #### Search using custom filters Our advanced search allows you to set custom search filters. You can filter by user_id, agent_id, app_id, date, and more. @@ -466,26 +581,33 @@ curl -X GET "https://api.mem0.ai/v1/entities/" \ Fetch all memories for a user, agent, or session using the getAll() method. + The `get_all` method supports two output formats: `v1.0` (default) and `v1.1`. To use the latest format, which provides more detailed information about each memory operation, set the `output_format` parameter to `v1.1`: + + #### Get all memories of a user ```python Python -user_memories = client.get_all(user_id="alex") +# The default output_format is v1.0 +user_memories = client.get_all(user_id="alex", output_format="v1.0") + +# To use the latest output_format (v1.1), set the output_format parameter to "v1.1" +user_memories = client.get_all(user_id="alex", output_format="v1.1") ``` ```javascript JavaScript -client.getAll({ user_id: "alex" }) +client.getAll({ user_id: "alex", output_format: "v1.1" }) .then(memories => console.log(memories)) .catch(error => console.error(error)); ``` ```bash cURL -curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex" \ +curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex&output_format=v1.1" \ -H "Authorization: Token your-api-key" ``` -```json Output +```json Output (v1.0) [ { "id":"f38b689d-6b24-45b7-bced-17fbb4d8bac7", @@ -507,6 +629,32 @@ curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex" \ } ] ``` + +```json Output (v1.1) +{ + "results": [ + { + "id": "f38b689d-6b24-45b7-bced-17fbb4d8bac7", + "memory": "是素食主义者,对坚果过敏。", + "agent_id": "travel-assistant", + "hash": "62bc074f56d1f909f1b4c2b639f56f6a", + "metadata": "None", + "created_at": "2024-07-25T23:57:00.108347-07:00", + "updated_at": "2024-07-25T23:57:00.108367-07:00" + }, + { + "id": "0a14d8f0-e364-4f5c-b305-10da1f0d0878", + "memory": "Will maintain personalized travel preferences for each user. Provide customized recommendations based on dietary restrictions, interests, and past interactions.", + "agent_id": "travel-assistant", + "hash": "35a305373d639b0bffc6c2a3e2eb4244", + "metadata": "None", + "created_at": "2024-07-26T00:31:03.543759-07:00", + "updated_at": "2024-07-26T00:31:03.543778-07:00" + } + ] +} +``` + @@ -514,21 +662,25 @@ curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex" \ ```python Python -client.get_all(agent_id="ai-tutor") +# The default output_format is v1.0 +client.get_all(agent_id="ai-tutor", output_format="v1.0") + +# To use the latest output_format (v1.1), set the output_format parameter to "v1.1" +client.get_all(agent_id="ai-tutor", output_format="v1.1") ``` ```javascript JavaScript -client.getAll({ agent_id: "ai-tutor" }) +client.getAll({ agent_id: "ai-tutor", output_format: "v1.1" }) .then(memories => console.log(memories)) .catch(error => console.error(error)); ``` ```bash cURL -curl -X GET "https://api.mem0.ai/v1/memories/?agent_id=travel-assistant" \ +curl -X GET "https://api.mem0.ai/v1/memories/?agent_id=travel-assistant&output_format=v1.1" \ -H "Authorization: Token your-api-key" ``` -```json Output +```json Output (v1.0) [ { "id":"f38b689d-6b24-45b7-bced-17fbb4d8bac7", @@ -550,6 +702,31 @@ curl -X GET "https://api.mem0.ai/v1/memories/?agent_id=travel-assistant" \ } ] ``` + +```json Output (v1.1) +{ + "results": [ + { + "id": "f38b689d-6b24-45b7-bced-17fbb4d8bac7", + "memory": "是素食主义者,对坚果过敏。", + "agent_id": "ai-tutor", + "hash": "62bc074f56d1f909f1b4c2b639f56f6a", + "metadata": "None", + "created_at": "2024-07-25T23:57:00.108347-07:00", + "updated_at": "2024-07-25T23:57:00.108367-07:00" + }, + { + "id": "0a14d8f0-e364-4f5c-b305-10da1f0d0878", + "memory": "My name is Alice.", + "agent_id": "ai-tutor", + "hash": "35a305373d639b0bffc6c2a3e2eb4244", + "metadata": "None", + "created_at": "2024-07-26T00:31:03.543759-07:00", + "updated_at": "2024-07-26T00:31:03.543778-07:00" + } + ] +} +``` #### Get the short-term memories for a session @@ -557,21 +734,25 @@ curl -X GET "https://api.mem0.ai/v1/memories/?agent_id=travel-assistant" \ ```python Python -short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024") +# The default output_format is v1.0 +short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024", output_format="v1.0") + +# To use the latest output_format (v1.1), set the output_format parameter to "v1.1" +short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024", output_format="v1.1") ``` ```javascript JavaScript -client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" }) +client.getAll({ user_id: "alex123", session_id: "trip-planning-2024", output_format: "v1.1" }) .then(memories => console.log(memories)) .catch(error => console.error(error)); ``` ```bash cURL -curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex123&session_id=trip-planning-2024" \ +curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex123&session_id=trip-planning-2024&output_format=v1.1" \ -H "Authorization: Token your-api-key" ``` -```json Output +```json Output (v1.0) [ { "id":"06d8df63-7bd2-4fad-9acb-60871bcecee0", @@ -602,8 +783,43 @@ curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex123&session_id=trip-pl } ] ``` + +```json Output (v1.1) +{ + "results": [ + { + "id": "06d8df63-7bd2-4fad-9acb-60871bcecee0", + "memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", + "user_id": "alex123", + "hash": "d2088c936e259f2f5d2d75543d31401c", + "metadata": "None", + "created_at": "2024-07-26T00:25:16.566471-07:00", + "updated_at": "2024-07-26T00:25:16.566492-07:00" + }, + { + "id": "b4229775-d860-4ccb-983f-0f628ca112f5", + "memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", + "user_id": "alex123", + "hash": "d2088c936e259f2f5d2d75543d31401c", + "metadata": "None", + "created_at": "2024-07-26T00:33:20.350542-07:00", + "updated_at": "2024-07-26T00:33:20.350560-07:00" + }, + { + "id": "df1aca24-76cf-4b92-9f58-d03857efcb64", + "memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", + "user_id": "alex123", + "hash": "d2088c936e259f2f5d2d75543d31401c", + "metadata": "None", + "created_at": "2024-07-26T00:51:09.642275-07:00", + "updated_at": "2024-07-26T00:51:09.642295-07:00" + } + ] +} +``` + #### Get specific memory diff --git a/mem0/llms/ollama.py b/mem0/llms/ollama.py index 6750f0dd4c..54d8b719fb 100644 --- a/mem0/llms/ollama.py +++ b/mem0/llms/ollama.py @@ -85,7 +85,8 @@ def generate_response( }, } if response_format: - params["format"] = response_format + params["format"] = "json" + if tools: params["tools"] = tools