> 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/mindview-api.md).

# Mindview API

获取用于构建响应的对话上下文，可以来自已存储的会话，也可以来自当前角色设置。

{% hint style="danger" %}
此 API 仅适用于专业版及以上计划。
{% endhint %}

## 获取 MindView 的提示词

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

通过以下两种方式之一获取 MindView 的提示数据：

1. **会话模式** (`session_id`）：从存储的交互记录中读取提示数据。
2. **角色模式** (`character_id` 仅）：根据当前角色配置生成静态提示数据。

#### 请求头

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

#### 请求体

| 名称            | 类型  | 说明                                    |
| ------------- | --- | ------------------------------------- |
| session\_id   | 字符串 | 会话标识符。如果提供，API 以会话模式运行。               |
| character\_id | 字符串 | 角色标识符。用于当 `session_id` 未提供时。          |
| 偏移量           | 整数  | 可选。仅与 `session_id`。默认 `-1` 返回最新的提示记录。 |

> 至少以下之一 `session_id` 或 `character_id` 是必需的。

{% tabs %}
{% tab title="200：成功（会话模式响应）" %}

```json
{
  "prompt_id": "f2f8a75e-...",
  "prompt": "[{\"role\":\"system\",\"content\":\"...\"}]",
  "model": "gpt-4o-mini",
  "temperature": 0.7,
  "max_tokens": 512,
  "top_p": 1.0,
  "frequency_penalty": 0.0,
  "presence_penalty": 0.0,
  "stop": null,
  "session_id": "sess_123",
  "mindview": "{\"static_prompt\": {...}, \"dynamic_prompt\": {...}}"
}
```

{% endtab %}

{% tab title="200：成功（角色模式响应）" %}

```json
{
  "mindview": "{\"static_prompt\": {...}, \"dynamic_prompt\": {...}}",
  "model": "gpt-4o-mini"
}
```

{% endtab %}

{% tab title="400：错误请求" %}

```json
{
  "ERROR": "必须提供 session_id 或 character_id",
  "Reference ID": "<transaction_id>"
}
```

```json
{
  "ERROR": "offset 必须是有效整数",
  "Reference ID": "<transaction_id>"
}
```

```json
{
  "ERROR": "偏移量大于记录数",
  "Reference ID": "<transaction_id>"
}
```

{% endtab %}
{% endtabs %}

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

#### 会话模式（`session_id`)

{% tabs %}
{% tab title="Python" %}

```python
import json
import requests

url = "https://api.convai.com/character/chatHistory/get_prompt"

headers = {
    "CONVAI-API-KEY": "<Your-API-Key>",
    "Content-Type": "application/json"
}

payload = {
    "session_id": "<Your-Session-ID>",
    "offset": -1
}

response = requests.post(url, headers=headers, data=json.dumps(payload))
print(response.text)
```

{% endtab %}

{% tab title="cURL" %}

```shell
curl -X POST "https://api.convai.com/character/chatHistory/get_prompt" \
  -H "CONVAI-API-KEY: <Your-API-Key>" \
  -H "Content-Type: application/json" \
  -d '{"session_id":"<Your-Session-ID>","offset":-1}'
```

{% endtab %}
{% endtabs %}

#### 角色模式（`character_id`)

{% tabs %}
{% tab title="Python" %}

```python
import json
import requests

url = "https://api.convai.com/character/chatHistory/get_prompt"

headers = {
    "CONVAI-API-KEY": "<Your-API-Key>",
    "Content-Type": "application/json"
}

payload = {
    "character_id": "<Your-Character-ID>"
}

response = requests.post(url, headers=headers, data=json.dumps(payload))
print(response.text)
```

{% endtab %}

{% tab title="cURL" %}

```shell
curl -X POST "https://api.convai.com/character/chatHistory/get_prompt" \
  -H "CONVAI-API-KEY: <Your-API-Key>" \
  -H "Content-Type: application/json" \
  -d '{"character_id":"<Your-Character-ID>"}'
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
实现说明：

* 如果同时提供了 `session_id` 和 `character_id` ，则使用会话模式。
* 在当前实现中， `mindview` 和 `prompt` 字段会被序列化为 JSON 字符串。
  {% endhint %}


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
