> ## Documentation Index
> Fetch the complete documentation index at: https://docs.memorylake.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 生成数据库记忆指引

> 用模型为数据库记忆起草指引

```
POST /openapi/memorylake/api/v3/workspaces/{workspaceId}/projects/{projectId}/memories/databases/{databaseId}/instruction/generate
```

用模型起草一段回答针对这个数据库的问题时遵循的指引，并返回这份草稿。草稿不会自动保存：请审阅并按需修改，再用[更新数据库记忆](https://docs.memorylake.cn/features/memorylake/api-reference/database/database-memories/update-database-memory.md)把它保存为记忆的 `instruction`。在此之前，记忆当前的指引保持不变。

生成需要调用模型，可能耗时较长。把 `stream` 设为 `true` 可以在等待期间保持连接：响应会变成服务器发送事件（SSE）流，在结果就绪前每 10 秒发送一次心跳注释。

> [!NOTE]
>
> **所需权限：** 项目上的 `project:dbset_modify`

### 路径参数

**workspaceId** `string` *required*

项目所属的工作空间 ID

**projectId** `string` *required*

项目 ID

**databaseId** `string` *required*

数据库记忆 ID

### 请求体

请求体可以不传。

**stream** `boolean` (default: false)

设为 `true` 时以服务器发送事件的形式接收结果，而不是一次性的 JSON 响应

```bash Single response theme={null}
curl -X POST 'https://app.memorylake.cn/openapi/memorylake/api/v3/workspaces/ws-8001856949ad4f6a5b6226f415ffa877/projects/proj-b93e8d95a8705bcc4ec42a65ba4cc7f8/memories/databases/doc-2e6a9c4f8b1d4a7e9c3b5d8f0a2c4e6b/instruction/generate' \
  -H 'Authorization: Bearer sk_xxxxxx'
```

```bash Streamed theme={null}
curl -N -X POST 'https://app.memorylake.cn/openapi/memorylake/api/v3/workspaces/ws-8001856949ad4f6a5b6226f415ffa877/projects/proj-b93e8d95a8705bcc4ec42a65ba4cc7f8/memories/databases/doc-2e6a9c4f8b1d4a7e9c3b5d8f0a2c4e6b/instruction/generate' \
  -H 'Authorization: Bearer sk_xxxxxx' \
  -H 'Content-Type: application/json' \
  -H 'Accept: text/event-stream' \
  -d '{
    "stream": true
  }'
```

### 响应

不传 `stream` 时，响应是一个 JSON 对象：

**data** `object`

**生成的指引**

**instruction** `string`

生成的草稿。在您用更新数据库记忆提交之前，它不会保存到记忆上。

`stream` 为 `true` 时，响应的内容类型为 `text/event-stream`，包含以下事件：

| 事件 | 数据 | 含义 |
| - | - | - |
| `data` | `{"instruction": "..."}` | 生成的草稿，只发送一次，包含完整文本。不会保存到记忆上。 |
| `end` | 空 | 流已成功结束。 |
| `error` | `{"error_code": "..."}` | 生成失败。之后不会再有 `end` 事件。 |

以 `:` 开头的行是心跳注释，忽略即可。流一旦开始，HTTP 状态码始终是 200，因此请检查是否收到 `error` 事件。流开始之前发现的问题（例如记忆不存在或缺少权限）会以普通的 JSON 错误响应返回。

```json Success (200) theme={null}
{
  "success": true,
  "data": {
    "instruction": "This database holds the sales schema. Use sales.orders for order and revenue questions: total_amount is the order value and created_at is when the order was placed. Join sales.customers on customer_id for customer details."
  }
}
```

```text Streamed (200) theme={null}
:keepalive

:keepalive

event:data
data:{"instruction":"This database holds the sales schema. Use sales.orders for order and revenue questions: total_amount is the order value and created_at is when the order was placed. Join sales.customers on customer_id for customer details."}

event:end
data:
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.