调用内联代理 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

调用内联代理

注意

Amazon Bedrock 的配置和调用内联代理功能目前为预览版,可能会有改变。

在调用内联代理之前,请确保您已满足先决条件

要调用内联代理,请使用适用于 Amazon Bedrock 的代理运行时终端节点发送 InvokeInlineAgentAPI 请求,并至少包含以下字段。

字段 使用案例
instruction 提供指令,告知内联代理应该做什么以及如何与用户交互。
foundationModel 指定基础模型,以供您创建的内联代理进行编排。例如,anthropic claude、meta Llama3.1 等。
sessionId 会话的唯一标识符。在不同请求中使用相同的值,以继续相同的对话。

以下字段是可选字段:

字段 使用案例
actionGroups 操作组列表,每个操作组定义了内联代理可以执行的操作。
knowledgeBases 知识库与内联代理关联,以增强模型生成的响应。
guardrailConfiguration 护栏配置,用于屏蔽话题、防止出现幻觉并为您的应用程序实施保护措施。
agentCollaboration 定义协作者代理如何处理多个协作者代理之间的信息,以协调最终响应。协作者代理也可以是主管。
collaboratorConfigurations 协作者代理的配置。
collaborators 协作者代理名单。
promptOverrideConfiguration 高级提示配置,用于覆盖默认提示。
enableTrace 指定是否开启追踪,以追踪内联代理的推理过程。
IdleSes TTLIn sion 秒 指定内联代理应当结束会话并删除所有存储信息的持续时间。
customerEncryptionKeyArn 指定用于加密代理资源的 KMS 密钥的 ARN
endSession 指定是否结束与内联代理的会话。
inlineSessionState 用于指定会话的各种属性的参数。
inputText 指定要发送给代理的提示。
reasoning_config 启用模型推理,以便模型解释它是如何得出结论的。在 additionalModelRequestFields 字段内使用。您必须指定用于模型推理的 budget_tokens 数量,这些是输出词元的一个子集。有关更多信息,请参阅使用模型推理增强模型响应

以下 InvokeInlineAgent API 示例提供了完整的内联代理配置,包括基础模型、指令、包含代码解释器的操作组、护栏和知识库。

response = bedrock_agent_runtime.invoke_inline_agent( // Initialization parameters: cannot be changed for a conversation sessionId='uniqueSessionId', customerEncryptionKeyArn: String, // Input inputText="Hello, can you help me with a task?", endSession=False, enableTrace=True, // Agent configurations foundationModel='anthropic.claude-3-7-sonnet-20250219-v1:0', instruction="You are a helpful assistant...", actionGroups=[ { 'name': 'CodeInterpreterAction', 'parentActionGroupSignature': 'AMAZON.CodeInterpreter' }, { 'actionGroupName': 'FetchDetails', 'parentActionGroupSignature': '', "actionGroupExecutor": { ... }, "apiSchema": { ... }, "description": "string", "functionSchema": { ... } } ], knowledgeBases=[ { knowledgeBaseId: "string", description: 'Use this KB to get all the info', retrievalConfiguration: { vectorSearchConfiguration: { filter: { ... }, numberOfResults: number, overrideSearchType: "string" } } } ], guardrailConfiguration={ guardrailIdentifier: 'BlockEverything', gurardrailVersion: '1.0' }, promptOverrideConfiguration: {...} // session properties: persisted throughout conversation inlineSessionState = { sessionAttributes = { 'key': 'value' }, promptSessionAttributes = {k:v}, returnControlInvocationResults = {...}, invocationId = 'abc', files = {...}, } }

您可以在请求中包含模型推理参数。以下是在 additionalModelRequestFields 中开启模型推理的单个提示的示例。

{ "basePromptTemplate": " ... ", "inferenceConfiguration": { "stopSequences": [ "</answer>" ] }, "parserMode": "DEFAULT", "promptCreationMode": "DEFAULT", "promptState": "DISABLED", "promptType": "ORCHESTRATION", "additionalModelRequestFields": "reasoning_config": { "type": "enabled", "budget_tokens": 1024 } }