chatgpt接口文档

ChatGPT是一个能够进行对话的自然语言生成模型。以下是ChatGPT接口的文档:

请求

  • model: (必需)模型的名称或模型的id。例如:”gpt-3.5-turbo”。
  • messages: (必需)一个包含对话消息的数组。每个消息对象都有两个属性:rolecontent

    • role可以是"system""user""assistant"之一。其中,"system"表示系统消息,"user"表示用户消息,"assistant"表示助手消息。
    • content是消息的内容。例如,"role": "user", "content": "tell me a joke"

以下是一个请求示例:

{
  "model": "gpt-3.5-turbo",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}
  ]
}

响应

成功的响应返回一个包含助手回复的对象:

{
  "id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve",
  "object": "chat.completion",
  "created": 1677649420,
  "model": "gpt-3.5-turbo",
  "usage": {"prompt_tokens": 56, "completion_tokens": 31, "total_tokens": 87},
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers."
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}

助手回复可以通过response['choices'][0]['message']['content']来访问。

如果发生错误,响应将包含错误信息而不是choices字段。

Python代码示例

这是一个使用OpenAI Python库发送请求的代码示例:

import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

这将返回包含助手回复的响应对象。您可以使用response['choices'][0]['message']['content']来访问助手的回复。

ChatGPT是OpenAI开发的一款基于GPT的自然语言生成模型,可以用于进行对话生成。ChatGPT接口提供了一个简单的方式来与ChatGPT模型进行交互。

模型介绍

ChatGPT模型是一个基于GPT的生成式对话模型。它通过读取一个上下文字符串,并生成一个回答字符串。通过连续的交互,可以创建一个对话流。

接口地址

https://api.openai.com/v1/chat/completions

请求方式

POST

请求参数

必选参数

  • model: 模型的名称,固定为”chatgpt”。
  • messages: 一个包含对话历史的数组。每个对话历史包含两个属性,role表示说话者的角色(”system”、”user”或”assistant”),content表示说话者的内容。

可选参数

  • temperature: 温度参数,用于控制输出的随机性。默认值为0.6。
  • max_tokens: 输出的最大令牌数。较小的值会导致更短的回答。默认值为50。

响应参数

  • id: 请求的唯一标识符。
  • object: 固定为”chat.completion”。
  • created: 请求的创建时间。
  • model: 使用的模型名称。
  • usage: 请求的模型使用信息。
  • choices: 一个数组,包含生成的回答。

示例

请求

import openai

openai.ChatCompletion.create(
  model="chatgpt",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

响应

{
  "id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve",
  "object": "chat.completion",
  "created": 1677649420,
  "model": "chatgpt",
  "usage": {
    "prompt_tokens": 56,
    "completion_tokens": 31,
    "total_tokens": 87
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers."
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}

注意事项

  • 每个对话历史的role属性必须是”system”、”user”或”assistant”之一。
  • 对于每个请求,最多只能包含20个对话历史。
  • 请求的对话历史的顺序非常重要,因为模型是根据历史生成回答的。
  • 模型的响应时间通常在几秒钟到几十秒之间。
  • 请求的messages参数为空数组时,模型将产生一个基于系统提示的回答。

chatgpt接口文档 发布者:luotuoemo,转转请注明出处:https://www.chatairc.com/15429/

(0)
luotuoemo's avatarluotuoemo
上一篇 2023年8月24日 下午1:56
下一篇 2023年8月24日 下午3:10

相关推荐

  • chatgpt制作个人网页代码

    以下是一个简单的HTML代码示例,用于创建个人网页: <!DOCTYPE html> <html> <head> <title>个人网页</title> </head> <body> <header> <h1>欢迎来到我的个人网页</h1>…

    2023年11月7日
    72800
  • chatgpt注册教程知乎

    以下是在知乎上找到的ChatGPT注册教程: 首先,打开知乎的官方网站(https://www.zhihu.com/)并登录您的帐户。 在页面的右上角,您会看到一个“注册”按钮。点击它以开始注册过程。 接下来,您将被要求填写一些个人信息,例如您的手机号码、电子邮件地址和密码。请确保提供准确的信息,并选择一个强密码。 在填写完所有必填信息后,阅读并同意知乎的用…

    2023年9月9日
    85900
  • chatgpt优点和缺点英语

    GPT-3 (Generative Pretrained Transformer 3) is a language generation model developed by OpenAI. Here are some of its advantages and disadvantages: Advantages of GPT-3: Language gen…

    2023年11月3日
    67900
  • gpt网页

    GPT (“Generative Pre-trained Transformer”) is a type of natural language processing model that uses deep learning techniques to generate human-like text based on a give…

    2023年11月16日
    71500
  • chatgpt 信息整合功能

    ChatGPT 的信息整合功能是指能够将对话中的多个回答或信息整合到一起,形成一个准确和全面的回答或信息。这个功能对于处理多轮对话中的复杂问题非常有用。 ChatGPT 可以通过注意力机制和上下文理解来整合信息。当用户提出一个问题时,ChatGPT 可以通过分析对话历史记录和上下文来确定最相关的信息。它可以识别出之前提到的关键信息,并将其整合到回答中,从而提…

    2023年9月25日
    66900

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:582059487@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
国内Chat Ai版本直接使用:https://chat.chatairc.com/