《ChatGPT Prompt Engineering for Developers》笔记

《ChatGPT Prompt Engineering for Developers》笔记

Created
May 15, 2023 02:28 AM
Tags
LLM
Category
Geek
Last Edited
Last updated May 22, 2023
Abstract
本文是观看吴恩达《ChatGPT Prompt Engineering for Developers》的笔记,记录了使用 ChatGPT 的一些技巧。
Related to Reading List (Column)
中英双语字幕|9集全完整版|chatGPT提示工程教学|吴恩达教你如何写提示词|ChatGPT Prompt Engineering for Developer_哔哩哔哩_bilibili
吴恩达联手OpenAI上线免费课程,带你一个半小时学会ChatGPT Prompt工程 !《chatGPT提示词工程》9集完整版 中英双语字幕, 视频播放量 10896、弹幕量 13、点赞数 408、投硬币枚数 267、收藏人数 1447、转发人数 206, 视频作者 程序猿小王Monkey-King, 作者简介 wangwl.net/r/ai |AI绘画教学|使用技巧|最新动态,相关视频:【中文完整版全9集】ChatGPT提示工程师|AI大神吴恩达教你写提示词|prompt engineering,【中文字幕】ChatGPT提示工程课程,吴恩达&OpenAI,【中文完整版全9集】第1集 引入-ChatGPT提示词工程师教程 吴恩达xOpenAI官方,ChatGPT提示工程师&AI大神吴恩达教你写提示词|prompt engineering【完整中字九集全】,根据吴恩达老师教程总结出中文版prompt教程,【中文讲解&示例】吴恩达&OpenAI的ChatGPT提示工程课程,非开发者也能懂的prompt engineering,ChatGPT万能提问公式,效率大增!,B站首推!官方精品【完整版九集】 中文字幕,ChatGPT Prompt提示词课程 斯坦福吴恩达 | OpenAI官方联合出品,十分钟速通吴恩达老师ChatGPT课程,ChatGPT Prompt Engineering实战,中文提示工程教学教程,从入门到精通(发现和吴恩达与OpenAI合作的课程里介绍的技巧英雄所见略同了)
中英双语字幕|9集全完整版|chatGPT提示工程教学|吴恩达教你如何写提示词|ChatGPT Prompt Engineering for Developer_哔哩哔哩_bilibili
视频大纲:
notion image

一、交互原则

1. 清晰且具体

使用分隔符

可以使用 ‘、“、 ```、<> 等符号将内容标记出来,避免模型错误的受内容中的指令干扰

要求结构化输出

可以要求模型使用 JSON、HTML 等格式输出,方便理解与使用

要求模型进行条件检查

要预先假设一些边界条件,让模型提前进行检查,避免出现错误

给出示例

可以通过给出一些任务成功的示例来让模型需要任务的要求

2. 给模型时间去思考

列出具体的工作步骤

通过列出具体的工作步骤,而不是直接提最终要求能减少模型犯的错误
prompt_2 = f""" Your task is to perform the following actions: 1 - Summarize the following text delimited by <> with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following keys: french_summary, num_names. Use the following format: Text: <text to summarize> Summary: <summary> Translation: <summary translation> Names: <list of names in Italian summary> Output JSON: <json with summary and num_names> Text: <{text}> """ response = get_completion(prompt_2) print("\nCompletion for prompt 2:") print(response)

让模型在给出结论前先给出自己的思路

通过让模型给出自己的解决思路,从而让模型能够更深入的思考
prompt = f""" Your task is to determine if the student's solution \ is correct or not. To solve the problem do the following: - First, work out your own solution to the problem. - Then compare your solution to the student's solution \ and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself. Use the following format: Question: ``` question here ``` Student's solution: ``` student's solution here ``` Actual solution: ``` steps to work out the solution and your solution here ``` Is the student's solution the same as actual solution \ just calculated: ``` yes or no ``` Student grade: ``` correct or incorrect ``` Question: ``` I'm building a solar power installation and I need help \ working out the financials. - Land costs $100 / square foot - I can buy solar panels for $250 / square foot - I negotiated a contract for maintenance that will cost \ me a flat $100k per year, and an additional $10 / square \ foot What is the total cost for the first year of operations \ as a function of the number of square feet. ``` Student's solution: ``` Let x be the size of the installation in square feet. Costs: 1. Land cost: 100x 2. Solar panel cost: 250x 3. Maintenance cost: 100,000 + 100x Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000 ``` Actual solution: """ response = get_completion(prompt) print(response)

