> 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 仅适用于专业版及以上套餐。
{% endhint %}

## 为角色生成背景故事

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

生成背景故事 API 以服务器发送事件（SSE）的形式实现。给定关于角色及其姓名的一些初始描述，该 API 可以生成该角色的描述。

#### 标头

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

#### 请求体

| 名称        | 类型  | 说明             |
| --------- | --- | -------------- |
| inputText | 字符串 | 角色的概述性描述。      |
| charName  | 字符串 | 正在为其生成描述的角色名称。 |

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

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

data: 你走下火车，迎面而来的是繁忙的城市喧嚣。

# 将会接收到多个事件，每个事件包含背景故事的一部分。
# 通过拼接所有事件中的 '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": "纽约市的一名律师。成功人士。固执。",
    "charName": "Mindy"
}

# 创建一个用于管理 cookies 和保持连接的会话
session = requests.Session()

# 使用 form-data 发送 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=纽约市的一名律师。成功人士。固执。' \
-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.
