DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Securing Generative AI Applications
  • GenAI in Java With Merlinite, Quarkus, and Podman Desktop AI Lab
  • GenAI in Application Development
  • The Future of Digital Products: Will AI-Assistants Replace Applications?

Trending

  • Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality
  • Explore the Complete Guide to Various Internet of Things (IoT) Protocols
  • Empowering Citizen Developers With Low- and No-Code Tools: Changing Developer Workflows and Empowering Non-Technical Employees to Build Apps
  • Unlocking Potential With Mobile App Performance Testing
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. A Single API for All Your Conversational Generative AI Applications

A Single API for All Your Conversational Generative AI Applications

Use the Converse API in Amazon Bedrock to create generative AI applications using a single API across multiple foundation models.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Jun. 11, 24 · Tutorial
Like (1)
Save
Tweet
Share
1.7K Views

Join the DZone community and get the full member experience.

Join For Free

You can now use the Converse API in Amazon Bedrock to create conversational applications like chatbots and support assistants. It is a consistent, unified API that works with all Amazon Bedrock models that support messages. The benefit is that you have a single code base (application) and use it with different models — this makes it preferable to use the Converse API over InvokeModel (or InvokeModelWithResponseStream) APIs.

I will walk you through how to use this API with the AWS SDK for Go v2.

Converse API Overview

Here is a super-high-level overview of the API — you will see these in action when we go through some of the examples.

  • The API consists of two operations - Converse and ConverseStream
  • The conversations are in the form of a Message object, which are encapsulated in a ContentBlock.
  • A ContentBlock can also have images, which are represented by an ImageBlock.
  • A message can have one of two roles - user or assistant
  • 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.

Let's explore a few sample apps now.

Basic Example

Refer to the **Before You Begin* section in this blog post to complete the prerequisites for running the examples. This includes installing Go, configuring Amazon Bedrock access, and providing necessary IAM permissions.*

Let's start off with a simple example. You can refer to the complete code here.

To run the example:

Go
 
git clone https://github.com/abhirockzz/converse-api-bedrock-go
cd converse-api-bedrock-go

go run basic/main.go


The response may be different in your case:

response may be different

The crux of the app is a for loop in which:

  • A types.Message instance is created with the appropriate role (user or assistant)
  • Sent using the Converse API
  • The response is collected and added to the existing list of messages
  • The conversation continues until the app is exited
Go
 
//...
    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)
    }
//...


I used the Claude Sonnet model in the example. Refer to Supported models and model features for a complete list.

Multi-Modal Conversations: Combine Image and Text

You can also use the Converse API to build multi-modal applications that work images — note that they only return text, for now.

You can refer to the complete code here.

To run the example:

Go
 
go run multi-modal-chat/main.go


I used the following picture of pizza and asked "What's in the image?":

Here is the output:

output

This is a simple single-turn exchange, but feel free to continue using a combination of images and text to continue the conversation.

The conversation for loop is similar to the previous example, but it has the added benefit of using the image data type with the help of types.ImageBlock:

Go
 
//...
types.ContentBlockMemberImage{
    Value: types.ImageBlock{
        Format: types.ImageFormatJpeg,
        Source: &types.ImageSourceMemberBytes{
            Value: imageContents,
        },
    },
}
//...


**Note: *imageContents is nothing but a []byte representation of the image.*

Streaming Chat

Streaming provides a better user experience because the client application does not need to wait for the complete response to be generated for it to start showing up in the conversation.

You can refer to the complete code here.

To run the example:

Go
 
go run chat-streaming/main.go


Streaming-based implementations can be a bit complicated. But in this case, it was simplified due to the clear API abstractions that the Converse API provided, including partial response types such as types.ContentBlockDeltaMemberText.

The application invokes ConverseStream API and then processes the output components in bedrockruntime.ConverseStreamOutput.

Go
 
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
}


Wrap Up

There are a few other awesome things the 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!

As I always say, Python does not have to be the only way to build generative AI-powered machine learning applications. As an AI engineer, choose the right tools (including foundation models) and programming languages for your solutions. I may be biased toward Go but this applies equally well to Java, JS/TS, C#, etc.

Happy building!

AI API applications generative AI

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Securing Generative AI Applications
  • GenAI in Java With Merlinite, Quarkus, and Podman Desktop AI Lab
  • GenAI in Application Development
  • The Future of Digital Products: Will AI-Assistants Replace Applications?

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: