> 本文内容是上一篇《使用 [Amazon Bedrock](https://aws.amazon.com/cn/bedrock/?trk=cndc-detail) 学习提示词工程系列分享(二)提示词需要迭代》的延续,同样基于deeplearning.ai 的《ChatGPT Prompt Engineering for Developers》课程整理而来。
### 代码
课程中的代码使用的是 OpenAI 的 GPT-3.5-turbo 模型, 在我们的例子中将替换为在 [Amazon Bedrock](https://aws.amazon.com/cn/bedrock/?trk=cndc-detail) 中托管的 Claude 3 Haiku 模型。
代码如下,您可以在 Notebook 中执行:
```js
import boto3
import json
bedrock = boto3.client(service_name='bedrock-runtime', region_name='us-west-2')
model_id = "anthropic.claude-3-haiku-20240307-v1:0"
text = f"""
"""
prompt_data = f"""
```{text}```
"""
response = bedrock.invoke_model(
modelId=model_id,
body=json.dumps(
{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1024,
"temperature": 0.8,
"top_p": 0.9,
"top_k": 200,
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": prompt_data}],
}
],
}
),
)
# Process and print the response
result = json.loads(response.get("body").read())
input_tokens = result["usage"]["input_tokens"]
output_tokens = result["usage"]["output_tokens"]
output_list = result.get("content", [])
print("Invocation details:")
print(f"- The input length is {input_tokens} tokens.")
print(f"- The output length is {output_tokens} tokens.")
print(f"- The model returned {len(output_list)} response(s):")
print("\\n")
for output in output_list:
print(output["text"])
```
### 总结类提示词
在本期文章中,我们将讨论大语言模型在应用程序中的一种非常常见的用途,即总结文本。
在当今技术飞速发展、信息爆炸的情况下,我们往往无法抽出足够的时间去阅读那些想要深入了解的内容。因此,大型语言模型的一个极具吸引力的应用场景就是为文本内容提供简洁的概述。这也正是许多团队将其集成到各种应用程序中的原因所在。
您可以在大型语言模型的 Web 界面中进行操作。许多人采用这种方法来概括文章,从而能够阅读到比以往更多的内容。如果您希望以编程方式来概括文章,您将在本课程中学习如何操作。
我们将以总结一款产品评论的任务为运行示例。电子商务网站每天都会有大量的评论,如果有一个工具来总结这些冗长的评论,就可以在更短的时间内快速浏览评论,以便更好地了解客户的想法。
下面是一段关于熊猫玩具的评论内容:
```js
prod_review = """
Got this panda plush toy for my daughter's birthday, who loves it and takes it everywhere. It's soft and super cute, and its face has a friendly look. It's a bit small for what I paid though. I think there might be other options that are bigger for the same price. It arrived a day earlier than expected, so I got to play with it myself before I gave it to her.
"""
```
#### 01 限制输出长度
这是一个生成摘要的提示词,指定模型的任务是生成一个电子商务网站上的产品评论的简短摘要,最多使用30个单词。
**提示词:**
```js
prompt_data = 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}```
"""
```
**Claude 3 Haiku 输出:**
Claude 3 Haiku 模型基于产品评论输出了一段简短的概述。
```js
This soft and cute panda plush toy was a hit with the reviewer's daughter. While slightly smaller than expected, it arrived early and has a friendly face.
```
#### 02 使输出结果关注特定领域
如果您对摘要内容有明确的要求,例如想要关注对于物流部门的反馈,您可以修改提示词来生成更适用于特定部门的摘要。要实现这一点,可以添加如下提示词:“Your task is to generate a short summary of a product review from an ecommerce site to give feedback to the Shipping deparmtment. ”
**提示词:**
```js
prompt_data = 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}```
"""
```
**Claude 3 Haiku 输出:**
这次生成的摘要把重点放在了商品比预期提前一天到达这部分和物流相关的内容,并指出这是一条正向评论。
```js
The panda plush toy arrived a day earlier than expected, which was a positive aspect of the shipping and delivery experience.
```
#### 03 使输出结果关注性价比
另一个例子中,如果我们不想收集对物流部门的反馈,而是为定价部门提供参考依据。定价部门的职责是为商品制定合适的价格,您可以指示大模型关注和性价比相关的信息。
**提示词:**
```js
prompt_data = f"""
Your task is to generate a short summary of a product review from an ecommerce site to give feedback to the pricing deparmtment, responsible for determining the price of the product.
Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects that are relevant to the price and perceived value.
Review: ```{prod_review}```
"""
```
**Claude 3 Haiku 输出:**
这次的输出关注了价格方面的评价,客户感觉对于这个价格来说产品有点小了,相同的价格也许能买到一个更大的玩具。
```js
The panda plush toy is soft, cute, and well-received, but it is perceived as small for the price. Considering larger options at the same price point may be advisable.
```
上面两个提示词生成了针对物流部门和定价部门的评价。您也可也尝试生成对于负责用户体验的产品部门的评价或者其他有意义的信息。
#### 04 尝试“提取”而不是“总结”
在前面的提示词中,虽然大模型生成了产品价格方面的评价,但同时也包含了其他信息,您可以使用“提取”而非“总结”,从而让大模型只输出和价格相关的信息。
**提示词:**
```js
prompt_data = f"""
Your task is to extract relevant information from a product review from an ecommerce site to give feedback to the pricing department.
From the review below, delimited by triple quotes extract the information relevant to price and perceived value. Limit to 30 words.
Review: ```{prod_review}```
"""
```
**Claude 3 Haiku 输出:**
这次的输出只提取了和价格相关的评价信息。
```js
The review indicates that the panda plush toy is "a bit small for what I paid" and suggests that there might be "other options that are bigger for the same price."
```
#### 05 总结多个产品评论
下面我们来看一个实际的例子,这个例子中会对多段超长的评价进行总结,以便在短时间内阅读更多的评论。
如下是完整代码:在代码中将5条超长的评论放到了列表里,循环进行总结。
```js
import boto3
import json
bedrock = boto3.client(service_name='bedrock-runtime', region_name='us-west-2')
model_id = "anthropic.claude-3-haiku-20240307-v1:0"
prod_review = """
Got this panda plush toy for my daughter's birthday, who loves it and takes it everywhere. It's soft and super cute, and its face has a friendly look. It's a bit small for what I paid though. I think there might be other options that are bigger for the same price. It arrived a day earlier than expected, so I got to play with it myself before I gave it to her.
"""
review_1 = prod_review
review_2 = """
Got this panda plush toy for my daughter's birthday, who loves it and takes it everywhere. It's soft and super cute, and its face has a friendly look. It's a bit small for what I paid though. I think there might be other options that are bigger for the same price. It arrived a day earlier than expected, so I got to play with it myself before I gave it to her.
"""
review_3 = """
Needed a nice lamp for my bedroom, and this one had additional storage and not too high of a price point. Got it fast - arrived in 2 days. The string to the lamp broke during the transit and the company happily sent over a new one. Came within a few days as well. It was easy to put together. Then I had a missing part, so I contacted their support and they very quickly got me the missing piece! Seems to me to be a great company that cares about their customers and products.
"""
review_4 = """
My dental hygienist recommended an electric toothbrush, which is why I got this. The battery life seems to be pretty impressive so far. After initial charging and leaving the charger plugged in for the first week to condition the battery, I've unplugged the charger and been using it for twice daily brushing for the last 3 weeks all on the same charge. But the toothbrush head is too small. I’ve seen baby toothbrushes bigger than this one. I wish the head was bigger with different length bristles to get between teeth better because this one doesn’t. Overall if you can get this one around the \$50 mark, it's a good deal. The manufacturer's replacements heads are pretty expensive, but you can get generic ones that are more reasonably priced. This toothbrush makes me feel like I've been to the dentist every day. My teeth feel sparkly clean!
"""
review_5 = """
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 like 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 at all-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 overall 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.
"""
reviews = [review_1, review_2, review_3, review_4, review_5]
for i in range(len(reviews)):
prompt_data = 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 20 words.
Review: ```{reviews[i]}```
"""
response = bedrock.invoke_model(
modelId=model_id,
body=json.dumps(
{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1024,
"temperature": 0.8,
"top_p": 0.9,
"top_k": 200,
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": prompt_data}],
}
],
}
),
)
result = json.loads(response.get("body").read())
output_list = result.get("content", [])
for output in output_list:
print(output["text"])
```
**Claude 3 Haiku 输出:**
程序循环输出了对于几段评价的简短总结。您可以用同样的方式来构建一个界面来对大量的评价生成简短的总结以便更快地浏览这些评论,并在需要的时候去查看原始评论。通过这种方式可以高效的了解所有客户的真实想法。
```js
Panda plush toy is a hit with daughter, though a bit small for the price; arrived earlier than expected.
Soft, cute panda plush toy delights daughter, but slightly small for price; arrived earlier than expected.
Excellent customer service and quality lamp with storage, despite initial transit issue. Prompt delivery and responsive support.
The electric toothbrush has impressive battery life, but the small head size is a drawback. Overall, it's a good value if purchased around \$50.
The review discusses a blender system that was on sale for \$49 but later increased to \$70-\$89, with the 11-piece system also increasing in price. The reviewer notes quality concerns and a motor issue after a year, suggesting a decline in overall product quality.
```
以上就是总结类提示词,您可以使用这些提示词来总结大量的文本信息,以便快速了解文本的内容。