🔥ChatGPT&GPT-4新纪元?🚀对话式AI的超能力探索启用!💰立即尝试预览API,未来已来!

ChatGPT与软件 1年前 (2023) lida
45 0 0

文章主题:ChatGPT, GPT-4, ChatCompletion API, Azure OpenAI

666ChatGPT办公新姿势,助力做AI时代先行者!

🔥ChatGPT&GPT-4新纪元?🚀对话式AI的超能力探索启用!💰立即尝试预览API,未来已来!

🌟ChatGPT与🔥GPT-4:对话新纪元的语言大师🌟🚀引领潮流的AI对话模型,ChatGPT和GPT-4正革新我们的交互体验!不像旧版的GPT-3仅仅文本对文本,它们进化为双向互动的智慧引擎。💡传统的输入输出模式已成过去,现在的模型犹如会话中的伙伴,期待以流畅、自然的格式响应——不再是单纯拼接提示,而是生成连贯的对话内容。 示例:用户:“你好”,模型回应:“你好呀!有什么我可以帮你的吗?”无论是在客服咨询、教育辅导,还是创意写作中,这种对话式输入都能让信息流动更加顺畅。💡非聊天场景下,此设计同样适用,只需稍作调整,它也能轻松适应各种需求。SEO优化提示:使用”对话模型”、”GPT-4升级”、”AI交互体验”等关键词,确保内容与目标搜索引擎相关性高。记得加入一些行业术语和未来趋势的预测,提升文章的专业性和吸引力。

在 Azure OpenAI 中,有两种不同的选项可用于与这些类型的模型进行交互:

聊天完成 API。

带有聊天标记语言 (ChatML) 的完成 API。

🚀🚀ChatCompletion API, the cutting-edge 🤖专用API, revolutionizes interaction with ChatGPT and GPT-4 prowess!两款顶尖模型正处于热辣🔥的预览阶段,等待你的探索。别无他选,这便是通往未来AI世界的金钥匙🔑,专为新GPT-4量身打造!🚀

🌟ChatML🚀,与文本巨匠(text-davinci-002)等一脉相承,采用相同的API实力,只需巧妙的`(ChatML)`提示牌,开启深度对话的新篇章!它虽不像专为聊天设计的Completion API那般纯粹,却以更灵活的方式解锁权限。但别忘了,额外的验证环节是它的防护盾,确保信息安全。仅限于GPT-35 Turbo的ChatGPT,这个选择可能稍显独特。底层架构的稳健更新,就像时间的见证者,未来可期。🚀

🌟🚀掌握最新ChatGPT&GPT-4,解锁高效沟通新纪元!💡欲体验卓越性能,务必遵循新指示🔥。旧版互动模式?OUTdated!尝试转变策略,ChatGPT将迅速洞察并给出简洁、高效的反馈。🎯每一步都至关重要,让ChatGPT引领你走向知识的海洋🌊,不再冗长等待,每一次交互都是价值的释放✨。欲了解更多高效沟通秘籍,立即行动,让我们一起探索这个AI语言巨变!🌍记得,优化你的互动体验,ChatGPT会更爱你哦😊💪

使用 ChatGPT 和 GPT-4 模型(预览)

🌟🚀掌握未来对话技术!将ChatGPT&GPT-4与Completion API无缝对接,只需一行代码🔥💻👩‍💻新手指南:轻松上手ChatGPT&GPT-4,我们的入门课程带你快速入门,无需编程基础🎉📚💡通过API,释放模型无限可能,无论聊天、创作还是解答问题,都能事半功倍!🚀欲了解更多详情?👉点击链接或搜索”ChatGPT API集成教程”,立即开启你的AI对话之旅🌍💻

🎉【最新动态】🚀 GPT-4 已揭开神秘面纱,现正以预览形式震撼登场!💡 作为行业领先技术的代表,Azure OpenAI 正向符合条件的用户敞开大门。只需轻轻一步,通过下方链接填写申请表格,您就有机会率先体验这一创新力作。👩‍💻👨‍💻请注意,此阶段的访问权限是有限的,感兴趣的开发者和专业人士请尽快行动。我们致力于保护隐私,所有信息将严格保密。🌍欲了解更多详情或有任何疑问,请随时查阅我们的官方更新,保证一手资讯不落空!📢别忘了,提升你的技术素养,引领未来趋势,GPT-4 等你来探索!🚀

Python实现代码

import os

import openai

openai.api_type = “azure”

openai.api_version = “2023-03-15-preview”

openai.api_base = os.getenv(“OPENAI_API_BASE”) # Your Azure OpenAI resources 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”: “Whats the difference between garbanzo beans and chickpeas?”},

]

)

print(response)

print(response[choices][0][message][content])

输出

