Heim  >  Artikel  >  Erfahren Sie, wie Sie die Modelle ChatGPT und GPT-4 verwenden

Erfahren Sie, wie Sie die Modelle ChatGPT und GPT-4 verwenden

WBOY
WBOYnach vorne
2023-06-03 17:03:252576Durchsuche

ChatGPT und GPT-4 sind Sprachmodelle, die für Konversationsschnittstellen optimiert sind. Diese Modelle verhalten sich anders als ältere GPT-3-Modelle. Frühere Modelle akzeptierten die Texteingabe und -ausgabe, und sie akzeptierten lediglich eine Eingabeaufforderungszeichenfolge und einen Rückgabetext, der nach Abschluss an die Eingabeaufforderung angehängt werden konnte. Allerdings können sowohl ChatGPT- als auch GPT-4-Modelle Konversationseingabe- und -ausgabenachrichten empfangen. Diese Modelle erwarten, dass die Eingabe ähnlich der Skriptformatierung für einen bestimmten Chat formatiert wird und eine Vervollständigung zurückgibt, die die vom Modell im Chat geschriebene Nachricht darstellt. Obwohl dieses Format für Konversationen mit mehreren Runden konzipiert ist, ist es auch in Nicht-Chat-Situationen nützlich.

In Azure OpenAI gibt es zwei verschiedene Optionen für die Interaktion mit diesen Modelltypen:

  • Chat Completion API.
  • API mit Chat Markup Language (ChatML) vervollständigt.

Die Chat Completion API ist eine neue dedizierte API für die Interaktion mit ChatGPT- und GPT-4-Modellen. Diese API ist die bevorzugte Methode für den Zugriff auf diese Modelle. Dies ist auch die einzige Möglichkeit, auf das neue GPT-4-Modell zuzugreifen.

ChatML verwendet dieselbe Vervollständigungs-API, die Sie für andere Modelle wie text-davinci-002 verwenden, die ein einzigartiges tokenbasiertes Eingabeaufforderungsformat namens Chat Markup Language (ChatML) erfordert. Dies bietet Zugriff auf einer niedrigeren Ebene als die dedizierte Chat-Vervollständigungs-API, erfordert aber auch eine zusätzliche Eingabevalidierung, unterstützt nur das ChatGPT-Modell (gpt-35-turbo) und das zugrunde liegende Format ändert sich mit der Zeit eher .

Dieser Artikel führt Sie durch den Einstieg in die neuen ChatGPT- und GPT-4-Modelle. Es ist wichtig, die hier beschriebenen Techniken anzuwenden, um die besten Ergebnisse zu erzielen. Wenn Sie versuchen, mit einem Modell wie bei älteren Modellreihen zu interagieren, wird das Modell normalerweise ausführlicher und liefert weniger nützliche Antworten.

Verwendung von ChatGPT- und GPT-4-Modellen

Der folgende Codeausschnitt zeigt die einfachste Möglichkeit, ChatGPT- und GPT-4-Modelle mit der Chat Completion API zu verwenden. Wenn Sie zum ersten Mal programmgesteuert mit diesen Modellen arbeiten, empfehlen wir Ihnen, mit unseren ChatGPT- und GPT-4-Schnellstarts zu beginnen.

GPT-4-Modelle sind derzeit nur auf Anfrage erhältlich. Bestehende Azure OpenAI-Kunden können den Zugriff anfordern, indem sie dieses Formular ausfüllen.

Python
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")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'])

Output

