A single API for all your conversational generative AI applications
Use the Converse API to create conversational generative AI applications with a single API across multiple models
Converse
API over InvokeModel (or InvokeModelWithResponseStream) APIs.- The API consists of two operations -
Converse
andConverseStream
- The conversations are in the form of a
Message
object, which are encapsulated in aContentBlock
. - A
ContentBlock
can also have images, which are represented by anImageBlock
. - A message can have one of two roles -
user
orassistant
- For streaming response, use the
ConverseStream
API - The streaming output (
ConverseStreamOutput
) has multiple events, each of which has different response items such as the text output, metadata etc.
1
2
3
4
git clone https://github.com/abhirockzz/converse-api-bedrock-go
cd converse-api-bedrock-go
go run basic/main.go

for
loop in which:- Sent using the
Converse
API - The response is collected and added to existing list of messages
- The conversation continues, until the app is exited
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
//...
for {
fmt.Print("\nEnter your message: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
userMsg := types.Message{
Role: types.ConversationRoleUser,
Content: []types.ContentBlock{
&types.ContentBlockMemberText{
Value: input,
},
},
}
converseInput.Messages = append(converseInput.Messages, userMsg)
output, err := brc.Converse(context.Background(), converseInput)
if err != nil {
log.Fatal(err)
}
reponse, _ := output.Output.(*types.ConverseOutputMemberMessage)
responseContentBlock := reponse.Value.Content[0]
text, _ := responseContentBlock.(*types.ContentBlockMemberText)
fmt.Println(text.Value)
assistantMsg := types.Message{
Role: types.ConversationRoleAssistant,
Content: reponse.Value.Content,
}
converseInput.Messages = append(converseInput.Messages, assistantMsg)
}
//...
Converse
API to build multi-modal application that work images - note that they only return text, for now.1
go run multi-modal-chat/main.go

1
2
3
4
5
6
7
8
9
10
//...
types.ContentBlockMemberImage{
Value: types.ImageBlock{
Format: types.ImageFormatJpeg,
Source: &types.ImageSourceMemberBytes{
Value: imageContents,
},
},
}
//...
imageContents
is nothing but a []byte
representation of the image.1
go run chat-streaming/main.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
func processStreamingOutput(output *bedrockruntime.ConverseStreamOutput, handler StreamingOutputHandler) (types.Message, error) {
var combinedResult string
msg := types.Message{}
for event := range output.GetStream().Events() {
switch v := event.(type) {
case *types.ConverseStreamOutputMemberMessageStart:
msg.Role = v.Value.Role
case *types.ConverseStreamOutputMemberContentBlockDelta:
textResponse := v.Value.Delta.(*types.ContentBlockDeltaMemberText)
handler(context.Background(), textResponse.Value)
combinedResult = combinedResult + textResponse.Value
case *types.UnknownUnionMember:
fmt.Println("unknown tag:", v.Tag)
}
}
msg.Content = append(msg.Content,
&types.ContentBlockMemberText{
Value: combinedResult,
},
)
return msg, nil
}
Converse
API does to make your life easier.- It allows you to pass inference parameters specific to a model.
- You can also use the
Converse
API to implement tool use in your applications. - If you are using Mistral AI or Llama 2 Chat models, the
Converse
API will embed your input in a model-specific prompt template that enables conversations - one less thing to worry about!
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.