AI Chatbot powered by Amazon Bedrock 🚀🤖
Tags: genai, bedrock, langchain, aws
Published: 2023-09-29 16:48:30

## Introduction
I have created a sample chatbot application that uses [Chainlit](https://github.com/Chainlit/chainlit) and [LangChain](https://github.com/langchain-ai/langchain) to showcase Amazon Bedrock.

You can interact with the AI assistant while switching between multiple models.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45nlkupdpd4vp02m149a.png)

This sample application has been tested in the following environments:

* AWS Region: us-east-1
* AWS Copilot CLI: v1.30.1
* Python: v3.11.5
* boto3: v1.28.57
* LangChain: v0.0.305
* Chainlit: v0.7.0

**Note:** To support the GA Version of the Bedrock API, make sure to use versions of LangChain and boto3 that are newer than those mentioned above.

## Source Code
Check out my GitHub repository!

{% embed https://github.com/hayao-k/Bedrock-AIChatbot-Sample %}

## Start using Bedrock
Before you can start using Amazon Bedrock, you must request access to each model. To add access to a model, go to the Model Access section in the Bedrock console and click Edit.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1dxu8lakvshdszsenn7.png)

Select the models you want to use and save the settings.

**Note:** Third-party models such as Jurassic-2 and Claude require access through the AWS Marketplace. This means you will also be charged a Marketplace fee for using the model.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2rp8xz11udmhdsz29pg.png)

It will take some time for each model to become available; models with an Access status of Access granted are ready for use.

**Note:** Some models, such as Titan Text G1 - Express, are in Preview and may not be immediately available.

## How to Deploy
The GitHub repository for the sample application contains a manifest file for deploying the container to AWS App Runner using the AWS Copilot CLI.

Follow these steps to deploy:

1. Install the AWS Copilot CLI if necessary

```bash
sudo curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux && sudo chmod +x /usr/local/bin/copilot
```

2. Deploy to App Runner!

```bash
 export AWS_REGION=us-east-1
 copilot app init bedrockchat-app
 copilot deploy --name bedrockchat --env dev
```

If the deployment is successful, access the URL displayed in the message. If the following screen appears, the deployment has succeeded!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkb60l81bi2axpse2lmq.png)


To delete an environment, execute the following command:

```bash
copilot app delete
```

## How to use the app
The model, temperature, and maximum token size can be changed from the Settings panel.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq9xzx8ljbwc99vci22u.png)

The sample code allows you to choose between Claude v2, Jurassic-2 Ultra, and Amazon Titan Text G1 - Express. This enables you to compare performance while switching between models.

**Note:** As of 9/29/2023, Amazon Titan Text G1 - Express is in Preview status. It will be rolled out gradually, so it may not be available for some accounts.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3typxm9c6gdib5ac0hm.png)

You can enjoy a free-flowing conversation with the AI assistant! The conversation history during the session is retained, allowing it to reply to you while considering the context.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka9lck0zqto1lpgb72iu.png)

I hope this will be of help to someone else.