{

“choices”: [

{

“finish_reason”: “stop”,

“index”: 0,

“message”: {

“content”: “There is no difference between garbanzo beans and chickpeas \u2014 the terms are used interchangeably to describe the same round, beige-colored legume. Garbanzo beans are the Spanish name for the legume, while chickpeas are the English name for the same legume. They are a common ingredient in many traditional Middle Eastern and Mediterranean dishes, such as hummus, falafel, and stews.”,

“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

}

}

There is no difference between garbanzo beans and chickpeas — the terms are used interchangeably to describe the same round, beige-colored legume. Garbanzo beans are the Spanish name for the legume, while chickpeas are the English name for the same legume. They are a common ingredient in many traditional Middle Eastern and Mediterranean dishes, such as hummus, falafel, and stews.

笔记

以下参数不适用于新的 ChatGPT 和 GPT-4 模型:logprobs、best_of和echo。如果您设置这些参数中的任何一个,您将收到一个错误。

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

停止:API 返回完整的模型输出。

length:由于 max_tokens 参数或令牌限制,模型输出不完整。

content_filter:由于来自我们的内容过滤器的标记而省略了内容。

null:API 响应仍在进行中或未完成。

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

模型版本控制

笔记

gpt-35-turbo相当于gpt-3.5-turboOpenAI 的模型。

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

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

使用聊天完成 API

OpenAI 训练了 ChatGPT 和 GPT-4 模型来接受格式化为对话的输入。messages 参数采用字典数组,其中包含按角色组织的对话。

基本聊天完成的格式如下:

{“role”: “system”, “content”: “Provide some context and/or instructions to the model”},

{“role”: “user”, “content”: “The users messages goes here”}

一个示例答案后跟一个问题的对话如下所示:

{“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,您可以使用基本系统消息,例如“Assistant 是一个由 OpenAI 训练的大型语言模型”。

{“role”: “system”, “content”: “Assistant is a large language model trained by OpenAI.”},

{“role”: “user”, “content”: “Whats the difference between garbanzo beans and chickpeas?”}

带说明的示例

对于某些场景,您可能希望向模型提供额外的指令来为模型能够执行的操作定义护栏。

{“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 youre unsure of an answer, you can say “I dont know” or “Im 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 youre not sure of an answer, you can say I dont know.

Context:

– Azure OpenAI Service provides REST API access to OpenAIs 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, were 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?”}

使用 Chat Completion 进行少量学习

您还可以为模型提供一些镜头示例。由于新的提示格式,少数镜头学习的方法略有改变。您现在可以在提示中包含用户和助手之间的一系列消息作为几个镜头示例。这些示例可用于播种常见问题的答案,以启动模型或向模型教授特定行为。

这只是您如何通过 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”},

在非聊天场景中使用聊天完成

Chat Completion 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 youve extracted from the text as a JSON object. Heres an example of your output format:

{

“name”: “”,

“company”: “”,

“phone_number”: “”

}”},

{“role”: “user”, “content”: “Hello. My name is Robert Smith. Im 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?”}

创建一个基本的对话循环

到目前为止的示例向您展示了与 Chat Completion API 交互的基本机制。此示例向您展示如何创建执行以下操作的对话循环:

持续获取控制台输入,并将其正确格式化为消息数组的一部分作为用户角色内容。

输出打印到控制台并格式化并作为助理角色内容添加到消息数组的响应。

这意味着每次提出新问题时,都会将到目前为止的对话记录连同最新问题一起发送。由于模型没有记忆,您需要为每个新问题发送更新的成绩单,否则模型将丢失先前问题和答案的上下文。

Python实现代码:

import os

import openai

openai.api_type = “azure”

openai.api_version = “2023-03-15-preview”

openai.api_base = os.getenv(“OPENAI_API_BASE”) # Your Azure OpenAI resources 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”)

当您运行上面的代码时,您将看到一个空白的控制台窗口。在窗口中输入您的第一个问题,然后按回车键。返回响应后,您可以重复该过程并继续提问

管理对话

前面的示例将一直运行,直到您达到模型的令牌限制。随着每个问题的提出和收到的答案,messages数组的大小都会增加。的令牌限制为 4096 个令牌,而和 的gpt-35-turbo令牌限制分别为 8192 和 32768。这些限制包括来自发送的消息数组和模型响应的令牌计数。messages 数组中的标记数与参数值的组合必须保持在这些限制之内,否则您将收到错误消息。gpt-4gpt-4-32kmax_tokens

您有责任确保提示和完成在令牌限制内。这意味着对于更长的对话,您需要跟踪令牌计数并仅向模型发送落在限制范围内的提示。

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

该代码需要 tiktoken 0.3.0。如果您有旧版本,请运行pip install tiktoken –upgrade.

Python实现代码:

import tiktoken

import openai

import os

openai.api_type = “azure”

openai.api_version = “2023-03-15-preview”

openai.api_base = os.getenv(“OPENAI_API_BASE”) # Your Azure OpenAI resources endpoint value .

openai.api_key = os.getenv(“OPENAI_API_KEY”)

system_message = {“role”: “system”, “content”: “You are a helpful assistant.”}

max_response_tokens = 250

token_limit= 4096

conversation=[]

conversation.append(system_message)

def num_tokens_from_messages(messages, model=”gpt-3.5-turbo-0301″):

encoding = tiktoken.encoding_for_model(model)

num_tokens = 0

for message in messages:

num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n

for key, value in message.items():

num_tokens += len(encoding.encode(value))

if key == “name”: # if theres a name, the role is omitted

num_tokens += -1 # role is always required and always 1 token

num_tokens += 2 # every reply is primed with <im_start>assistant

return num_tokens

while(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”)

在此示例中,一旦达到令牌计数,对话记录中最旧的消息将被删除。del用于代替pop()效率,我们从索引 1 开始,以便始终保留系统消息并仅删除用户/助理消息。随着时间的推移,这种管理对话的方法可能会导致对话质量下降,因为模型将逐渐失去对话早期部分的上下文。

另一种方法是将对话持续时间限制为最大令牌长度或特定轮数。一旦达到最大令牌限制并且如果您要允许对话继续,模型将丢失上下文,您可以提示用户他们需要开始新的对话并清除消息数组以开始全新的对话可用令牌限制。

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

🔥ChatGPT&GPT-4新纪元?🚀对话式AI的超能力探索启用!💰立即尝试预览API,未来已来!

AI时代,掌握AI大模型第一手资讯!AI时代不落人后!

免费ChatGPT问答,办公、写作、生活好得力助手!

扫码右边公众号,驾驭AI生产力!

相关文章