> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/backstory-api.md).

# 背景故事 API

使用流式 API 端点根据简短描述和名称生成角色背景故事，仅适用于付费计划。

{% hint style="danger" %}
此 API 仅适用于 Professional 套餐及更高套餐。
{% endhint %}

## 为角色生成背景故事

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/generate-backstory`

“生成背景故事”API 以服务器发送事件（SSE）的形式实现。提供角色的初始描述和姓名后，此 API 可为该角色生成一段描述。

#### 请求头

| 名称                                              | 类型  | 说明                                         |
| ----------------------------------------------- | --- | ------------------------------------------ |
| CONVAI-API-KEY<mark style="color:红色;">\*</mark> | 字符串 | 为每位用户提供的唯一 API 密钥。登录 Convai 账户后，可在密钥图标下找到。 |

#### 请求正文

| 名称        | 类型  | 说明          |
| --------- | --- | ----------- |
| inputText | 字符串 | 对角色的概括性描述。  |
| charName  | 字符串 | 要生成描述的角色姓名。 |

{% tabs %}
{% tab title="200：成功。返回多个 SSE 事件。" %}

```json
# 输出格式：
# 响应将以服务器发送事件（SSE）的形式流式传输。
# 每个事件都包含一个 'data' 字段，其中是背景故事的一部分。
# 单个事件示例：

data: You step off the train, greeted by the bustling city sounds.

# 将接收到多个事件，每个事件都包含背景故事的一部分。
# 将所有事件中的 'data' 内容拼接起来，即可组成完整的背景故事。
```

{% endtab %}

{% tab title="401 API 密钥验证失败" %}

```json
{
    "ERROR": "提供的 API 密钥无效。"
}
```

{% endtab %}
{% endtabs %}

以下是一些示例代码，用于演示此端点的请求格式 -->

{% tabs %}
{% tab title="Python" %}
{% code overflow="wrap" %}

```python
import json
from sseclient import SSEClient
import requests

url = "https://api.convai.com/character/generate-backstory"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
    'Accept': 'text/event-stream'
}

form_data = { 
    "inputText": "Lawyer in the New York City. Achiever. Stubborn.",
    "charName": "Mindy"
}

# 创建会话以管理 Cookie 并保持连接
session = requests.Session()

# 发送包含表单数据的 POST 请求，并以流的形式接收响应
response = session.post(url, headers=headers, data=form_data, stream=True)

# 根据响应创建 SSE 客户端
client = SSEClient(response)

# 处理事件
full_response = ""
for event in client.events():
    if event.data:
        full_response += event.data

print(full_response)
```

{% endcode %}
{% endtab %}

{% tab title="cURL" %}
{% code overflow="wrap" %}

```shell
curl -X POST 'https://api.convai.com/character/generate-backstory' \
-H 'CONVAI-API-KEY: <Your-API-Key>' \
-H 'Accept: text/event-stream' \
-d 'inputText=Lawyer in the New York City. Achiever. Stubborn.' \
-d 'charName=Mindy'
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/backstory-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
