Welcome to building GenAI Telegram Chat assistant using Amazon Bedrock Agents.
📱 You can ask agent to pull message updates from a specific Telegram chat.
The chat name doesn't have to be an exact match, because orchestration Lambda function will handle name-to-id conversion.
🔃 If chat is in a language other than English, you can ask to translate it to English
📑 You can ask for a summary of messages from a specific chat.
If your chat is in a language other than English, the language will be detected and the chat will be translated to English before summarization.
🔍 You can query a specific chat by asking any questions.
If your chat is in a language other than English, the language will be detected and the chat will be translated to English before querying.
Agents for Amazon Bedrock helps you accelerate generative artificial intelligence (AI) application development by orchestrating multistep tasks. They can make different API calls. Agents extend FMs to understand user requests, break down complex tasks into multiple steps, carry on a conversation to collect additional information, and take actions to fulfill the request.
- AWS News Blog: Enable Foundation Models to Complete Tasks With Agents for Amazon Bedrock
- AWS News Blog: Agents for Amazon Bedrock is now available with improved control of orchestration and visibility into reasoning
- How Agents for Amazon Bedrock works
Go to Telegram bot father, create bot and save the token. Be careful with the token and save it in a safe place. We will use it when getting message updates.
Add your bot to chat(s) from where you want to receive updates.
We will create a DynamoDB table with Telegram chats that our bot can reach. The main purpose of the table is to align chat names with their ids. The Bedrock agent can refer the chat by name (even without exact match), and Lambda function can find in DynamoDB a corresponding chat id and filter messages updates by this chat id.
As per May 2024, Bedrock agents are available in the following regions:
- US East (N. Virginia)
- US West (Oregon)
- Asia Pacific (Singapore)
- Asia Pacific (Sydney)
- Asia Pacific (Tokyo)
- Europe (Frankfurt)
That's why it will be a good idea to create DynamoDB table and Lambda Function in the same region where we are planning to create an agent (to decrease cross-region latency and avoid potential cross-region data transfer costs).
- Navigate to the DynamoDB console and click on
Create table
. - Enter
bedrock_agent_chats
asTable name
andbot_id
asPartition key
. Click on theCreate table
button.
- Once the table is created, let's populate it with values. Click on the created table, then click on the
Actions
button and selectCreate item
from the drop-down list. - Paste
100200300
asbot_id
(or another Id value, but make sure to use the same bot Id in Lambda function environment variables later on) - Click on the
Add new attribute
button and selectList
. Pastechats
as Attribute name - Click on the first
Insert a field
button and chooseMap
- Click on the lowest
Insert a field
button and chooseString
. Pastechat_id
as Attribute name and your Telegram chat id (typically starting from '-') as value. How to get telegram chat id. - Click on the lowest
Insert a field
button and chooseString
. Pastechat_name
as Attribute name and your Telegram chat name as value. - Repeat steps 6-8 to add more chats, once ready, click on the
Create item
button.
Lambda Function will manage all the logic required for the agent actions. Code contains set of APIs that Bedrock agent will call. The function will then format the response and send it back to the agent.
- Navigate to the Lambda Console and click on
Create function
button. Make sure thatUS East (N. Virginia)
orUS West (Oregon)
is selected as region. - Paste
ChatSummarizer
as a function name and choosePython 3.12
as a runtime - Click on
Create function
button in the bottom of the page
- Once the function is created, click on the Configuration Tab in the same page and Choose
Permissions
from the left side panel - Click on
Add permissions
button in Resource-based policy statement section to provide the permission to invoke lambda functions from Bedrock - Provide Statement Id as
agent
, Principal asbedrock.amazonaws.com
and Action aslambda:InvokeFunction
. Click Save after adding all the above three information.
- Add the following Policy Statement to your Execution role, so Lambda can call Bedrock. (You can find details here)
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": "*"
}
]
}
- Add
AmazonDynamoDBReadOnlyAccess
andTranslateReadOnly
policies to your Execution role, so Lambda can call DynamoDB, Comprehend and Translate.
- Once permissions are updated, go back to the
ChatSummarizer
lambda, click on theConfiguration
Tab in the same page and ChooseEnvironment variables
from the left side panel - Click
Edit
button to add environment variables - By clicking
Add environment variable
button add the following key value pairs:
BOT_ID
:100200300
(you can change it to any unique value, make sure you use the same Id for your bot_id partition key in DynamoDB table)BOT_TOKEN
: insert your Telegram bot token obtained in Step 0DEFAULT_CHAT_ID
: value should start from-
, paste the Telegram chat id you want to use as defaultDYNAMODB_TABLE_NAME
:bedrock_agent_chats
- Choose
General configuration
from the left side panel - Click on the
Edit
button - Modify
Timeout
by increasing it to 15 seconds to make sure your Lambda won't fail while waiting for Bedrock models to respond. - Click on the
Save
button
- To use this project code, clone the repo:
https://github.com/dashapetr/smart-assistant-agent.git
cd smart-assistant-agent
- Create a new directory named package into which you will install your dependencies.
mkdir package
- Install your dependencies in the package directory.
pip install --target ./package requests
pip install --target ./package fuzzywuzzy
- Create a .zip file with the installed libraries at the root.
cd package
zip -r ../my_deployment_package.zip .
This generates a my_deployment_package.zip
file in your project directory.
- Add code files to the root of the .zip file
cd ..
zip my_deployment_package.zip lambda_function.py
zip my_deployment_package.zip tools.py
- Now you can go back to the Lambda function, click on the
Code
tab in the same page - Click on the
Upload from
button and select.zip file
in the dropdown list - Select
my_deployment_package.zip
and upload it, click on theSave
button - Your code should appear inside the
Code source
window
Zip deployment package creation steps are described with additional details in the documentation.
Since our Lambda contains a set of APIs, you may want to create several test events to test each API.
- Click on the
Test
tab near the top of the page. - Fill in
Event name
:summarize
- Paste the code from
test-payload-lambda/summarize.json
inEven JSON
window. This will be a test event for thesummarize
API that matches how the Agent will send a request. - Click on
Save
and thenTest
to execute the Lambda function. You should see the results of the function invocation, which will be a summarization response from the Titan Model.
- Click on
Create new event
button and repeat steps 2-4 to add more test events (you can find JSON payloads in thetest-payload-lambda
folder)
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to HTTP APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code.
Bedrock agent will be able to understand what tool to use based on the user request, the agent then will call the correct endpoint due to the OpenAPI spec.
- Go to S3 console and click on the
Create bucket
button - Make sure that
US East (N. Virginia)
orUS West (Oregon)
is selected as region (select the same where your DynamoDB and Lambda reside). - Give your bucket a unique name, for example
bucket-for-agent-NUMBERS
. ClickCreate bucket
.
- Select your created bucket, click on the
Upload
button. - You can drag and drop
openapi-schema.json
file and then click on theUpload
button
Now as all the preparation is done, we can proceed with Bedrock agent creation.
Prerequisites: we will use Anthropic Claude 3 Haiku v1
as model, make sure that you have access to it in your account.
Note: the new simplified experience to create and manage Agents in the US East (N. Virginia) and US West (Oregon) AWS Regions has been introduced recently. The steps below reflect this simplified version.
- Go to Bedrock console, select
Agents
in the left navigation panel, then click on theCreate Agent
button - Provide
smart-assistant-agent
as Agent name, provide a description (optional). Click on theCreate
button. - You are now presented with the Agent builder, the place where you can access and edit the overall configuration of an agent.
We will select
Anthropic Claude 3 Haiku v1
as model (Pricing); Paste the following text as Instructions for the Agent:
You are a smart assistant designed to pull out the messages from a specific Telegram chat.
You will translate pulled messages into English if they are not in English.
If you are asked to summarize messages, summarize messages.
If you are asked a specific question, query the chat messages based on the provided prompt.
Leave all the rest as default. Then, choose Save
to update the configuration of the agent.
- Next, we will add Action group: choose
Add
in the Action groups section. Usechat-reading-summarizing-action-group
as Action group name. In the Action group type section, selectDefine with API schemas
. In the Action group invocation section, selectSelect an existing Lambda function
and selectChatSummarizer
as Lambda function. In the Action group schema section, selectSelect an existing API schema
, browse S3 and select theopenapi-schema.json
file. Click onSave and exit
.
Once the agent is created, feel free to test it! Look for the Test Agent
section, choose Prepare
to prepare the agent. Ask questions and observe results.
Additionally, you can evaluate the chained process of agents reasoning by looking at Trace for your request. It contains reasoning steps for Pre-Processing trace, Orchestration and Knowledge Base, and Post-Processing trace
Bedrock Agents would be a great choice for orchestration and automation for chains-like tasks. They are very flexible and easily extensible, so they can act as smart assistant in various scenarios (for example, customer support).
If you enjoyed agents, you might also like:
- Amazon Bedrock Agents Quickstart workshop by Banjo Obayomi
- Building an Amazon Bedrock JIRA Agent with Source Code Knowledge Base - Part 1 and Part 2 by Martyn Kilbryde
- Shopify Customer Care powered by GenAI by Ricardo Ceci
- Build a retail agent by Manju Prasad and Amit Arora
- Webscrape and internet search agent by Justin Ossai
- Automate the insurance claim lifecycle using Agents and Knowledge Bases for Amazon Bedrock by Kyle Blocksom