

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 프롬프트 관리 코드 샘플 실행
<a name="prompt-management-code-ex"></a>

프롬프트 관리를 위해 일부 코드 샘플을 시도하려면 선호하는 방법에 해당하는 탭을 선택하고 안내된 단계를 수행합니다. 다음 코드 샘플은 AWS API를 사용하도록 자격 증명을 설정했다고 가정합니다. 이렇게 설정되어 있지 않은 경우 [API 시작하기](getting-started-api.md) 섹션을 참조하세요.

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

1. 다음 코드 스니펫을 실행하여 AWS SDK for Python (Boto3)을 로드하고, 클라이언트를 만들고, 두 개의 변수(`genre` 및 `number`)를 사용하여 음악 재생 목록을 만드는 프롬프트를 만들 수 있도록 [CreatePrompt](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreatePrompt.html) [Agents for Amazon Bedrock 빌드 타임 엔드포인트](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)를 만듭니다.

   ```
   # 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) [Agents for 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) [Agents for 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) [Agents for 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) [Agents for 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) [Agents for 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 코드 샘플 실행](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) [Agents for 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) [Agents for 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
   )
   ```

------