OpenAI-ChatGPT最新官方接口《聊天交互多轮对话》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(二)(附源码)
淘宝搜:【天降红包222】领超级红包,京东搜:【天降红包222】
淘宝互助,淘宝双11微信互助群关注公众号 【淘姐妹】
- Chat completions Beta 聊天交互
- 前言
- Introduction 导言
- Response format 提示格式
- Managing tokens
- Counting tokens for chat API calls 为聊天API调用标记计数
- Instructing chat models 指导聊天模型
- Chat 【【微信】】 聊天与完成
- FAQ 问与答
- 其它资料下载
Using the OpenAI Chat API, you can build your own applications with and to do things like: 使用OpenAI Chat API,您可以使用 和 构建自己的应用程序,以执行以下操作:
- Draft an email or other piece of writing 起草一封电子邮件或其他书面材料
- Write Python code 编写Python代码
- Answer 【【微信】】 documents 回答有关一组文档的问题
- Create con【【微信】】 创建会话代理
- Gi【【微信】】al language interface 给予你的软件一个自然语言界面
- Tutor in a range of subjects 一系列科目的导师
- Translate languages 翻译语言
- Simulate characters for 【【微信】】e 模拟视频游戏的角色等等
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中尝试新的聊天格式。
ChatGPT的聊天交互是与用户进行交流并为他们提供创新体验的最强大工具。一句话可以概况:“将聊天交互发挥到极致,定制专属用户体验,以此解决用户痛点,是最佳创新之道。”它可以帮助用户快速定位问题并获得正确的答案,提升用户体验的同时,也能提高工作效率、减少耗时。此外,ChatGPT还可以根据用户特征和需求,提供个性化的服务,让使用者在交互中感受到独一无二的体验。
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 con【【微信】】, it’s just as useful for single-turn tasks without any con【【微信】】uch as those previously served by instruction following models like ). 虽然聊天格式旨在使多轮对话变得容易,但它对于没有任何对话的单轮任务同样有用(例如以前由指令跟随模型(如 )提供的任务)。
An example API call looks as follows: 示例API调用如下所示:
The main input is the messages parameter. Messages must be an array of message objects, where each object has a role (either “system”, “user”, or “assistant”) and content (the content of the message). Con【【微信】】 can be as short as 1 message or fill many pages. 主输入是messages参数。消息必须是消息对象的数组,其中每个对象都有一个角色(“系统”、“用户”或“助理”)和内容(消息的内容)。对话可以短至1条消息或填写许多页面。
Typically, a con【【微信】】 formatted with a system message first, followed by alternating user and assistant messages. 通常,首先使用系统消息格式化对话,然后交替使用用户和助理消息。
The system message helps set the beha【【微信】】. In the example above, the assistant was instructed with “You are a helpful assistant.” 系统消息有助于设置助手的行为。在上面的示例中,助理被指示“你是一个有用的助理。”
gpt-3.5-turbo-0301 does not always pay strong attention to system messages.Future models will be trained to pay stronger attention to system messages. GPT-3.5-turbo-0301并不总是特别关注系统消息。未来的模型将被训练为更加关注系统消息。
The user messages help instruct the assistant. They can be generated by 【【微信】】s of an application, or set by a de【【微信】】on. 用户消息帮助指示助理。它们可以由应用程序的最终用户生成,也可以由开发人员设置为指令。
The assistant messages help store prior responses. They can also be written by a de【【微信】】mples of desired behavior. 助理消息帮助存储先前的响应。它们也可以由开发人员编写,以帮助给予所需行为的示例。
Including the con【【微信】】tory helps when user instructions refer to prior messages. In the example above, 【【微信】】’s final question of “Where was it played?” only makes sense in the context of the prior messages about the World Series of 2020. Because the models ha【【微信】】ests, all rele【【微信】】e supplied via the conversation. If a con【【微信】】n the model’s token limit, it will need to be shortened in some way. 包括对话历史有助于用户指示引用先前的消息。在上面的示例中,用户的最终问题“在哪里播放的?“只有在之前关于2020年世界大赛的信息中才有意义。因为模型没有过去请求的记忆,所有相关信息必须通过会话提供。如果一个对话不能满足模型的标记限制,它将需要以某种方式缩短。
An example API response looks as follows: API响应示例如下所示:
In Python, the assistant’s reply can be extracted with . 在Python中,助手的回复可以用 提取。
E【【微信】】ude a . The possible 【【微信】】: 每个响应都将包含一个 。 的可能值为:
- : API returned complete model output :API返回完整模型输出
- : Incomplete model output due to max_tokens parameter or token limit :由于 max_tokens 参数或标记限制,模型输出不完整
- : Omitted content due to a flag from our content filters :由于我们的内容过滤器中的标记而忽略了内容
- : API response still in progress or incomplete :API响应仍在进行中或未完成
Language models read text in chunks called tokens. In English, a token can be as short as one character or as long as one word (e.g., or), and in some languages tokens can be e【【微信】】cter or even longer than one word. 语言模型以称为标记的块读取文本。在英语中,标记可以短至一个字符或长至一个单词(例如, 或 ),并且在一些语言中,标记甚至可以比一个字符更短或者甚至比一个单词更长。
For example, the string is encoded into six tokens: . 例如,字符串 被编码为六个标记: 。
The total number of tokens in an API call affects: API调用中的标记总数影响:
- How much your API call costs, as you pay per token 您的API调用成本是多少,按每个标记标记支付
- How long your API call takes, as writing more tokens takes more time 您的API调用需要多长时间,因为编写更多标记需要更多时间
- Whether your API call works at all, as total tokens must be below the model’s maximum limit (4096 tokens for ) 您的API调用是否有效,会受到标记总数必须低于模型的最大限制( 为4096个标记)
Both input and output tokens count toward these 【【微信】】. For example, if your API call used 10 tokens in the message input and you recei【【微信】】age output, you would be billed for 30 tokens. 输入和输出标记都计入这些数量。例如,如果您的API调用在消息输入中使用了10个标记,而您在消息输出中收到了20个标记,则您将收取30个标记的费用。
To see how many tokens are used by an API call, check the field in the API response (e.g., ). 要查看API调用使用了多少标记,请检查API响应中的 字段(例如,)。
Chat models like and use tokens in the same way as other models, but because of their message-based formatting, it’s more difficult to count how many tokens will be used by a conversation. 像 和 这样的聊天模型使用标记的方式与其他模型相同,但由于它们基于消息的格式,因此更难以计算会话将使用多少标记。
Below is an example function for counting tokens for messages passed to gpt-3.5-turbo-0301. 下面是一个示例函数,用于对传递到gpt-3.5-turbo-0301的消息的标记进行计数。
The exact way that messages are con【【微信】】hange from model to model. So when future model 【【微信】】, the answers returned by this function may be only approximate. The ChatML documentation explains how messages are con【【微信】】e OpenAI API, and may be useful for writing your own function. 将消息转换为标记的确切方式可能因模型而异。因此,当未来的模型版本发布时,此函数返回的答案可能只是近似值。ChatML文档解释了OpenAI API如何将消息转换为标记,并且可能对编写您自己的函数很有用。
Next, create a message and pass it to the function defined abo【【微信】】his should match the value returned by the API usage parameter: 接下来,创建一条消息并将其传递给上面定义的函数以查看标记计数,这应该与API使用参数返回的值匹配:
To confirm the number generated by our function abo【【微信】】 API returns, create a new Chat Completion: 要确认我们上面的函数生成的数字与API返回的数字相同,请创建一个新的聊天完成:
To see how many tokens are in a text string without making an API call, use OpenAI’s tiktoken Python library. Example code can be found in the OpenAI Cookbook’s guide on how to count tokens with tiktoken. 要在不进行API调用的情况下查看文本字符串中有多少标记,请使用OpenAI的tiktoken Python库。示例代码可以在OpenAI Cookbook关于如何使用tiktoken进行标记计数的指南中找到。
Each message passed to the API consumes the number of tokens in the content, role, and other fields, plus a few extra for behind-the-scenes formatting. This may change slightly in the future. 传递给API的每条消息都会消耗内容、角色和其他字段中的标记数量,外加一些用于幕后格式化的额外标记。这在未来可能会略有变化。
If a con【【微信】】 too many tokens to fit within a model’s maximum limit (e.g., more than 4096 tokens for ), you will ha【【微信】】, omit, or otherwise shrink your text until it fits. Beware that if a message is remo【【微信】】put, the model will lose all knowledge of it. 如果对话具有太多标记而不能适应模型的最大限制(例如,超过4096个标记(对于 ),您将不得不截断、省略或以其他方式缩小文本,直到它适合为止。请注意,如果从消息输入中删除了一条消息,则模型将丢失所有关于它的知识。
Note too that very long con【【微信】】 are more likely to recei【【微信】】. For example, a con【【微信】】okens long will have its reply cut off after just 6 tokens. 也要注意,很长的对话更有可能收到不完整的回复。例如,长度为4090个标记的 会话将在仅6个标记之后切断其回复。
Best practices for instructing models may change from model 【【微信】】. The advice that follows applies to and may not apply to future models. 指导模型的最佳实践可能因模型版本而异。以下建议适用于,可能不适用于未来的模型。
Many con【【微信】】 begin with a system message to gently instruct the assistant. For example, here is one of the system messages used for ChatGPT: 许多对话以系统消息开始,以温和地指示助理。例如,以下是用于ChatGPT的系统消息之一:
You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. Knowledge cutoff: {knowledge_cutoff} Current date: {current_date} 你是ChatGPT,一个由OpenAI训练的大型语言模型。尽可能简明扼要地回答。知识截止:{knowledge_cutoff} 当前日期: {current_date}
In general, does not pay strong attention to the system message, and therefore important instructions are often better placed in a user message. 一般来说, 不太关注系统消息,因此重要的指令通常最好放在用户消息中。
If the model isn’t generating the output you want, feel free to iterate and experiment with potential impro【【微信】】. You can try approaches like: 如果模型没有生成您想要的输出,请随意迭代并尝试潜在的改进。您可以尝试以下方法:
- Make your instruction more explicit 让你的指示更明确
- Specify the format you want the answer in 指定您希望答案采用的格式
- Ask the model to think step by step or debate pros and cons before settling on an answer 让模型一步一步地思考,或者在确定答案之前讨论利弊
For more prompt engineering ideas, read the OpenAI Cookbook guide on techni【【微信】】ty. 有关更多快速工程想法,请阅读OpenAI Cookbook关于提高可靠性的技术指南。
Beyond the system message, the and are two of many options de【【微信】】 the output of the chat models. For , higher 【【微信】】.8 will make the output more random, while lower 【【微信】】.2 will make it more focused and deterministic. In the case of , if you want to limit a response to a certain length, max tokens can be set to an arbitrary number. This may cause issues for example if you set the max tokens 【【微信】】ut will be cut-off and the result will not make sense to users. 除了系统消息之外, 和是开发人员必须影响聊天模型输出的许多选项中的两个。对于,较高的值(如0.8)将使输出更加随机,而较低的值(如0.2)将使其更加集中和确定。在的情况下,如果您希望将响应限制为特定长度,则可以将设置为任意数字。这可能会导致问题,例如,如果您将最大标记值设置为5,因为输出将被切断,结果对用户没有意义。
Because performs at a similar capability to text-da【【微信】】% the price per token, we recommend for most use cases. 由于 的性能与 相似,但每个标记的价格为10%,因此我们建议在大多数用例中使用 。
For many de【【微信】】, the transition is as simple as rewriting and retesting a prompt. 对于许多开发人员来说,转换就像重写和重新测试提示一样简单。
For example, if you translated E
OpenAI-ChatGPT最新官方接口《AI绘图》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(二)(附源码)
openaichatGPT下载,openaichatGAT- Image generation Beta 图片生成
- 前言
- Introduction
- Usage
- Generations
- Edits 编辑
- Variations
- Language-specific tips 特定语言提示
- Python 语言
- Using in-memory image data 使用内存中的图像数据
- Operating on image data 操作图像数据
- Error handling
- Node.js 语言
- Using in-memory image data 使用内存中的图像数据
- Working with TypeScript 使用TypeScript
- Error handling
- 其它资料下载
Learn how to generate or manipulate images with our DALL・E models 了解如何使用我们的DALL・E模型生成或操作图像
ChatGPT不仅仅是一个强大的自然语言处理模型,其DALL・E模型还可以帮助用户快速生成多种多样的图像。用户可以根据文字快速生成图片,即可自动生成出特定的图片,大大提高了工作效率。对于多媒体行业从业者来说,DALL・E模型强大的图像处理能力,可以帮助用户节省大量时间,让用户更加专注地精心制作自己的作品,从而提高工作效率。
The Images API pro【【微信】】 interacting with images: 图像API提供了三种与图像交互的方法:
- Creating images from scratch based on a text prompt 基于文本提示从头开始创建图像
- Creating edits of an existing image based on a new text prompt 基于新文本提示创建现有图像的编辑
- Creating 【【微信】】ng image 创建现有图像的变体
This guide co【【微信】】 these three API endpoints with useful code samples. To see them in action, check out our DALL・E pre【【微信】】. 本指南涵盖了使用这三个API端点的基础知识和有用的代码示例。要查看它们的实际操作,请查看我们的DALL・E预览应用程序。
The Images API is in beta. During this time the API and models will e【【微信】】back. To ensure all users can prototype comfortably, the default rate limit is 50 images per minute. You can learn more about rate limits in our rate limit guide. 图像API处于beta阶段。在此期间,API和模型将根据您的反馈进行改进。为了确保所有用户都能轻松地进行原型制作,默认速率限制为每分钟50张图像。您可以在我们的费率限制指南中了解有关费率限制的更多信息。
The image generations endpoint allows you to create an original image gi【【微信】】. Generated images can have a size of 256x256, 512x512, or 1024x1024 pixels. Smaller sizes are faster to generate. You can re【【微信】】me using the n parameter. 图像生成端点允许您在给出文本提示的情况下创建原始图像。生成的图像可以具有256 x256、512 x512或1024 x1024像素的大小。较小的尺寸生成速度更快。您可以使用n参数一次请求1-10个图像。
python代码如下:
node.js代码如下:
curl 代码如下:
The more detailed the description, the more likely you are to get the result that you or your end user want. You can explore the examples in the DALL・E pre【【微信】】 for more prompting inspiration. Here’s a 【【微信】】: 描述越详细,就越有可能获得您或最终用户想要的结果。您可以在DALL・E预览应用程序中探索示例,以获得更多提示灵感。这里有一个简单的例子:
PROMPT 提示
a white siamese cat 白色暹罗猫
【【淘密令】】 产生
PROMPT 提示
a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears 一个近距离,工作室摄影肖像的白色暹罗猫,看起来好奇,背光的耳朵
【【淘密令】】 产生 Each image can be returned as either a URL or Base64 data, using the response_format parameter. URLs will expire after an hour. 使用response_format参数,每个图像都可以作为URL或Base64数据返回。URL将在一小时后过期。
The image edits endpoint allows you to edit and extend an image by uploading a mask. The transparent areas of the mask indicate where the image should be edited, and the prompt should describe the full new image, not just the erased area. This endpoint can enable experiences like the editor in our DALL・E pre【【微信】】. 图像编辑端点允许您通过上传遮罩来编辑和扩展图像。蒙版的透明区域指示应该编辑图像的位置,提示应该描述完整的新图像,而不仅仅是擦除的区域。此端点可以实现类似于DALL・E预览应用中的编辑器的体验。
python代码如下:
node.js代码如下:
curl 代码如下:
The uploaded image and mask must both be s【【微信】】an 4MB in size, and also must ha【【微信】】ch other. The non-transparent areas of the mask are not used when generating the output, so they don’t necessarily need to match the original image like the example above. 上传的图像和蒙版必须都是小于4MB的方形PNG图像,并且彼此的尺寸必须相同。生成输出时不使用蒙版的非透明区域,因此它们不一定需要像上面的示例那样匹配原始图像。
The image 【【微信】】ows you to generate a variation of a gi【【微信】】. 图像变体端点允许您生成给定图像的变体。
python代码如下:
node.js代码如下:
curl 代码如下:
The Python examples in the guide abo【【微信】】ad image data from disk. In some cases, you may ha【【微信】】ory instead. Here’s an example API call that uses image data stored in a object: 上面的Python示例使用 函数从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个使用存储在 对象中的图像数据的示例API调用:
It may be useful to perform operations on images before passing them to the API. Here’s an example that uses to resize an image: 在将图像传递给API之前对图像执行操作可能是有用的。下面是一个使用 调整图像大小的示例:
API re【【微信】】 return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a statement, and the error details can be found in : API请求可能由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 语句处理,错误细节可以在 中找到:
The Node.js examples in the guide abo【【微信】】 image data from disk. In some cases, you may ha【【微信】】ory instead. Here’s an example API call that uses image data stored in a Node.js object: 上述指南中的Node.js示例使用 模块从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个使用存储在Node.js 对象中的图像数据的示例API调用:
If you’re using TypeScript, you may encounter some 【【微信】】rguments. Here’s an example of working around the type mismatch by explicitly casting the argument: 如果你使用TypeScript,你可能会遇到一些图像文件参数的怪异。下面是一个通过显式转换参数来解决类型不匹配的示例:
And here’s a similar example for in-memory image data: 下面是内存中图像数据的类似示例:
API re【【微信】】 return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a statement, and the error details can be found in either or : API请求可能由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 语句处理,错误详细信息可以在 或 中找到:
如果大家想继续了解人工智能相关学习路线和知识体系,欢迎大家翻阅我的另外一篇博客《重磅 | 完备的人工智能AI 学习――基础知识学习路线,所有资料免关注免套路直接网盘下载》 这篇博客参考了Github知名开源平台,AI技术平台以及相关领域专家:Datawhale,ApacheCN,AI有道和黄海广博士等约有近100G相关资料,希望能帮助到所有小伙伴们。