Create a chatbot
Nuclia is a RAG-based search engine that generates answers based on a set of documents. That is essentially waht is needed to create a chatbot.
The main difference between basic question answering and a chatbot is that a chatbot is able to maintain a conversation with the user. This is achieved by keeping track of the context of the conversation.
Nuclia /ask
endpoint is stateless, and does not keep track of the conversation. To create a chatbot, you need to keep track of the conversation on your side.
To inject the context in the /ask
call, you can use the context
parameter. It is a list of objects that represent the context of the conversation. Each object has an author
and a text
field. You need to pass bothe the user's questions (author: "USER"
) and the Nuclia's answers (author: "NUCLIA"
).
By setting the rephrase
parameter to true
, Nuclia will rephrase the last question according the context in order to retrieve accurate data. In our example below, the user asks "What did she invent?" and the context allows to understand that "she" refers to Hedy Lamarr.
Here is an example of a call to the /ask
endpoint with conversation context:
- API
- CLI
- Python SDK
curl --request POST 'https://<zone>.nuclia.cloud/api/v1/kb/<your-knowledge-box-id>/ask' \
--header 'X-NUCLIA-SERVICEACCOUNT: Bearer YOUR_API_KEY' \
--header 'content-type: application/json' \
--data '{
"query": "What did she invent?",
"context": [
{
"author": "USER",
"text": "Who is Hedy Lamarr?"
},
{
"author": "NUCLIA",
"text": "Hedy Lamarr was an actor, inventor, and amateur engineer."
}
],
"rephrase": true
}'
nuclia kb search ask --query='{"query":"What did she invent?","context":[{"author": "USER","text":"Who is Hedy Lamarr?"},{"author": "NUCLIA","text":"Hedy Lamarr was an actor, inventor, and amateur engineer."}],"rephrase":true}'
from nuclia import sdk
# Nuclia knowledge box URL and API key
KNOWLEDGE_BOX_URL = "https://<zone>.nuclia.cloud/api/v1/kb/<your-knowledge-box-id>"
API_KEY = "<your-api-key>"
# Authenticate with the Nuclia SDK
sdk.NucliaAuth().kb(url=KNOWLEDGE_BOX_URL, token=API_KEY)
# Ask a question with context
sdk.NucliaSearch().ask(
query={
"query": "What did she invent?",
"context": [
{
"author": "USER",
"text": "Who is Hedy Lamarr?"
},
{
"author": "NUCLIA",
"text": "Hedy Lamarr was an actor, inventor, and amateur engineer."
}
],
"rephrase": True
}
)