# AI Chatbot powered by Amazon Bedrock 🚀🤖

## 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.

![](https://d7xrug9ye6o3w.cloudfront.net/overview.png align="left")

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!

%[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.

![](https://camo.qiitausercontent.com/52dab616000be207e88689463d49041929f2cf9e/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3132353130352f32343832323766382d353636332d383330642d653938342d3437656237303835306438612e706e67 align="left")

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.

![](https://camo.qiitausercontent.com/9318b9aede91ab7bfd323100df7609eaa6695aee/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3132353130352f62613838663532332d623737372d346664352d616435372d3231323035306133643738352e706e67 align="left")

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.

![](https://camo.qiitausercontent.com/bb97a8907a589946cf1917e3c9fd3cb6823598b5/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3132353130352f36373839356437642d653835382d623035382d346539382d3063613535383236613532352e706e67 align="left")

## 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!

![](https://camo.qiitausercontent.com/24d4fa1ce28ce17c4730ec90e452ebfcb1e51c07/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3132353130352f64633066306631362d613865622d366563392d356164322d3937326436633732333937332e706e67 align="left")

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.

![](https://camo.qiitausercontent.com/31b9b0ff3338c0f86c92781e61af24b127b7c8bf/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3132353130352f39396463326339352d346136622d396431652d646166392d3731663939393136323066372e706e67 align="left")

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.

![](https://camo.qiitausercontent.com/f8034393a99c63c88655bd8adf2381ee4d82e42e/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3132353130352f64643335643366662d643561302d666265632d623335392d6430383564376364616137372e706e67 align="left")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695960398620/5ae3791e-e861-486d-9ba6-163ce531fe24.png align="center")

I hope this will be of help to someone else.
