Skip to content

Instantly share code, notes, and snippets.

@artyom
Created June 22, 2024 18:26
Show Gist options
  • Save artyom/b50dea7de96fe63fef7962f955dc90e1 to your computer and use it in GitHub Desktop.
Save artyom/b50dea7de96fe63fef7962f955dc90e1 to your computer and use it in GitHub Desktop.
Using AWS Bedrock converse API with Anthropic Claude 3.5 Sonnet
module bedrock-llm
go 1.23rc1
require (
github.com/aws/aws-sdk-go-v2/config v1.27.21
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0
)
require (
github.com/aws/aws-sdk-go-v2 v1.30.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.21 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.8 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.14 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.21.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.25.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.29.1 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
)
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
)
func main() {
log.SetFlags(0)
args := runArgs{}
flag.StringVar(&args.q, "q", args.q, "your prompt to LLM")
flag.Parse()
if err := run(context.Background(), args); err != nil {
log.Fatal(err)
}
}
type runArgs struct {
q string
}
func run(ctx context.Context, args runArgs) error {
if args.q == "" {
return errors.New("no query provided")
}
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return err
}
cl := bedrockruntime.NewFromConfig(cfg)
const claudeModelId = "anthropic.claude-3-5-sonnet-20240620-v1:0"
var modelId = claudeModelId
out, err := cl.ConverseStream(ctx, &bedrockruntime.ConverseStreamInput{
ModelId: &modelId,
Messages: []types.Message{
{
Role: types.ConversationRoleUser,
Content: []types.ContentBlock{&types.ContentBlockMemberText{Value: args.q}},
},
},
})
if err != nil {
return err
}
stream := out.GetStream()
defer stream.Close()
for evt := range stream.Events() {
switch v := evt.(type) {
case *types.ConverseStreamOutputMemberContentBlockDelta:
if d, ok := v.Value.Delta.(*types.ContentBlockDeltaMemberText); ok {
fmt.Print(d.Value)
}
case *types.ConverseStreamOutputMemberContentBlockStop:
fmt.Println()
case *types.ConverseStreamOutputMemberMessageStart:
case *types.ConverseStreamOutputMemberMessageStop:
default:
log.Printf("unknown event type %T: %+v", evt, evt)
}
}
return stream.Err()
}
@artyom
Copy link
Author

artyom commented Jun 28, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment