使用OpenAI GPT模型的最佳实践

2023年08月21日 由 camellia 发表 285 0

自GPT模型发布以来,每个人都在不断使用它们。从简单问题到复杂编码开发,GPT模型可以帮助用户快速完成。这就是为什么随着时间的推移,该模型会变得更加庞大的原因。


为了帮助用户获得最佳输出结果,OpenAI为使用GPT模型提供了最佳实践。这些实践来自许多用户不断实验这个模型并找到最佳方法的经验。


2


在本文中,我将总结你使用OpenAI GPT模型时需要知道的最佳实践。


GPT最佳实践


GPT模型的输出结果取决于你的提示。只要给出明确的指令,它就会提供你期望的结果。改进GPT输出的一些建议包括:


1.在提示中提供详细信息以获得相关答案。例如,我们可以将提示从“给我一个计算正态分布的代码”改为“用Python提供带有代码示例的标准分布计算。在每个部分中放置注释,并解释为什么每个代码都以这种方式执行。”


2.给出一个角色或例子,并添加输出的长度。我们可以为模型提供一个角色或例子,以便更清楚地解释。例如,我们可以传递系统角色参数,以一种老师向学生解释事情的方式进行解释。通过提供角色,GPT模型将以我们需要的方式提供结果。如果你想更改角色,这是一个示例代码。

import openai

openai.api_key = ""

res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=100,
temperature=0.7,
messages=[
{
"role": "system",
"content": """

When I ask to explain something, bring it in a way that teacher
would explain it to students in every paragraph.

""",
},
{
"role": "user",
"content": """

What is golden globe award and what is the criteria for this award? Summarize them in 2 paragraphs.

""",
},
],
)

还可以提供示例结果,以指导GPT模型如何回答你的问题。例如,在此代码中,我传递了如何解释情感,并希望GPT模型可以模仿我的风格。

res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=100,
temperature=0.7,
messages=[
{
"role": "system",
"content": "Answer in a consistent style.",
},
{
"role": "user",
"content": "Teach me about Love",
},
{
"role": "assistant",
"content": "Love can be sweet, can be sour, can be grand, can be low, and can be anything you want to be",
},
{
"role": "user",
"content": "Teach me about Fear",
},
],
)


3.指定完成任务的步骤。请提供详细的步骤说明如何获得最佳输出。详细说明GPT模型应如何运行。例如,我们在此代码中使用前缀和翻译来提供2步指令的详细分解。

res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=100,
temperature=0.7,
messages= [
{
"role": "system",
"content": """
Use the following step-by-step instructions to respond to user inputs.

step 1 - Explain the question input by the user in 2 paragraphs or less with the prefix "Explanation: ".

Step 2 - Translate the Step 1 into Indonesian, with a prefix that says "Translation: ".

""",
},
{
"role": "user",
"content":"What is heaven?",
},
])


4.提供参考资料、链接或引用。如果我们已经对问题有各种参考资料,我们可以将它们作为GPT模型提供输出的依据。列出你认为与你的问题相关的参考资料,并将它们传递给系统角色。


5.给予GPT时间“思考”。在急于提供不正确结果之前,提供一个查询让GPT详细处理提示。特别是如果我们将助手角色传递给错误结果,我们希望GPT能够独立思考。例如,下面的代码显示了我们如何要求GPT模型对用户输入更加严格。

res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=100,
temperature=0.7,
messages= [
{
"role": "system",
"content": """

Work on your solution to the problem, then compare your solution to the user and evaluate
if the solution is correct or not. Only decide if the solution is correct once you have done the problem yourself.

""",
},
{
"role": "user",
"content":"1 + 1 = 3",
},
])


6.使用代码执行使GPT获得精确结果。对于更长或更复杂的计算,GPT可能无法按预期工作,因为模型可能提供不准确的结果。为了缓解这个问题,我们可以要求GPT模型编写和运行代码,而不是直接计算。这样,GPT可以依赖代码而不是计算结果。例如,我们可以提供如下输入。

res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=100,
temperature=0.7,
messages= [
{
"role": "system",
"content": """

Write and execute Python code by enclosing it in triple backticks,
e.g. ```code goes here```. Use this to perform calculations.

""",
},
{
"role": "user",
"content":"""

Find all real-valued roots of the following polynomial equation: 2*x**5 - 3*x**8- 2*x**3 - 9*x + 11.

""",
},
])


结论


GPT模型是目前最好的模型之一,以下是一些改进GPT模型输出的最佳实践:


  1. 在提示中提供详细信息以获得相关答案
  2. 给出一个角色或例子,并添加输出的长度
  3. 指定完成任务的步骤
  4. 提供参考资料、链接或引用
  5. 给予GPT时间“思考”
  6. 使用代码执行以获得精确结果
文章来源:https://www.kdnuggets.com/2023/08/best-practices-openai-gpt-model.html
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消