Use in script
Code for accessing the knowledge base is automatically added to the script if you connect the knowledge base with the Add code to script option.
The code is located in the main file, in the NoMatch
state.
You can also copy this code or J‑Graph state in the Knowledge base → RAG section.
To access the knowledge base, the $rag
service is used.
Request types
-
You can create a chat in your knowledge base and send requests to it. The knowledge base will take chat history into account when generating responses.
-
Single requests to the knowledge base.
The knowledge base will take the history into account only if you provide it in your request.
Default code
state: NoMatch
event!: noMatch
scriptEs6:
const secretName = "Example";
// If there is no chat yet, the bot creates a new chat in the knowledge base
$client.ragChat_Example = $client.ragChat_Example || await $rag.chat.create(secretName);
// The bot gets the response
$rag.chat.processQuery({secretName, chatId: $client.ragChat_Example.id, query: $request.query})
.then(chatResponse => {
// Send the response to the user
$conversationApi.sendTextToClient(chatResponse.response);
})
.catch(error => {
// Error message
$conversationApi.sendTextToClient("Error: " + error);
});
# If the user asks another question while waiting, the bot will be able to answer it instantly
state: Waiting
q!: No answer yet?
a: Just one more minute…
In the NoMatch
state, the bot:
- If there is no chat yet, creates a new chat in the knowledge base using
$rag.chat.create
. - Gets the response using the
$rag.chat.processQuery
method. The method is called asynchronously. - Sends a response using
$conversationApi.sendTextToClient
.
- This script is not supported in the phone channel.
- If the knowledge base does not generate an answer within 2 minutes, an error will occur.
You can adapt this code for your script. See examples and the description of the $rag
methods.
Ways to access knowledge base
Select the way you want to access the knowledge base. When selecting, consider the following parameters:
- Channel in which you will use the bot. In some channels, the bot cannot asynchronously send a response to the user.
- The time it takes your knowledge base to generate a response. For example, if the bot calls a method synchronously, it can wait for a response for no more than 25 seconds.
Type | Channels | Knowledge base response time | Description |
---|---|---|---|
Synchronous bot response | All | Up to 25 seconds | The bot sends a request to the knowledge base and waits for a response. The bot cannot perform other reactions or respond to user requests until it receives a response from the knowledge base. |
Asynchronous bot response | Not supported in the phone channel. | Up to 2 minutes | The bot sends a request to the knowledge base and waits for a response. During this time, the bot can continue to respond to user requests. |
Create request and check status | All | No limit | The bot sends a request to the knowledge base. If the user asks, the bot checks the status of the request. |
With any of the ways, you can use different types of requests: single or within a chat.