Advanced usage of AI Assistant
Use AI Assistant to integrate AI-driven help into your application. The simplest way to use AI Assistant is by attaching an instance directly to the document with the setAiAssistant
method, as described in the setup guide. However, if you need more control, the initialize
method and individual steps in this guide provide advanced flexibility. The responseState
flow allows observing responses, and messages can be sent using emitMessage
.
AI Assistant initialization
val aiAssistant = AiAssistant.standaloneAiAssistant( AiAssistantConfiguration( "https://your-ai-assistant-server.com", "your-jwt-token", "your-session-id", "your-user-id" // Optional ))
With a configured instance of AI Assistant, you have more control over the setup. The allows you to ingest a document early — before loading a document in the user interface (UI) — which can drastically reduce the time before the UI is ready for chatting with the assistant, especially when working with large documents. You can also programmatically retrieve the data from the assistant to get the session history or process the responses from the assistant in your own code.
For AI Assistant to begin processing the document, call the initialize
method, which requires a data provider and a list of document identifiers.
Attaching a document to AI Assistant
To attach a document to AI Assistant, use initialize
with the permanent and changing IDs of the document, which can be taken from the PdfDocument
instance:
val permanentDocumentId = StringUtils.byteToHex(pdfDocument.permanentId) val documentChangingId = StringUtils.byteToHex(pdfDocument.changeId) val identifier = DocumentIdentifiers(permanentDocumentId, documentChangingId) aiAssistant.initialize(dataProvider, identifier)
The initialize
method automatically:
-
Checks if the document is already ingested.
-
Ingests the document if necessary.
-
Establishes a socket connection.
-
Retrieves the session history.
If you prefer to do this manually, follow the steps outlined below.
-
Check if a document is already ingested:
val result = aiAssistant.checkIfDocumentIsAlreadyIngested(documentId, fileHash)
-
Ingest a document:
val ingestionResponse = aiAssistant.ingestDocument(dataProvider, jwtToken)
-
Establish a socket connection:
aiAssistant.initializeSocketConnection()
-
Retrieve the session history:
val history = aiAssistant.getSessionHistory()
Observing responses
After initialization, observe responses using the responseState
flow and update your UI accordingly:
val responseState: Flow<CompletionResponse?> = aiAssistant.responseState
Emitting messages
To interact with AI Assistant, use the emitMessage
method:
aiAssistant.emitMessage("Your query here", permanentDocumentId)
This sends a message to AI Assistant and receives a response via responseState
.
Terminating AI Assistant
To stop AI Assistant, call the terminate
method:
aiAssistant.terminate()