Skip to content

Commit

Permalink
Add region configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTraub committed Nov 29, 2023
1 parent c8b68f8 commit 28aa8ee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 33 deletions.
4 changes: 2 additions & 2 deletions gov2/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!--custom.instructions.end-->

#### Hello Amazon Bedrock
Expand Down
22 changes: 9 additions & 13 deletions gov2/bedrock-runtime/actions/invoke_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"log"
"fmt"
"os"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions gov2/bedrock-runtime/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ func main() {
"scenario", "",
fmt.Sprintf("The scenario to run. Must be one of %v.", choices))

var region string
flag.StringVar(&region, "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)
}
Expand Down
12 changes: 5 additions & 7 deletions gov2/bedrock-runtime/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ type ClaudeResponse struct {
// and config files.
func main() {

var region string
flag.StringVar(&region, "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)
Expand All @@ -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,
}

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion gov2/bedrock-runtime/scenarios/scenario_invoke_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 28aa8ee

Please sign in to comment.