1+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ package actions
5+
6+ import (
7+ "context"
8+ "encoding/json"
9+ "fmt"
10+ "log"
11+ "os"
12+ "strings"
13+
14+ "github.com/aws/aws-sdk-go-v2/config"
15+ "github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
16+ "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
17+ )
18+
19+ const (
20+ promptImage = `Describe what you see in this image in detail.`
21+ )
22+
23+ func main () {
24+ modelID := "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
25+ // load AWS configure
26+ cfg , err := config .LoadDefaultConfig (context .TODO (),
27+ config .WithRegion ("us-west-2" ), // replace your bedrock region
28+ )
29+ if err != nil {
30+ log .Fatal ("unable to load SDK configuration:" , err )
31+ }
32+
33+ // create bedrock runtime client
34+ client := bedrockruntime .NewFromConfig (cfg )
35+
36+ // read image file
37+ imagePath := "C:\\ Users\\ pc\\ Downloads\\ img.png" // Update with your image path
38+ imageBytes , err := os .ReadFile (imagePath )
39+ if err != nil {
40+ log .Fatal ("unable to read image:" , err )
41+ }
42+
43+ // create image block with format specified
44+ imageBlock := types.ContentBlockMemberImage {
45+ Value : types.ImageBlock {
46+ Format : types .ImageFormatPng , // Add the format based on your image type
47+ Source : & types.ImageSourceMemberBytes {
48+ Value : imageBytes ,
49+ },
50+ },
51+ }
52+
53+ // create message
54+ messages := types.Message {
55+ Role : "user" ,
56+ Content : []types.ContentBlock {
57+ & types.ContentBlockMemberText {
58+ Value : promptImage ,
59+ },
60+ & imageBlock ,
61+ },
62+ }
63+
64+ ctx := context .Background ()
65+
66+ // Use ConverseStream instead of Converse
67+ resp , err := client .ConverseStream (ctx , & bedrockruntime.ConverseStreamInput {
68+ ModelId : & modelID ,
69+ Messages : []types.Message {messages },
70+ })
71+ if err != nil {
72+ panic (err )
73+ }
74+
75+ // Get the event stream
76+ stream := resp .GetStream ()
77+ defer stream .Close ()
78+
79+ fmt .Println ("Streaming response:" )
80+ var fullResponse strings.Builder
81+
82+ // Process the streaming response
83+ for event := range stream .Events () {
84+ // Type switch to handle different event types
85+ switch v := event .(type ) {
86+ case * types.ConverseStreamOutputMemberContentBlockDelta :
87+ // Handle content block delta
88+ if delta , ok := v .Value .Delta .(* types.ContentBlockDeltaMemberText ); ok {
89+ text := delta .Value
90+ fmt .Print (text )
91+ fullResponse .WriteString (text )
92+ }
93+ case * types.ConverseStreamOutputMemberMessageStop :
94+ // Handle message stop event (contains usage info)
95+ fmt .Println ("\n \n Message complete" )
96+ }
97+ }
98+
99+ // Check for any errors that occurred during streaming
100+ if err := stream .Err (); err != nil {
101+ fmt .Printf ("\n Stream error: %v\n " , err )
102+ }
103+
104+ fmt .Println ("\n Done streaming" )
105+ fmt .Printf ("\n Full response:\n %s\n " , fullResponse .String ())
106+ }
107+
108+ func printJSON (data interface {}) {
109+ jsonBytes , err := json .MarshalIndent (data , "" , " " )
110+ if err != nil {
111+ log .Printf ("Error marshaling JSON: %v" , err )
112+ return
113+ }
114+ log .Printf ("\n JSON:\n %s\n " , string (jsonBytes ))
115+ }
0 commit comments