# GenAI Intent detection

The **GenAI intent detection** tool uses advanced text classification to analyze messages and works with virtual assistants within Answers to enhance communication. It accurately categorizes the intent of end users' messages, ensuring that your chatbot delivers contextually relevant responses.

In the GenAI intent detection tool, configure the intents that you want the GenAI model to identify. Based on these intents, you can redirect end users to a specific part of the chatbot to provide them with better support with their queries.

Note
These intents are different from Answers intents because you do not need to train the GenAI intents with specific training phrases. You only need to provide examples of user messages that are specific to your use case.

For more information about intents, refer to the [Intents documentation](https://www.infobip.com/docs/answers/ai-chatbots/intents).

## Prerequisites [#prerequisites]

Contact your Infobip account manager to activate AI features in your account.

## Key features [#features-genai-intent-detection]

- Classifying messages
- Organizing the conversational flow

## Configure the element [#configure-genai-intent-detection]

To add the **GenAI intent detection** element to your chatbot, drag it from **Apps from Exchange**. Complete the following fields.

### Request [#request-configure-genai-intent-detection]

Provide instructions for the chatbot to identify the intent. Complete the following fields.

#### User message

Specify the message sent by the end user. GenAI identifies the intent of this message.

You can do one of the following:

- Select the text attribute that contains the end user's message. Example: *menu_response*.
- Select the predefined attribute, **lastReceivedTextMessage**.

#### Prompt instruction, System message, Intents

The prompt instruction, system message, and intents work together to identify the intent of the end user's message.

To configure these fields, do one of the following:

- Configure the field manually.
- Save the field value in an attribute and then specify this attribute in the field.

First, configure the prompt instruction and system message. Then, configure the intents. Refer to the following sections.

##### Prompt instruction and system message

The following table shows the prompt and system message templates that you can use when configuring the request parameters.
  
Important
Infobip recommends that you keep the prompt and system message template format unchanged, because they will work well in most cases. Replace the capitalized words with terms specific to your use case.

| Prompt template | System message template |
| --- | --- |
| - Question: **{query_str}**- Help me classify user's question in one of the categories | -CLIENTis aBANK- I need you to classify user question into one of categories [CATEGORY_1,CATEGORY_2, agent]-CATEGORY_1category refers to the following types of queries:-- [HOW TO APPLY FOR PERSONAL FINANCING, WHO IS ELIGIBLE FOR PERSONAL LOAN, WHAT ARE THE TERMS AND CONDITIONS, WHAT ARE THE PAYMENT TERMS]-CATEGORY_2category refers to the following types of queries:-- [I LOST MY CARD, PLEASE DISABLE MY STOLEN CARD, I WANT TO REPORT A LOST CARD]- agent category refers to the following types of queries:-- [Let me talk to an agent, I want to talk to a real person, Transfer me to a live agent]- User question will be delimited by **.- Your answer must be in a valid JSON format, with the key: category, and the value is one of the categories defined above. |

##### Intents

The intents must have the same name as the categories that are specified in the system message.

In the above example, the categories are
*CATEGORY_1*, *CATEGORY_2*, and *agent*.

You do not need to add training phrases for these intents.

Important
If none of the specified intents are found, the intent element returns *unknown*. This usually happens when messages do not fit into any of the defined intents. You do not need to define the *unknown* intent in the element because it is automatically added.

#### Output tokens (Optional)

Configure the maximum number of tokens that the tool can use in a response.

If you use a low value in this field, the response is truncated. Usually, the GenAI Intent Detection element returns very small outputs of approximately 50 tokens. You can increase the value if you need longer, customized responses.

### Response [#response-configure-genai-intent-detection]

Configure the response. For more information, refer to the [**Response**](#configure-response-how-to) section.

### Fallback [#fallback-configure-genai-intent-detection]

Specify the action that the chatbot needs to take if the **GenAI intent detection** element fails to execute. For more information, refer to the [**Fallback**](https://www.infobip.com/docs/answers/chatbot-elements/core-elements#how-to-fallback) section.

### Tests (Optional)

Create one or more test cases to check whether the intents are identified correctly.

In the test case, do the following.
- In the **User message** field, specify a sample message from the end user.
- If you have used attributes to configure the fields in the element, specify the values of the attributes.
- In the **Expected response** field, specify the intent into which the user message needs to be classified.

After you create the test cases, run them, and view the results.

For an example of a test case, refer to the [Example](#example-genai-intent-detection) section.

For more information about creating and working with test cases, refer to the [**Test cases**](#test-cases-how-to) section.

## Preview the behavior of the element  [#preview-genai-intent-detection]

Enter examples of end user messages and prompts, and check if the response from GenAI is as expected.

Do the following:

1. Select **Preview behavior**.
2. In the **User message** field, enter an example of a message from the end user. Example: *How much can I borrow?*
3. Configure the other fields, if required.
4. Select **Preview behavior**.

The response from GenAI is displayed in the right pane. The response shows the intent that GenAI matched for the end user's message.

If the result is not what you expect, do the following until you get the required output.

1. Modify the fields in the **Preview** screen.
2. Select **Preview behavior**.

If the result is as expected, select **Apply changes to parameters** to update the fields in the **GenAI intent detection** element.

For an example of a preview, refer to the [Example](#example-genai-intent-detection) section.

## Configure the next steps [#next-steps-genai-intent-detection]

After configuring the GenAI Intent detection element, define the next steps for specific intents. Example: Use a **Conditions** element to route the chatbot flow based on the end user's intent.

## Example [#example-genai-intent-detection]

In this example, the chatbot is for a bank. When end users ask questions, the **GenAI intent detection** element identifies the intent associated with the question. You can then redirect the end user to the dialog for that intent.

1. To obtain the question from the end user, add a Text element. Example: *How can I help you today?*
2. Add the **GenAI intent detection** element.
3. Pass the response from the end user to the **GenAI intent detection** element. To do so, specify **lastReceivedTextMessage** in the **User message** field in the element.
4. In the **prompt instruction** field, instruct GenAI to classify the end user's response into one of the configured intents. Use the following prompt.

    ```plaintext copy filename=""
        - Question: **{query_str}**
        - Help me classify user's question in one of the categories
    ```

5. In the **System message** field, add the following instructions.
    ```plaintext copy filename=""
        - InfoBank is a bank
        - I need you to classify user question into one of categories [Loans, Lost, Agent]
        - Loans category refers to the following types of queries:
        -- [How to apply for personal financing, who is eligible for personal loan, what are the terms and conditions, what are the payment terms]
        - Lost category refers to the following types of queries:
        -- [I lost my card,  Please disable my stolen card, I want to report a lost card]
        - Agent category refers to the following types of queries:
        -- [Let me talk to an agent, I want to talk to a real person, Transfer me to a live agent]
        - User question will be delimited by **.
        - Your answer must be in a valid JSON format, with the key: category, and the value is one of the categories defined above.
    ```

5. In the **Intents** field, create intents for each category that you specified in the **System message** field. So, create the *Loans*, *Lost*, and *Agent* intents.

7. Create an attribute, *intent_category*, to save the response from the **GenAI intent detection** element.
8. In the **GenAI intent detection** element > [**Response**](#configure-response-how-to) tab > **Response body** field, configure the following.

    **Attribute**: *intent_category*
    
    **Path**: $.response

8. Configure the fields in the [**Fallback**](https://www.infobip.com/docs/answers/chatbot-elements/core-elements#how-to-fallback) tab.
8. In the **Tests** tab, [create test cases](#test-cases-how-to). In each test case, provide a sample end user message and specify the intent to which the message must be classified.

    Example:

    **User message** is *What is the eligibility for borrowing?*

    **Expected response** is the intent, *Loans*.

8. Run the test case and view the result to check whether the intent detected by GenAI matches the intent specified in the test case. Check whether the **Actual response** matches the **Expected response**.

11. Preview the behavior of the **GenAI intent detection** element. In the preview screen, enter a sample end user message in the **User message** field and select **Preview behavior**. Check whether the response in the right pane is correct.

    Example:
    
    **User message** is *How much can I borrow?*

    Check whether the response in the right pane is *Loans*.

12. [Test](https://www.infobip.com/docs/answers/simulate-the-conversation#simulator) the entire chatbot. In the **Test** tab of the chatbot, enter a message about one of the banking services.

    Example: Send the message, *My credit card has been stolen*, to the chatbot. Check whether GenAI detects the correct intent.

## How to [#how-to]

### Configure the response [#configure-response-how-to]

Configure the following fields in the **Response** tab of the GenAI element.

##### Save response code to attribute (Optional)

Select the attribute in which to save the response code.

##### Response body attributes

Select the attribute in which to save the body of the response, and the related path.

You can also view and copy the response code JSON schema.

Note
If you need to manipulate the data received in the response, ensure that you use a **JSON** attribute type.

#### Timeout (Optional) [#timeout-api]

Specify how long you want to wait for the response. The minimum value is 1 second and the maximum value is 60 seconds.

### Work with test cases [#test-cases-how-to]

Use the **Test** tab in the element to test the functionality of the element without testing the entire chatbot.

To test the element, do the following.

1. Create test cases. Infobip recommends that you create the test cases in a .csv file and import it. Refer to the **Import test cases** documentation in the following sections.
2. Run the test cases.
3. View the results of the test cases.
4. If the results are not as expected, update the fields in the GenAI element, and run the test cases again.

For more information, refer to the following sections.

##### Import test cases

You can import saved test cases. Select **Import** and select the required file.
Supported file format is .csv.

##### Create a test case manually

To create a new test case, do the following.

1. Select **Add test case**.
2. Name the test case.
3. Configure the fields as required.
4. Select **Save**.

##### Example of using attributes in a test case

The system instruction is to repeat the end user's message through role play.
*Act like a {{Role}}*
In the test case, do the following.

- Specify the end user's message in the **Prompt instruction** field.
- Specify the value of the *Role* attribute as *Pirate*.
- Specify the **Expected response** as *Ahoy*.

The following image shows the results of the test case.

##### Run test cases

After you create the required test cases, run them.

- To run a specific test case, select the three dots next to the test case and select **Run**.
- To run all the test cases, select **Run all tests**.

You get a message that tells you whether the tests succeeded.

##### View results of test cases

Check whether the response from GenAI is as expected.

Select the three dots next to a test case and select **View result**.

##### Edit test cases

Select the three dots next to the test case that you want to edit and select **Edit**.

##### Delete test cases

- To delete a specific test case, select the three dots next to the test case and select **Delete**.
- To delete all the test cases, select **Delete all tests**.

##### Export test cases

You can export the test cases that you create. Select **Export**.

All the test cases are saved in a .csv file.