Skip to main content

$rag.query.generateAnswer

The method generates a response to the user query.

caution

The method is available only in the ECMAScript 6 runtime and is asynchronous.

Syntax

The method accepts the following arguments:

ArgumentTypeDescriptionRequired
secretNameStringName of the knowledge base secret.Yes
queryStringText of the user query.Yes
historyArrayHistory of the dialog with the bot. You can pass the history so that the knowledge base takes the context into account. Get the history using $rag.getChatHistory.No
settingsObject

Query processing settings. By default, the settings from the Tovie Data Agent project are used.

The settings format matches the format of the identically named object in the POST /api/knowledge-hub/query request in the Tovie Data Agent API.
No
timeoutNumber

Timeout in milliseconds for the method execution. If the timeout is exceeded, an error will occur.

By default, the timeout is not set.
No

You can pass arguments to the method in different ways.

Specify the arguments in order:

await $rag.query.generateAnswer("MyKnowledgeHub", "What does the Example service do?", undefined, undefined, 5000);

Return value

The method returns an object with the result of the request processing. The response field contains the answer to the user question.

{
"id": 12345,
"request": "What does the Example service do?",
"status": "FINISHED",
"createdAt": "2024-12-02T16:10:52.873498Z",
"response": "The Example service processes user requests …",
"updatedAt": "2024-12-02T16:10:58.238115Z",
"comment": null
}

The object has the same format as the response to the POST /api/knowledge-hub/query request in the Tovie Data Agent API.

How to use

In this state, the bot sends a response from the knowledge base:

state: Answer
intent!: /question
scriptEs6:
// The bot gets the dialog history
const history = await $rag.getChatHistory();
// The bot sends the request to the knowledge base
$rag.query.generateAnswer("MyKnowledgeHub", $request.query, history)
.then(answer => {
$conversationApi.sendTextToClient(answer.response);
})
.catch(error => {
$conversationApi.sendTextToClient("An error occured: " + error);
});
tip

For examples of working with the knowledge base and different ways to send requests to it, see Using in a script.