Ausführen von Codebeispielen für das Prompt-Management - Amazon Bedrock

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Ausführen von Codebeispielen für das Prompt-Management

Zum Ausprobieren einiger Codebeispiele für das Prompt-Management wählen Sie die Registerkarte für Ihre bevorzugte Methode aus und folgen dann den Schritten. Bei den folgenden Codebeispielen wird davon ausgegangen, dass Sie Ihre Anmeldeinformationen für die Verwendung der AWS-API eingerichtet haben. Falls noch nicht geschehen, finden Sie weitere Informationen unter Erste Schritte mit der API.

Python
  1. Führen Sie den folgenden Codeausschnitt aus, um AWS SDK für Python (Boto3) zu laden, einen Client zu erstellen und einen Prompt zu generieren, der mithilfe von zwei Variablen (genre und number) eine Musik-Playlist anlegt, indem Sie eine CreatePrompt-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock stellen:

    # 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")
  2. Führen Sie den folgenden Codeausschnitt aus, um den Prompt zu sehen, die Sie gerade erstellt haben (zusammen mit allen anderen Prompts in Ihrem Konto), um eine ListPrompts-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock zu stellen:

    # List prompts that you've created client.list_prompts()
  3. Sie sollten die ID des Prompts, den Sie im Feld id erstellt haben, im Objekt des Felds promptSummaries sehen. Führen Sie den folgenden Codeausschnitt aus, um Informationen zu dem Prompt anzuzeigen, den Sie erstellt haben, indem Sie eine GetPrompt-Anfrage mit einem Build-Time-Endpunkt für Agenten für Amazon Bedrock stellen:

    # Get information about the prompt that you created client.get_prompt(promptIdentifier=prompt_id)
  4. Erstellen Sie eine Version des Prompts und rufen Sie dessen ID ab, indem Sie den folgenden Codeausschnitt ausführen, um eine CreatePromptVersion-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock zu stellen:

    # 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")
  5. Zeigen Sie Informationen über die Prompt-Version, die Sie gerade erstellt haben, zusammen mit Informationen zur Entwurfsversion an, indem Sie den folgenden Codeausschnitt ausführen, um eine ListPrompts-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock zu stellen:

    # List versions of the prompt that you just created client.list_prompts(promptIdentifier=prompt_id)
  6. Zeigen Sie Informationen zu der Prompt-Version an, die Sie erstellt haben, indem Sie den folgenden Codeausschnitt ausführen, um eine GetPrompt-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock stellen:

    # Get information about the prompt version that you created client.get_prompt( promptIdentifier=prompt_id, promptVersion=prompt_version )
  7. Testen Sie den Prompt, indem Sie ihn einem Flow hinzufügen. Folgen Sie dazu den Schritten unter Codebeispiele von Amazon Bedrock Flows ausführen. Führen Sie bei der Flow-Erstellung im ersten Schritt den folgenden Codeausschnitt aus, um den Prompt zu verwenden, den Sie erstellt haben, anstatt einen Inline-Prompt im Flow zu definieren (ersetzen Sie den ARN der Prompt-Version im Feld promptARN durch den ARN der Version des Prompts, die Sie erstellt haben):

    # 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 } )
  8. Löschen Sie die Prompt-Version, die Sie soeben erstellt haben, indem Sie den folgenden Codeausschnitt ausführen, um eine DeletePrompt-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock zu stellen:

    # Delete the prompt version that you created client.delete_prompt( promptIdentifier=prompt_id, promptVersion=prompt_version )
  9. Löschen Sie den Prompt, den Sie soeben erstellt haben, vollständig, indem Sie den folgenden Codeausschnitt ausführen, um eine DeletePrompt-Anfrage mit einem Build-Time-Endpunkt von Agenten für Amazon Bedrock zu stellen:

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