技术分享 | 使用 Amazon Bedrock 学习提示词工程系列分享(二)提示词需要迭代

HTML
Amazon Bedrock
0
0
> 本文内容是上一篇《[Amazon Bedrock](https://aws.amazon.com/cn/bedrock/?trk=cndc-detail) Claude 3 Opus 模型抢先体验》的延续,同样基于 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"]) ``` ### 初始提示词 在使用大型语言模型构建应用程序时,通常很难在第一次尝试时就编写出能够在应用程序中使用的提示词,这很正常,重要的是要有一个迭代过程来改进它,直到提示词能够最终完成特定的任务。 ![image.png](https://dev-media.amazoncloud.cn/39d4ff1b6f5b4bb6b4dce448f66646d8_image.png "image.png") 图片源自网络 编写能够最终应用到大模型应用程序中的提示词的过程和模型训练的过程非常相似: 1、指定想要完成的任务。 2、结合上一篇文章中讲到的原则,尝试编写一个提示词并执行它,看看得到的结果。 3、如果第一次效果不够好,就需要进行迭代,找出是否指令不够清晰,或者是否没有给模型足够的时间来思考等等,并逐渐完善。 4、循环这个过程,直到最终得到能适用于应用程序的提示词。 来看一个例子: 这是一张关于一款椅子的信息清单,清单中列出了和椅子相关的主要信息。比如尺寸,型号,材料等等。 ```js fact_sheet_chair = """ OVERVIEW - Part of a beautiful family of mid-century inspired office furniture, including filing cabinets, desks, bookcases, meeting tables, and more. - Several options of shell color and base finishes. - Available with plastic back and front upholstery (SWC-100) or full upholstery (SWC-110) in 10 fabric and 6 leather options. - Base finish options are: stainless steel, matte black, gloss white, or chrome. - Chair is available with or without armrests. - Suitable for home or business settings. - Qualified for contract use. CONSTRUCTION - 5-wheel plastic coated aluminum base. - Pneumatic chair adjust for easy raise/lower action. DIMENSIONS - WIDTH 53 CM | 20.87” - DEPTH 51 CM | 20.08” - HEIGHT 80 CM | 31.50” - SEAT HEIGHT 44 CM | 17.32” - SEAT DEPTH 41 CM | 16.14” OPTIONS - Soft or hard-floor caster options. - Two choices of seat foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3) - Armless or 8 position PU armrests MATERIALS SHELL BASE GLIDER - Cast Aluminum with modified nylon PA6/PA66 coating. - Shell thickness: 10 mm. SEAT - HD36 foam COUNTRY OF ORIGIN - Italy """ ``` 在线零售网站的营销人员需要用这些数据编写产品描述。大模型的任务就是帮助营销团队完成这项工作,我们来执行如下提示词看看效果: ```js prompt_data = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. Technical specifications: ```{fact_sheet_chair}``` """ ``` 下面是第一次尝试的提示词生成的结果,模型已经很好地完成了根据技术数据表编写产品描述的任务。但问题是,这个描述太长了。 **Claude 3 Haiku 输出:** ```js Introducing the SWC-100 and SWC-110 chairs, part of a stunning mid-century inspired office furniture collection. These versatile chairs are designed to elevate your workspace, whether it's a home office or a professional setting. Crafted with meticulous attention to detail, the SWC chairs feature a sleek and modern aesthetic. With several shell color and base finish options, including stainless steel, matte black, gloss white, and chrome, you can customize the look to perfectly complement your existing decor. Comfort is at the forefront of the SWC design. The chairs are available with either plastic back and front upholstery (SWC-100) or full upholstery (SWC-110), offering a range of fabric and leather options to suit your personal style. The pneumatic chair adjustment allows for easy raise and lower action, ensuring a comfortable seating position throughout the day. Designed for both home and business settings, the SWC chairs are qualified for contract use, making them a versatile choice for a variety of environments. The 5-wheel plastic-coated aluminum base provides stability and mobility, while the soft or hard-floor caster options ensure a smooth ride across different surfaces. Attention to detail is evident in the construction of the SWC chairs. The shell is made of cast aluminum with a modified nylon PA6/PA66 coating, ensuring durability and a sleek, contemporary look. The seat is crafted with high-density HD36 foam for superior comfort and support. Whether you choose the armless or 8-position PU armrest option, the SWC chairs offer customizable features to suit your individual needs. With a width of 53 cm, a depth of 51 cm, and a height of 80 cm, these chairs are designed to provide ample support and comfort for a wide range of users. Crafted in Italy, the SWC chairs exemplify the best of mid-century inspired design, blending timeless elegance with modern functionality. Elevate your workspace and experience the perfect balance of style and comfort with the SWC-100 and SWC-110 chairs. ``` ### 问题一:生成文字过长 提示词第一次尝试遇到的问题是生成的结果过长,您可能希望它更短一些。所以我们需要更新提示词,增加一句话“最多使用50个单词”来限制输出的长度。 ```js prompt_data = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. Use at most 50 words. Technical specifications: ```{fact_sheet_chair}``` """ ``` 这一次模型输出了一段更加简洁清晰的介绍。 Claude 3 Haiku 输出: ```js Elevate your workspace with our mid-century inspired office chair, offering a range of customizable options to suit your style and comfort needs. Crafted with premium materials and available in various shell colors and base finishes, this versatile chair is perfect for both home and business settings. ``` 您也可以使用其他方式限制长度,比如“最多使用三个句子”来限制生成的结果。 ```js prompt_data = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. Use at most 3 centences. Technical specifications: ```{fact_sheet_chair}``` """ ``` ### 问题二:生成文字关注错误的细节 大模型已经按照我们的要求输出了简洁的产品描述,但是如果这个网站的客户不是最终的消费者,而是面向家具零售商,那这段输出的描述就是不合适的,因为和消费者相比,零售商会更关心产品的技术细节和材料等信息。那么,我们需要修改提示词,让它输出更多的技术和材料方面的细节。下面这段提示词指示大模型这段描述针对的是家具零售商,所以它应该关注技术参数和产品的材料。 ```js prompt_data = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. Use at most 50 words. Technical specifications: ```{fact_sheet_chair}``` """ ``` 下面是新的输出,更多的关注了技术参数和材料方面的信息,铝合金外壳和塑料涂层底座等等。所以通过迭代提示词,可以让它更专注于特定的特征 **Claude 3 Haiku 输出:** ```js This mid-century inspired office chair is constructed with a cast aluminum shell coated in a modified nylon PA6/PA66 for durability. The 5-wheel plastic coated aluminum base provides stability and adjustability, making it suitable for both home and business settings ``` 下面让我们继续优化提示词。 在描述的结尾包括产品 ID,这款椅子的两种配置 SWC 110,SOC 100。可以提示模型在描述的最后添加7个字符的产品 ID。 ```js prompt_data = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. At the end of the description, include every 7-character Product ID in the technical specification. Use at most 50 words. Technical specifications: ```{fact_sheet_chair}``` """ ``` 可以看到最终大模型按照我们的提示生成了期望的结果。在产品描述的后面输出了产品 ID。 ```js Crafted with a cast aluminum shell and a 5-wheel plastic coated aluminum base, this mid-century inspired office chair offers a perfect blend of style and durability. Available in a range of shell colors and base finishes, it is suitable for both home and business settings. Product IDs: SWC-100, SWC-110. ``` ### 问题三:产品描述中需要包含尺寸表 您在各种应用程序中看到的成功的提示词都是通过这样的迭代过程最终得到的。下面让我们再看一个更加复杂的例子。在这个例子中要求模型在输出描述之后,输出包含产品尺寸的表格,然后将其格式化为 HTML。 ```js prompt_data = f""" Your task is to help a marketing team create a description for a retail website of a product based on a technical fact sheet. Write a product description based on the information provided in the technical specifications delimited by triple backticks. The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. At the end of the description, include every 7-character Product ID in the technical specification. After the description, include a table that gives the product's dimensions. The table should have two columns. In the first column include the name of the dimension. In the second column include the measurements in inches only. Give the table the title 'Product Dimensions'. Format everything as HTML that can be used in a website. Place the description in a <div> element. Technical specifications: ```{text}``` """ ``` 模型按照要求生成了 HTML 格式的结果。实际上,您可能需要在多次迭代后才能得到一个这样的提示词。很少有人能够一开始就写出这样的提示词来处理数据表,并且输出 HTML 格式。 **Claude 3 Haiku 输出:** ```js <div> The SWC Series of mid-century inspired office chairs is constructed with high-quality materials for exceptional durability and comfort. The chair's shell is made from a cast aluminum base with a modified nylon PA6/PA66 coating, providing a sleek and durable finish. The seat is crafted with HD36 high-density foam, ensuring long-lasting support and comfort. The chair is available in a variety of shell color and base finish options, including stainless steel, matte black, gloss white, and chrome, allowing you to customize the chair to suit your office or home decor. Additionally, the chair can be upholstered with either plastic or full upholstery in a range of fabric and leather options. The SWC Series chair features a 5-wheel plastic-coated aluminum base and a pneumatic adjustment mechanism for easy height adjustment, making it suitable for both home and business settings. The chair is also qualified for contract use, ensuring it meets the necessary standards for commercial environments. The available Product IDs for the SWC Series chairs are: SWC-100 and SWC-110. </div> <table title="Product Dimensions"> <tr> <th>Dimension</th> <th>Measurement (inches)</th> </tr> <tr> <td>Width</td> <td>20.87</td> </tr> <tr> <td>Depth</td> <td>20.08</td> </tr> <tr> <td>Height</td> <td>31.50</td> </tr> <tr> <td>Seat Height</td> <td>17.32</td> </tr> <tr> <td>Seat Depth</td> <td>16.14</td> </tr> </table> ``` 我们通过下面的代码来检验一下这是不是一个有效的 HTML。 ```js from IPython.display import display, HTML display(HTML(output["text"])) ``` **Claude 3 Haiku 输出:** ![image.png](https://dev-media.amazoncloud.cn/1e5cbeab2c224188ad1d329983c13880_image.png "image.png") 可以看到输出结果非常理想。 在本篇内容中,我们只用了一个例子来说明如何迭代一个提示词。对于更复杂的应用程序,会需要更多的数据,比如说10个甚至50个或者100个产品数据表,然后针对大量的案例来迭代完善提示词。 对于更成熟的应用程序,需要更多的示例来对提示词进行评估,例如在数十个数据表上测试不同的提示词的表现如何。但通常只有在应用程序更成熟并且必须使用这些指标来推动提示词改进的最后几个步骤时才会这样做。 好了,通过上面的内容我们学习了提示词的迭代。许多开发人员都会经历这个提示词的迭代过程。多尝试一些提示词,看它是否给出了期望的结果,然后思考如何迭代提示词。一个好的提示词工程师并不在于一开始就写出完美的提示词,而在于有一个完善的迭代过程来不断优化它。
0
目录
关闭