$rag.retrieveChunks
The method returns chunks relevant to the user’s query from a knowledge base.
The method is available only in the ECMAScript 6 runtime and is asynchronous.
Syntax
The method accepts the following arguments:
Argument | Type | Description | Required |
---|---|---|---|
secretName | String | Name of the knowledge base secret. | Yes |
query | String | Text of the user query. | Yes |
history | Array | History 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 |
settings | Object | Chunk search settings. By default, the settings from the Tovie Data Agent project are used. Thesettings format matches the format of the identically named object in the POST /api/knowledge-hub/retrieve request in the Tovie Data Agent API. | No |
timeout | Number | 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.
- Positional arguments
- Via object
Specify the arguments in order:
await $rag.retrieveChunks("MyKnowledgeHub", "What does the Example service do?", undefined, undefined, 5000);
Pass an object whose fields match the names of the arguments:
await $rag.retrieveChunks({
secretName: "MyKnowledgeHub",
query: "What does the Example service do?",
timeout: 5000
});
Return value
The method returns an object with a list of chunks. They are sorted in descending order of relevance.
{
"chunks": [
{
"score": 0.7486038,
"content": "The Example service processes user requests …",
"docId": "Documentation.pdf",
"metadata": null
},
{
"score": 0.7337575,
"content": "The Example service can handle high loads …",
"docId": "Services.pdf",
"metadata": null
}
]
}
The object has the same format as the response to the POST /api/knowledge-hub/retrieve
request in the Tovie Data Agent API.
How to use
In this state, the bot sends the most relevant chunk to the user:
state: Chunks
intent!: /question
scriptEs6:
// The bot gets the dialog history
const history = await $rag.getChatHistory();
// The bot sends the request to the knowledge base
const chunks = await $rag.retrieveChunks("MyKnowledgeHub", $request.query, history);
$reactions.answer(chunks.chunks[0].content);