Add Message
This API allows you to add one or more messages to a specific conversation. As illustrated in the examples bellow, you can add messages in real time during a user-assistant interaction, import historical messages in bulk, or enrich the conversation with user preferences and behavior data. All added messages are transformed into memories by MemOS, enabling their retrieval in future conversations to support chat history management, user behavior tracking, and personalized interactions.
import os
import requests
import json
# Replace with your API Key
os.environ["MEMOS_API_KEY"] = "YOUR_API_KEY"
os.environ["MEMOS_BASE_URL"] = "https://memos.memtensor.cn/api/openmem/v1"
data = {
"user_id": "memos_user_123",
"conversation_id": "0610",
"messages": [
{"role": "user", "content": "I’ve planned to travel to Guangzhou this summer. What chain hotels are available for accommodation?"},
{"role": "assistant", "content": "You can consider options like 7 Days Inn, All Seasons, Hilton, etc."},
{"role": "user", "content": "I’ll choose 7 Days Inn."},
{"role": "assistant", "content": "Alright, feel free to ask me if you have any other questions."}
]
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Token {os.environ['MEMOS_API_KEY']}"
}
url = f"{os.environ['MEMOS_BASE_URL']}/add/message"
res = requests.post(url=url, headers=headers, data=json.dumps(data))
print(f"result: {res.json()}")
# Please ensure that MemOS has been installed (pip install MemoryOS -U)
from memos.api.client import MemOSClient
# Initialize MemOS client with API Key to start sending requests
client = MemOSClient(api_key="YOUR_API_KEY")
messages = [
{"role": "user", "content": "I’ve planned to travel to Guangzhou this summer. What chain hotels are available for accommodation?"},
{"role": "assistant", "content": "You can consider options like 7 Days Inn, All Seasons, Hilton, etc."},
{"role": "user", "content": "I’ll choose 7 Days Inn."},
{"role": "assistant", "content": "Alright, feel free to ask me if you have any other questions."}
]
user_id = "memos_user_123"
conversation_id = "0610"
res = client.add_message(messages=messages, user_id=user_id, conversation_id=conversation_id)
print(f"result: {res}")
curl --request POST \
--url https://memos.memtensor.cn/api/openmem/v1/add/message \
--header 'Authorization: Token YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"user_id": "memos_user_123",
"conversation_id": "0610",
"messages": [
{"role": "user", "content": "I’ve planned to travel to Guangzhou this summer. What chain hotels are available for accommodation?"},
{"role": "assistant", "content": "You can consider options like 7 Days Inn, All Seasons, Hilton, etc."},
{"role": "user", "content": "I’ll choose 7 Days Inn."},
{"role": "assistant", "content": "Alright, feel free to ask me if you have any other questions."}
]
}'
Authorizations
Token API_key, available in API Console > API Keys
Body
Unique identifier of the user associated with the message.
Unique identifier of the conversation.
Array of message objects representing the memory content. The total token limit for the message array is 8k. Each object contains:
Unique identifier of the Agent associated with the added message, primarily used to query exclusive memories between a user and that Agent during retrieval.
Unique identifier of the App associated with the added message, primarily used to query exclusive memories of a user under that App during retrieval.
List of custom tags used to mark the topic or category of the added message.
Custom metadata field capable of storing any structured data related to the added message, such as location, source, version, etc., primarily for precise filtering or source tracking during retrieval.
Whether memories generated from the added message are allowed to be written to the public memory store. When enabled, generated memories can be retrieved by other users in the project.
Scope of knowledgebases where memories generated from the added message are allowed to be written.
Whether to enable asynchronous memory addition. When enabled, memory will be added asynchronously in the background to avoid blocking the call chain.
Response
Successful Response
API status code. See Error Code for details
Object representing the result of adding the message.
API response message, e.g., "Message added successfully".
import os
import requests
import json
# Replace with your API Key
os.environ["MEMOS_API_KEY"] = "YOUR_API_KEY"
os.environ["MEMOS_BASE_URL"] = "https://memos.memtensor.cn/api/openmem/v1"
data = {
"user_id": "memos_user_123",
"conversation_id": "0610",
"messages": [
{"role": "user", "content": "I’ve planned to travel to Guangzhou this summer. What chain hotels are available for accommodation?"},
{"role": "assistant", "content": "You can consider options like 7 Days Inn, All Seasons, Hilton, etc."},
{"role": "user", "content": "I’ll choose 7 Days Inn."},
{"role": "assistant", "content": "Alright, feel free to ask me if you have any other questions."}
]
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Token {os.environ['MEMOS_API_KEY']}"
}
url = f"{os.environ['MEMOS_BASE_URL']}/add/message"
res = requests.post(url=url, headers=headers, data=json.dumps(data))
print(f"result: {res.json()}")
# Please ensure that MemOS has been installed (pip install MemoryOS -U)
from memos.api.client import MemOSClient
# Initialize MemOS client with API Key to start sending requests
client = MemOSClient(api_key="YOUR_API_KEY")
messages = [
{"role": "user", "content": "I’ve planned to travel to Guangzhou this summer. What chain hotels are available for accommodation?"},
{"role": "assistant", "content": "You can consider options like 7 Days Inn, All Seasons, Hilton, etc."},
{"role": "user", "content": "I’ll choose 7 Days Inn."},
{"role": "assistant", "content": "Alright, feel free to ask me if you have any other questions."}
]
user_id = "memos_user_123"
conversation_id = "0610"
res = client.add_message(messages=messages, user_id=user_id, conversation_id=conversation_id)
print(f"result: {res}")
curl --request POST \
--url https://memos.memtensor.cn/api/openmem/v1/add/message \
--header 'Authorization: Token YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"user_id": "memos_user_123",
"conversation_id": "0610",
"messages": [
{"role": "user", "content": "I’ve planned to travel to Guangzhou this summer. What chain hotels are available for accommodation?"},
{"role": "assistant", "content": "You can consider options like 7 Days Inn, All Seasons, Hilton, etc."},
{"role": "user", "content": "I’ll choose 7 Days Inn."},
{"role": "assistant", "content": "Alright, feel free to ask me if you have any other questions."}
]
}'