ChatGPT and GPT-4 are language models optimized for conversational interfaces.. These models behave differently than older GPT-3 models. Past models accepted text input and output, and what they did was accept a prompt string and return text that could be appended to the prompt when completed. However, both ChatGPT and GPT-4 models can receive conversational input and output messages. These models expect input to be formatted similar to the script formatting for a specific chat and return completion representing the message written by the model in the chat. Even though this format is designed for multi-turn conversations, you'll still find it useful in non-chat situations.
In Azure OpenAI, there are two different options for interacting with these types of models:
The Chat Completion API is a new dedicated API for interacting with ChatGPT and GPT-4 models. This API is the preferred method of accessing these models. This is also the only way to access the new GPT-4 model.
ChatML uses the same completion API you use for other models (such as text-davinci-002), which requires a unique token-based prompt format called Chat Markup Language (ChatML). This provides lower level access than the dedicated chat completion API, but also requires additional input validation, only supports the ChatGPT (gpt-35-turbo) model, and the underlying format is more likely to change over time Change.
This article will guide you to get started with the new ChatGPT and GPT-4 models. It is important to use the techniques described here to get the best results. Typically, if you try to interact with a model the way older model series did, the model becomes verbose and provides less useful responses.
The following code snippet shows the most basic way to use ChatGPT and GPT-4 models with the Chat Completion API. If this is your first time using these models programmatically, we recommend starting with our ChatGPT and GPT-4 quickstarts.
GPT-4 models are currently available by request only. Existing Azure OpenAI customers can request access by filling out this form.
Pythonimport osimport openaiopenai.api_type = "azure"openai.api_version = "2023-05-15" openai.api_base = os.getenv("OPENAI_API_BASE")# Your Azure OpenAI resource's endpoint value.openai.api_key = os.getenv("OPENAI_API_KEY")response = openai.ChatCompletion.create(engine="gpt-35-turbo", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.messages=[{"role": "system", "content": "Assistant is a large language model trained by OpenAI."},{"role": "user", "content": "Who were the founders of Microsoft?"}])print(response)print(response['choices'][0]['message']['content'])
{"choices": [{"finish_reason": "stop","index": 0,"message": {"content": "The founders of Microsoft are Bill Gates and Paul Allen. They co-founded the company in 1975.","role": "assistant"}}],"created": 1679014551,"id": "chatcmpl-6usfn2yyjkbmESe3G4jaQR6bsScO1","model": "gpt-3.5-turbo-0301","object": "chat.completion","usage": {"completion_tokens": 86,"prompt_tokens": 37,"total_tokens": 123}}NoteThe following parameters are not applicable to the new ChatGPT and GPT-4 models: , and . If any of these parameters are set, you will receive an error.
logprobsbest_of
echo
finish_reasonfinish_reason
max_tokens
gpt-35-turbo Equivalent to OpenAI’s model.
gpt-3.5-turbo
gpt-35-turbogpt-4
gpt-4-32k
03010314
{"role": "system", "content": "Provide some context and/or instructions to the model"},{"role": "user", "content": "The users messages goes here"}A conversation with a sample answer followed by a question looks like this : copy
{"role": "system", "content": "Provide some context and/or instructions to the model."},{"role": "user", "content": "Example question goes here."},{"role": "assistant", "content": "Example answer goes here."},{"role": "user", "content": "First question/message for the model to actually respond to."}
系统角色也称为系统消息,包含在数组的开头。此消息提供模型的初始说明。您可以在系统角色中提供各种信息,包括:
您可以为您的使用案例自定义系统角色,也可以只包含基本说明。系统角色/消息是可选的,但建议至少包含一个基本角色/消息以获得最佳结果。
在系统角色之后,您可以在用户和助手之间包含一系列消息。
{"role": "user", "content": "What is thermodynamics?"}
若要触发来自模型的响应,应以用户消息结尾,指示轮到助手响应。您还可以在用户和助手之间包含一系列示例消息,作为进行少量镜头学习的一种方式。
这里有一些示例,展示出可用于 ChatGPT 和 GPT-4 模型的不同样式的提示。这些例子只是一个开端,您可以尝试采用不同的提示来定制自己的用例行为。
如果您希望 ChatGPT 模型的行为类似于 chat.openai.com,您可以使用基本的系统消息,例如“助手是由 OpenAI 训练的大型语言模型”。
{"role": "system", "content": "Assistant is a large language model trained by OpenAI."},{"role": "user", "content": "Who were the founders of Microsoft?"}
对于某些方案,您可能希望向模型提供其他说明,以定义模型能够执行的操作的护栏。
{"role": "system", "content": "Assistant is an intelligent chatbot designed to help users answer their tax related questions.Instructions: - Only answer questions related to taxes. - If you're unsure of an answer, you can say "I don't know" or "I'm not sure" and recommend users go to the IRS website for more information. "},{"role": "user", "content": "When are my taxes due?"}
为了提供额外的对话上下文,您可以在系统消息中包含相关的数据或信息。如果只需要包含少量信息,则可以在系统消息中对其进行硬编码。使用嵌入或 Azure 认知搜索等产品,可以在查询时检索与大量数据相关的最相关信息的模型。
{"role": "system", "content": "Assistant is an intelligent chatbot designed to help users answer technical questions about Azure OpenAI Serivce. Only answer questions using the context below and if you're not sure of an answer, you can say 'I don't know'.Context:- Azure OpenAI Service provides REST API access to OpenAI's powerful language models including the GPT-3, Codex and Embeddings model series.- Azure OpenAI Service gives customers advanced language AI with OpenAI GPT-3, Codex, and DALL-E models with the security and enterprise promise of Azure. Azure OpenAI co-develops the APIs with OpenAI, ensuring compatibility and a smooth transition from one to the other.- At Microsoft, we're committed to the advancement of AI driven by principles that put people first. Microsoft has made significant investments to help guard against abuse and unintended harm, which includes requiring applicants to show well-defined use cases, incorporating Microsoft’s principles for responsible AI use."},{"role": "user", "content": "What is Azure OpenAI Service?"}
您还可以为模型提供一些镜头示例。由于新的提示格式,少数镜头学习的方法略有变化。现在,您可以在提示中包含用户和助手之间的一系列消息作为几个镜头示例。这些示例可用于为常见问题的答案提供种子,以启动模型或向模型教授特定行为。
这只是一个使用 ChatGPT 和 GPT-4 进行有限镜头学习的案例。您可以尝试不同的方法,看看哪种方法最适合您的使用案例。
{"role": "system", "content": "Assistant is an intelligent chatbot designed to help users answer their tax related questions. "},{"role": "user", "content": "When do I need to file my taxes by?"},{"role": "assistant", "content": "In 2023, you will need to file your taxes by April 18th. The date falls after the usual April 15th deadline because April 15th falls on a Saturday in 2023. For more details, see https://www.irs.gov/filing/individuals/when-to-file."},{"role": "user", "content": "How can I check the status of my tax refund?"},{"role": "assistant", "content": "You can check the status of your tax refund by visiting https://www.irs.gov/refunds"}
聊天完成 API 旨在处理多轮对话,但它也适用于非聊天方案。
例如,对于实体提取方案,可以使用以下提示:
{"role": "system", "content": "You are an assistant designed to extract entities from text. Users will paste in a string of text and you will respond with entities you've extracted from the text as a JSON object. Here's an example of your output format:{ "name": "", "company": "", "phone_number": ""}"},{"role": "user", "content": "Hello. My name is Robert Smith. I'm calling from Contoso Insurance, Delaware. My colleague mentioned that you are interested in learning about our comprehensive benefits policy. Could you give me a call back at (555) 346-9322 when you get a chance so we can go over the benefits?"}
迄今为止,我们所提供的样例演示了与聊天完成 API 交互的基本机制。此示例演示如何创建执行以下操作的会话循环:
这意味着每次提出新问题时,到目前为止的对话记录都会与最新问题一起发送。由于模型没有内存,因此您需要为每个新问题发送更新的成绩单,否则模型将丢失以前问题和答案的上下文。
蟒import osimport openaiopenai.api_type = "azure"openai.api_version = "2023-05-15" openai.api_base = os.getenv("OPENAI_API_BASE")# Your Azure OpenAI resource's endpoint value .openai.api_key = os.getenv("OPENAI_API_KEY")conversation=[{"role": "system", "content": "You are a helpful assistant."}]while(True):user_input = input()conversation.append({"role": "user", "content": user_input})response = openai.ChatCompletion.create(engine="gpt-3.5-turbo", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.messages = conversation)conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']})print("\n" + response['choices'][0]['message']['content'] + "\n")
当您运行上面的代码时,您将获得一个空白的控制台窗口。在窗口中输入您的第一个问题,然后按回车键。返回回复后,您可以重复该过程并继续提问。
前面的示例将一直运行,直到达到模型的令牌限制。随着每个问题的提出和答案的接收,数组的大小就会增加。的令牌限制为 4096 个令牌,而 和 的令牌限制分别为 8192 和 32768。这些限制包括来自发送的消息数组和模型响应的令牌计数。消息数组中的令牌数与参数值的组合必须保持在这些限制之下,否则您将收到错误。messages
gpt-35-turbo
gpt-4
gpt-4-32k
max_tokens
你有责任确保提示和完成在令牌限制范围内。在处理较长的对话时,您需要追踪令牌计数,并且只有在超过限制时才向模型发送提示。
以下代码示例显示了一个简单的聊天循环示例,其中包含使用 OpenAI 的 tiktoken 库处理 4096 令牌计数的技术。
代码需要 抖音令牌 .如果您有旧版本运行 .0.3.0
pip install tiktoken --upgrade
import tiktokenimport openaiimport osopenai.api_type = "azure"openai.api_version = "2023-05-15" openai.api_base = os.getenv("OPENAI_API_BASE")# Your Azure OpenAI resource's endpoint value .openai.api_key = os.getenv("OPENAI_API_KEY")system_message = {"role": "system", "content": "You are a helpful assistant."}max_response_tokens = 250token_limit= 4096conversation=[]conversation.append(system_message)def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):encoding = tiktoken.encoding_for_model(model)num_tokens = 0for message in messages:num_tokens += 4# every message follows <im_start>{role/name}\n{content}<im_end>\nfor key, value in message.items():num_tokens += len(encoding.encode(value))if key == "name":# if there's a name, the role is omittednum_tokens += -1# role is always required and always 1 tokennum_tokens += 2# every reply is primed with <im_start>assistantreturn num_tokenswhile(True):user_input = input("") conversation.append({"role": "user", "content": user_input})conv_history_tokens = num_tokens_from_messages(conversation)while (conv_history_tokens+max_response_tokens >= token_limit):del conversation[1] conv_history_tokens = num_tokens_from_messages(conversation)response = openai.ChatCompletion.create(engine="gpt-35-turbo", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.messages = conversation,temperature=.7,max_tokens=max_response_tokens,)conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']})print("\n" + response['choices'][0]['message']['content'] + "\n")
在此示例中,达到令牌计数后,将删除对话脚本中最旧的消息。 代替效率,我们从索引 1 开始,以便始终保留系统消息并仅删除用户/助手消息。随着时间的推移,这种管理对话的方法可能会导致对话质量下降,因为模型将逐渐失去对话早期部分的上下文。del
pop()
另一种方法是将对话持续时间限制为最大令牌长度或一定数量的回合数。达到最大令牌限制后,如果要允许对话继续,模型将失去上下文,则可以提示用户他们需要开始新的对话,并清除消息数组以启动具有完整令牌限制的全新对话。
前面演示的代码的令牌计数部分是 OpenAI 说明书示例之一的简化版本。
微软已经宣布System Center 2022已经发布,可点击进入ChatGPT工具插件导航大全。最新版本带来了 System Center Operations Manager (SCOM)、Virtual Machine Manager (VMM)、System Center Orchestrator (SCORCH)、...
点击进入:ChatGPT工具插件导航大全 当微软推出 Windows 10 操作系统时,它表示 Windows 10 将是 Windows 的最后一个也是最终版本。去年,微软发布了 Windows 11,并有传闻称该公司正在研发 Windows 12。 ...
点击进入:ChatGPT工具插件导航大全 Windows 11 Pro ISO文件在哪下载最新版?如何下载原装纯净版Win11 ?点击进入 持续更新!Valve 的 Steam Deck 便携式游戏机是带有 Steam 自己的 Steam Deck UI 皮肤的迷你 Win...
点击进入:ChatGPT工具插件导航大全 Windows 11 Pro ISO文件在哪下载最新版?如何下载原装纯净版Win11 ?点击进入 持续更新! *更新:英伟达 回应了我们的要求,并指出永恒边缘在DLSS 的官方 Unity 集成可用之前...
The above is the detailed content of Learn how to use ChatGPT and GPT-4 models. For more information, please follow other related articles on the PHP Chinese website!