View a markdown version of this page

获取推荐 - Amazon Bedrock AgentCore

获取推荐

检索建议的状态和结果。轮询此操作,直到建议达到终止状态(COMPLETEDFAILED)。

代码示例

AgentCore CLI

agentcore run recommendation提交了一份开枪就忘记的任务。传--wait给方块直到它达到终端状态,或者稍后通过 ID 进行检查:

agentcore view recommendation <recommendation-id>

列出所有推荐任务,或以 JSON 格式查看:

agentcore view recommendation agentcore view recommendation <recommendation-id> --json
AWS SDK (boto3)

轮询直到完成并提取结果:

import time import boto3 client = boto3.client("bedrock-agentcore", region_name="us-west-2") # Poll until terminal state while True: result = client.get_recommendation(recommendationId=recommendation_id) status = result["status"] print(f"Status: {status}") if status in ("COMPLETED", "FAILED"): break time.sleep(15) # Extract system prompt result if status == "COMPLETED": rec_result = result.get("recommendationResult", {}) if "systemPromptRecommendationResult" in rec_result: sys_result = rec_result["systemPromptRecommendationResult"] print(f"Recommended prompt:\n{sys_result['recommendedSystemPrompt']}") if "explanation" in sys_result: print(f"Explanation:\n{sys_result['explanation']}") if "configurationBundle" in sys_result: bundle = sys_result["configurationBundle"] print(f"New bundle version: {bundle['versionId']}") elif "toolDescriptionRecommendationResult" in rec_result: tool_result = rec_result["toolDescriptionRecommendationResult"] for tool in tool_result.get("tools", []): print(f"{tool['toolName']}: {tool['recommendedToolDescription']}") if "explanation" in tool: print(f" Explanation: {tool['explanation']}") elif status == "FAILED": rec_result = result.get("recommendationResult", {}) r = rec_result.get("systemPromptRecommendationResult") or rec_result.get("toolDescriptionRecommendationResult") print(f"Error: [{r['errorCode']}] {r['errorMessage']}")

提取配置包参考以用于 A/B 测试:

result = client.get_recommendation(recommendationId=recommendation_id) if result["status"] == "COMPLETED": rec_result = result["recommendationResult"] sys_result = rec_result.get("systemPromptRecommendationResult", {}) if "configurationBundle" in sys_result: bundle_arn = sys_result["configurationBundle"]["bundleArn"] version_id = sys_result["configurationBundle"]["versionId"] print(f"Use in A/B test: bundle={bundle_arn}, version={version_id}")

请求参数

参数 Type 必需 描述

recommendationId

字符串

返回的推荐编号StartRecommendation。作为路径参数传递。

响应

字段 Type 说明

recommendationId

字符串

建议的唯一标识符。

recommendationArn

字符串

该建议的 ARN。

name

字符串

推荐名称。

type

字符串

SYSTEM_PROMPT_RECOMMENDATIONTOOL_DESCRIPTION_RECOMMENDATION

status

字符串

当前状态:PENDINGIN_PROGRESSCOMPLETEDFAILED、或DELETING

recommendationConfig

对象

您提交的配置。

recommendationResult

对象

状态为时出现COMPLETED。包含优化的配置。形状取决于推荐类型。

createdAt

Timestamp

建议的创建时间。

updatedAt

Timestamp

上次更新建议的时间。

系统提示结果字段

recommendationResult.systemPromptRecommendationResult当类型为SYSTEM_PROMPT_RECOMMENDATION:时出现

字段 Type 说明

recommendedSystemPrompt

字符串

优化的系统提示文本。

configurationBundle

对象

当输入为配置包时出现。包含新捆绑包版本的bundleArnversionId

explanation

字符串

解释产生该建议的原因以及建议的修改背后的原因。

errorCode

字符串

失败时出现。错误代码。

errorMessage

字符串

失败时出现。 Human-readable 描述。

工具描述结果字段

recommendationResult.toolDescriptionRecommendationResult当类型为TOOL_DESCRIPTION_RECOMMENDATION:时出现

字段 Type 说明

tools

列表

Per-tool 结果。每个条目都包含toolNamerecommendedToolDescription、和explanation

configurationBundle

对象

当输入为配置包时出现。包含新捆绑包版本的bundleArnversionId

errorCode

字符串

失败时出现。错误代码。

errorMessage

字符串

失败时出现。 Human-readable 描述。

响应示例

已完成的系统提示建议(内联输入):

{ "recommendationId": "MyPromptRec-Ab1Cd2Ef3G", "recommendationArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:recommendation/MyPromptRec-Ab1Cd2Ef3G", "name": "my-prompt-rec", "type": "SYSTEM_PROMPT_RECOMMENDATION", "status": "COMPLETED", "recommendationConfig": { ... }, "recommendationResult": { "systemPromptRecommendationResult": { "recommendedSystemPrompt": "<optimized system prompt text>", "explanation": "<explanation of why the recommendation was generated>" } }, "createdAt": "2025-03-15T10:00:00Z", "updatedAt": "2025-03-15T10:05:30Z" }

已完成的系统提示建议(配置包输入):

{ "recommendationId": "MyBundleRec-Xy9Zw8Vq1R", "recommendationArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:recommendation/MyBundleRec-Xy9Zw8Vq1R", "name": "my-bundle-prompt-rec", "type": "SYSTEM_PROMPT_RECOMMENDATION", "status": "COMPLETED", "recommendationConfig": { ... }, "recommendationResult": { "systemPromptRecommendationResult": { "recommendedSystemPrompt": "<optimized system prompt text>", "configurationBundle": { "bundleArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:configuration-bundle/myBundle-Ab1Cd2Ef3G", "versionId": "12345678-1234-1234-1234-123456789012" }, "explanation": "<explanation of why the recommendation was generated>" } }, "createdAt": "2025-03-15T10:00:00Z", "updatedAt": "2025-03-15T10:06:12Z" }

已完成的工具描述推荐:

{ "recommendationId": "MyToolRec-Qr5St6Uv7W", "recommendationArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:recommendation/MyToolRec-Qr5St6Uv7W", "name": "my-tool-rec", "type": "TOOL_DESCRIPTION_RECOMMENDATION", "status": "COMPLETED", "recommendationConfig": { ... }, "recommendationResult": { "toolDescriptionRecommendationResult": { "tools": [ { "toolName": "<tool-name-1>", "recommendedToolDescription": "<optimized description for tool 1>", "explanation": "<explanation of why this tool description was changed>" }, { "toolName": "<tool-name-2>", "recommendedToolDescription": "<optimized description for tool 2>", "explanation": "<explanation of why this tool description was changed>" } ] } }, "createdAt": "2025-03-15T11:00:00Z", "updatedAt": "2025-03-15T11:04:45Z" }

建议失败:

{ "recommendationId": "MyFailedRec-Mn3Op4Qr5S", "recommendationArn": "arn:aws:bedrock-agentcore:us-west-2:123456789012:recommendation/MyFailedRec-Mn3Op4Qr5S", "name": "my-failed-rec", "type": "SYSTEM_PROMPT_RECOMMENDATION", "status": "FAILED", "recommendationConfig": { ... }, "recommendationResult": { "systemPromptRecommendationResult": { "errorCode": "<error-code>", "errorMessage": "<human-readable error description>" } }, "createdAt": "2025-03-15T12:00:00Z", "updatedAt": "2025-03-15T12:01:10Z" }

错误

错误 HTTP 状态 说明

ResourceNotFoundException

404

未找到具有指定 ID 的建议。

ValidationException

400

推荐 ID 格式无效。

AccessDeniedException

403

权限不足。

ThrottlingException

429

超出请求速率。

InternalServerException

500

Service-side 错误。