{"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}}

Hinweis

Die folgenden Parameter sind für neue ChatGPT- und GPT-4-Modelle nicht verfügbar: , und . Wenn einer dieser Parameter festgelegt ist, erhalten Sie eine Fehlermeldung. logprobsbest_ofechologprobsbest_ofecho

每个响应都包含一个 .的可能值为:finish_reasonfinish_reason

  • 停止:API 返回完整的模型输出。
  • 长度:由于参数或令牌限制max_tokens模型输出不完整。
  • content_filter:由于内容过滤器中的标志而省略了内容。
  • :API 响应仍在进行中或不完整。

请考虑设置为比正常值稍高的值,例如 300 或 500。这可确保模型在到达消息末尾之前不会停止生成文本。max_tokens

模型版本控制

 注意

gpt-35-turbo等效于 OpenAI 的模型。gpt-3.5-turbo

与以前的 GPT-3 和 GPT-3.5 模型不同,该模型以及 和 模型将继续更新。创建这些模型的部署时,还需要指定模型版本。gpt-35-turbogpt-4gpt-4-32k

目前,只有版本可用于 ChatGPT 和 GPT-4 型号。我们将来会继续提供更新版本。您可以在我们的模型页面上找到模型弃用时间。03010314

Jede Antwort enthält einen . Mögliche Werte sind: finish_reasonfinish_reason <h2 id="working-with-the-chat-completion-api" class="heading-anchor"></h2> <p></p>Stop<p>: API gibt die vollständige Modellausgabe zurück. </p> <button class="action position-relative display-none-print" type="button" data-bi-name="copy"></button>Länge<button class="action position-relative display-none-print" type="button" data-bi-name="copy">: Unvollständige max_tokens-Modellausgabe aufgrund von Parameter- oder Token-Limits. </button><p>content_filter</p>: Inhalt aufgrund der Markierung im Inhaltsfilter weggelassen. <button class="action position-relative display-none-print" type="button" data-bi-name="copy"></button><button class="action position-relative display-none-print" type="button" data-bi-name="copy">Leer</button>: Die API-Antwort ist noch in Bearbeitung oder unvollständig. 🎜🎜Erwägen Sie, den Wert auf einen etwas höheren Wert als normal einzustellen, z. B. 300 oder 500. Dadurch wird sichergestellt, dass das Modell nicht aufhört, Text zu generieren, bevor das Ende der Nachricht erreicht ist. <code>max_tokens🎜

Modellversionierung🎜🎜 Hinweis🎜🎜gpt-35-turbo Entspricht dem OpenAI-Modell. gpt-3.5-turbo🎜🎜Im Gegensatz zu früheren GPT-3- und GPT-3.5-Modellen wird dieses Modell sowie die und-Modelle weiterhin aktualisiert. Beim Erstellen von Bereitstellungen dieser Modelle müssen Sie auch die Modellversion angeben. gpt-35-turbogpt-4gpt-4-32k🎜🎜Derzeit sind nur Versionen für die Modelle ChatGPT und GPT-4 verfügbar. Wir werden auch in Zukunft aktualisierte Versionen bereitstellen. Die Verfallszeiten von Modellen finden Sie auf unserer Modellseite. 03010314🎜🎜Mithilfe der Chat Completion API🎜🎜OpenAI hat die ChatGPT- und GPT-4-Modelle trainiert, Eingaben im Konversationsformat zu akzeptieren. Der Parameter „messages“ ist ein Array von Wörterbüchern, die nach Rollen geordnete Konversationen enthalten. 🎜🎜Das grundlegende Chat-Vervollständigungsformat ist wie folgt: 🎜🎜🎜🎜Kopieren🎜🎜
{"role": "system", "content": "Provide some context and/or instructions to the model"},{"role": "user", "content": "The users messages goes here"}
🎜Ein Gespräch mit einer Beispielantwort gefolgt von einer Frage sieht so aus: 🎜🎜🎜🎜Kopieren🎜🎜
{"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。这些限制包括来自发送的消息数组和模型响应的令牌计数。消息数组中的令牌数与参数值的组合必须保持在这些限制之下,否则您将收到错误。messagesgpt-35-turbogpt-4gpt-4-32kmax_tokens

你有责任确保提示和完成在令牌限制范围内。在处理较长的对话时,您需要追踪令牌计数,并且只有在超过限制时才向模型发送提示。

以下代码示例显示了一个简单的聊天循环示例,其中包含使用 OpenAI 的 tiktoken 库处理 4096 令牌计数的技术。

代码需要 抖音令牌 .如果您有旧版本运行 .0.3.0pip 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 开始,以便始终保留系统消息并仅删除用户/助手消息。随着时间的推移,这种管理对话的方法可能会导致对话质量下降,因为模型将逐渐失去对话早期部分的上下文。delpop()

另一种方法是将对话持续时间限制为最大令牌长度或一定数量的回合数。达到最大令牌限制后,如果要允许对话继续,模型将失去上下文,则可以提示用户他们需要开始新的对话,并清除消息数组以启动具有完整令牌限制的全新对话。

前面演示的代码的令牌计数部分是 OpenAI 说明书示例之一的简化版本。

后续步骤

相关文章

  • 了解如何使用 ChatGPT 和 GPT-4 模型

    微软宣布 System Center 2022 全面上市

    微软已经宣布System Center 2022已经发布,可点击进入ChatGPT工具插件导航大全。最新版本带来了 System Center Operations Manager (SCOM)、Virtual Machine Manager (VMM)、System Center Orchestrator (SCORCH)、...

  • 了解如何使用 ChatGPT 和 GPT-4 模型

    Windows 12 的下一个“最终”版本可能已经在开发中

    点击进入:ChatGPT工具插件导航大全 当微软推出 Windows 10 操作系统时,它表示 Windows 10 将是 Windows 的最后一个也是最终版本。去年,微软发布了 Windows 11,并有传闻称该公司正在研发 Windows 12。 ...

  • 了解如何使用 ChatGPT 和 GPT-4 模型

    Valve 正在努力确保 Steam Deck 支持 Windows 11

    点击进入:ChatGPT工具插件导航大全 Windows 11 Pro ISO文件在哪下载最新版?如何下载原装纯净版Win11 ?点击进入   持续更新!Valve 的 Steam Deck 便携式游戏机是带有 Steam 自己的 Steam Deck UI 皮肤的迷你 Win...

  • 了解如何使用 ChatGPT 和 GPT-4 模型

    AMD FidelityFX 可能击败 Nvidia DLSS – 这就是原因

    点击进入:ChatGPT工具插件导航大全 Windows 11 Pro ISO文件在哪下载最新版?如何下载原装纯净版Win11 ?点击进入   持续更新! *更新:英伟达 回应了我们的要求,并指出永恒边缘在DLSS 的官方 Unity 集成可用之前...

Das obige ist der detaillierte Inhalt vonErfahren Sie, wie Sie die Modelle ChatGPT und GPT-4 verwenden. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yundongfang.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen