Query API


The query API is available in SLU v4.1 or later.

In some cases, it may be necessary to retrieve a response generated by your AI assistant to use it further in the dialog logic. For example, you may need to get the information the user requests and save it to a file, send it by email and so on.

Alan AI provides the query API to query corpus data programmatically. The query API can be helpful for:

  • Education and training

  • Data analysis

  • Reporting and so on

To query corpus data programmatically, add api.query_v1() to the dialog script. The query API takes two parameters:

  • Data corpus against which the query is run

  • Question to be asked

Dialog script
let text = corpus(`
    My name is Alan

intent('Answer my question', async p => {
    let answer = await api.query_v1({corpus: text, question: "What is your name?"});
    p.play('Here you go')

Example of use

Let’s assume you want to get an answer to a question from the FAQ data you have and store it. For this, you can add the following code to the dialog script:

Dialog script
let FAQ = corpus(`
    Why is my smartphone battery draining quickly? Possible causes include excessive app usage, background processes, high screen brightness, push notifications, and weak cellular signals. To address this, close unnecessary apps, reduce screen brightness, disable push notifications for non-essential apps, and keep your device updated.
    How do I fix a frozen or unresponsive smartphone? Try a soft reset by holding the power button for 10 seconds. If that fails, perform a force restart by holding the power and volume down buttons for 10-15 seconds.

intent("Can you tell me $(QUERY* .+)", async p => {
    p.play("Just a second...");
    let answer = await api.query_v1({corpus: FAQ, question: p.QUERY.value});
    if (answer) {
        let final = await answer.gather();
    } else {
        p.play(`Sorry, I cannot help you with that`);

Here, the corpus data is saved to FAQ. The user’s question is captured with the QUERY slot and then passed to api.query_v1 together with the corpus data. To get the answer, the gather() method is used that collects the final response. The result is then written to Alan Studio logs.