ChatGPT 接口文档
ChatGPT 是一种基于人工智能技术的自动对话生成模型,可以用于实现自动化对话系统。本文将介绍 ChatGPT 的接口文档,包括 API 的功能、请求参数和返回结果等内容。
## 功能
ChatGPT 接口提供了以下功能:
1. 生成对话回复:输入用户的对话历史和一个新的用户消息,模型将生成一个合适的回复。
## 请求参数
ChatGPT 接口的请求参数包括:
1. `model`: 指定使用的模型,默认为 ChatGPT。
2. `messages`: 包含对话历史和用户消息的数组。每个消息对象包含 `role` 和 `content` 两个属性,`role` 可以是 "system", "user" 或 "assistant",`content` 是消息的内容。
3. `max_tokens`: 指定生成回复的最大字数,默认为 50。
示例请求:
```json
{
"model": "ChatGPT",
"messages": [
{"role": "system", "content": "初始化对话"},
{"role": "user", "content": "你好,有什么可以帮到您的?"}
],
"max_tokens": 20
}
```
## 返回结果
ChatGPT 接口的返回结果是一个包含模型生成回复的 JSON 对象。其中 `id` 属性为请求的唯一标识符,`object` 属性为 "chat.completion",`created` 属性为请求的时间戳,`model` 属性为使用的模型名称。
回复消息的结果包含在 `choices` 数组中,每个回复消息包含 `message` 对象,其中包含 `role` 和 `content` 两个属性,分别表示消息的角色和内容。
示例结果:
```json
{
"id": "cmpl-29tlgkn3e742f1iijdv7j-0",
"object": "chat.completion",
"created": 1677649420,
"model": "ChatGPT",
"choices": [
{"message": {"role": "assistant", "content": "您好,请问有什么可以帮到您的?"}}
]
}
```
## 示例代码
以下是使用 Python 进行 ChatGPT 接口调用的示例代码:
```python
import requests
url = "https://api.openai.com/v1/chat/completions"
data = {
"model": "ChatGPT",
"messages": [
{"role": "system", "content": "初始化对话"},
{"role": "user", "content": "你好,有什么可以帮到您的?"}
],
"max_tokens": 20
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer your_api_key"
}
response = requests.post(url, json=data, headers=headers)
result = response.json()
# 处理返回结果
reply = result["choices"][0]["message"]["content"]
print(reply)
```
以上就是 ChatGPT 接口文档的内容。通过调用 ChatGPT 接口,您可以方便地实现自动对话功能,为用户提供更好的服务体验。希望这份文档对您有所帮助!