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的头像luotuoemo
上一篇 2023年8月24日 下午1:56
下一篇 2023年8月24日 下午3:10

相关推荐

  • chatgpt 如何改变农业工程技术

    改变农业工程技术可以通过以下几个方面的努力: 采用现代化技术:引入现代化的农业工程技术,如无人机、机器人、传感器等,用于农田管理、病虫害监测、灌溉和施肥等,提高农业生产效率和质量。 推广节能环保技术:应用节能环保的农业工程技术,如太阳能和风能设备,减少对传统能源的依赖,降低温室气体排放和环境污染。 提升智能化水平:发展人工智能技术在农业工程中的应用,如智能控…

    2023年7月7日
    10500
  • 用chatgpt写论文怎么减少重复率

    要减少ChatGPT生成的论文中的重复率,可以采取以下几种方法: 增加多样性:ChatGPT的生成结果往往倾向于生成类似的短语和句子。为了增加多样性,可以使用不同的随机种子或调整模型的温度参数。较高的温度参数会使得生成的结果更加随机多样,但也可能导致语义不准确或不连贯的句子。 添加惩罚机制:可以使用惩罚机制来约束ChatGPT生成的结果,使其避免生成重复的短…

    2023年9月30日
    10400
  • chatgpt技术原理与应用

    chatgpt可以用来做什么? ChatGPT是聊天机器人,该聊天机器人可以在模仿人类说话风格的同时回答大量的问题。在现实世界之中,例如数字营销、线上内容创作、回答客户服务查询,甚至可以用来帮助调试代码。ChatGPT 由 GPT-3.5 系列模型提供支持,这些模型使用 Azure AI 超级计算基础设施上的文本和代码数据进行训练。人工智能的能力已经得到提升…

    2023年9月3日
    10000
  • 怎么用chatgpt插件联网

    您可以使用以下步骤将ChatGPT插件连接到网络: 确保您已安装OpenAI Python库。您可以使用以下命令进行安装: pip install openai 导入所需的库: import openai import json 设置OpenAI API密钥。您需要在OpenAI网站上创建一个帐户并生成一个API密钥。 openai.api_key = &#…

    2023年9月25日
    17200
  • openapi

    OpenAPI is a specification for building APIs (Application Programming Interfaces) using a standardized format to describe the endpoints, operations, parameters, and responses of th…

    2023年12月1日
    8700

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:582059487@qq.com

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

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