# Functions

Functions are a very useful and important tool when configuring Suvvy, allowing Suvvy to perform specific actions (invoke a knowledge base file, table, web-hook, etc.).

## How do functions work?

At the core of each Suvvy mechanism, where some action is required (in addition to responses), whether it's a knowledge base, tables, web-hooks, or something else, there lies a "function" responsible for its operation. Within a function, there are two parameters:

* function name
* function description

The name provides a general meaning, while the description is more detailed.

> For example, for a table serving as a price list, which Suvvy needs to read to respond to a client, the function name and description might be:
>
> * Name: `price_table`
> * Description: Used for providing product costs

<figure><img src="https://2542240793-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnmK9dnkx224HgS0VSB5J%2Fuploads%2FSSjALxwwwnz3oJAzO1hG%2Fimage.png?alt=media&#x26;token=42362669-7b25-4dac-9cab-068578332903" alt=""><figcaption></figcaption></figure>

This information gives Suvvy an idea of where the cost information is stored, so when a client asks, "How much does it cost?" Suvvy understands that this information is stored in the given table.

{% hint style="warning" %}
**Note**

There are more complex situations when calculations are done **in multiple steps** or when using **information from different sources.**

For example, when data first needs to be obtained from the first table, and then this information is passed to a second table to retrieve data according to this combination.
{% endhint %}

In this case, each table is assigned its **own name** and **description**.

> For example, in the first table, we obtain the product category and its identifier (ID), and then, using the category ID, calculate the cost in another table, where the volume and cost of the product are specified. In this case, the parameters for the first table would be:
>
> * Name: `categories_table`
> * Description: Used for obtaining product category IDs
>
> For the second table:
>
> * Name: `price_table`
> * Description: Used for obtaining product costs by ID.

At this point, one might wonder why not specify everything in one table right away? Here, it's divided simply as an example.

In this case, with such a scheme, we can specify a prompt in the instructions like this:

{% tabs %}
{% tab title="Example prompt with two tables" %}
\#COST CALCULATION

If the client requests the product cost, perform the following steps in order:

1. Obtain the product ID by calling the `categories_table` function and passing the required product to it
2. Retrieve the product cost by calling the `price_table` function and passing the product ID obtained in the previous step.
   {% endtab %}
   {% endtabs %}

**Why are we calling two functions here? Doesn't the bot understand it from the description?**

It does, but it's a matter of precision. If we want to increase it, it's better to explicitly state the sequence of steps in the instructions.

## Function Calls in Instructions

For calling functions in Suvvy's system instructions, the construction is used:

<mark style="color:blue;">**call function**</mark> , where is the specific function.

Below are types of functions and examples of their use.

## Pre-installed Functions

Pre-installed functions include the function for working with the knowledge base. When it's necessary to forcibly invoke a specific file in the knowledge base, the function is used:

<mark style="color:blue;">**get\_file\_text("")**</mark>, where is the file name to search for in the Knowledge Base section.

<figure><img src="https://2542240793-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnmK9dnkx224HgS0VSB5J%2Fuploads%2FIPhVsgKqvHtPImcPyhHA%2Fimage.png?alt=media&#x26;token=2cb01430-7f12-496a-83e6-1ea00107fbb0" alt=""><figcaption></figcaption></figure>

For instance, if a client doesn't explicitly write that they want to connect with an operator, but in some cases, we want to reduce the chance that Suvvy won't call the correct file, we can use the following method:

{% tabs %}
{% tab title="Example get\_file\_text" %}
{% code overflow="wrap" %}

```
If the client is angry, he has a complaint or a raised tone, by which we can understand his negative attitude, call function get_file_text("Switch to manager").
```

{% endcode %}
{% endtab %}
{% endtabs %}

In this case, Suvvy will call the file even though the client did not request to switch to a manager.

## Custom Functions

At present, custom functions include functions from the [Tables](https://docs.suvvy.ai/en/working-with-tables/tablicy-csv-xls-google) and [Actions](https://github.com/suvvyai/docs/blob/master/en/osnovnye-nastroiki/prompts/broken-reference/README.md) sections.

Each table or action has its own unique function name and description. It is assigned individually for each separate table or action.

{% hint style="info" %}
Functions themselves have descriptions, and Suvvy **understands** when they need to be called on its own.

Including functions in the instructions is not mandatory on its own, but we consider cases where the function call may be **not obvious** to the bot, or the sequence of actions has its branches that need to be described.
{% endhint %}

By analogy with the previous point, we can call necessary functions using the construction:

<mark style="color:blue;">**call function**</mark> , where is the specific function.

For tables, having the word "table" in the name is mandatory (added automatically).

When calling a table or action, we must specify what we're passing to it to ensure Suvvy doesn't pass unnecessary information.

For example, if we want to call a web-hook that will contact the CRM and obtain information about a product, considering delivery to a specific city, we can create the instruction as follows:

{% tabs %}
{% tab title="Example of a web-hook call" %}
{% code overflow="wrap" %}

```
If the customer requests information about the product, call the item_information function and pass the product name and the delivery city there.
```

{% endcode %}
{% endtab %}
{% endtabs %}
