|
| 1 | +--- |
| 2 | +title: Chat |
| 3 | +desc: An integrated RAG closed-loop API covering retrieval, generation, and storage. Supports MemCube-based personalized responses and automatic memory persistence. |
| 4 | +--- |
| 5 | + |
| 6 | +:::note |
| 7 | +For a complete list of API fields, formats, and other details, see the [Chat API Reference](/api_docs/chat/chat). |
| 8 | +::: |
| 9 | + |
| 10 | +**API Paths**: |
| 11 | +* **Full Response**: `POST /product/chat/complete` |
| 12 | +* **Streaming Response (SSE)**: `POST /product/chat/stream` |
| 13 | + |
| 14 | +**Description**: This API is the core business orchestration entry point of MemOS. It automatically retrieves relevant memories from the specified `readable_cube_ids`, generates a response based on the current context, and optionally writes the conversation result back to `writable_cube_ids`, enabling self-evolution of AI applications. |
| 15 | + |
| 16 | + |
| 17 | +## 1. Core Architecture: ChatHandler Orchestration Flow |
| 18 | + |
| 19 | +1. **Memory Retrieval**: Calls **SearchHandler** against `readable_cube_ids` to extract relevant facts, preferences, and tool context from isolated Cubes. |
| 20 | +2. **Context-Enhanced Generation**: Injects the retrieved memory fragments into the prompt and calls the specified LLM (via `model_name_or_path`) to generate a targeted response. |
| 21 | +3. **Automatic Memory Loop (Storage)**: When `add_message_on_answer=true`, the system asynchronously calls **AddHandler** to store this conversation in the specified Cubes — no manual add calls required. |
| 22 | + |
| 23 | +## 2. Key API Parameters |
| 24 | + |
| 25 | +### 2.1 Identity & Context |
| 26 | +| Parameter | Type | Required | Description | |
| 27 | +| :--- | :--- | :--- | :--- | |
| 28 | +| **`query`** | `str` | Yes | The user's current question or input. | |
| 29 | +| **`user_id`** | `str` | Yes | Unique user identifier, used for authentication and data isolation. | |
| 30 | +| `history` | `list` | No | Short-term conversation history to maintain coherence within the current session. | |
| 31 | +| `session_id` | `str` | No | Session ID. Acts as a "soft signal" to boost recall weight of related memories within this session. | |
| 32 | + |
| 33 | +### 2.2 MemCube Read/Write Control |
| 34 | +| Parameter | Type | Default | Description | |
| 35 | +| :--- | :--- | :--- | :--- | |
| 36 | +| **`readable_cube_ids`** | `list` | - | **Read:** List of memory Cubes allowed for retrieval (can span personal and public libraries). | |
| 37 | +| **`writable_cube_ids`** | `list` | - | **Write:** Target Cube list where auto-generated memories should be stored after the conversation. | |
| 38 | +| **`add_message_on_answer`** | `bool` | `true` | Whether to enable automatic write-back. Recommended to keep memory continuously updated. | |
| 39 | + |
| 40 | +### 2.3 Algorithm & Model Configuration |
| 41 | +| Parameter | Type | Default | Description | |
| 42 | +| :--- | :--- | :--- | :--- | |
| 43 | +| `mode` | `str` | `fast` | Retrieval mode: `fast`, `fine`, or `mixture`. | |
| 44 | +| `model_name_or_path` | `str` | - | Specifies the LLM model name or path to use. | |
| 45 | +| `system_prompt` | `str` | - | Overrides the default system prompt. | |
| 46 | +| `temperature` | `float` | - | Sampling temperature, controlling the creativity of generated text. | |
| 47 | +| `threshold` | `float` | `0.5` | Relevance threshold for memory recall. Memories scoring below this value are discarded. | |
| 48 | + |
| 49 | +## 3. How It Works |
| 50 | + |
| 51 | +MemOS provides two response modes to choose from: |
| 52 | + |
| 53 | +### 3.1 Full Response (`/complete`) |
| 54 | +* **Behavior**: Waits for the model to generate the full output, then returns it as a single JSON response. |
| 55 | +* **Use Cases**: Non-interactive tasks, backend logic processing, or simple applications with low real-time requirements. |
| 56 | + |
| 57 | +### 3.2 Streaming Response (`/stream`) |
| 58 | +* **Behavior**: Uses the **Server-Sent Events (SSE)** protocol to push tokens in real time. |
| 59 | +* **Use Cases**: Chatbots, intelligent assistants, and other UI interactions that require a typewriter-style streaming effect. |
| 60 | + |
| 61 | +## 4. Quick Start |
| 62 | + |
| 63 | +We recommend using the built-in `MemOSClient` from the open-source edition. The following example shows how to ask for R language learning advice while leveraging memory: |
| 64 | + |
| 65 | +```python |
| 66 | +from memos.api.client import MemOSClient |
| 67 | + |
| 68 | +client = MemOSClient(api_key="...", base_url="...") |
| 69 | + |
| 70 | +# Initiate a chat request |
| 71 | +res = client.chat( |
| 72 | + user_id="dev_user_01", |
| 73 | + query="Based on my previous preferences, recommend an R language data cleaning workflow", |
| 74 | + readable_cube_ids=["private_cube_01", "public_kb_r_lang"], # Read: personal preferences + public knowledge base |
| 75 | + writable_cube_ids=["private_cube_01"], # Write: persist to personal space |
| 76 | + add_message_on_answer=True, # Enable automatic memory write-back |
| 77 | + mode="fine" # Use fine-grained retrieval mode |
| 78 | +) |
| 79 | + |
| 80 | +if res: |
| 81 | + print(f"AI Response: {res.data}") |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +:::note |
| 86 | +**Developer Tip:** |
| 87 | +To debug in the `Playground` environment, use the dedicated debug streaming endpoint /product/chat/stream/playground. |
| 88 | +::: |
0 commit comments