Work with multiple documents in AI Assistant

This guide shows how to use AI Assistant with multiple documents, enabling users to ask questions and get insights across their entire document collection using AIAssistantViewController.

When working with multiple documents, AI Assistant enables users to:

  • Ask questions that span across multiple documents in your app
  • Get contextual answers that reference specific content from any document
  • Navigate seamlessly between documents based on links

Working with multiple documents is particularly powerful for apps that handle document workflows, research materials, or any scenario where users require the content of multiple related documents simultaneously.

Before starting ensure you have a working setup running AI Assistant for your project.

Using AI Assistant with multiple documents requires a new license component for AI Assistant, which will be available in the AI Assistant 1.5 release. Contact our Sales team for more information.

Creating the configuration

To support multiple documents, create an AIAssistantConfiguration that includes all document IDs in its JWT. The JWT serves as the authorization token that tells the AI Assistant server which documents are available for this session.

func createAIAssistantConfiguration(for documents: [Document]) -> AIAssistantConfiguration {
let sessionID = "multi-document-ios-session"
let claims: [String: Any] = [
"document_ids": documents.compactMap { $0.documentId?.hexadecimalEncodedString() },
"session_ids": [sessionID]
]
// In production, generate JWT server-side for security
let jwt = generateJWT(claims: claims)
// Use the server URL where your AI Assistant is hosted.
let serverURL = URL(string: "http://localhost:4000")!
return AIAssistantConfiguration(serverURL: serverURL, jwt: jwt, sessionID: sessionID)
}

Presenting the UI

Create an AIAssistantSession with document collection you want to make available to the AI Assistant and your configuration and use it to create AIAssistantViewController and present the view controller.

func setupAIAssistant(with documents: [Document]) -> AIAssistantViewController {
let configuration = createAIAssistantConfiguration(for: documents)
let session = AIAssistantSession(documents: documents, configuration: configuration)
let aiAssistantViewController = AIAssistantViewController(session: session)
aiAssistantViewController.delegate = self
return aiAssistantViewController
}

AIAssistantViewController handles setting up the connection to AI Assistant and shows the chat interface to start interacting with the documents once it is ready.

Handling document navigation

Implement AIAssistantViewControllerDelegate to handle navigation when the AI Assistant directs users to specific content. This delegate method is called whenever the AI Assistant wants to show the user relevant information in a particular document.

func aiAssistantViewController(_ aiAssistantViewController: AIAssistantViewController, navigateTo document: Document, pageIndex: PageIndex, rects: [CGRect]) {
// Switch to the correct document in your interface
switchToDocument(document)
// Navigate to the specific page and highlight relevant areas
navigateToPage(pageIndex, highlightingRects: rects)
}

The rects parameter contains the specific areas on the page that the AI Assistant wants to highlight in PDF coordinate space.

If you are using PDFViewController to display documents you can forward the call to this delegate method as it also conforms to AIAssistantViewControllerDelegate:

func navigateToPage(_ pageIndex: PageIndex, rects: [CGRect]) {
// Navigate to and highlight the specific locations
pdfController.aiAssistantViewController(aiAssistantViewController,navigateTo: document,pageIndex: pageIndex, rects: rects)
}

Integration patterns

For projects using PDFTabbedViewController, integrate the AI Assistant to work seamlessly with your existing document tabs. You can make the relevant document tab automatically visible when navigating to content from separate documents.

func aiAssistantViewController(_ aiAssistantViewController: AIAssistantViewController, navigateTo document: Document, pageIndex: PageIndex, rects: [CGRect]) {
// Switch to the document tab
tabbedViewController.setVisibleDocument(document, scrollToPosition: true, animated: true)
// Navigate to and highlight the specific locations
if let pdfController = tabbedViewController.pdfController {
pdfController.aiAssistantViewController(aiAssistantViewController, navigateTo: document, pageIndex: pageIndex, rects: rects)
}
}

Managing document changes

When your document collection changes, you need to recreate the AIAssistantViewController with an updated JWT that reflects the new document set. This ensures the AI Assistant has access to the correct documents.

func addDocument(_ document: Document) {
// Remove existing AI Assistant
aiAssistantViewController?.removeFromParent()
aiAssistantViewController = nil
// Recreate with updated document list
let aiAssistantViewController = setupAIAssistant(with: documents)
}

Example integrations

Have a look at MultiDocumentAIAssistantExample(opens in a new tab) in our Catalog project for an example of how you can use multiple documents with AI Assistant.

OSZAR »