-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
hello.go
104 lines (81 loc) · 3.12 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[gov2.bedrock-runtime.Hello]
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)
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)
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(context.Background(), &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)
}
// snippet-end:[gov2.bedrock-runtime.Hello]