Creating a Telegram bot with RAG and function calling
Language models can maintain conversation context and perform knowledge base search using Yandex Cloud ML SDK, RAG
In this tutorial, you will create a wine sales chat assistant based on the YandexGPT 5 model. The assistant relies on function calling and RAG to access the database of wines and regions as well as the price list indicating wines in stock. You will configure the assistant in a Yandex DataSphere notebook and interact with it via Telegram.
To create an AI assistant for Telegram:
- Set up your infrastructure.
- Create an API key for the service account.
- Create a Telegram bot.
- Create secrets.
- Clone the repository.
- Test the bot.
If you no longer need the resources you created, delete them.
Getting started
Before getting started, register in Yandex Cloud, set up a community, and link your billing account to it.
- On the DataSphere home page
, click Try for free and select an account to log in with: Yandex ID or your working account with the identity federation (SSO). - Select the Yandex Identity Hub organization you are going to use in Yandex Cloud.
- Create a community.
- Link your billing account to the DataSphere community you are going to work in. Make sure you have a linked billing account and its status is
ACTIVE
orTRIAL_ACTIVE
. If you do not have a billing account yet, create one in the DataSphere interface.
Required paid resources
The infrastructure support cost for creating an AI assistant includes:
- Fee for using DataSphere computing resources.
- Fee for text generation by the model.
Set up your infrastructure
Log in to the Yandex Cloud management console
If you have an active billing account, you can go to the cloud page
Note
If you are using an identity federation to work with Yandex Cloud, you might not have access to billing details. In this case, contact your Yandex Cloud organization administrator.
Create a folder
- In the management console
, select a cloud and click Create folder. - Name your folder, e.g.,
data-folder
. - Click Create.
Create a service account for the DataSphere project
You need to create a service account to access the model from the notebook.
- Navigate to
data-folder
. - On the left-hand panel, click
All services and select Identity and Access Management, or find it using the search bar on the dashboard. - Click Create service account.
- Enter a name for the service account, e.g.,
gpt-user
. - Click Add role and assign the
ai.languageModels.user
role to the service account. - Click Create.
Add the service account to the project
To enable the service account to access the model from the notebook, add it to the list of project members.
-
Select the project in your community or on the DataSphere home page
in the Recent projects tab. - In the Members tab, click Add member.
- Select the
gpt-user
account and click Add.
Create an API key for the service account
To enable the service account to access the model, create an API key.
- In the management console
, navigate todata-folder
. - On the left-hand panel, click
All services and select Identity and Access Management, or find it using the search bar on the dashboard. - In the left-hand panel, select
Service accounts. - In the list that opens, select the
gpt-user
service account. - In the top panel, click
Create new key and select Create API key. - In the Scope field, select
yc.ai.languageModels.execute
. - Click Create.
- Save the ID and secret key.
Create a Telegram bot
- Open a chat with @botfather
. - Follow the prompts to choose a name and nickname for your Telegram bot.
- After the bot is created successfully, you will receive a link to the chat with your bot and a token. Save them for the next steps.
Create secrets
To use the API key, folder ID, and bot access token from the notebook, create secrets with the relevant values.
-
Select the project in your community or on the DataSphere home page
in the Recent projects tab. - Under Project resources, select
Secret. - Click Create.
- In the Name field, enter the name for the secret:
api_key
. - In the Value field, paste the key ID.
- Click Create.
- Similarly, create two more secrets:
folder_id
with the folder ID andtg_token
with the Telegram bot token.
Clone the repository
-
Select the project in your community or on the DataSphere home page
in the Recent projects tab. - Click Open project in JupyterLab and wait until loading is complete.
- On the left-hand panel, under
File Browser, create or select the folder to store the repository clone and navigate to it. - In the top menu, click Git and select Clone a Repository.
- In the window that opens, enter the repository URI,
https://github.com/yandex-cloud-examples/yc-ai-wine-assistant
. - Enable the Download the repository option and click Clone.
Run the notebook
The notebook code connects to the model, provides it with the relevant functions, and configures the Telegram bot.
- Navigate to the cloned repository folder.
- Open the
advanced-assistant.ipynb
notebook. - Run the notebook cells one by one. Do not run cells in the Deleting unused resources section until you finish using the bot.
- The last cell under Creating a wine assistant in Telegram will get your bot ready to run.
Test the bot
-
Open the chat with your bot using the link you got earlier.
-
Click START.
-
Enter your query in the message input field. Here is an example:
What wine pairs well with white chocolate?
Result:
White chocolate may go well with white and rosé dessert or fortified wines. Consider options like Chardonnay, Sémillon, Riesling, or Viognier. If you are interested in a specific wine, I can check its availability and price in our price list.
How to delete the resources you created
To stop paying for the resources you created, delete the project.