Skip to main content

Bot with Jay Knowledge Hub

This lesson shows how to call Jay Knowledge Hub from Aimylogic bots. The bot uses HTTP request blocks to get information from the knowledge base.

info

Jay Knowledge Hub is an intelligent question-answering system by Just AI. It uses large language models (LLMs) and the RAG technology. This system finds the required information in a set of documents and generates answers for users. For more information about Jay Knowledge Hub, see the website and documentation.

Prepare knowledge base

  1. Go to Jay Knowledge Hub.

  2. Create a project.

  3. Add sources and index the knowledge base. See the Jay Knowledge Hub documentation for instructions on preparing the knowledge base.

Aimylogic script

In this script, the bot:

  1. Creates a chat in Jay Knowledge Hub.
  2. Gets a question from the user.
  3. Sends the question to the knowledge base chat and receives a response.
  4. Sends the response to the user.

The knowledge base takes into account all history in its chat by default. You do not need to pass the history in the script.

Create chat

  1. Create a screen called Create chat. Add a HTTP request block to it.

  2. Specify the HTTP request block settings to call the POST /api/knowledge-hub/chat method of the Jay Knowledge Hub API:

    • Method is POST.
    • URL is https://khub.just-ai.com/api/knowledge-hub/chat.

As a result, the bot creates a chat in Jay Knowledge Hub at the beginning of the dialog.

Get question from user

  1. Create another screen. Name it Get answer. Add a text to $var block to it. In the block, specify:

    • Message: Ask a question, I will find the answer.
    • Variable Name: question.
  2. Connect the Success exit from the HTTP request block to the Get question screen.

Get question

If the chat in the knowledge base is created successfully, the bot prompts for a question and saves the text into the $question variable.

Send request to knowledge base

  1. Create a screen called Request to knowledge base. Add a HTTP request block to it.

  2. Specify the HTTP request block settings to call the POST /api/knowledge-hub/chat/{chatId}/query method of the Jay Knowledge Hub API:

    • Method is POST.

    • URL is https://khub.just-ai.com/api/knowledge-hub/chat/${chatId}/query.

      Here, the URL contains the $chatId variable. The variable stores the ID that was obtained when creating the chat.

  3. Connect the Transition after entering a $question exit on the Get question screen with the Request to knowledge base screen.

Send request

The bot gets a question and sends it to the knowledge base.

Sending response to user

  1. Create a new screen called Send to user. Add a Text block to the screen with the response: Answer from the knowledge base: $baseAnswer. The bot sends the knowledge base response that is stored in this variable.

  2. Add a Transition block to the same screen and connect the block to the Get question screen.

Send response

As a result, the bot sends a response to the user. After that, the bot prompts for a question again.

For each new question, the knowledge base prepares a response taking the chat history into account.

Possible errors

Errors when sending HTTP requests

Errors might occur when you send requests in HTTP request blocks. For example, if the Jay Knowledge Hub token has expired.

To handle such errors, create new screens and connect them to the Error exits of the HTTP request blocks. This way you can inform the user that the HTTP request failed.

Errors when generating response in knowledge base

Errors can occur when the knowledge base is generating a response. For example, if the generation in the knowledge base ended with an error, the base sends a response, but $httpResponse.status is set to FAILED.

To handle such errors, use the Conditions block. Send a response to the user only if $httpResponse.status is set to FINISHED:

$httpResponse.status === "FINISHED"

Script timeout

It can take a long time for the knowledge base to generate a response.

If the knowledge base is generating a response for more than 70 seconds, the bot sends an error message.