

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

# 获取推荐
<a name="recommendations-get"></a>

检索推荐的状态和结果。轮询此操作，直到建议达到终端状态（`COMPLETED`或`FAILED`）。

## 代码示例
<a name="get-rec-examples"></a>

**Example**  
 `agentcore run recommendation`提交一份 “一劳永逸” 的工作。传递`--wait`到区块直到其达到终端状态，或者稍后按 ID 进行检查：  

```
agentcore view recommendation <recommendation-id>
```
列出所有推荐任务，或以 JSON 格式查看：  

```
agentcore view recommendation
agentcore view recommendation <recommendation-id> --json
```
轮询直至完成并提取结果：  

```
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}")
```

## 请求参数
<a name="get-rec-params"></a>


| 参数 | Type | 必需 | 描述 | 
| --- | --- | --- | --- | 
|  `recommendationId`  | 字符串 | 是 | 返回的推荐 ID `StartRecommendation`。作为路径参数传递。 | 

## 响应
<a name="get-rec-response"></a>


| 字段 | Type | 说明 | 
| --- | --- | --- | 
|  `recommendationId`  | 字符串 | 建议的唯一标识符。 | 
|  `recommendationArn`  | 字符串 | 该建议的 ARN。 | 
|  `name`  | 字符串 | 推荐名称。 | 
|  `type`  | 字符串 |  `SYSTEM_PROMPT_RECOMMENDATION` 或 `TOOL_DESCRIPTION_RECOMMENDATION`。 | 
|  `status`  | 字符串 | 当前状态：`PENDING``IN_PROGRESS`、`COMPLETED`、`FAILED`、或`DELETING`。 | 
|  `recommendationConfig`  | 对象 | 您提交的配置。 | 
|  `recommendationResult`  | 对象 | 状态为时出现`COMPLETED`。包含优化的配置。形状取决于推荐类型。 | 
|  `createdAt`  | Timestamp | 创建推荐的时间。 | 
|  `updatedAt`  | Timestamp | 上次更新建议的时间。 | 

### 系统提示结果字段
<a name="get-rec-result-sysprompt"></a>

`recommendationResult.systemPromptRecommendationResult`当类型为`SYSTEM_PROMPT_RECOMMENDATION`：时出现


| 字段 | Type | 说明 | 
| --- | --- | --- | 
|  `recommendedSystemPrompt`  | 字符串 | 优化的系统提示文本。 | 
|  `configurationBundle`  | 对象 | 当输入是配置包时出现。包含新捆绑版本的`bundleArn`和`versionId`。 | 
|  `explanation`  | 字符串 | 解释提出该建议的原因以及修改建议背后的理由。 | 
|  `errorCode`  | 字符串 | 失败时出现。错误代码。 | 
|  `errorMessage`  | 字符串 | 失败时出现。 Human-readable 描述。 | 

### 工具描述结果字段
<a name="get-rec-result-tooldesc"></a>

`recommendationResult.toolDescriptionRecommendationResult`当类型为`TOOL_DESCRIPTION_RECOMMENDATION`：时出现


| 字段 | Type | 说明 | 
| --- | --- | --- | 
|  `tools`  | 列表 | Per-tool 结果。每个条目都包含`toolName``recommendedToolDescription`、和`explanation`。 | 
|  `configurationBundle`  | 对象 | 当输入是配置包时出现。包含新捆绑版本的`bundleArn`和`versionId`。 | 
|  `errorCode`  | 字符串 | 失败时出现。错误代码。 | 
|  `errorMessage`  | 字符串 | 失败时出现。 Human-readable 描述。 | 

## 响应示例
<a name="get-rec-example-responses"></a>

已完成的系统提示建议（内联输入）：

```
{
    "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"
}
```

## 错误
<a name="get-rec-errors"></a>


| 错误 | HTTP 状态 | 说明 | 
| --- | --- | --- | 
|  `ResourceNotFoundException`  | 404 | 未找到具有指定 ID 的推荐。 | 
|  `ValidationException`  | 400 | 推荐 ID 格式无效。 | 
|  `AccessDeniedException`  | 403 | 权限不足。 | 
|  `ThrottlingException`  | 429 | 超出请求速率。 | 
|  `InternalServerException`  | 500 | Service-side 错误。 | 