Skip to main content

Widgets API

Agentic RAG's widgets are custom elements, and they offers an API that can be interacted with from the outside. It allows you to achieve a better integration between the widget and your own application.

Search widget API​

This triggers a search.

Example:

const widget = document.getElementsByTagName('nuclia-search-bar')[0];
widget.search('What is the answer to the Ultimate Question of Life, the Universe, and Everything?');

reloadSearch​

This triggers a reloading of the search results.

const widget = document.getElementsByTagName('nuclia-search-bar')[0];
widget.reloadSearch();

onInit​

Return a promise that resolves when the widget is initialized.

const widget = document.getElementsByTagName('nuclia-search-bar')[0];
widget.onInit().then(() => {
console.log('Widget is initialized');
});

setInitHook and modifyHeaders​

setInitHook allows you to pass a function that will be executed when the widget is initialized before any call to the API is made.

It is useful to modify the Agentic RAG options, typically setting extra HTTP headers using modifyHeaders:

widget.setInitHook((nuclia) => {
nuclia.options.modifyHeaders = (headers) => {
headers['x-whatever'] = 'some-value';
return headers;
};
});

A typical use case is to set a CSRF token in the headers.

onError​

This is a hook that will be called when a network error occurs.

widget.onError.subscribe((error) => {
console.error('An error occurred', error.status, error.detail);
};

setPreviewBaseUrl​

When using permalink and openNewTab features, allows defining a custom page that will be used to launch the viewer. This page is meant to contain the <nuclia-viewer> widget.

widget.setPreviewBaseUrl('https://your-site/preview');

setSearchConfiguration​

Set the search configuration id to pass in search_configuration parameter on /ask or /find.

widget.setSearchConfiguration('config1');

block / unblock​

Disable the input field and display a warning message.

widget.block('We have reached your usage limit.');
widget.unblock();

Chat widget API​

(Note: the search widget API is also available on the chat widget, except for search and reloadSearch methods.)

ask​

This triggers a question answering.

Parameters:

  • question (string): The question to ask.
  • reset (boolean): If true, the current chat will be reset before asking the question.
  • doNotTriggerSearch (boolean): If true, the question is entered in the input but the call is not triggered.

Example:

const widget = document.getElementsByTagName('nuclia-chat')[0];
widget.ask('What is the answer to the Ultimate Question of Life, the Universe, and Everything?');

openChat and closeChat​

These methods open and close the chat, respectively. They do not apply when using the inline mode.

Example:

const widget = document.getElementsByTagName('nuclia-chat')[0];
widget.openChat();