使用 Amazon Bedrock 学习提示词工程系列分享(四)推理类提示词

Python
API
JSON
大语言模型
Amazon Bedrock
0
0
> 本文内容是上一篇《使用 [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"]) ``` ### 推理类提示词 本期要分享的是推理类提示词,模型接受文本作为输入并进行推理分析。这可能是提取标签、提取名称、理解文本的情感等。在传统的[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)工作流程中,如果您想从一段文本中提取正面或负面的情感,您必须收集标签数据集,训练模型,部署模型并进行推理。这样做可能效果不错,但整个过程工作量非常大。而且对于每个任务,如情感分析、提取名称或其他任务,您都需要训练并部署单独的模型。 如果使用大语言模型来完成类似的任务,您只需要编写提示词即可马上生成结果,极大地提高了应用开发的速度。而且您只需要使用一个模型、调用 API 即可完成许多不同的任务,而不需要训练和部署许多不同的模型。 #### 1、产品评价文本情感分类 下面的例子使用的是一个台灯的评论,评论显示这个台灯本身并不完美,但这位顾客似乎对过程很满意,并觉得这是一家关心顾客和产品的好公司。 ```js lamp_review = """ Needed a nice lamp for my bedroom, and this one had additional storage and not too high of a price point. Got it fast. The string to our 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. I had a missing part, so I contacted their support and they very quickly got me the missing piece! Lumina seems to me to be a great company that cares about their customers and products!! """ ``` 下面编写一个提示词来分析这个评论的情感类别。 **提示词:** ```js prompt_data = f""" What is the sentiment of the following product review, which is delimited with triple backticks? Review text: '''{lamp_review}''' """ ``` **Claude 3 haiku 输出:** ```js Based on the review text provided, the sentiment appears to be positive. The review highlights several positive aspects of the product and the company's customer service: - The lamp met the reviewer's needs and had a good price point. - The delivery was fast. - When the lamp had an issue during transit, the company promptly sent a replacement. - The replacement part was also delivered quickly when the reviewer contacted support. - The reviewer concludes by stating that Lumina seems to be a great company that cares about their customers and products. Overall, the review conveys a very favorable sentiment towards the product and the company's responsiveness and customer service. ``` 结果显示该产品评论的情感是积极的,大模型做出来准确的识别。对于客户支持工作来讲,了解某个特定用户是否生气是很重要的,这可以帮助他们采取及时有效的措施来解决问题。 上面的输出很详细,但是似乎太长了,下面我们让大模型直接输出这是一条积极的评论还是消极的评论: ```js prompt_data = f""" What is the sentiment of the following product review, which is delimited with triple backticks? Give your answer as a single word, either "positive" or "negative". Review text: '''{lamp_review}''' """ ``` **Claude 3 haiku 输出:** ```js positive ``` #### 2、文本情感分类 您可能有一个情感分类问题,比如这篇评论的作者是在表达愤怒吗?因为如果真的有顾客生气了,那可能就值得额外的关注,引入客户支持或客户成功部门了解情况并为客户解决问题。如果使用监督学习来构建分类模型,这又是一项复杂的任务。而通过大语言模型,您只需要更改一些提示词,询问客户所表现出来的情绪。 **提示词:** ```js prompt_data = 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}''' """ ``` **Claude 3 haiku 输出:** ```js The emotions expressed in the review are: gratitude, satisfaction, appreciation, relief, happiness ``` 您也可以直接让大模型识别客户是否表现出愤怒的情绪: ```js prompt_data = f""" Is the writer of the following review expressing anger? The review is delimited with triple backticks. Give your answer as either yes or no. Review text: '''{lamp_review}''' """ ``` **Claude 3 haiku 输出:** ```js No, the writer of the review is not expressing anger. The review is overall positive, highlighting the fast delivery, easy assembly, and good customer service from the company. br ``` #### 3、文本信息提取 您还可以基于大语言模型做更多的事情,特别是从客户评论中获取更丰富的信息。信息抽取是自然语言处理中的一部分,可以从文本中提取你想要的信息。 ##### 3.1 提取产品和公司名称 在下面这个提示词中,要求大模型识别以下信息:购买的商品和制造商的名称。如果您正在尝试总结来自电子商务网站的评论,可能需要大量评论来确定商品名称、制造商名称、正面和负面情感,以跟踪顾客对于各个商品或制造商的积极或消极情绪的变化趋势,这可以帮助企业了解消费者对产品或品牌的整体评价和反馈。 在这个例子中,将要求大模型将响应格式化为 JSON 对象,并将商品和品牌作为 Key 值。 **提示词:** ```js 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: '''{text}''' ``` **Claude 3 haiku 输出:** ```js { "Item": "lamp", "Brand": "Lumina" } ``` 以上是输出内容:商品是台灯,品牌是 Lumina,您可以轻松地将其加载到 Python 字典数据结构中,然后对此输出进行额外的处理。 在上面的示例中,展示了如何编写提示词来识别情感、确定是否有人生气,并提取商品和品牌。 ##### 3.2 一次性执行多个任务 同时提取多种信息的一种方法是使用多个提示词并多次执行,但实际上您可以编写一个单一的提示来同时提取所有的信息。比如:提取商品评论的情感倾向、评论者是否表达出愤怒情绪、是否商品了购买等等。并将是否表现出愤怒情绪格式化为布尔值。 ```js prompt_data = f""" Identify the following items from the review text: - Sentiment (positive or negative) - Is the reviewer expressing anger? (true or false) - Item purchased by reviewer - Company that made the item The review is delimited with triple backticks. Format your response as a JSON object with "Sentiment", "Anger", "Item" and "Brand" as the keys. If the information isn't present, use "unknown" as the value. Make your response as short as possible. Format the Anger value as a boolean. Review text: '''{lamp_review}''' """ ``` **Claude 3 haiku 输出:** ```js { "Sentiment": "positive", "Anger": false, "Item": "lamp", "Brand": "Lumina" } ``` 大模型输出了一个 JSON:情感为正面;愤怒值为 false,没有加引号,因为要求将其输出为布尔值;商品是台灯;品牌是 Lumia。通过这种方式,您可以使用一个提示词从一段文本中提取多个字段。 #### 4、文本主题推断 大语言模型的一个很棒的应用是推断主题。给定一段文章,推断出这篇文章是关于什么的?涉及到哪些主题?下面是一篇虚构的报纸文章,描述了政府工作人员对他们所在的政府机构的看法和感受。 ```js story = """ In a recent survey conducted by the government, public sector employees were asked to rate their level of satisfaction with the department they work at. The results revealed that NASA was the most popular department with a satisfaction rating of 95%. One NASA employee, John Smith, commented on the findings, stating, "I'm not surprised that NASA came out on top. It's a great place to work with amazing people and incredible opportunities. I'm proud to be a part of such an innovative organization." The results were also welcomed by NASA's management team, with Director Tom Johnson stating, "We are thrilled to hear that our employees are satisfied with their work at NASA. We have a talented and dedicated team who work tirelessly to achieve our goals, and it's fantastic to see that their hard work is paying off." The survey also revealed that the Social Security Administration had the lowest satisfaction rating, with only 45% of employees indicating they were satisfied with their job. The government has pledged to address the concerns raised by employees in the survey and work towards improving job satisfaction across all departments. """ ``` 基于这篇文章,我们可以编写提示词来确定文本中正在讨论的五个主题。并且把每个项目简化为一个或两个单词,并以逗号分隔的列表形式返回。 ```js prompt_data = f""" Determine five topics that are being discussed in the following text, which is delimited by triple backticks. Make each item one or two words long. Format your response as a list of items separated by commas. Text sample: '''{story}''' """ ``` **Claude 3 haiku 输出:** ```js Government, Public sector, NASA, Satisfaction, Job satisfaction br ``` 以上是大模型的输出,推理出这篇文章是关于关于工作满意度,涉及 NASA 等等。从文章中提取主题列表是一个不错的做法,您还可以将主题列表进一步细分,比如提取前5个主要主题。如果有一系列文章,还可以利用大型语言模型来帮助索引不同的主题。 #### 5、创建新闻提醒或订阅 假设有一个新闻网站或其他机构,关注的主题有 NASA、地方政府、工程、员工满意度、联邦政府等,需要确定一篇文章涉及了哪些主题。可以使用如下提示词, 要求大模型给出每个主题在文章中是否出现的标记(0或1)。 ```js 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: nasa, local government, engineering, employee satisfaction, federal government Text sample: '''{story}''' ``` **Claude 3 haiku 输出:** ```js Here is the list with 0 or 1 for each topic: nasa: 1 local government: 0 engineering: 0 employee satisfaction: 1 federal government: 1 ``` 本文中所使用的文章涉及了 NASA、员工满意度和联邦政府这三个主题,而没有涉及地方政府和工程。可以看到大模型给出了正确的输出。这种不需要事先标注训练数据就能进行预测的方法,在[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)中被称为"零样本学习"(zero shot learning)。通过这种方式可以快速准确地确定新闻文章中所涉及的主题,如果有关注的内容就可以进行提醒。 以上就是推理类提示词,您可以在 notebook 中运行并调整提示词让大语言模型输出想要的内容。 **前述特定亚马逊云科技生成式人工智能相关的服务仅在亚马逊云科技海外区域可用,亚马逊云科技中国仅为帮助您了解行业前沿技术和发展海外业务选择推介该服务。*
0
目录
关闭