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.
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.
Die folgenden Codebeispiele zeigen, wie Amazon Bedrock Runtime mit einem AWS Software Development Kit (SDK) verwendet wird.
Szenarien sind Code-Beispiele, die Ihnen zeigen, wie Sie bestimmte Aufgaben ausführen, indem Sie mehrere Funktionen innerhalb eines Services aufrufen oder mit anderen AWS-Services kombinieren.
Eine vollständige Liste der AWS SDK-Entwicklerhandbücher und Codebeispiele finden Sie unterAmazon Bedrock mit einem AWS SDK verwenden. Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK-Versionen.
Erste Schritte
Die folgenden Codebeispiele zeigen, wie Sie mit Amazon Bedrock beginnen können.
- Go
-
- SDK für Go V2
-
Anmerkung
Es gibt noch mehr dazu. GitHub Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. package main import ( "context" "encoding/json" "flag" "fmt" "log" "os" "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" ) // Each model provider defines their own individual request and response formats. // For the format, ranges, and default values for the different models, refer to: // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html type ClaudeRequest struct { Prompt string `json:"prompt"` MaxTokensToSample int `json:"max_tokens_to_sample"` // Omitting optional request parameters } type ClaudeResponse struct { Completion string `json:"completion"` } // main uses the AWS SDK for Go (v2) to create an Amazon Bedrock Runtime client // and invokes Anthropic Claude 2 inside your account and the chosen region. // This example uses the default settings specified in your shared credentials // and config files. func main() { region := flag.String("region", "us-east-1", "The AWS region") flag.Parse() fmt.Printf("Using AWS region: %s\n", *region) ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx, config.WithRegion(*region)) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } client := bedrockruntime.NewFromConfig(sdkConfig) modelId := "anthropic.claude-v2" prompt := "Hello, how are you today?" // Anthropic Claude requires you to enclose the prompt as follows: prefix := "Human: " postfix := "\n\nAssistant:" wrappedPrompt := prefix + prompt + postfix request := ClaudeRequest{ Prompt: wrappedPrompt, MaxTokensToSample: 200, } body, err := json.Marshal(request) if err != nil { log.Panicln("Couldn't marshal the request: ", err) } result, err := client.InvokeModel(ctx, &bedrockruntime.InvokeModelInput{ ModelId: aws.String(modelId), ContentType: aws.String("application/json"), Body: body, }) if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { fmt.Printf("Error: The Bedrock service is not available in the selected region. Please double-check the service availability for your region at https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/.\n") } else if strings.Contains(errMsg, "Could not resolve the foundation model") { fmt.Printf("Error: Could not resolve the foundation model from model identifier: \"%v\". Please verify that the requested model exists and is accessible within the specified region.\n", modelId) } else { fmt.Printf("Error: Couldn't invoke Anthropic Claude. Here's why: %v\n", err) } os.Exit(1) } var response ClaudeResponse err = json.Unmarshal(result.Body, &response) if err != nil { log.Fatal("failed to unmarshal", err) } fmt.Println("Prompt:\n", prompt) fmt.Println("Response from Anthropic Claude:\n", response.Completion) }
-
Einzelheiten zur API finden Sie InvokeModel
in der AWS SDK für Go API-Referenz.
-
- JavaScript
-
- SDK für JavaScript (v3)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. /** * @typedef {Object} Content * @property {string} text * * @typedef {Object} Usage * @property {number} input_tokens * @property {number} output_tokens * * @typedef {Object} ResponseBody * @property {Content[]} content * @property {Usage} usage */ import { fileURLToPath } from "node:url"; import { BedrockRuntimeClient, InvokeModelCommand, } from "@aws-sdk/client-bedrock-runtime"; const AWS_REGION = "us-east-1"; const MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0"; const PROMPT = "Hi. In a short paragraph, explain what you can do."; const hello = async () => { console.log("=".repeat(35)); console.log("Welcome to the Amazon Bedrock demo!"); console.log("=".repeat(35)); console.log("Model: Anthropic Claude 3 Haiku"); console.log(`Prompt: ${PROMPT}\n`); console.log("Invoking model...\n"); // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: AWS_REGION }); // Prepare the payload for the model. const payload = { anthropic_version: "bedrock-2023-05-31", max_tokens: 1000, messages: [{ role: "user", content: [{ type: "text", text: PROMPT }] }], }; // Invoke Claude with the payload and wait for the response. const apiResponse = await client.send( new InvokeModelCommand({ contentType: "application/json", body: JSON.stringify(payload), modelId: MODEL_ID, }), ); // Decode and return the response(s) const decodedResponseBody = new TextDecoder().decode(apiResponse.body); /** @type {ResponseBody} */ const responseBody = JSON.parse(decodedResponseBody); const responses = responseBody.content; if (responses.length === 1) { console.log(`Response: ${responses[0].text}`); } else { console.log("Haiku returned multiple responses:"); console.log(responses); } console.log(`\nNumber of input tokens: ${responseBody.usage.input_tokens}`); console.log(`Number of output tokens: ${responseBody.usage.output_tokens}`); }; if (process.argv[1] === fileURLToPath(import.meta.url)) { await hello(); }
-
Einzelheiten zur API finden Sie InvokeModelin der AWS SDK for JavaScript API-Referenz.
-
- Python
-
- SDK für Python (Boto3)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Senden Sie mit der InvokeModel Operation eine Aufforderung an ein Modell.
""" Uses the Amazon Bedrock runtime client InvokeModel operation to send a prompt to a model. """ import logging import json import boto3 from botocore.exceptions import ClientError logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def invoke_model(brt, model_id, prompt): """ Invokes the specified model with the supplied prompt. param brt: A bedrock runtime boto3 client param model_id: The model ID for the model that you want to use. param prompt: The prompt that you want to send to the model. :return: The text response from the model. """ # Format the request payload using the model's native structure. native_request = { "inputText": prompt, "textGenerationConfig": { "maxTokenCount": 512, "temperature": 0.5, "topP": 0.9 } } # Convert the native request to JSON. request = json.dumps(native_request) try: # Invoke the model with the request. response = brt.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract and print the response text. response_text = model_response["results"][0]["outputText"] return response_text except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") raise def main(): """Entry point for the example. Uses the AWS SDK for Python (Boto3) to create an Amazon Bedrock runtime client. Then sends a prompt to a model in the region set in the callers profile and credentials. """ # Create an Amazon Bedrock Runtime client. brt = boto3.client("bedrock-runtime") # Set the model ID, e.g., Amazon Titan Text G1 - Express. model_id = "amazon.titan-text-express-v1" # Define the prompt for the model. prompt = "Describe the purpose of a 'hello world' program in one line." # Send the prompt to the model. response = invoke_model(brt, model_id, prompt) print(f"Response: {response}") logger.info("Done.") if __name__ == "__main__": main()
Senden Sie mit der Converse-Operation eine Benutzernachricht an ein Modell.
""" Uses the Amazon Bedrock runtime client Converse operation to send a user message to a model. """ import logging import boto3 from botocore.exceptions import ClientError logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def converse(brt, model_id, user_message): """ Uses the Converse operation to send a user message to the supplied model. param brt: A bedrock runtime boto3 client param model_id: The model ID for the model that you want to use. param user message: The user message that you want to send to the model. :return: The text response from the model. """ # Format the request payload using the model's native structure. conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. response = brt.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] return response_text except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") raise def main(): """Entry point for the example. Uses the AWS SDK for Python (Boto3) to create an Amazon Bedrock runtime client. Then sends a user message to a model in the region set in the callers profile and credentials. """ # Create an Amazon Bedrock Runtime client. brt = boto3.client("bedrock-runtime") # Set the model ID, e.g., Amazon Titan Text G1 - Express. model_id = "amazon.titan-text-express-v1" # Define the message for the model. message = "Describe the purpose of a 'hello world' program in one line." # Send the message to the model. response = converse(brt, model_id, message) print(f"Response: {response}") logger.info("Done.") if __name__ == "__main__": main()
-
Einzelheiten zur API finden Sie InvokeModelin AWS SDK for Python (Boto3) API Reference.
-
Hallo Amazon Bedrock
Die folgenden Codebeispiele zeigen, wie Sie mit Amazon Bedrock beginnen können.
- Go
-
- SDK für Go V2
-
Anmerkung
Es gibt noch mehr dazu. GitHub Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. package main import ( "context" "encoding/json" "flag" "fmt" "log" "os" "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" ) // Each model provider defines their own individual request and response formats. // For the format, ranges, and default values for the different models, refer to: // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html type ClaudeRequest struct { Prompt string `json:"prompt"` MaxTokensToSample int `json:"max_tokens_to_sample"` // Omitting optional request parameters } type ClaudeResponse struct { Completion string `json:"completion"` } // main uses the AWS SDK for Go (v2) to create an Amazon Bedrock Runtime client // and invokes Anthropic Claude 2 inside your account and the chosen region. // This example uses the default settings specified in your shared credentials // and config files. func main() { region := flag.String("region", "us-east-1", "The AWS region") flag.Parse() fmt.Printf("Using AWS region: %s\n", *region) ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx, config.WithRegion(*region)) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } client := bedrockruntime.NewFromConfig(sdkConfig) modelId := "anthropic.claude-v2" prompt := "Hello, how are you today?" // Anthropic Claude requires you to enclose the prompt as follows: prefix := "Human: " postfix := "\n\nAssistant:" wrappedPrompt := prefix + prompt + postfix request := ClaudeRequest{ Prompt: wrappedPrompt, MaxTokensToSample: 200, } body, err := json.Marshal(request) if err != nil { log.Panicln("Couldn't marshal the request: ", err) } result, err := client.InvokeModel(ctx, &bedrockruntime.InvokeModelInput{ ModelId: aws.String(modelId), ContentType: aws.String("application/json"), Body: body, }) if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { fmt.Printf("Error: The Bedrock service is not available in the selected region. Please double-check the service availability for your region at https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/.\n") } else if strings.Contains(errMsg, "Could not resolve the foundation model") { fmt.Printf("Error: Could not resolve the foundation model from model identifier: \"%v\". Please verify that the requested model exists and is accessible within the specified region.\n", modelId) } else { fmt.Printf("Error: Couldn't invoke Anthropic Claude. Here's why: %v\n", err) } os.Exit(1) } var response ClaudeResponse err = json.Unmarshal(result.Body, &response) if err != nil { log.Fatal("failed to unmarshal", err) } fmt.Println("Prompt:\n", prompt) fmt.Println("Response from Anthropic Claude:\n", response.Completion) }
-
Einzelheiten zur API finden Sie InvokeModel
in der AWS SDK für Go API-Referenz.
-
- JavaScript
-
- SDK für JavaScript (v3)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. /** * @typedef {Object} Content * @property {string} text * * @typedef {Object} Usage * @property {number} input_tokens * @property {number} output_tokens * * @typedef {Object} ResponseBody * @property {Content[]} content * @property {Usage} usage */ import { fileURLToPath } from "node:url"; import { BedrockRuntimeClient, InvokeModelCommand, } from "@aws-sdk/client-bedrock-runtime"; const AWS_REGION = "us-east-1"; const MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0"; const PROMPT = "Hi. In a short paragraph, explain what you can do."; const hello = async () => { console.log("=".repeat(35)); console.log("Welcome to the Amazon Bedrock demo!"); console.log("=".repeat(35)); console.log("Model: Anthropic Claude 3 Haiku"); console.log(`Prompt: ${PROMPT}\n`); console.log("Invoking model...\n"); // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: AWS_REGION }); // Prepare the payload for the model. const payload = { anthropic_version: "bedrock-2023-05-31", max_tokens: 1000, messages: [{ role: "user", content: [{ type: "text", text: PROMPT }] }], }; // Invoke Claude with the payload and wait for the response. const apiResponse = await client.send( new InvokeModelCommand({ contentType: "application/json", body: JSON.stringify(payload), modelId: MODEL_ID, }), ); // Decode and return the response(s) const decodedResponseBody = new TextDecoder().decode(apiResponse.body); /** @type {ResponseBody} */ const responseBody = JSON.parse(decodedResponseBody); const responses = responseBody.content; if (responses.length === 1) { console.log(`Response: ${responses[0].text}`); } else { console.log("Haiku returned multiple responses:"); console.log(responses); } console.log(`\nNumber of input tokens: ${responseBody.usage.input_tokens}`); console.log(`Number of output tokens: ${responseBody.usage.output_tokens}`); }; if (process.argv[1] === fileURLToPath(import.meta.url)) { await hello(); }
-
Einzelheiten zur API finden Sie InvokeModelin der AWS SDK for JavaScript API-Referenz.
-
- Python
-
- SDK für Python (Boto3)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Senden Sie mit der InvokeModel Operation eine Aufforderung an ein Modell.
""" Uses the Amazon Bedrock runtime client InvokeModel operation to send a prompt to a model. """ import logging import json import boto3 from botocore.exceptions import ClientError logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def invoke_model(brt, model_id, prompt): """ Invokes the specified model with the supplied prompt. param brt: A bedrock runtime boto3 client param model_id: The model ID for the model that you want to use. param prompt: The prompt that you want to send to the model. :return: The text response from the model. """ # Format the request payload using the model's native structure. native_request = { "inputText": prompt, "textGenerationConfig": { "maxTokenCount": 512, "temperature": 0.5, "topP": 0.9 } } # Convert the native request to JSON. request = json.dumps(native_request) try: # Invoke the model with the request. response = brt.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract and print the response text. response_text = model_response["results"][0]["outputText"] return response_text except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") raise def main(): """Entry point for the example. Uses the AWS SDK for Python (Boto3) to create an Amazon Bedrock runtime client. Then sends a prompt to a model in the region set in the callers profile and credentials. """ # Create an Amazon Bedrock Runtime client. brt = boto3.client("bedrock-runtime") # Set the model ID, e.g., Amazon Titan Text G1 - Express. model_id = "amazon.titan-text-express-v1" # Define the prompt for the model. prompt = "Describe the purpose of a 'hello world' program in one line." # Send the prompt to the model. response = invoke_model(brt, model_id, prompt) print(f"Response: {response}") logger.info("Done.") if __name__ == "__main__": main()
Senden Sie mit der Converse-Operation eine Benutzernachricht an ein Modell.
""" Uses the Amazon Bedrock runtime client Converse operation to send a user message to a model. """ import logging import boto3 from botocore.exceptions import ClientError logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def converse(brt, model_id, user_message): """ Uses the Converse operation to send a user message to the supplied model. param brt: A bedrock runtime boto3 client param model_id: The model ID for the model that you want to use. param user message: The user message that you want to send to the model. :return: The text response from the model. """ # Format the request payload using the model's native structure. conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message to the model, using a basic inference configuration. response = brt.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 512, "temperature": 0.5, "topP": 0.9}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] return response_text except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") raise def main(): """Entry point for the example. Uses the AWS SDK for Python (Boto3) to create an Amazon Bedrock runtime client. Then sends a user message to a model in the region set in the callers profile and credentials. """ # Create an Amazon Bedrock Runtime client. brt = boto3.client("bedrock-runtime") # Set the model ID, e.g., Amazon Titan Text G1 - Express. model_id = "amazon.titan-text-express-v1" # Define the message for the model. message = "Describe the purpose of a 'hello world' program in one line." # Send the message to the model. response = converse(brt, model_id, message) print(f"Response: {response}") logger.info("Done.") if __name__ == "__main__": main()
-
Einzelheiten zur API finden Sie InvokeModelin AWS SDK for Python (Boto3) API Reference.
-
- SDK für Go V2
-
Anmerkung
Es gibt noch mehr dazu. GitHub Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. package main import ( "context" "encoding/json" "flag" "fmt" "log" "os" "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" ) // Each model provider defines their own individual request and response formats. // For the format, ranges, and default values for the different models, refer to: // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html type ClaudeRequest struct { Prompt string `json:"prompt"` MaxTokensToSample int `json:"max_tokens_to_sample"` // Omitting optional request parameters } type ClaudeResponse struct { Completion string `json:"completion"` } // main uses the AWS SDK for Go (v2) to create an Amazon Bedrock Runtime client // and invokes Anthropic Claude 2 inside your account and the chosen region. // This example uses the default settings specified in your shared credentials // and config files. func main() { region := flag.String("region", "us-east-1", "The AWS region") flag.Parse() fmt.Printf("Using AWS region: %s\n", *region) ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx, config.WithRegion(*region)) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } client := bedrockruntime.NewFromConfig(sdkConfig) modelId := "anthropic.claude-v2" prompt := "Hello, how are you today?" // Anthropic Claude requires you to enclose the prompt as follows: prefix := "Human: " postfix := "\n\nAssistant:" wrappedPrompt := prefix + prompt + postfix request := ClaudeRequest{ Prompt: wrappedPrompt, MaxTokensToSample: 200, } body, err := json.Marshal(request) if err != nil { log.Panicln("Couldn't marshal the request: ", err) } result, err := client.InvokeModel(ctx, &bedrockruntime.InvokeModelInput{ ModelId: aws.String(modelId), ContentType: aws.String("application/json"), Body: body, }) if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { fmt.Printf("Error: The Bedrock service is not available in the selected region. Please double-check the service availability for your region at https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/.\n") } else if strings.Contains(errMsg, "Could not resolve the foundation model") { fmt.Printf("Error: Could not resolve the foundation model from model identifier: \"%v\". Please verify that the requested model exists and is accessible within the specified region.\n", modelId) } else { fmt.Printf("Error: Couldn't invoke Anthropic Claude. Here's why: %v\n", err) } os.Exit(1) } var response ClaudeResponse err = json.Unmarshal(result.Body, &response) if err != nil { log.Fatal("failed to unmarshal", err) } fmt.Println("Prompt:\n", prompt) fmt.Println("Response from Anthropic Claude:\n", response.Completion) }
-
Einzelheiten zur API finden Sie InvokeModel
in der AWS SDK für Go API-Referenz.
-