让你的prompt发挥魔力!OpenAI官方推荐的Prompt Engineering方法

优化策略

策略1: 清楚的指示

  • 输出的长度
  • 专业性
  • 格式
  • 。。。

技巧1: 在输入中增加细节以获得更相关的回答

Worse Better
How do I add numbers in Excel How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called “Total”

技巧2: 角色扮演(不是

调接口时System和User分别给予prompt

response = client.chat.completions.create(
    messages=[
      	{
            "role": "system",
            "content": "When I ask for help to write something, you will reply with a document that contains at least one joke about them in every paragraph",
        },
        {
            "role": "user",
            "content": "Write a thank you note to my steel bolt vendor for getting the delivery in on time and in short notice",
        },
    ],
    model="gpt-3.5-turbo",
)

技巧3: 使用分隔符来清楚地区分输入的不同部分

SYSTEM You will be provided with a pari of articles(delimited with XML tags) about the same topic. First summarize the arguments of each article. Then indicate which of them makes a better argument and explain why
USER
insert first article here

insert second article here

技巧4: 明确完成任务需要的步骤

SYSTEM Use the following step-by-step instructions to respond to user inputs.
Step1 - The user will provide you with text in triple quotes. Summarize this text in one sentence with a prefix that says “Summary:”
Step2 - Translate the summary from Step1 into Chinese, with a prefix that says “Translation”
USER “”“insert text here”“”

技巧5: 提供例子

few-shot prompting

技巧6: 明确期望的输出长度

Summarize the text delimited by triple quotes in about 50 words

策略2: 提供参考文本

技巧1: 指示大模型用参考文本回答

SYSTEM Use the provided articles delimited by triple quotes to answer questions. If the answer cannot be found in the articles, write “I cound not find an answer”
User
Question:

技巧2: 指示大模型用制定参考文本中的引用来回答

策略3: 把复杂任务分解为较简单的子任务

技巧1: 使用意图分类来识别与用户输入最相关的指令

eg: 首先,

子任务1: 根据用户的输入对问题进行分类

System:

You will be provided with customer service queries. Classify each query into a primary category and a secondary category. Provide your output in json format with the keys: primary and secondary.

Primary categories: Billing, Technical Support, Account Management, or General Inquiry.

Billing secondary categories:

  • Unsubscribe or upgrade
  • Add a payment method
  • Explanation for charge
  • Dispute a charge

Technical Support secondary categories:

  • Troubleshooting
  • Device compatibility
  • Software updates

Account Management secondary categories:

  • Password reset
  • Update personal information
  • Close account
  • Account security

General Inquiry secondary categories:

  • Product information
  • Pricing
  • Feedback
  • Speak to a human

USER: I need to get my Internet working again

子任务2: 根据上一个子任务的结果输出解决办法

**SYSTEM:**You will be provided with customer service inquiries that require troubleshooting in a technical support context. Help the user by:

  • Ask them to check that all cables to/from the router are connected. Note that it is common for cables to come loose over time.
  • If all cables are connected and the issue persists, ask them which router model they are using
  • Now you will advise them how to restart their device:
    – If the model number is MTD-327J, advise them to push the red button and hold it for 5 seconds, then wait 5 minutes before testing the connection.
    – If the model number is MTD-327S, advise them to unplug and replug it, then wait 5 minutes before testing the connection.
  • If the customer’s issue persists after restarting the device and waiting 5 minutes, connect them to IT support by outputting {“IT support requested”}.
  • If the user starts asking questions that are unrelated to this topic then confirm if they would like to end the current chat about troubleshooting and classify their request according to the following scheme:

技巧2: 需要长对话的对话应用,可以总结或过滤之前的对话

可以达到模型输入上限前总结,也可以用emmbeddings

技巧3: 分段总结长文档并递归地构建完整的摘要

策略4: 给模型“考虑”的时间

技巧1: 对于一些问题,先指示模型计算自己的结果,之后再问用户的的结果是否正确

SYSTEM
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.
USER
Problem Statement: 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.

  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
    ASSISTANT
    Let x be the size of the installation in square feet.
  4. Land cost: 100x
  5. Solar panel cost: 250x
  6. Maintenance cost: 100,000 + 10x
    Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000

The student’s solution is incorrect. They made an error in the maintenance cost calculation, using 100x instead of 10x. The correct total cost for the first year of operations is 360x + 100,000.

技巧2: 使用 “Inner monologue" 或一系列的queries 来隐藏推理部分(和上一个技巧相反)

在不想给用户展示推理过程的时候使用这一技巧

SYSTEM
Follow these steps to answer the user queries.

Step 1 - First work out your own solution to the problem. Don’t rely on the student’s solution since it may be incorrect. Enclose all your work for this step within triple quotes (“”").

Step 2 - Compare your solution to the student’s solution and evaluate if the student’s solution is correct or not. Enclose all your work for this step within triple quotes (“”").

Step 3 - If the student made a mistake, determine what hint you could give the student without giving away the answer. Enclose all your work for this step within triple quotes (“”").

Step 4 - If the student made a mistake, provide the hint from the previous step to the student (outside of triple quotes). Instead of writing “Step 4 - …” write “Hint:”.
USER
Problem Statement:

Student Solution:

技巧3: 在模型给出答案后让模型自己检查

**USER:**Are there more relevant excerpts? Take care not to repeat excerpts. Also ensure that excerpts contain all relevant context needed to interpret them - in other words don’t extract small snippets that are missing important context.

策略5: 使用外部工具

技巧1: 使用emmbeding

技巧2: 使用代码执行得到结果

SYSTEM

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

USER

Find all real-valued roots of the following polynomial: 3x**5 - 5x4 - 3*x3 - 7*x - 10.

技巧3: Function calling

策略6: 系统地测试变化

让你的prompt发挥魔力!OpenAI官方推荐的Prompt Engineering方法_第1张图片

技巧1: 对比金标准答案来评估模型的输出

SYSTEM

You will be provided with text delimited by triple quotes that is supposed to be the answer to a question. Check if the following pieces of information are directly contained in the answer:

  • Neil Armstrong was the first person to walk on the moon.
  • The date Neil Armstrong first walked on the moon was July 21, 1969.
  • For each of these points perform the following steps:
    • 1 - Restate the point.
    • 2 - Provide a citation from the answer which is closest to this point.
    • 3 - Consider if someone reading the citation who doesn’t know the topic could directly infer the point. Explain why or why not before making up your mind.
    • 4 - Write “yes” if the answer to 3 was yes, otherwise write “no”. Finally, provide a count of how many “yes” answers there are. Provide this count as {“count”: }.

你可能感兴趣的:(prompt,人工智能,python,gpt-3,chatgpt)