[Yandex Cloud documentation](../../index.md) > [Yandex DataSphere](../index.md) > [Tutorials](index.md) > Usage > Creating an AI assistant for Telegram

# Creating an AI assistant for Telegram

{% note info %}

We do not recommend using AI Assistant API in new projects. To create AI agents, use the Responses API.

{% endnote %}

Language models can maintain conversation context and perform knowledge base search using Yandex Cloud ML SDK, [RAG](https://en.wikipedia.org/wiki/Retrieval-augmented_generation), and [AI Assistant API](https://aistudio.yandex.ru/docs/en/ai-studio/responses/index).

In this tutorial, you will create a wine sales chat assistant based on the YandexGPT 5 model. The assistant relies on [function calling](https://aistudio.yandex.ru/docs/en/ai-studio/concepts/generation/function-call) 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:

1. [Set up your infrastructure](#infra).
1. [Create an API key for the service account](#create-key).
1. [Create a Telegram bot](#create-bot).
1. [Create secrets](#create-secrets).
1. [Clone the repository](#clone-repo).
1. [Test the bot](#test-bot).

If you no longer need the resources you created, [delete them](#clear-out).

## Getting started {#before-you-begin}

Before getting started, register in Yandex Cloud, set up a [community](../concepts/community.md), and link your [billing account](../../billing/concepts/billing-account.md) to it.
1. [On the DataSphere home page](https://datasphere.yandex.cloud), click **Try for free** and select an account to log in with: Yandex ID or your working account with the identity federation (SSO).
1. Select the [Yandex Identity Hub organization](../../organization/index.md) you are going to use in Yandex Cloud.
1. [Create a community](../operations/community/create.md).
1. [Link your billing account](../operations/community/link-ba.md) to the DataSphere community you are going to work in. Make sure you have a linked billing account and its [status](../../billing/concepts/billing-account-statuses.md) is `ACTIVE` or `TRIAL_ACTIVE`. If you do not have a billing account yet, create one in the DataSphere interface.

### Required paid resources {#paid-resources}

The infrastructure support cost for creating an AI assistant includes:

* Fee for using [DataSphere computing resources](../pricing.md).
* Fee for [text generation](https://aistudio.yandex.ru/docs/en/pricing) by the model.

## Set up your infrastructure {#infra}

Log in to the Yandex Cloud [management console](https://console.yandex.cloud) and select the organization you use to access DataSphere. On the [**Yandex Cloud Billing**](https://center.yandex.cloud/billing/accounts) page, make sure you have a billing account linked.

If you have an active billing account, you can go to the [cloud page](https://console.yandex.cloud/cloud) to create or select a folder to run your infrastructure.

{% note info %}

If you are using an [identity federation](../../organization/concepts/add-federation.md) to work with Yandex Cloud, you might not have access to billing details. In this case, contact your Yandex Cloud organization administrator.

{% endnote %}

### Create a folder {#create-folder}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select a cloud and click ![create](../../_assets/console-icons/plus.svg) **Create folder**.
  1. Name your folder, e.g., `data-folder`.
  1. Click **Create**.

{% endlist %}

### Create a service account for the DataSphere project {#create-sa}

You need to create a [service account](../../iam/concepts/users/service-accounts.md) to access the model from the notebook.

{% list tabs group=instructions %}

- Management console {#console}

  1. Navigate to `data-folder`.
  1. On the left-hand panel, click ![image](../../_assets/console-icons/dots-9.svg) **All services** and select **Identity and Access Management**, or find it using the search bar on the dashboard.
  1. Click **Create service account**.
  1. Specify the service account name, e.g., `gpt-user`.
  1. Click **Add role** and assign the `ai.languageModels.user` role to this service account.
  1. Click **Create**.

{% endlist %}

### Add the service account to the project {#sa-to-project}

To enable the service account to access the model from the notebook, add it to the list of project members.

{% list tabs group=instructions %}

- Management console {#console}

  1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
  1. In the **Members** tab, click **Add member**.
  1. Select the `gpt-user` account and click **Add**.

{% endlist %}

## Create an API key for the service account {#create-key}

To enable the service account to access the model, create an [API key](../../iam/concepts/authorization/api-key.md).

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to `data-folder`.
  1. On the left-hand panel, click ![image](../../_assets/console-icons/dots-9.svg) **All services** and select **Identity and Access Management**, or find it using the search bar on the dashboard.
  1. In the left-hand panel, select ![FaceRobot](../../_assets/console-icons/face-robot.svg) **Service accounts**.
  1. In the list that opens, select the `gpt-user` service account.
  1. In the top panel, click ![image](../../_assets/console-icons/plus.svg) **Create new key** and select **Create API key**.
  1. In the **Scope** field, select `yc.ai.languageModels.execute`.
  1. Click **Create**.
  1. Save the ID and the secret key.

{% endlist %}

## Create a Telegram bot {#create-bot}

1. Open a chat with [@botfather](https://t.me/botfather).
1. Follow the prompts to choose a name and nickname for your Telegram bot.
1. 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 {#create-secrets}

To use the API key, folder ID, and bot access token from the notebook, create [secrets](../concepts/secrets.md) with the relevant values.

1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
1. Under **Project resources**, select ![secret](../../_assets/console-icons/shield-check.svg) **Secret**.
1. Click **Create**.
1. In the **Name** field, enter the name for the secret: `api_key`.
1. In the **Value** field, paste the key ID.
1. Click **Create**.
1. Similarly, create two more secrets: `folder_id` with the folder ID and `tg_token` with the Telegram bot token.

## Clone the repository {#clone-repo}

1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
1. Click **Open project in JupyterLab** and wait for the loading to complete.
1. On the left-hand panel, under ![folder](../../_assets/datasphere/jupyterlab/folder.svg) **File Browser**, create or select the folder to store the repository clone and navigate to it.
1. In the top menu, click **Git** and select **Clone a Repository**.
1. In the window that opens, enter the repository URI, `https://github.com/yandex-cloud-examples/yc-ai-wine-assistant`.
1. Enable the **Download the repository** option and click **Clone**.

## Run the notebook {#run-notebook}

The notebook code connects to the model, provides it with the relevant functions, and configures the Telegram bot.

1. Navigate to the cloned repository folder.
1. Open the `advanced-assistant.ipynb` notebook.
1. Run the notebook cells one by one. Do not run cells in the **Deleting unused resources** section until you finish using the bot.
1. The last cell under **Creating a wine assistant in Telegram** will get your bot ready to run.

## Test your bot {#test-bot}

1. Open the chat with your bot using the link you got [earlier](#create-bot).
1. Click **START**.
1. Enter your query in the message input field. Here is an example:

   ```text
   What wine pairs well with white chocolate?
   ```

   Result:

   ```text
   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 {#clear-out}

To stop paying for the resources you created, [delete the project](../operations/projects/delete.md).