自定义 AI 行为:Amazon Bedrock Converse API 中的系统提示词和推理参数




系列概述
本系列将引导您学习 Amazon Bedrock Converse API:
前提条件
在开始之前,请确保您已满足所有前提条件。您需要做到:
步骤 1:安装 Amazon Bedrock Runtime 客户端
首先,从 AWS SDK for JavaScript v3 中安装 Amazon Bedrock Runtime 客户端:
npm install @aws-sdk/client-bedrock-runtime
步骤 2:导入所需模块
在新的 JavaScript 文件中,导入所需的模块:
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
步骤 3:创建 BedrockRuntimeClient 实例
创建 BedrockRuntimeClient 实例,并指定该模型可用的亚马逊云科技区域:
const client = new BedrockRuntimeClient({ region: "us-east-1" });
步骤 4:指定模型 ID
指定要使用的模型的 ID。在此示例中,我们将使用 Claude 3 Haiku:
const modelId = "anthropic.claude-3-haiku-20240307-v1:0";
注:您可在文档中找到支持 Amazon Bedrock Converse API 的模型列表和包含所有模型 ID 的列表。
步骤 5:准备要发送的消息
准备第一条用户消息并将其添加到新会话数组中:
const userMessage = "What is 'rubber duck debugging'";
const conversation = [
{
role: "user",
content: [{ text: userMessage }]
}
];
步骤 6:定义系统提示词和其他推理参数
系统提示词是一种特殊类型的消息,可为 AI 模型提供额外的上下文或说明。与代表用户输入的用户消息不同,系统提示词可用于指导模型的行为和设置对响应的期望。
在本示例中,我们将使用系统提示词来指示模型以押韵的方式响应:
const systemPrompt = [{ text: "You must always respond in rhymes." }];
除系统提示词外,我们还可以通过指定推理参数来进一步对模型的行为进行自定义。推理参数使我们能够从长度和随机性等多个方面控制生成的响应。
在本示例中,我们将设置以下推理参数:
const additionalParameters = {
maxTokens: 100,
temperature: 0.5
};
这些参数会将响应限制为最多 100 个 Tokens,并使用温度参数设为 0.5 来在创造性和连贯性之间保持平衡。
将系统提示词和推理参数结合起来,即可通过对模型的行为进行微调来使其适合您的特定用例。尝试使用不同的值来观察它们对生成的响应的影响。
步骤 7:发送请求
使用 ConverseCommand 将对话、系统提示词和其他参数发送给模型:
const response = await client.send(
new ConverseCommand({
modelId,
messages: conversation,
system: systemPrompt,
inferenceConfig: additionalParameters
})
);
将对话发送给指定的模型并返回其响应。
打印出模型的响应:
const responseText = response.output.message.content[0].text;
console.log(responseText);
开始运行程序
代码完成了,现在就运行它,看看 AI 如何进行多轮对话吧!
以下是完整代码,仅供参考:
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
const client = new BedrockRuntimeClient({ region: "us-east-1" });
const modelId = "anthropic.claude-3-haiku-20240307-v1:0";
const userMessage = "What is 'rubber duck debugging'";
const conversation = [
{
role: "user",
content: [{ text: userMessage }]
}
];
const systemPrompt = [{ text: "You must always respond in rhymes." }];
const additionalParameters = {
maxTokens: 100,
temperature: 0.5
};
const response = await client.send(
new ConverseCommand({
modelId,
messages: conversation,
system: systemPrompt,
inferenceConfig: additionalParameters
})
);
const responseText = response.output.message.content[0].text;
console.log(responseText);
运行它:
1. 保存代码并将代码文件命名为 bedrock_customized.js
2. 在终端中,导航到包含的 bedrock_customized.js 目录
3. 运行以下命令:
node bedrock_customized.js
如果一切设置无误,您应该会在控制台中看到模型的响应被打印出来:
Rubber duck debugging, a clever way,
To solve problems without delay.
You talk to a duck, real or toy,
And the solution you will enjoy.
By explaining the issue out loud,
Your mind becomes less clouded.
The duck listens, never judges,
And the answer often just nudges.
It's a technique that's quite profound,
When you're stuck, it helps you rebound.
So next time you're in a bind,
Grab a duck and let your thoughts unwind.
后续步骤
恭喜您成功使用系统提示词和推理参数调整模型响应。
想了解更多?以下是一些值得继续探索的想法:
- 尝试不同模型,比较不同模型的响应。以下是所有支持 Amazon Bedrock Converse API 的模型列表。
- 挑战使用另一种编程语言重写程序。此处有 Python、Java、C# 等多种语言的示例。
- 通过《Amazon Bedrock 用户指南》继续深入了解 Amazon Bedrock Converse API。