From 28aa8eebd25826622e240846aa08993d766d9bfa Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Tue, 28 Nov 2023 16:14:33 +0100 Subject: [PATCH] Add region configuration --- gov2/bedrock-runtime/README.md | 4 ++-- gov2/bedrock-runtime/actions/invoke_model.go | 22 ++++++++----------- .../invoke_model_with_response_stream.go | 8 +++---- gov2/bedrock-runtime/cmd/main.go | 8 +++---- gov2/bedrock-runtime/hello/hello.go | 12 +++++----- .../scenarios/scenario_invoke_models.go | 2 +- 6 files changed, 23 insertions(+), 33 deletions(-) diff --git a/gov2/bedrock-runtime/README.md b/gov2/bedrock-runtime/README.md index 8a3f1442748..40d53347060 100644 --- a/gov2/bedrock-runtime/README.md +++ b/gov2/bedrock-runtime/README.md @@ -63,10 +63,10 @@ functions within the same service. By default, examples are set to `us-east-1`. To specify a different region, use the `-region` flag as shown in this example: ``` -go run ./cmd -scenario=invokemodel -region=eu-central-1 +go run ./hello -region=eu-central-1 ``` -Be aware that not all regions may support Bedrock and its models, yet. Verify service availability for your region [here](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/). For available models per region, refer to the [AWS Management Console](https://console.aws.amazon.com/bedrock). +Be aware that not all regions may support Bedrock and its models yet. Verify service availability for your region [here](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/). For available models per region, refer to the [Bedrock dashboard](https://console.aws.amazon.com/bedrock) in the AWS Management Console. #### Hello Amazon Bedrock diff --git a/gov2/bedrock-runtime/actions/invoke_model.go b/gov2/bedrock-runtime/actions/invoke_model.go index 9a0302a2c98..d1053bfbc5d 100644 --- a/gov2/bedrock-runtime/actions/invoke_model.go +++ b/gov2/bedrock-runtime/actions/invoke_model.go @@ -8,7 +8,6 @@ import ( "encoding/json" "log" "fmt" - "os" "strings" "github.com/aws/aws-sdk-go-v2/aws" @@ -72,13 +71,12 @@ func (wrapper InvokeModelWrapper) InvokeClaude(prompt string) (string, error) { if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { - fmt.Printf("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") + log.Printf("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("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) + log.Printf("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("Couldn't invoke Anthropic Claude. Here's why: %v\n", err) + log.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err) } - os.Exit(1) } var response ClaudeResponse @@ -135,13 +133,12 @@ func (wrapper InvokeModelWrapper) InvokeJurassic2(prompt string) (string, error) if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { - fmt.Printf("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") + log.Printf("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("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) + log.Printf("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("Couldn't invoke AI21 Labs Jurassic-2. Here's why: %v\n", err) + log.Printf("Couldn't invoke AI21 Labs Jurassic-2. Here's why: %v\n", err) } - os.Exit(1) } var response Jurassic2Response @@ -196,13 +193,12 @@ func (wrapper InvokeModelWrapper) InvokeLlama2(prompt string) (string, error) { if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { - fmt.Printf("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") + log.Printf("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("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) + log.Printf("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("Couldn't invoke Meta Llama 2. Here's why: %v\n", err) + log.Printf("Couldn't invoke Meta Llama 2. Here's why: %v\n", err) } - os.Exit(1) } var response Llama2Response diff --git a/gov2/bedrock-runtime/actions/invoke_model_with_response_stream.go b/gov2/bedrock-runtime/actions/invoke_model_with_response_stream.go index 44dfe259c98..a9043bc612a 100644 --- a/gov2/bedrock-runtime/actions/invoke_model_with_response_stream.go +++ b/gov2/bedrock-runtime/actions/invoke_model_with_response_stream.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "log" - "os" "strings" "github.com/aws/aws-sdk-go-v2/aws" @@ -74,13 +73,12 @@ func (wrapper InvokeModelWithResponseStreamWrapper) InvokeModelWithResponseStrea if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { - fmt.Printf("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") + log.Printf("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("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) + log.Printf("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("Couldn't invoke Anthropic Claude. Here's why: %v\n", err) + log.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err) } - os.Exit(1) } resp, err := processStreamingOutput(output, func(ctx context.Context, part []byte) error { diff --git a/gov2/bedrock-runtime/cmd/main.go b/gov2/bedrock-runtime/cmd/main.go index aa5a20d4c4a..6aa67599b18 100644 --- a/gov2/bedrock-runtime/cmd/main.go +++ b/gov2/bedrock-runtime/cmd/main.go @@ -37,18 +37,16 @@ func main() { "scenario", "", fmt.Sprintf("The scenario to run. Must be one of %v.", choices)) - var region string - flag.StringVar(®ion, "region", "us-east-1", "The AWS region") - + var region = flag.String("region", "us-east-1", "The AWS region") flag.Parse() - fmt.Println("The selected region is: ", region) + fmt.Printf("Using AWS region: %s\n", *region) if runScenario, ok := scenarioMap[*scenario]; !ok { fmt.Printf("'%v' is not a valid scenario.\n", *scenario) flag.Usage() } else { - sdkConfig, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region)) + sdkConfig, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(*region)) if err != nil { log.Fatalf("unable to load SDK config, %v", err) } diff --git a/gov2/bedrock-runtime/hello/hello.go b/gov2/bedrock-runtime/hello/hello.go index 86ecbb24093..243a30d6094 100644 --- a/gov2/bedrock-runtime/hello/hello.go +++ b/gov2/bedrock-runtime/hello/hello.go @@ -39,13 +39,12 @@ type ClaudeResponse struct { // and config files. func main() { - var region string - flag.StringVar(®ion, "region", "us-east-1", "The AWS region") + region := flag.String("region", "us-east-1", "The AWS region") flag.Parse() - fmt.Println("Region: ", region) + fmt.Printf("Using AWS region: %s\n", *region) - sdkConfig, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region)) + sdkConfig, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(*region)) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) @@ -61,10 +60,10 @@ func main() { // Anthropic Claude requires you to enclose the prompt as follows: prefix := "Human: " postfix := "\n\nAssistant:" - prompt = prefix + prompt + postfix + wrappedPrompt := prefix + prompt + postfix request := ClaudeRequest { - Prompt: prompt, + Prompt: wrappedPrompt, MaxTokensToSample: 200, } @@ -95,7 +94,6 @@ func main() { if err != nil { log.Fatal("failed to unmarshal", err) } - fmt.Println("Prompt:\n", prompt) fmt.Println("Response from Anthropic Claude:\n", response.Completion) } diff --git a/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go b/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go index 58ffd9ea7ef..5dd3e073754 100644 --- a/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go +++ b/gov2/bedrock-runtime/scenarios/scenario_invoke_models.go @@ -102,7 +102,7 @@ func (scenario InvokeModelsScenario) InvokeLlama2(prompt string) { } func (scenario InvokeModelsScenario) InvokeWithResponseStream(prompt string) { - log.Println("\nClaude with response stream:\n") + log.Println("\nClaude with response stream:") _, err := scenario.responseStreamWrapper.InvokeModelWithResponseStream(prompt) if err != nil { panic(err) } log.Println()