二、模型局限性

1. 幻觉

模型偶尔会阐述一些看上去合理但并其实并不正确的事。
解决思路是:要求模型在给出答案时要提供相应的引用信息

三、迭代

很难一次就想到正确的 prompt,因此需要根据模型的回答来不断迭代
notion image

1. 迭代过程

  1. 给出明确且具体的指令;
  1. 查看模型的输出;
  1. 分析为什么没有得到想要的输出,是因为指令不够明确,还是没有给模型足够的思考时间?
  1. 改进思路和提示语,进一步明确指令,给模型更多思考时间。
  1. 回到第一步继续,直到得到满意的结果。

四、功能

1. 文本摘要

摘要是 LLM 一项非常实用的应用,可以帮助我们快速从长文本中找到自己需要的信息,而无需花时间亲自读完整段文本,从而大大加快我们我们查资料、了解信息的过程。
prompt = f""" Your task is to generate a short summary of a product \ review from an ecommerce site. Summarize the review below, delimited by triple backticks, in at most 30 words. Review: \`\`\`{prod_review}\`\`\` """ response = get_completion (prompt) print (response)
同时还可以要求模型仅关注某个具体方面的信息,
prompt = f""" Your task is to generate a short summary of a product \ review from an ecommerce site to give feedback to the \ Shipping deparmtment. Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects \ that mention shipping and delivery of the product. Review: \`\`\`{prod_review}\`\`\` """ response = get_completion (prompt) print (response)
还可以将文本摘要的任务转换为信息提取,只保留我们关注的方面而不要其他信息
prompt = f""" Your task is to extract relevant information from \ a product review from an ecommerce site to give \ feedback to the Shipping department. From the review below, delimited by triple quotes \ extract the information relevant to shipping and \ delivery. Limit to 30 words. Review: \`\`\`{prod_review}\`\`\` """ response = get_completion (prompt) print (response)

2. 推断

情感分析、实体识别与主题提取是三种常见的推断工作。以往的深度学习工程师如果需要实现这三个任务,则至少要用不同的数据集把模型训练为三种不同的权重。而 ChatGPT 搭配合适的提示语把这些曾经麻烦的任务变得唾手可得。

情感分析

可以让模型概括一段文字所表达的情感,或是直接让其进行判断
prompt = f""" Identify a list of emotions that the writer of the \ following review is expressing. Include no more than \ five items in the list. Format your answer as a list of \ lower-case words separated by commas. Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response)

实体识别

prompt = f""" Identify the following items from the review text: - Item purchased by reviewer - Company that made the item The review is delimited with triple backticks. \ Format your response as a JSON object with \ "Item" and "Brand" as the keys. If the information isn't present, use "unknown" \ as the value. Make your response as short as possible. Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response)

主题提取

主题是一种比摘要更简洁直观地让我们了解文章的方式。一篇文章的摘要包含了其大部分主要内容,而主题只涉及文章内容属于哪些领域,讨论了哪些话题。通过创建自动推断文章主题的脚本,我们可以实现文章自动归类和新闻提醒功能。
topic_list = [ "nasa", "local government", "engineering", "employee satisfaction", "federal government" ] prompt = f""" Determine whether each item in the following list of \ topics is a topic in the text below, which is delimited with triple backticks. Give your answer as list with 0 or 1 for each topic.\ List of topics: {", ".join(topic_list)} Text sample: '''{story}''' """ # 这里要求模型直接生成JSON格式更规范! response = get_completion(prompt) topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')} if topic_dict['nasa'] == 1: print("ALERT: New NASA story!")

