Skip to content

Commit

Permalink
Add custom instructions to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTraub committed Nov 29, 2023
1 parent c3e5c00 commit c8b68f8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
8 changes: 7 additions & 1 deletion gov2/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ functions within the same service.


<!--custom.instructions.start-->
#### Region configuration
By default, examples are set to `us-east-1`. To specify a different region, use the `-region` flag as shown in this example:

> **Note:** These examples use the region `us-east-1`. To change the region,
```
go run ./cmd -scenario=invokemodel -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).
<!--custom.instructions.end-->

#### Hello Amazon Bedrock
Expand Down
18 changes: 9 additions & 9 deletions gov2/bedrock-runtime/actions/invoke_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func (wrapper InvokeModelWrapper) InvokeClaude(prompt string) (string, error) {
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "no such host") {
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")
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")
} else if strings.Contains(errMsg, "Could not resolve the foundation model") {
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)
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)
} else {
log.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err)
fmt.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err)
}
os.Exit(1)
}
Expand Down Expand Up @@ -135,11 +135,11 @@ func (wrapper InvokeModelWrapper) InvokeJurassic2(prompt string) (string, error)
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "no such host") {
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")
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")
} else if strings.Contains(errMsg, "Could not resolve the foundation model") {
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)
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)
} else {
log.Printf("Couldn't invoke AI21 Labs Jurassic-2. Here's why: %v\n", err)
fmt.Printf("Couldn't invoke AI21 Labs Jurassic-2. Here's why: %v\n", err)
}
os.Exit(1)
}
Expand Down Expand Up @@ -196,11 +196,11 @@ func (wrapper InvokeModelWrapper) InvokeLlama2(prompt string) (string, error) {
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "no such host") {
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")
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")
} else if strings.Contains(errMsg, "Could not resolve the foundation model") {
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)
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)
} else {
log.Printf("Couldn't invoke Meta Llama 2. Here's why: %v\n", err)
fmt.Printf("Couldn't invoke Meta Llama 2. Here's why: %v\n", err)
}
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (wrapper InvokeModelWithResponseStreamWrapper) InvokeModelWithResponseStrea
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "no such host") {
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")
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")
} else if strings.Contains(errMsg, "Could not resolve the foundation model") {
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)
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)
} else {
log.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err)
fmt.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err)
}
os.Exit(1)
}
Expand Down
20 changes: 16 additions & 4 deletions gov2/bedrock-runtime/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -52,8 +54,9 @@ func main() {

client := bedrockruntime.NewFromConfig(sdkConfig)

modelId := "anthropic.claude-v2"

prompt := "Hello, how are you today?"
fmt.Println("Prompt:\n", prompt)

// Anthropic Claude requires you to enclose the prompt as follows:
prefix := "Human: "
Expand All @@ -68,14 +71,22 @@ func main() {
body, err := json.Marshal(request)

result, err := client.InvokeModel(context.Background(), &bedrockruntime.InvokeModelInput {
ModelId: aws.String("anthropic.claude-v2"),
ModelId: aws.String(modelId),
ContentType: aws.String("application/json"),
Body: body,
})

if err != nil {
log.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err)
}
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

Expand All @@ -85,6 +96,7 @@ func main() {
log.Fatal("failed to unmarshal", err)
}

fmt.Println("Prompt:\n", prompt)
fmt.Println("Response from Anthropic Claude:\n", response.Completion)
}

Expand Down

0 comments on commit c8b68f8

Please sign in to comment.