Response generation
Synchronous call
During a synchronous call, the bot cannot perform other reactions or respond to user requests until it gets a response from the knowledge base.
Within chat
state: Answer
intent!: /question
scriptEs6:
try {
// If there is no chat yet, the bot creates a new chat in the knowledge base
$client.chat = $client.chat || await $rag.chat.create("MyKnowledgeHub");
// The bot gets response from the knowledge base
var chatResponse = await $rag.chat.processQuery(
"MyKnowledgeHub", $client.chat.id, $request.query
)
if (chatResponse.status === "FINISHED") {
// Send response to the user
$reactions.answer(chatResponse.response);
} else {
// Error message if the status is not FINISHED
$reactions.answer("Sorry, something went wrong. Status: " + chatResponse.status);
}
} catch (error) {
// Error message if the bot failed to send the request
$reactions.answer("An error occurred: " + error);
}
In this example, the bot:
-
If there is no chat yet, creates a new chat using
$rag.chat.create
. -
Gets the response using the
$rag.chat.processQuery
method. The bot calls the method synchronously usingawait
.cautionIf the knowledge base generates a response for more than 25 seconds, then the bot will not receive it. In this case, call the method asynchronously.
-
Sends a response or error message.
Single request
state: Answer
intent!: /question
scriptEs6:
try {
// The bot gets the response from the knowledge base
var ragResponse = await $rag.query.generateAnswer("MyKnowledgeHub", $request.query);
if (ragResponse.status === "FINISHED") {
// Send the response to the user
$reactions.answer(ragResponse.response);
} else {
// Error message if the status is not FINISHED
$reactions.answer("Sorry, something went wrong. Status: " + ragResponse.status);
}
} catch (error) {
// Error message if the bot failed to send the request
$reactions.answer("An error occurred: " + error);
}
In this example, the bot:
-
Gets the response using the
$rag.query.generateAnswer
method. The bot calls the method synchronously usingawait
.cautionIf the knowledge base generates a response for more than 25 seconds, then the bot will not receive it. In this case, call the method asynchronously.
-
Sends a response or error message.
Asynchronous call
During an asynchronous call, the bot can continue to respond to user requests even if it has not yet received a response from the knowledge base.
Within chat
- main.sc
- knowledgeHub.js
require: knowledgeHub.js
name = knowledgeHub
type = scriptEs6
theme: /
state: Answer
intent!: /question
scriptEs6:
knowledgeHub.sendRequest("MyKnowledgeHub", $request.query);
# If the user asks a question while waiting, the bot will be able to answer it instantly
state: Waiting
q!: No answer yet?
a: Just one more minute…
async function sendRequest(secretName, query) {
try {
// If the chat does not exist yet, the bot creates a new chat in the knowledge base
$client.chat = $client.chat || await $rag.chat.create(secretName);
// The bot gets the response from the knowledge base
var chatResponse = await $rag.chat.processQuery(
secretName, $client.chat.id, query
);
if (chatResponse.status === "FINISHED") {
// Send the response to the user
$conversationApi.sendTextToClient(chatResponse.response);
} else {
// Error message if the status is not FINISHED
$conversationApi.sendTextToClient("Sorry, something went wrong. Status: " + chatResponse.status);
}
} catch (error) {
// Error message if the bot failed to send the request
$conversationApi.sendTextToClient("An error occurred: " + error);
}
}
export default {
sendRequest
};
In this example, the bot:
- If there is no chat yet, creates a new chat using
$rag.chat.create
. - Gets the response using the
$rag.chat.processQuery
method. The method is called asynchronously, inside the asynchronoussendRequest
function. - 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.
Single request
- main.sc
- knowledgeHub.js
require: knowledgeHub.js
name = knowledgeHub
type = scriptEs6
theme: /
state: Answer
intent!: /question
scriptEs6:
knowledgeHub.sendRequest("MyKnowledgeHub", $request.query);
# If the user asks a question while waiting, the bot will be able to answer it instantly
state: Waiting
q!: No answer yet?
a: Just one more minute…
async function sendRequest(secretName, query) {
try {
// The bot gets the response from the knowledge base
var ragResponse = await $rag.query.generateAnswer(secretName, query);
if (ragResponse.status === "FINISHED") {
// Send the reponse to the user
$conversationApi.sendTextToClient(ragResponse.response);
} else {
// Error message the if status is not FINISHED
$conversationApi.sendTextToClient(
"Sorry, something went wrong. Status: " + ragResponse.status
);
}
} catch (error) {
// Error message if the bot failed to send the request
$conversationApi.sendTextToClient("An error occurred: " + error);
}
}
export default {
sendRequest
};
In this example, the bot:
-
Gets the response using the
$rag.query.generateAnswer
method. The method is called asynchronously, inside the asynchronoussendRequest
function. -
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.