OpenAI 双语文档参考 Chat completions Beta

本文介绍了如何使用OpenAI的ChatAPI,通过gpt-3.5-turbo和gpt-4来构建应用程序,执行如起草文档、编写代码、问答、创建会话代理等任务。API调用涉及消息参数,包括系统、用户和助手的角色,以及如何管理令牌和对话历史。此外,文章还提供了关于如何指导聊天模型和调整输出的建议。

Chat completions Beta

Using the OpenAI Chat API, you can build your own applications with gpt-3.5-turbo and gpt-4 to do things like:
使用 OpenAI Chat API,您可以使用 gpt-3.5-turbogpt-4 构建自己的应用程序来执行以下操作:

  • Draft an email or other piece of writing
    起草电子邮件或其他书面文件
  • Write Python code 编写 Python 代码
  • Answer questions about a set of documents
    回答有关一组文件的问题
  • Create conversational agents 创建会话代理
  • Give your software a natural language interface
    为您的软件提供自然语言界面
  • Tutor in a range of subjects
    一系列科目的导师
  • Translate languages 翻译语言
  • Simulate characters for video games and much more
    模拟视频游戏中的角色等等

This guide explains how to make an API call for chat-based language models and shares tips for getting good results. You can also experiment with the new chat format in the OpenAI Playground.
本指南解释了如何为基于聊天的语言模型进行 API 调用,并分享获得良好结果的技巧。您还可以在 OpenAI Playground 中试验新的聊天格式。

Introduction

Chat models take a series of messages as input, and return a model-generated message as output.
聊天模型将一系列消息作为输入,并返回模型生成的消息作为输出。

Although the chat format is designed to make multi-turn conversations easy, it’s just as useful for single-turn tasks without any conversations (such as those previously served by instruction following models like text-davinci-003).
尽管聊天格式旨在使多轮对话变得简单,但它对于没有任何对话的单轮任务同样有用(例如以前由指令遵循模型提供服务的任务,如 text-davinci-003 )。

An example API call looks as follows:
示例 API 调用如下所示:

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
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?"<
使用 OpenAIChat Completions API 可以实现与语言模型的交互,生成符合上下文逻辑的对话内容。该接口支持多轮对话,允许开发者通过传递历史消息(messages)来保持对话状态,从而提供更加自然、连贯的回答。 要调用 Chat Completions API,首先需要安装 `openai` Python 库,并配置 API 密钥。可以通过以下方式初始化客户端: ```python from openai import OpenAI client = OpenAI( api_key="sk-YourKey", base_url="https://api.chatanywhere.tech/v1" ) ``` 接着,调用 `chat.completions.create` 方法进行文本生成: ```python response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "user", "content": "请举几个罗马帝国的趣事"} ] ) ``` 在返回的响应中,可以通过 `response.choices[0].message.content` 获取模型生成的内容[^2]。 此外,Chat Completions API 支持多轮对话,只需将之前的对话记录作为 `messages` 参数传入即可。例如: ```python messages = [ {"role": "user", "content": "你好,请介绍一下你自己"}, {"role": "assistant", "content": "我是由 OpenAI 训练的语言模型 GPT-4,我可以帮助你回答问题、创作文字等"}, {"role": "user", "content": "那你能告诉我一些关于机器学习的基础知识吗?"} ] response = client.chat.completions.create( model="gpt-4o", messages=messages ) ``` 这种方式能够使模型理解上下文并生成更贴切的回应[^1]。 需要注意的是,OpenAI 还推出了新的 Responses API,它采用事件驱动架构,更适合处理复杂的多步骤对话逻辑和推理任务。Responses API 与 Chat Completions API 不同之处在于其输出结构和状态管理机制[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值