How to get generative answers
The /ask
endpoint allows you to get generative answers from a Knowledge Box.
For example, if you store information about Hedy Lamarr in your Knowledge Box, you can ask questions like:
Who is Hedy Lamarr?
You will get a generative answer like:
Hedy Lamarr was an actress and inventor known for her contributions to the development of wireless communication technology.
Then, you can continue chatting with the Knowledge Box, based on the context of the previous question:
What did she do during the war?
Here, "she" is understood as "Hedy Lamarr", because it refers to the first question.
Data structure
As the answer generation is a slow process, the /ask
endpoint is delivering a readable HTTP stream.
The stream is a newline-delimited JSON, according the NDJSON format.
Each line is a JSON object containing an item of the response:
{ item: AskResponseItem}
The possible item types are:
retrieval
: The search results matching the query (same as the/find
endpoint). They are the paragraphs passed to the generative model.answer
: The generative answer.metadata
: The amount of tokens consumed by the query and the answer generation and the time taken to produce the response.citations
: The paragraphs actually used to generated the answer (among the search results initially passed to the generative model) and the positions of the corresponding parts of the answer.status
: The status of the response when complete. It can besuccess
orerror
.error
: The error message when the status iserror
.relations
: The relations of the entities mentioned in the query.
Usage
-
You can get a fully decoded response directly using the Nuclia Python CLI/SDK.
-
To get generative answers in the Nuclia search widget, you need to enable the
answers
feature:<script src="https://cdn.nuclia.cloud/nuclia-video-widget.umd.js"></script>
<nuclia-search-bar
knowledgebox="YOUR-KB"
zone="ZONE"
features="answers"
></nuclia-search-bar>
<nuclia-search-results></nuclia-search-results> -
For testing, you can use it with
curl
:curl 'https://<ZONE>.nuclia.cloud/api/v1/kb/<YOUR-KB>/ask' -H 'content-type: application/json' --data-raw '{"query":"Who is Hedy Lamarr?","context":[]}' -H "x-synchronous: true"
noteThe
x-synchronous
header on the/ask
is mostly meant for testing purpose. Without this header, the default behavior is to return a readable stream, as it allows to display the beginning of the answer without waiting for the end of the generation. Thex-synchronous
header turns the response in a regular HTTP response, so it makes the query slower, as it waits for the end of the generation before returning the answer. -
To implement your own chat widget, you can get inspiration from the Nuclia search widget implementation:
- Reading a readable HTTP stream (check the
getStream
method) - Decoding the result
- Reading a readable HTTP stream (check the