资讯详情

资讯详情

【大模型应用开发极简入门】提示工程一:1. 通过context、task、role文本结构设计有效的提示词、 2. OpenAI的提示词任务示例

文章目录一. chat_completion函数二. 设计有效的提示词1.上下文1.1. 更多细节的上下文1.2. 让GPT改进上下文2.任务2.1. 提供足够的任务信息2.2. OpenAI的任务示例语法纠正总结TL;DR概要Python转自然语言计算时间复杂度修复Python bug产生python函数3.角色了解LLM和OpenAI API的基础知识之后我们就可以了解一些更高阶的知识比如提示工程、零样本学习、少样本学习到为特定任务微调模型接下来我们将要了解提供开发LLM驱动型应用程序所需的一切知识。一. chat_completion函数先来回顾一下chat_completion函数defchat_completion(prompt,modelgpt-4,temperature0):resopenai.ChatCompletion.create(modelmodel,messages[{role:user,content:prompt}],temperaturetemperature,)print(res[choices][0][message][content])model和temperature是两个可选特征分别被默认设置为gpt-4和0。为了说明提示工程的原理我们使用示例文本“As Descartes said, I think therefore”正如笛卡儿所说我思故。如果将此文本输入GPT-4那么模型自然会通过迭代式地添加最可能出现的标记来补全句子chat_completion(As Descartes said, I think therefore)模型的输出消息如下所示I am.This famous philosophical statement,also knownasCogito,ergosum, emphasizes the existence of the self through the act of thinkingordoubting.Descartes used this statementasa foundational principleinhis philosophy,arguing that ones own existenceisthe most certainandindubitable fact that can be known.需要注意的是提示工程可能会影响OpenAI API的使用成本。该成本与你发送给OpenAI并从其接收的标记数成正比。如所述我们强烈建议使用max_tokens参数以避免费用超出预期。另外在openai库的方法中使用不同的参数因为如果使用temperature、top_p和max_tokens等参数那么即使使用相同的提示词你也可能得到截然不同的结果。二. 设计有效的提示词很多任务可以通过提示词来完成包括摘要、文本分类、情感分析和问题回答。在所有这些任务中我们通常需要在提示词中定义三大要素角色、上下文和任务如下图。这三大要素并非都是必需的它们的顺序也不是固定的。不过如果能够构造好提示词的结构并明确定义这三大要素你就能够得到不错的结果。请注意即使使用了这三大要素对于复杂的任务你也可能需要采用更高级的技巧如零样本学习、少样本学习和微调。1.上下文提示词的第一大要素是上下文。在为LLM编写输入文本时必须尽可能详细地描述上下文。为了说明这一点假设我们想使用GPT-4创建一个应用程序用于推荐午餐的主菜。1.1. 更多细节的上下文接下来我们将比较在两个上下文中获得的结果。第一个上下文提供较少的细节第二个上下文则提供较多的细节。假设使用以下较短的提示词chat_completion(Give me a suggestionforthe main coursefortodays lunch.)我们得到以下输出消息Grilled chickenwithroasted vegetablesandquinoa.现在在提示词中加入更多上下文信息prompt Context: I do 2 hours of sport a day. I am vegetarian, and I dont like green vegetables. I am conscientious about eating healthily. Task: Give me a suggestion for a main course for todays lunch.chat_completion(prompt)我们得到以下输出消息For todays lunch,you cantrya ChickpeaandSweet Potato Curry servedwithQuinoa.This dishispackedwithprotein,fiber,andessential nutrients,whilealso being vegetarianandnotrelying on green vegetables.The curry can be madewithchickpeas,sweet potatoes,tomatoes,onions,garlic,anda blend of spices like turmeric,cumin,andcoriander.Cook the quinoa separatelyandserve it alongside the curryfora deliciousandhealthy meal.在第二个例子中由于提示词含有更多上下文因此模型给出的句子更详细它推荐了一道富含蛋白质的健康素食菜肴。1.2. 让GPT改进上下文为了找到可改进之处你可以请GPT-4提些意见。在下面的示例中我们修改提示词不再要求模型回答问题而是指示它以增强补全效果为目标来向我们提出关于上下文的问题。使用以下提示词prompt Context: I do 2 hours of sport a day. I am vegetarian and I dont like green vegetables. I am very careful to eat healthily. Task: Give me a suggestion for a main course for todays lunch? Do not perform the requested task! Instead, can you ask me questions about the context so that when I answer, you can perform the requested task more efficiently? chat_completion(prompt)GPT-4向我们提出以下问题Sure,I have a few questions tohelpme suggest a suitable main courseforyou:1.Whattypeof sport do you do,anddo you haveanyspecific nutritional requirements related to it?2.Are thereanyother vegetablesoringredients that you particularly enjoyordislike?3.Do you prefer a hotorcold mealforlunch?4.Are youopento trying new recipesordo you prefer sticking to familiar dishes?模型提出的问题通常与提示词相关。根据这些问题我们可以在提示词中更新上下文。总之我们建议你在构建提示词时经常使用这个技巧。2.任务任务定义了你对GPT-4模型的用法并且应该明确且具体。你应该提供足够的任务信息并在提示词中使用合适的短语来引导模型给出你所期望的结果。2.1. 提供足够的任务信息在前面的例子中任务是让模型为午餐推荐一道主菜。我们可以为该任务添加细节以说明我们想要的内容。在本例中我们要求模型添加一张表格列出每种食材所需的克数prompt Context: I do 2 hours of sport a day. I am vegetarian, and I dont like green vegetables. I am conscientious about eating healthily. Task: Give me a suggestion for a main course for todays lunch. With this suggestion, I also want a table with two columns where each row contains an ingredient from the main course. The first column of the table is the name of the ingredient. The second column of the table is the number of grams of that ingredient needed for one person. Do not give the recipe for preparing the main course. chat_completion(prompt)模型给出以下输出结果Suggestionfortodays lunch:Stuffed Bell PepperswithQuinoaandBlack Beans|Ingredient|Grams per person||---------------------|------------------||Bell peppers|200g||Quinoa|60g||Black beans|75g||Onion|50g||Garlic|5g||Olive oil|10g||Tomato sauce|50g||Cumin|1g||Chili powder|1g||Salt|1g||Pepper|1g||Shredded cheese|30g|2.2. OpenAI的任务示例OpenAI API示例页面列出了48个任务示例。这些示例展示了GPT模型可以执行的任务其中每个示例都配有相关的提示词和演示。见官网 Prompt examples虽然这些示例使用了GPT-3模型和Completion端点但对于ChatCompletion端点来说原理是相同的并且这些示例很好地说明了如何给OpenAI模型指派任务。语法纠正纠正句子中的语病并将其修改为标准的英语句子。fromopenaiimportOpenAI clientOpenAI()responseclient.chat.completions.create(modelgpt-3.5-turbo,messages[{role:system,content:You will be provided with statements, and your task is to convert them to standard English.},{role:user,content:She no went to the market.}],temperature0.7,max_tokens64,top_p1)总结给二年级学生概括一下fromopenaiimportOpenAI clientOpenAI()responseclient.chat.completions.create(modelgpt-3.5-turbo,messages[{role:system,content:Summarize content you are provided with for a second-grade student.},{role:user,content:Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[20] and is on average the third-brightest natural object in the night sky after the Moon and Venus.}],temperature0.7,max_tokens64,top_p1)TL;DR概要TL;DR是“too long; didn’t read”的首字母缩写意为“太长了没读”。有人发现只需在文本末尾添加Tl;dr即可请求模型对文本进行总结。提示词示例如下。A neutron star[...]atomic nuclei.Tl;drPython转自然语言用自然语言解释一段Python代码。fromopenaiimportOpenAI clientOpenAI()responseclient.chat.completions.create(modelgpt-4,messages[{role:system,content:You will be provided with a piece of code, and your task is to explain it in a concise way.},{role:user,content:class Log:\n def __init__(self, path):\n dirname os.path.dirname(path)\n os.makedirs(dirname, exist_okTrue)\n f open(path, \a\)\n \n # Check that the file is newline-terminated\n size os.path.getsize(path)\n if size 0:\n f.seek(size - 1)\n end f.read(1)\n if end ! \\\n\:\n f.write(\\\n\)\n self.f f\n self.path path\n \n def log(self, event):\n event[\_event_id\] str(uuid.uuid4())\n json.dump(event, self.f)\n self.f.write(\\\n\)\n \n def state(self):\n state {\complete\: set(), \last\: None}\n for line in open(self.path):\n event json.loads(line)\n if event[\type\] \submit\ and event[\success\]:\n state[\complete\].add(event[\id\])\n state[\last\] event\n return state}],temperature0.7,max_tokens64,top_p1)计算时间复杂度计算一个函数的时间复杂度。fromopenaiimportOpenAI clientOpenAI()responseclient.chat.completions.create(modelgpt-3.5-turbo,messages[{role:system,content:You will be provided with Python code, and your task is to calculate its time complexity.},{role:user,content:def foo(n, k):\n accum 0\n for i in range(n):\n for l in range(k):\n accum i\n return accum}],temperature0.7,max_tokens64,top_p1)修复Python bug修复含有bug的Python代码。fromopenaiimportOpenAI clientOpenAI()responseclient.chat.completions.create(modelgpt-4,messages[{role:system,content:You will be provided with a piece of Python code, and your task is to find and fix bugs in it.},{role:user,content:import Random\n a random.randint(1,12)\n b random.randint(1,12)\n for i in range(10):\n question \What is \a\ x \b\? \\n answer input(question)\n if answer a*b\n print (Well done!)\n else:\n print(\No.\)}],temperature0.7,max_tokens64,top_p1)SQL请求构建简单的SQL查询。提示词示例如下。Create a SQL request to findallusers who liveinCaliforniaandhave over1000credits.产生python函数fromopenaiimportOpenAI clientOpenAI()responseclient.chat.completions.create(modelgpt-4,messages[{role:user,content:Write a Python function that takes as input a file path to an image, loads the image into memory as a numpy array, then crops the rows and columns around the perimeter if they are darker than a threshold value. Use the mean value of rows and columns to decide if they should be marked for deletion.}],temperature0.7,max_tokens64,top_p1)3.角色在撰写提示词时影响模型的最后一种方式是赋予其一个角色。当然角色和上下文可以独立使用但同时使用可以加强对模型输出的控制。在以下示例中我们赋予模型运动营养专家的角色prompt # Role: You are a nutritionist designing healthy diets for high-performance athletes. You take into account the nutrition needed for a good recovery. # Context: I do 2 hours of sport a day. I am vegetarian, and I dont like green vegetables. I am conscientious about eating healthily. # Task: Based on your expertise defined in your role, give me a suggestion for a main course for todays lunch. With this suggestion, I also want a table with two columns where each row in the table contains an ingredient from the main course. The first column of the table is the name of the ingredient. The second column of the table is the number of grams of that ingredient needed for one person. Do not give the recipe for preparing the main course. chat_completion(prompt)我们得到以下结果Main Course Suggestion:QuinoaandChickpea Stuffed Bell Peppers Ingredient Table:|Ingredient|Grams per Person||---------------------|------------------||Quinoa|60||Chickpeas|100||Bell Peppers|200||Olive Oil|10||Onion|50||Garlic|5||Tomato|100||Feta Cheese|30||Kalamata Olives|20||Fresh Basil|5||Salt|2||Black Pepper|1|提示词可用于调整像GPT模型这样的LLM的概率分布集。它们可以被视为模型指南引导模型生成特定类型的结果。注意上述提到的上下文、任务、角色的提示词格式只是一种方法你完全可以创建不明确定义这些要素的提示词。不要受限于“上下文−任务−角色”框架而应将其视为帮助你有效设计提示词的工具。
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →