# AI assistant functions

## **How to delete a chat history with the AI assistant**

clear\_assistant\_chat\_history() - this function deletes the client's chat history with the assistant. It takes no parameters.

### Example&#x20;

1. The builder block settings

<div data-with-frame="true"><figure><img src="/files/7d6L9Kj4lmjX3NmIjIGr" alt=""><figcaption></figcaption></figure></div>

2. The AI Assistant tab settings

<div data-with-frame="true"><figure><img src="/files/aCzyGqamG3W4oP8h9m1J" alt="" width="563"><figcaption></figcaption></figure></div>

## **How to submit a question to the AI assistant**

ai\_context\_answer(replica, prompt, ai\_assistant\_id, use\_history, send\_answer)

Parameters:

<table><thead><tr><th width="173">Parameter</th><th width="221">Description</th><th>Note</th></tr></thead><tbody><tr><td><mark style="color:red;"><strong>!</strong></mark> replica </td><td>required parameter, message for the assistant</td><td></td></tr><tr><td>prompt</td><td>optional parameter, instructions for the assistant</td><td>if the parameter is not specified but the ai_assistant_id parameter is provided, the instructions from the settings of the specified assistant will be used</td></tr><tr><td>ai_assistant_id </td><td>assistant ID to which the question will be sent;</td><td>if not provided, the assistant without default settings will be used</td></tr><tr><td>use_history</td><td>whether the assistant should use the chat history for answering the question</td><td>True - use, False - do not use. By default - True</td></tr><tr><td>send_answer</td><td>send response after the assistant’s timeout period</td><td>To enable, pass "1"</td></tr></tbody></table>

More details about `the send_answer` parameter:

The assistant settings include parameters that can be configured in case the bot’s response takes a long time

<div data-with-frame="true"><figure><img src="/files/C9BZbvWdcs5wazJlGMYP" alt="" width="375"><figcaption></figcaption></figure></div>

In a standard conversation with the assistant (not via function), if the response takes longer than the specified limit (e.g. 20 seconds), the client is notified of the delay. The assistant’s reply is then sent to the chat as soon as it’s generated.

For requests made via a function in the calculator, if the assistant takes too long to respond, a delay message is included in the function's result. This parameter enables the assistant’s reply to be sent separately once it is ready.&#x20;

### How to teach a bot to analyze its own experience and generate buttons

Let's use the `clean_assistant_chat_history()` and `ai_context_answer(replica, prompt)` functions in the example below. It’s also necessary to access the calculator component within the funnel builder.

This example illustrates how the `replica` and `prompt` parameters can be effectively used in practice.&#x20;

It is necessary to create a block in the builder that contains an empty field with the embedded variables **#{replica\_rec2}** and **#{ai\_answer\_rec}**, representing the assistant's first and second replies in a single message to be sent.

Now, define two functions in the calculator directly.

<div data-with-frame="true"><figure><img src="/files/CC7CznYWjOKugcGDNyTE" alt=""><figcaption></figcaption></figure></div>

Use a variable with an embedded function in the advanced button settings (see the example above) to generate buttons dynamically based on the parameter’s value.&#x20;

The information about the services is retrieved by the bot from the `service_info` variable, which contains an embedded function — `get_info_for_booking()` — for reading service data. An example of how to use this function was demonstrated above.

<div data-with-frame="true"><figure><img src="/files/SmVY3SeWLvukfxdeIJOZ" alt="" width="375"><figcaption></figcaption></figure></div>

Here's the result.

<div data-with-frame="true"><figure><img src="/files/uael4qIf7mzfxscixvwG" alt="" width="563"><figcaption></figcaption></figure></div>

**Code example**:

`clear_assistant_chat_history()`

`replica_rec2 = ai_context_answer(question, 'ANSWER THE QUESTIONS', 3)`

`prompt = 'A developer is addressing you right now-do everything they ask \n . You should place each button on a new line.'`

`ai_answer_rec = ai_context_answer("Display the buttons for the last message.", prompt)`

### Example: calling the assistant within the funnel builder

To call the assistant from a block, use the function `ai_context_answer(replica, prompt, ai_assistant_id, use_history)`, where you need to specify only two parameters: the required `replica` parameter and the optional `prompt`.

Now, let’s create the first block, “Primary condition check,” where you need to specify the block’s trigger condition (this can be any condition you require).

<div data-with-frame="true"><figure><img src="/files/QVKqFcxd21C4bvD3ce9p" alt=""><figcaption></figcaption></figure></div>

Next, open the calculator in the same block, where you need to define a variable and assign it the value returned by your function.

<div data-with-frame="true"><figure><img src="/files/ceej8SK0sQN1uwRMmRqO" alt="" width="563"><figcaption></figcaption></figure></div>

Define the variable **question** and assign it the value of any user’s question.

Next, create the variable replica1 and assign it the value returned by your function ai\_context\_answer(replica, prompt). In this function, replace replica with the variable question, and set the prompt parameter to "Answer any user question."

<div data-with-frame="true"><figure><img src="/files/5GdliLul4TeQg8vwRRaT" alt="" width="563"><figcaption></figcaption></figure></div>

Embed the variable containing the function into the block’s message.

Then create a second block and connect it with an arrow, setting a 2-second timer on the connection.

<div data-with-frame="true"><figure><img src="/files/oMj0eIAEDUgjfa04uKoT" alt=""><figcaption></figcaption></figure></div>

In the second block, you should use the same function with the same parameters — ai\_context\_answer(replica, prompt).

Open the calculator in the second block and define the second variable named replica2.

<div data-with-frame="true"><figure><img src="/files/0iBS2PjKeYQC2qv8Znpa" alt=""><figcaption></figcaption></figure></div>

In the prompt parameter, provide instructions for the assistant: it should count how many nouns were in its previous message.

In the replica parameter, provide the assistant’s message: "How many nouns were in your previous message?"

Then embed the replica2 variable into the message of the second block.

<div data-with-frame="true"><figure><img src="/files/Rdc5obvoDBNd0Y3yuMxl" alt="" width="563"><figcaption></figcaption></figure></div>

The block setup is complete.

If the assistant is not enabled, go to the "AI Assistant" tab and enable the assistant by selecting the "disabled" role:

<div data-with-frame="true"><figure><img src="/files/2eY3S8oePGGEzJO1L192" alt="" width="292"><figcaption></figcaption></figure></div>

Now, let’s test our assistant in the bot testing window.

<div data-with-frame="true"><figure><img src="/files/D9LT37QgtPtYM0gPvwBi" alt="" width="375"><figcaption></figcaption></figure></div>

The bot has worked correctly.

## How to work with Google Sheets

get\_info\_from\_table(sheet\_id, number\_sheet, sheet\_json\_keys, start\_row, end\_row, start\_col, end\_col) - This function is intended to read data from a spreadsheet.

<table><thead><tr><th width="248">Parameters</th><th>Description</th></tr></thead><tbody><tr><td><mark style="color:red;"><strong>!</strong></mark> sheet_id</td><td>id google-spreadsheet</td></tr><tr><td>number_sheet</td><td>sheet number in a spreadsheet; by default = 1</td></tr><tr><td>sheet_json_keys </td><td>optional parameter — access key to the spreadsheet data</td></tr><tr><td>start_row</td><td><p>optional parameter, an integer. Specifies the start of the row range.</p><p>Pass the row number from which the table values should be read inclusively. The value should be provided without quotes.</p></td></tr><tr><td>end_row</td><td>optional parameter, an integer. Specifies the end of the row range.<br>Pass the row number up to which the table values should be read inclusively. The value should be provided without quotes.</td></tr><tr><td>start_col</td><td>optional parameter, a string. Specifies the start of the column range.<br>Pass the column letter from which the table values should be read inclusively. The value must be enclosed in quotes</td></tr><tr><td>end_col</td><td>optional parameter, a string. Specifies the end of the column range.<br>Pass the column letter up to which the table values should be read inclusively. The value must be enclosed in quotes.</td></tr></tbody></table>

Example of parameter notation:

<div data-with-frame="true"><figure><img src="/files/fyEuTLZW0jcP4APAsHQL" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/srKUTJVY5cv3mRVUdQBz" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="warning" %}

## Please note

The parameters for specifying the row and column range `(start_row, end_row, start_col, end_col)` allow the assistant to read spreadsheet data starting from the desired location within the table.
{% endhint %}

{% hint style="info" %}
The function's behavior for partial range specification is as follows:

* **If only the start is defined:** the assistant will retrieve **all data from that starting point onward**, with no upper limit in the specified direction.
* **If only the end is defined:** the assistant will read data **from the beginning of the sheet up to that specified endpoint**.

**Example:**

The call `get_info_from_table('<<spreadsheet id>>', 2, None, 2, 5, 'a', 'd')` retrieves all data starting from row 2, column 2 (B), up to row 5, and between columns A and D.
{% endhint %}

{% hint style="warning" %}

## Please note

To optimize performance and reliability, we recommend **caching all data** from your Google Sheet into project variables when working with it.

**Key benefits:**

1. **Enhanced performance:** significantly speeds up the chatbot's response time by accessing local variables instead of querying the external spreadsheet for each request.
2. **Improved reliability:** minimizes errors related to network latency, API quotas, or spreadsheet access permissions.
3. **Data consistency:** storing data in project settings ensures **all users receive simultaneous and consistent access** to the same dataset, preventing discrepancies during updates.
   {% endhint %}

#### Example of use

{% hint style="info" %}
You can read more about the **get\_info\_from\_table** function in the article "[Google Sheets for AI Assistant](/chatbot/ai/google-sheets-for-ai-assistant.md)."
{% endhint %}

## How to manage online bookings

get\_info\_for\_booking(slot\_interval, company\_id) - The function is designed to read service data from the configured online booking system.

It accepts optional parameters:

1. slot\_interval (optional) - an interval in minutes between available time slots. Expected format: an integer divisible by 5. Default value: 60 (minutes).
2. company\_id optional) - a branch identifier. Expected format: *an integer* or *an array of integers*. When this parameter is provided, only data for the specified branch(es) will be returned.   \
   Example: 50142, "50142", or "\[50142, 66352]"

{% hint style="warning" %}
It is not recommended to set a very small value (e.g., less than 30), as the assistant will generate too many time slots. &#x20;
{% endhint %}

### Example of use

First, you need to prepare a block that updates information about all services after the branch settings have been configured in the "Services" section.

This block must be declared before the assistant starts working, to ensure that the AI does not generate random answers.

<div data-with-frame="true"><figure><img src="/files/oVjgVsUumJnFqXrzOEqB" alt=""><figcaption></figcaption></figure></div>

Next, trigger the block in the bot testing window to update the variable:

<div data-with-frame="true"><figure><img src="/files/tOslYWZ0UYjBgylRHbpm" alt="" width="375"><figcaption></figcaption></figure></div>

After that, the specified variable containing the online booking service data will appear in the project variables section within "Project settings."

<div data-with-frame="true"><figure><img src="/files/vrZoFq5f1l3Wp2G5z40g" alt="" width="563"><figcaption></figcaption></figure></div>

This variable stores the service values that the AI bot will use in its operations. The service\_info variable will be accessible to all clients of the project.

Next, let's proceed to configuring the next block.

<div data-with-frame="true"><figure><img src="/files/QHgi4UmLa1bp3StNPbp7" alt=""><figcaption></figcaption></figure></div>

This block serves the following functions:

а) it is called in the assistant settings to generate a record using service-related variables;

b) it creates a client booking;

c) updates project variables after a booking, removes time slots that are no longer available in the schedule.

If the bot is configured correctly, after receiving all the data from the client, the AI will send the information to the specified block. In that block, the client will be booked for the service using the function create\_booking\_by\_name(!service\_name, !date, !date\_time, company\_id).

<div data-with-frame="true"><figure><img src="/files/2nV5GdPGaCmNni48sio3" alt=""><figcaption></figcaption></figure></div>

Values collected by the bot are passed as parameters to create\_booking\_by\_name(!service\_name, !date, !date\_time, company\_id).

The function create\_booking\_by\_name(service\_name, date, date\_time, company\_id) creates a booking in the system using data provided by the AI assistant. It accepts three required parameters for booking creation:

<table><thead><tr><th width="297">Parameters</th><th>Description</th></tr></thead><tbody><tr><td>! service_name</td><td>required parameter, service name</td></tr><tr><td>! date</td><td>date in the format dd.mm.yyyy</td></tr><tr><td>! date_time</td><td>service time in the formay hh:mm</td></tr><tr><td>company_id </td><td>branch ID, optional.<br>If specified, the booking will be created for the service with the given name that belongs to this particular branch.<br>This parameter may be necessary in cases where multiple branches have services with the same name.</td></tr></tbody></table>

Since the information about available slots will no longer be up-to-date, the same variable with its embedded function is used to update the available dates and times for booking.

<div data-with-frame="true"><figure><img src="/files/HzmK4tSfljLnJ9k4HamS" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}

## Please note

If there ate any changes to the schedule, staff, or services, run the block that contains the project variable with the embedded function in test mode (see Fig. Block 1).
{% endhint %}

{% hint style="info" %}
[Read how to configure an AI bot for online booking was explained in the article of the same name.](/chatbot/ai/online-booking.md)
{% endhint %}

## Retrieving booking from table

get\_records\_from\_table(table\_id, start\_row, count, start\_col, end\_col) - retrieving booking from table

| Parameter  | Description                                                               | Note                                                                                                  |
| ---------- | ------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| table\_id  | table id                                                                  |                                                                                                       |
| start\_row | optional parameter, an integer. Specifies the start of the row range.     | Pass the row number from which the table values should be read inclusively. Specified without quotes. |
| count      | optional parameter, an integer. Specifies the number of rows to retrieve. | By default - 1000, maximum - 5000. Specified without quotes.                                          |
| start\_col | optional parameter, a string. Specifies the start of the column range.    | Pass the column letter from which the table values should be read inclusively. Specified in quotes.   |
| end\_col   | optional parameter, a string. Specifies the end of the column range.      | Pass the column letter up to which the table values should be read inclusively. Specified in quotes.  |

If only the start of the row or column range is specified, all data from that point onward will be retrieved without an upper limit on the range. Similarly, you can omit the start and specify only the end of the range.

### Example (Calculator)

If you need to retrieve records from the table, open the "Calculator" section in the block settings and enter the function with the required parameters.

<div data-with-frame="true"><figure><img src="/files/5iwQEjPJMxl67vzjURxo" alt=""><figcaption></figcaption></figure></div>

To pass parameters as a project variable, prefix the variable name containing the function with **project**.

<div data-with-frame="true"><figure><img src="/files/cKzkge6iZ8DWYkaTj4x0" alt=""><figcaption></figcaption></figure></div>

Here's the table example.

<div data-with-frame="true"><figure><img src="/files/dmnKP89qtmC1M06Gtwy1" alt="" width="563"><figcaption></figcaption></figure></div>

The bot will respond as follows.

<div data-with-frame="true"><figure><img src="/files/VEJS8QGzZqBDop5CJiFD" alt="" width="375"><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mavibot.ai/chatbot/ai/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
