Ask API - questions

Answer a question using LLM technology.


The questions API takes time to process and generate answer. The SDK provides a simple polling mechanism to retrieve the answer.

Syntax

// get answer object
const answer = await client.api.ask.questions(payload);

// get question ID if you are interested
const questionId = answer.questionId;

// polling response
let intervalId;
intervalId = setInterval(async () => {
const response = await answer.get();

// do something with response
// ...

if (response.finished) {
clearInterval(intervalId);
}
}, 1000);

Alternatively, the answer object is also async iterable, which can be processed with for await ... of pattern:

// get answer object
const answer = await client.api.ask.questions(payload);

for await (const response of answer) {
// do something with response
// ...
}

// abort polling when necessary
answer.abort();

Payload

The payload parameter is an object with the following properties:

Name Type Description
question string The question.
parent_question_id string The ID of the previous question. Make this query a follow-up question by specifying this ID.
fq string A query string in Solr syntax which restricts the superset of products to return, without influencing the overall ranking.
source_fl array of strings A list of fields to be returned on sources items. The fields product_id, title, snippet are always included. Default: ["cover_image"].
related_resource_fl array of strings A list of fields to be returned on related_resources items. The fields product_id, title, snippet are always included. Default: ["cover_image"].

Return value

A Promise of response object with the following properties:

Name Type Description
answer string The answer to the question.
question string The question.
question_id string The question ID which be can used to retrieve answer updates.
sources array of objects An array of articles that the answer is based on.
related_resources array of objects An array of articles that the answer is related to.
followup_questions array of strings An array of sugessions for follow-up questions.
finished boolean Whether the answer is finished.

Examples

const payload = {
question: 'How was the economy in 2022?'
};

for (const { answer, sources } of await client.api.ask.questions(payload)) {
// do something with response
// ...
}

Learn more

For advanced usage, see REST API.