写好Prompt的几个必备技巧

参考资料:整理自Andrew Ng和openai合作出品的课程,原课程链接:https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/2/guidelines

原则一:编写明确而具体的指示

1. “少量示例”(“Few-shot”)提示法

prompt里给一些示例,能更好地让LLM按照你想要的方式执行任务。

下图中的get_completion 函数功能即引用openai的pip包进行预测,具体可以参考课程页面。

prompt = f"""
Your task is to answer in a consistent style.

<child>: Teach me about patience.

<grandparent>: The river that carves the deepest \ 
valley flows from a modest spring; the \ 
grandest symphony originates from a single note; \ 
the most intricate tapestry begins with a solitary thread.

<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)

模型结果:
在这里插入图片描述

2. 用分隔符来隔离prompt中的不同部分

可以用"“”, <>等分隔符。比如下图要求chatgpt对一段话做总结,其他指示正常输入,然后用“”“分隔符把需要总结的语句单独分隔出来。这样能帮助LLM更好的了解prompt的意图。

在这里插入图片描述

3. 要求结构化输出

比如JSON, HTML等格式

prompt = f"""
Generate a list of three made-up book titles along \ 
with their authors and genres. 
Provide them in JSON format with the following keys: 
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)

模型结果:
在这里插入图片描述

4. 要求模型检查是否满足条件

只有在满足条件的情况下才继续进行按照指示做任务,否则直接返回不满足条件

text_1 = f"""
Making a cup of tea is easy! First, you need to get some \ 
water boiling. While that's happening, \ 
grab a cup and put a tea bag in it. Once the water is \ 
hot enough, just pour it over the tea bag. \ 
Let it sit for a bit so the tea can steep. After a \ 
few minutes, take out the tea bag. If you \ 
like, you can add some sugar or milk to taste. \ 
And that's it! You've got yourself a delicious \ 
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes. 
If it contains a sequence of instructions, \ 
re-write those instructions in the following format:

Step 1 - ...
Step 2 - …
…
Step N - …

If the text does not contain a sequence of instructions, \ 
then simply write \"No steps provided.\"

"""{text_1}"""
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)

模型结果:
在这里插入图片描述

原则二:给模型时间“思考”

1、指定完成任务所需的步骤

text = f"""
In a charming village, siblings Jack and Jill set out on \ 
a quest to fetch water from a hilltop \ 
well. As they climbed, singing joyfully, misfortune \ 
struck—Jack tripped on a stone and tumbled \ 
down the hill, with Jill following suit. \ 
Though slightly battered, the pair returned home to \ 
comforting embraces. Despite the mishap, \ 
their adventurous spirits remained undimmed, and they \ 
continued exploring with delight.
"""
# example 1
prompt_1 = f"""
Perform the following actions: 
1 - Summarize the following text delimited by triple \
backticks 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.

Separate your answers with line breaks.

Text:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)

2、让模型在得出结论之前先自己解决问题

比如下图中,给定数学题,让LLM判断学生答案对错,直接让gpt判断,会判断学生答案正确(实际是错误的)。这种情况下,如果prompt中让gpt自己先给出正确答案,再对比学生答案,就能得到正确的结果。

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)

模型结果:
在这里插入图片描述

如何减少幻觉(hallucination)现象

指看起来很有道理实际上毫无根据的一些回答。
解决方案是,可以在prompt中先让LLM去找相关资料,找不到资料就直接回复无法找到。

在这里插入图片描述

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