Using Google Sheets as a Base for a GPT Bot (Instead of Notion)

Image generated on request "Using a Google Sheet as a Base for a GPT Bot"

Image generated by request “Using Google Sheets as a Base for GPT Bot”

The reason for writing this article was the departure of Notion from Russia, since we were actively connecting databases in Notion to our GPT bots (article about this: https://habr.com/ru/articles/820377/)

So, let's say we have this table:

Apartment database for GPT bot

Apartment database for GPT bot

Step #1: Create a GPT Bot

It's very easy to make a GPT bot and link it to Telegram on ProTalk:

Creating a Bot in One Button on ProTalk

Creating a Bot in One Button on ProTalk

Immediately upon creating the bot, we will link it to Telegram:

When creating a bot, we will immediately set up a token and nickname for the bot in Telegram

When creating a bot, we will immediately set up a token and nickname for the bot in Telegram

Step #2: Connect the function to the bot

First, let's go to the function catalog and search for the query: “what to use for google database“.

AI-powered search for matching features

AI-powered search for matching features

We connect the found function to the bot:

In the function block, select the function we need

In the function block, select the function we need

Step #3: Create a role for the GPT bot

In order for our AI consultant to be able to filter data by apartments, we need to explain to it what columns are in our table (database), this is how it can be done:

Ты должна подбирать квартиры на основе данных в таблице: 
https://docs.google.com/spreadsheets/d/1wcKA2__qfMjImLUiEOhK-lHUf0_rt5tmc-dvYEqepro/edit?usp=sharing

Колонки для запроса из таблицы: A - H
Для фильтрации используй эти колонки:
`С` - Цена аренды (в сутки) - числовое поле
`В` - Размер квартиры в м2 - числовое поле
`G` - VIP сегмент - фиксированные значения: `Да`, `Нет`
This is how we will enable the GPT bot to filter data in a Google spreadsheet itself.

This is how we will enable the GPT bot to filter data in a Google spreadsheet itself.

Testing the bot

For example, let's give our bot the following request: “I need VIP but not more than 2400“.

And this is what we will see in the answer:

Google spreadsheet filtering result by client request and GPT bot response

Google spreadsheet filtering result by client request and GPT bot response

Conclusion

If you have any questions while creating such integration, please write in the comments or to me at Telegram and I will try to answer your questions.

P.S. Function code

For those who want to do this integration themselves, here is the code for the filtering function itself:

def get_filtered_google_table(arguments):
    import requests
    import csv
    from io import StringIO
    import re
    url = arguments['url']
    sheet_index = (arguments.get('sheet_index', 1)) - 1
    filter_query = arguments.get('filter_query', '')
    pattern = r"/spreadsheets/d/([a-zA-Z0-9-_]+)"
    spreadsheet_id = re.findall(pattern, url)[0]
    file_url = f'https://docs.google.com/spreadsheets/d/{spreadsheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_index}&tq={filter_query}'
    response = requests.get(file_url)
    csv_content = response.content.decode('utf-8')
    csv_reader = csv.reader(StringIO(csv_content.replace("  ", ",")), delimiter=",")
    csv_rows = list(csv_reader)
    return csv_rows

Here is an example of a function call:

arguments = {
    'url': 'https://docs.google.com/spreadsheets/d/1wcKA2__qfMjImLUiEOhK-lHUf0_rt5tmc-dvYEqepro/edit?usp=sharing',
    'filter_query': 'SELECT A, B, C, E WHERE C > 2000 AND E LIKE "%балкон%"'
}
filtered_data = get_filtered_google_table(arguments)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *