在 Amazon Bedrock 上使用 Anthropic Claude 系统 Prompt

API
生成式人工智能
Amazon Bedrock
0
0
系统 prompt 是定义生成式 AI 模型对用户输入的响应策略的一种好方法。这篇博文将介绍什么是系统 prompt,以及如何在基于 Anthropic Claude 2.x 和 3 的应用中使用系统 prompt。 Amazon Bedrock 生成式人工智能编程 你知道和 Amazon Bedrock 上的 Anthropic Claude 模型互动时可以使用系统prompt 吗? 下面我们将首先了解一下系统 prompt 的概念,然后演示在 Claude 2.x 的文本补全 API 和 Claude 3 的新消息 API 中如何运用系统 prompt。 ⚠ 友情提示:本文包含示例代码。 如果你对系统 prompt 和 Claude 的两种 API 已有一定了解,可以直接跳到代码部分。文章末尾,我提供了两个完整的示例代码。 ### 什么是系统 prompt? 今天我们讨论的是 Claude,所以我们来看看 Anthropic 对系统 prompt 的解释: *系统 prompt 是在向 Claude 提出问题或下达任务之前提供上下文、指令和指南的一种方式。你可以使用系统 prompt 来为对话设定背景,包括 Claude 的角色、个性、语气或任何其他有助于其更好理解和响应用户输入的相关信息。* 简而言之,系统 prompt 实际上是一种上下文学习模式,可以有效地定义模型在交互过程中的上下文、范围、边界和输出格式。 使用系统 prompt 的目的是确保 AI 输出符合特定领域的目标或任务要求。以下是一些典型的使用场景: - **处理比萨订单**:将模型的作用范围指定为根据比萨餐厅的菜单和位置接单。 - **作为技术支持,协助排查故障**:向模型中输入产品详情、常见问题及答案和决策树等内容,帮助用户解决技术问题。 - **代码调试**:向模型中输入有关库、框架和编程语言版本的信息,帮助识别软件漏洞并提出修复建议。 ### Amazon Bedrock 支持的两种 Claude API 现在,你应该对系统 prompt 有一定了解了。下面,让我们看看 Amazon Bedrock 上不同版本的 Claude 所提供的两种 API: - 文本补全 API:Claude 1 和 2.x 版本均支持文本补全 API。 - 消息 API:新的 Claude 3 版本引入了消息 API。 **文本补全 API** Anthropic Claude 1 和 2 版本在 Amazon Bedrock 上线后,Claude 模型便向亚马逊云科技客户全面开放,并随后在 re:Invent 2023 中推出了 Claude 2.1。2.1 及之前版本的 Claude 模型均提供了**文本补全 API**。这个 API 根据用户提供的 prompt 优化单轮文本生成。Prompt 模版如下: *备注:文章末尾提供了完整的代码示例以供参考。* ```js # The prompt format for the text completion API (Claude 1 and 2.x) user_prompt = "Tell me a story." prompt = "Human: " + user_prompt + "\n\nAssistant:" ``` 我在 Claude v2.1 上使用这个 prompt 后,我得到了如下的输出: *The young boy wandered into the dark, mysterious forest, hoping to find the rare flower his mother needed to recover from her illness, but instead encountered a wise old owl who offered him cryptic advice about believing in himself.(一个小男孩走进了一处幽暗而神秘的森林,希望找到能让他母亲康复的稀有花朵,但却遇到了一只智慧的老猫头鹰,劝诫他要相信自己。)* **在文本补全 API 中使用系统 Prompt** 只需将系统 prompt 添加到用户 prompt 之前即可。 ```js # Using a system prompt with the text completion API (Claude 1 and 2.x) system_prompt = "All your output must be pirate speech 🦜" user_prompt = "Tell me a story." prompt = "System:" + system_prompt + "\n\nHuman: " + user_prompt + "\n\nAssistant:" ``` 向 Claude 2.1 发送这个 prompt 后,得到了如下输出: *Yarrr, 'twas a dark 'n stormy night when Blackbeard 'n his scurvy crew set sail on the seven seas, plunderin' merchant ships fer pieces of eight 'n fine silks, before returnin' to Tortuga fer a night of rum-filled debauchery!(Yarrr, 那是一个暴风雨交加的黑夜,黑胡子和他那些卑鄙船员们航行四海,掠夺商船获取金币和绫罗绸缎,然后满载而归地回到托尔图加岛享受充满朗姆酒的放纵之夜!)* **消息 API** 2024 年 3 月,随着 Claude 3 Sonnet 的推出,Amazon Bedrock 推出了**消息 API**。这个 API 针对对话交互(如聊天机器人或虚拟助手)和多模态请求(例如,发送图片并通过文本 prompt 提问)场景进行了功能优化。 经过训练后,Claude 3 能够处理用户 (user) 与助手 (assistant) 之间的多轮对话。创建新消息时,你可以使用 messages 参数指定之前的对话。模型根据之前的几轮对话生成下一条消息。 每条输入消息都必须是一个包含角色 (role) 和内容 (content) 字段的对象。你可以输入一条单独的用户角色的消息,或者包含多条用户和助手角色交互的消息。 *备注:文章末尾提供了完整的代码示例以供参考。* 以下是封装在消息 API 请求对象中的消息示例: ```js // The request object for the messages API (Claude 3) { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Tell me a story." } ] } ] } ``` 可以看到,这里仅有一条消息,角色为 user,而 Claude 将返回一条包含响应内容的新消息: ```js // The response object returned by the messages API { "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "Orphaned as a child, she overcame poverty, discrimination, and countless ..." } ] ... } ``` **在消息 API 中使用系统 Prompt** 只需在请求对象中添加 “`system`” 参数即可添加系统 prompt,示例如下: ```js // The request including a system prompt with the Messages API { "system": "All your output must be pirate speech 🦜", "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Tell me a story." } ] } ] } ``` 输出结果如下所示: ```js // The response object returned by the messages API { "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "Ahoy, matey! Hoist the mainsail an' brace yeself ... 🏴 ☠️" } ], ... } ``` 就是这么简单! 如果这篇文章对你有帮助或让你有所收获,欢迎点赞或在评论区留言。 如需了解更多信息,请参考以下指南: - Amazon Bedrock 代码示例:提供持续更新的示例列表,涵盖各种模型和编程语言。 - 推理参数参考:介绍适用于 Amazon Bedrock 上的 Claude 及所有其他模型的推理参数。 - community.aws 上的生成式 AI 专栏:这里有许多关于 Amazon Bedrock 和生成式 AI 的精选文章。 ### 代码示例 如果你想要动手实践,这里给大家提供了两个适用于这两种 API 的完整 Python 脚本示例。祝你编程愉快! **文本补全 API 中添加系统 Prompt(完整示例)** ```js # This Python example demonstrates the use of a system prompt with the # Text Completion API for Claude 2.x import boto3 import json # Initialize the client with the service and region client = boto3.client('bedrock-runtime', 'us-east-1') # Define model ID and prompt model_id = 'anthropic.claude-v2:1' system_prompt = 'All your output must be pirate speech 🦜' user_prompt = 'Tell me a story.' prompt = f"System: {system_prompt}\n\nHuman: {user_prompt}\n\nAssistant:" # Create the request body body = { "prompt": prompt, "max_tokens_to_sample": 200, "temperature": 0.5, "stop_sequences": ["\n\nHuman:"] } # Invoke the model and print the response response = client.invoke_model(modelId=model_id, body=json.dumps(body)) print(json.loads(response["body"].read())["completion"]) ``` **消息 API 中添加系统 Prompt(完整示例)** ```js # This Python example demonstrates the use of a system prompt with the # Messages API for Claude 3 import boto3 import json client = boto3.client(service_name="bedrock-runtime", region_name="us-east-1") model_id = "anthropic.claude-3-sonnet-20240229-v1:0" response = client.invoke_model( modelId=model_id, body=json.dumps( { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "system": "All your output must be pirate speech 🦜", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Tell me a story." } ] } ], } ), ) # Process and print the response(s) response_body = json.loads(response.get("body").read()) for output in response_body.get("content", []): print(output["text"]) ``` 享受编程的乐趣吧! 本文中的任何观点仅代表作者个人的观点,不代表亚马逊云科技的观点。
0
目录
关闭