Last active
February 7, 2024 19:46
-
-
Save micahwalter/fe9401be7ba252223d8fe1844b08f8bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"time" | |
"github.com/aws/aws-sdk-go-v2/config" | |
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | |
) | |
type Body struct { | |
Prompt string `json:"prompt"` | |
MaxTokens int `json:"max_tokens_to_sample"` | |
Temperature int `json:"temperature"` | |
TopK int `json:"top_k"` | |
TopP float64 `json:"top_p"` | |
StopSequences []string `json:"stop_sequences"` | |
AnthropicVersion string `json:"anthropic_version"` | |
} | |
type AnthropicResponseBody struct { | |
Completion string `json:"completion"` | |
StopReason string `json:"stop_reason"` | |
} | |
func main() { | |
// execution timer | |
start := time.Now() | |
// get an artwork from CMA | |
id := 160729 | |
const endpoint = "https://openaccess-api.clevelandart.org/api/" | |
resp, err := http.Get(fmt.Sprintf(endpoint+"artworks/%v", id)) | |
if err != nil { | |
log.Fatalf("error: %v", err) | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != 200 { | |
log.Fatalf("could not fetch data: %s", resp.Status) | |
} | |
b, err := io.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatalf("could not read response: %v", err) | |
} | |
fmt.Println(string(b)) | |
// create a prompt and send to Amazon Bedrock | |
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1")) | |
if err != nil { | |
log.Fatalf("unable to load AWS SDK config, %v", err) | |
} | |
svc := bedrockruntime.NewFromConfig(cfg) | |
accept := "*/*" | |
contentType := "application/json" | |
modelId := "anthropic.claude-v2" | |
var body Body | |
document := "<document>" + string(b) + "</document>\n\n" | |
prompt := "\n\nHuman: " + document + "Write a short poem inspired by the artwork described by the <document> \n\nAssistant:" | |
body.Prompt = prompt | |
body.MaxTokens = 300 | |
body.Temperature = 1 | |
body.TopK = 250 | |
body.TopP = 0.999 | |
body.StopSequences = []string{ | |
"\n\nHuman:", | |
} | |
bodyString, err := json.Marshal(body) | |
if err != nil { | |
fmt.Printf("unable to Marshal, %v", err) | |
} | |
bedrockResp, err := svc.InvokeModel(context.TODO(), &bedrockruntime.InvokeModelInput{ | |
Accept: &accept, | |
ModelId: &modelId, | |
ContentType: &contentType, | |
Body: bodyString, | |
}) | |
if err != nil { | |
log.Fatalf("error from Bedrock, %v", err) | |
} | |
//var out map[string]any | |
var out AnthropicResponseBody | |
err = json.Unmarshal(bedrockResp.Body, &out) | |
if err != nil { | |
fmt.Printf("unable to Unmarshal JSON, %v", err) | |
} | |
// for k, v := range out { | |
// fmt.Printf("k: %s, v: %v\n", k, v) | |
// } | |
fmt.Println(out.Completion) | |
fmt.Println(out.StopReason) | |
elapsed := time.Since(start) | |
log.Printf("execution took %s", elapsed) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment