OpenAI Prompt Engineering-7. 文本扩写

OpenAI的Prompt Engineering 课程

6. 文本扩写

扩写指的是将一篇较短的文本进行扩写工作,比如根据一个指令或者一个主题列表让大语言模型生成一个较长的文本。下面我们将通过一个案例来说明如何使用大语言模型生成一个基于某些信息的个性化电子邮件。 另外我们还将引入一个新的模型参数temperature(温度),来改变模型在探索和多样性方面的程度。

在开始案例之前,我们先优化一下之前的调用DeepSeek API的接口函数,以便支持参数temperature

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def get_completion(api_key, prompt, model="deepseek-chat"):
if not api_key:
raise ValueError("API Key is missing.")
if not prompt:
raise ValueError("Prompt is missing.")
messages = [{"role": "user", "content": prompt}]
client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")

"""
我们建议您根据如下表格,按使用场景设置 temperature。
场景 温度
代码生成/数学解题 0.0
数据抽取/分析 1.0
通用对话 1.3
翻译 1.3
创意类写作/诗歌创作 1.5
"""
response = client.chat.completions.create(
model=model,
messages=messages,
stream=False,
temperature=0,
)

return response.choices[0].message.content

我查阅了 DeepSeek 的文档,文档里对temperature有明确的设置建议,我们可以参考一下。

现在我们开始案例演示,我们要要为客户的评论写一个自定义的电子邮件回复,所以先给定一个客户评论和情绪,我们将生成一个自定义的回复。我们给的指令是:你是一个客户服务的 AI 助理,你的任务是给一个有价值的客户发送电子邮件回复,如果情绪是积极或者中性的那么就感谢用户的评论,如果情绪是消极的,那么就道歉,并建议用户联系客户服务部。并以 AI 客户代理的身份在邮件中签名。(Isa Fulford说告知用户这是 AI 生成的,并让用户知道保持透明是很重要的😄),我们先将temperature设置为 1.0。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# and the original customer message, customize the email
sentiment = "negative"
# review for a blender
review = f"""
So,they still had the 17 piece system on seasonal\
sale for around $49 in the month of November,about\
half off, but for some reason (call it price gouging)
around the second week of December the prices all went
up to about anywhere from between $70-$89 for the same
system. And the 11 piece system went up around $10 or
so in price also from the earlier sale price of $29.\
So it looks okay,but if you look at the base, the part
\
where the blade locks into place doesn't look as good \
as in previous editions from a few years ago, but I\
plan to be very gentle with it (example,I crush\
very hard items 1ike beans,ice,rice,etc.in the\
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making
smoothies, finely cut and freeze the fruits and\
vegetables (if using spinach-lightly stew soften the \
spinach then freeze until ready for use-and if making \
sorbet,use a small to medium sized food processor)\
that you plan to use that way you can avoid adding so!
much ice if atfall-when making your smoothie.\
After about a Year, the motor was making a funny noise.\
I called customer service but the warranty expired \
already,so I had to buy another one. FYI: The overal1\
quality
has gone done in these types of products,so\
they are kind of counting on brand recognition and\
consumer loyalty to maintain sales. Got it in about\
two days.
"""

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 cut to customer service.
Make sure to usefspecitic details trom the review.
write in a concise and professional tone.
Sign the email as
~AI customer agent.
Customer review:```{review}```
Review sentiment: {sentiment}
"""

来看看模型的输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
**Subject:** Thank You for Your Feedback  

Dear Valued Customer,

Thank you for taking the time to share your detailed review. We sincerely apologize for the concerns you’ve raised regarding the price changes, product quality, and warranty experience. Your feedback is important to us, and we regret that your experience did not meet your expectations.

We understand your frustration with the pricing adjustments and the durability of the product. Please know that we strive to offer fair pricing and high-quality items, and we’re sorry this wasn’t reflected in your case.

If you’d like to discuss this further, our customer service team would be happy to assist you. You can reach them at [customer service email/phone].

Thank you again for your honest feedback—it helps us improve.

Best regards,
~AI customer agent

下面我们来详细解释一下temperature的作用,假如对于我最喜欢的食物是,这个提示词模型预测最有可能的下一个词是披萨,接下来的两种可能是寿司和玉米饼,因此在temperature为 0 的情况下,模型总是会选择可能性最大的下一个词,比如在刚刚的例子是就是披萨。temperature在高一些可能会选择寿司,如果temperature更高一些,那么它有可能会选择玉米饼。所以说如果temperature设置的越高,每次拿到的响应结果的差异可能也越来越大。如果我们想保持结果的可预测性,那么建议将temperature设置为 0,在这节课程之前,我们一直使用的是默认temperature(这个是针对 OpenAI API而言的,我查了 DeepSeek 的文档,默认temperature是 1.0),如果想要建立一个可靠且可预测的系统,那么应该设置为 0。如果你想让模型更有创意,希望能有更多不同的输出 ,那么可以设置更高的temperature值。

7-1 temperature示意图

然后我还是使用上面使用 AI 给用户的评论回复电子邮件的案例来实验,我将temperature 设置为 0,然后多次发送了请求,得到的答案都是:

1
2
3
4
5
6
7
8
9
10
11
12
**Subject:** Thank You for Your Feedback  

Dear Valued Customer,

Thank you for taking the time to share your detailed review. We sincerely apologize for the frustrations you’ve experienced, including the price changes and concerns about the product’s quality and durability. Your feedback is important to us, and we regret that your experience did not meet expectations.

We’re sorry to hear about the issue with the motor and the expired warranty. If you’d like further assistance, please don’t hesitate to reach out to our customer service team—we’d be happy to help however we can.

We appreciate your honest input and will share it with our team to drive improvements. Thank you for your loyalty, and we hope to have the opportunity to serve you better in the future.

Best regards,
~AI customer agent

如果设置为 1.0,那么每次得到的回答都不太一样,至于有没有创意这个事情在这个案例中见仁见智吧。