3. 文本转换

翻译

由于 LLM 是使用互联网资源训练的,其中自然包括了很多种语言,所以它可以进行翻译工作,同时还可以为它补充语境。
prompt = f""" Translate the following text to Spanish in both the \ formal and informal forms: 'Would you like to order a pillow?' """ response = get_completion(prompt) print(response)

语气转换

在不同场合人们通常使用不同的语气,LLM 还可以帮助我们将一段话转换成多种语气。
prompt = f""" Translate the following from slang to a business letter: 'Dude, This is Joe, check out this spec on this standing lamp.' """ response = get_completion(prompt) print(response)

格式转换

可以让 LLM 把一段数据在不同格式之间进行转换,如 JSON、XML 等

语法检查

这也是 LLM 一个主要应用场景,它可以帮你检查并校正语法或是拼写错误
text = [ "The girl with the black and white puppies have a ball.", # The girl has a ball. "Yolanda has her notebook.", # ok "Its going to be a long day. Does the car need it’s oil changed?", # Homonyms "Their goes my freedom. There going to bring they’re suitcases.", # Homonyms "Your going to need you’re notebook.", # Homonyms "That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms "This phrase is to cherck chatGPT for speling abilitty" # spelling ] for t in text: prompt = f"""Proofread and correct the following text and rewrite the corrected version. If you don't find and errors, just say "No errors found". Don't use any punctuation around the text: ```{t}```""" response = get_completion(prompt) print(response)

4. 扩写

扩展是指输入一小段文本(例如一系列说明或主题列表)并让大型语言模型生成一段较长的文本(例如电子邮件或关于某个主题的文章)的任务。扩展除了可以帮我们写一些报告之外,还可以用于作为头脑风暴的伙伴帮助我们获取灵感。
在执行扩写任务时,模型的 Temperature 参数会影响结果的探索和多样性。Temperature 是 GPT 模型的一个重要参数,它意味着模型对不同输出可能性的探索程度或随机性的高度。
ChatGPT 是通过不断预测下一个最可能出现的单词来构建输出。在图中的例子中,对于前文“我最喜欢的食物”,模型认为最可能出现在下一个词位置的三个单词分别是披萨、寿司、炸玉米饼,概率分别为 53%、30% 和 5% 。如果我们把温度设为 0,模型将始终将选择概率最高的比萨作为下一个词输出,而在较高的温度值下,模型则有机会选择出一些不太可能出现的词作为下个单词,例如炸玉米饼。
notion image
prompt = f""" You are a customer service AI assistant. Your task is to send an email reply to a valued customer. Given the customer email delimited by ```, \ Generate a reply to thank the customer for their review. If the sentiment is positive or neutral, thank them for \ their review. If the sentiment is negative, apologize and suggest that \ they can reach out to customer service. Make sure to use specific details from the review. Write in a concise and professional tone. Sign the email as `AI customer agent`. Customer review: \`\`\`{review}\`\`\` Review sentiment: {sentiment} """ response = get_completion (prompt) print (response)

5. 聊天机器人

ChatGPT 的 API 中是可以提供聊天的上文信息的,其中 messages 参数是一个列表,列表中的每一项包括一段话和说该段话的角色,通常角色有三个 system,assistant,user。
  • system 角色用于最初给模型定基调,提醒模型其作用
  • assistant 角色是模型返回的答复
  • user 角色代表用户每次的输入
notion image
通过维护一个 messages 列表便可实现一个聊天机器人。
messages = [ {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Hi, my name is Isa'}, {'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \ Is there anything I can help you with today?"}, {'role':'user', 'content':'Yes, you can remind me, What is my name?'} ] response = get_completion_from_messages(messages, temperature=1) print(response)