

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# プロンプト管理コードサンプルを実行する
<a name="prompt-management-code-ex"></a>

プロンプト管理用のコードサンプルを試すには、使用する方法に対応するタブを選択して、手順に従ってください。以下のコードサンプルでは、AWS API を使用するための認証情報が設定済みであることを前提としています。設定していない場合は、「[API の使用を開始する](getting-started-api.md)」を参照します。

------
#### [ Python ]

1. 次のコードスニペットを実行して AWS SDK for Python (Boto3) をロードし、クライアントを作成し、[CreatePrompt](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreatePrompt.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成して、2 つの変数 (`genre` と `number`) を使用して音楽プレイリストを作成するプロンプトを作成します。

   ```
   # Create a prompt in Prompt management
   import boto3
   
   # Create an Amazon Bedrock Agents client
   client = boto3.client(service_name="bedrock-agent")
   
   # Create the prompt
   response = client.create_prompt(
       name="MakePlaylist",
       description="My first prompt.",
       variants=[
           { 
               "name": "Variant1",
               "modelId": "amazon.titan-text-express-v1",
               "templateType": "TEXT",
               "inferenceConfiguration": {
                   "text": {
                       "temperature": 0.8
                   }
               },
               "templateConfiguration": { 
                   "text": {
                       "text": "Make me a {{{{genre}}}} playlist consisting of the following number of songs: {{{{number}}}}."
                   }
               }
         }
       ]
   )
                           
   prompt_id = response.get("id")
   ```

1. 次のコードスニペットを実行して、[ListPrompts](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_ListPrompts.html) [Amazon Bedrock エージェントのビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成するために (アカウント内の他のプロンプトとともに) 作成したばかりのプロンプトを確認します。

   ```
   # List prompts that you've created
   client.list_prompts()
   ```

1. オブジェクトの `id` フィールドで作成したプロンプトの ID は、`promptSummaries` フィールドに表示されます。次のコードスニペットを実行して、[GetPrompt](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_GetPrompt.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成してできたプロンプトの情報を表示します。

   ```
   # Get information about the prompt that you created
   client.get_prompt(promptIdentifier=prompt_id)
   ```

1. プロンプトのバージョンを作成し、次のコードスニペットを実行して ID を取得して、[CreatePromptVersion](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreatePromptVersion.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成します。

   ```
   # Create a version of the prompt that you created
   response = client.create_prompt_version(promptIdentifier=prompt_id)
                           
   prompt_version = response.get("version")
   prompt_version_arn = response.get("arn")
   ```

1. 作成したばかりのプロンプトバージョンに関する情報を、ドラフトバージョンに関する情報とともに表示し、次のコードスニペットを実行して、[ListPrompts](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_ListPrompts.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成します。

   ```
   # List versions of the prompt that you just created
   client.list_prompts(promptIdentifier=prompt_id)
   ```

1. 作成したばかりのプロンプトバージョンの情報を表示し、次のコードスニペットを実行して、[GetPrompt](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_GetPrompt.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成します。

   ```
   # Get information about the prompt version that you created
   client.get_prompt(
       promptIdentifier=prompt_id, 
       promptVersion=prompt_version
   )
   ```

1. 「[Amazon Bedrock フローのコードサンプルを実行する](flows-code-ex.md)」の手順に従って、プロンプトをフローに追加し、プロンプトをテストします。フローを作成する最初のステップでは、フローでインラインプロンプトを定義する代わりに、作成したプロンプトを使用するには、次のコードスニペットを実行します (`promptARN` フィールドのプロンプトバージョンの ARN を、作成したプロンプトバージョンの ARN に置き換えます)。

   ```
   # Import Python SDK and create client
   import boto3
   
   client = boto3.client(service_name='bedrock-agent')
   
   FLOWS_SERVICE_ROLE = "arn:aws:iam::123456789012:role/MyPromptFlowsRole" # Flows service role that you created. For more information, see https://docs.aws.amazon.com/bedrock/latest/userguide/flows-permissions.html
   PROMPT_ARN = prompt_version_arn # ARN of the prompt that you created, retrieved programatically during creation.
   
   # Define each node
   
   # The input node validates that the content of the InvokeFlow request is a JSON object.
   input_node = {
       "type": "Input",
       "name": "FlowInput",
       "outputs": [
           {
               "name": "document",
               "type": "Object"
           }
       ]
   }
   
   # This prompt node contains a prompt that you defined in Prompt management.
   # It validates that the input is a JSON object that minimally contains the fields "genre" and "number", which it will map to the prompt variables.
   # The output must be named "modelCompletion" and be of the type "String".
   prompt_node = {
       "type": "Prompt",
       "name": "MakePlaylist",
       "configuration": {
           "prompt": {
               "sourceConfiguration": {
                   "resource": {
                       "promptArn": ""
                   }
               }
           }
       },
       "inputs": [
           {
               "name": "genre",
               "type": "String",
               "expression": "$.data.genre"
           },
           {
               "name": "number",
               "type": "Number",
               "expression": "$.data.number"
           }
       ],
       "outputs": [
           {
               "name": "modelCompletion",
               "type": "String"
           }
       ]
   }
   
   # The output node validates that the output from the last node is a string and returns it as is. The name must be "document".
   output_node = {
       "type": "Output",
       "name": "FlowOutput",
       "inputs": [
           {
               "name": "document",
               "type": "String",
               "expression": "$.data"
           }
       ]
   }
   
   # Create connections between the nodes
   connections = []
   
   #   First, create connections between the output of the flow input node and each input of the prompt node
   for input in prompt_node["inputs"]:
       connections.append(
           {
               "name": "_".join([input_node["name"], prompt_node["name"], input["name"]]),
               "source": input_node["name"],
               "target": prompt_node["name"],
               "type": "Data",
               "configuration": {
                   "data": {
                       "sourceOutput": input_node["outputs"][0]["name"],
                       "targetInput": input["name"]
                   }
               }
           }
       )
   
   # Then, create a connection between the output of the prompt node and the input of the flow output node
   connections.append(
       {
           "name": "_".join([prompt_node["name"], output_node["name"]]),
           "source": prompt_node["name"],
           "target": output_node["name"],
           "type": "Data",
           "configuration": {
               "data": {
                   "sourceOutput": prompt_node["outputs"][0]["name"],
                   "targetInput": output_node["inputs"][0]["name"]
               }
           }
       }
   )
   
   # Create the flow from the nodes and connections
   client.create_flow(
       name="FlowCreatePlaylist",
       description="A flow that creates a playlist given a genre and number of songs to include in the playlist.",
       executionRoleArn=FLOWS_SERVICE_ROLE,
       definition={
           "nodes": [input_node, prompt_node, output_node],
           "connections": connections
       }
   )
   ```

1. 作成したばかりのプロンプトバージョンを削除するには、次のコードスニペットを実行して、[DeletePrompt](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DeletePrompt.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成します。

   ```
   # Delete the prompt version that you created
   client.delete_prompt(
       promptIdentifier=prompt_id, 
       promptVersion=prompt_version
   )
   ```

1. 作成したばかりのプロンプトを完全に削除するには、次のコードスニペットを実行して、[DeletePrompt](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DeletePrompt.html) [Amazon Bedrock エージェントビルドタイムエンドポイント](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)を作成します。

   ```
   # Delete the prompt that you created
   client.delete_prompt(
       promptIdentifier=prompt_id
   )
   ```

------