ChatGPT OpenAPI 接入

厍和颂
2023-12-01

请注意,在实际使用中,需要使用 API 密钥,并根据 API 文档中的信息调用正确的端点和参数。

基础信息

  • Base URL: https://api.chatgpt.com/v1/
  • 认证方式: API Key

认证信息

您需要在 API 请求中包含您的 API 密钥以进行身份验证。

请求头(Headers):

  • Authorization: Bearer YOUR_API_KEY

端点列表

客户服务端点

获取对话记录

  • PATH: /conversations/:conversationId/history
  • METHOD: GET
  • 参数:
    • conversationId: 必选。要获取历史记录的对话 ID。
  • 例:
curl --request GET \
  --url https://api.chatgpt.com/v1/conversations/123456/history \
  --header 'Authorization: Bearer YOUR_API_KEY'

发送消息

  • PATH: /conversations/:conversationId/send
  • METHOD: POST
  • 参数:
    • conversationId: 必选。要发送消息的对话 ID。
    • message: 必选。发送的消息。
  • 例:
curl --request POST \
  --url https://api.chatgpt.com/v1/conversations/123456/send \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"message": "hello"}'

ChatGPT 语言模型端点

生成文本

  • PATH: /models/chatgpt/generate
  • METHOD: POST
  • 参数:
    • prompt: 必选。作为生成文本的提示。
    • max_length: 可选。生成文本的最大长度。
    • temperature: 可选。生成文本的温度。
  • 例:
curl --request POST \
  --url https://api.chatgpt.com/v1/models/chatgpt/generate \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"prompt": "Hello", "max_length": 50, "temperature": 0.5}'

错误信息

如果API 请求发生错误,ChatGPT OpenAPI 返回以下格式的JSON响应:

{
  "error": {
    "code": integer,
    "message": string
  }
}

其中,code 是 HTTP 状态码,message 含有有关该错误的更多详细信息。

 类似资料: