View a markdown version of this page

レコメンデーションを取得する - Amazon Bedrock AgentCore

レコメンデーションを取得する

レコメンデーションのステータスと結果を取得します。レコメンデーションが終了状態 (COMPLETED または ) になるまで、このオペレーションをポーリングしますFAILED

コードサンプル

AgentCore CLI

agentcore run recommendation は、fire-and-forgetジョブを送信します。終了状態になるまで --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}")

リクエストパラメータ

パラメータ タイプ 必須 説明

recommendationId

文字列

はい

によって返されるレコメンデーション IDStartRecommendation。パスパラメータとして渡されます。

[応答]

フィールド タイプ 説明

recommendationId

文字列

レコメンデーションの一意の識別子。

recommendationArn

String

レコメンデーションの ARN。

name

String

レコメンデーション名。

type

String

SYSTEM_PROMPT_RECOMMENDATION または TOOL_DESCRIPTION_RECOMMENDATION

status

String

現在のステータス: PENDINGIN_PROGRESSCOMPLETEDFAILED、または DELETING

recommendationConfig

オブジェクト

送信した設定。

recommendationResult

オブジェクト

ステータスが の場合に表示されますCOMPLETED。最適化設定が含まれています。シェイプはレコメンデーションタイプによって異なります。

createdAt

タイムスタンプ

レコメンデーションが作成された日時。

updatedAt

タイムスタンプ

レコメンデーションが最後に更新された日時。

システムプロンプトの結果フィールド

タイプが recommendationResult.systemPromptRecommendationResultの場合、 に存在しますSYSTEM_PROMPT_RECOMMENDATION

フィールド タイプ 説明

recommendedSystemPrompt

文字列

最適化システムプロンプトテキスト。

configurationBundle

オブジェクト

入力が設定バンドルであった場合に表示されます。新しいバンドルバージョンの bundleArnversionId が含まれます。

explanation

String

レコメンデーションが生成された理由と、提案された変更の背後にある理由の説明。

errorCode

String

失敗時に存在します。エラーコード。

errorMessage

String

失敗時に存在します。人間が読める説明。

ツールの説明結果フィールド

タイプが recommendationResult.toolDescriptionRecommendationResultの場合、 に存在しますTOOL_DESCRIPTION_RECOMMENDATION

フィールド タイプ 説明

tools

[List] (リスト)

ツールごとの結果。各エントリには、toolNamerecommendedToolDescription、および が含まれますexplanation

configurationBundle

オブジェクト

入力が設定バンドルであった場合に表示されます。新しいバンドルバージョンの bundleArnversionId が含まれます。

errorCode

String

失敗時に存在します。エラーコード。

errorMessage

String

失敗時に存在します。人間が読める説明。

レスポンス例

完了したシステムプロンプトのレコメンデーション (インライン入力):

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

サービス側のエラー。