Back to Devexpress

IAIDocProcessingService.AskAIAsync(PdfDocumentProcessor, String, RagOptions, CancellationToken) Method

officefileapi-devexpress-dot-aiintegration-dot-docs-dot-iaidocprocessingservice-dot-askaiasync-x28-pdfdocumentprocessor-string-ragoptions-cancellationtoken-x29.md

latest6.0 KB
Original Source

IAIDocProcessingService.AskAIAsync(PdfDocumentProcessor, String, RagOptions, CancellationToken) Method

Returns a response to a custom question about the document content.

Namespace : DevExpress.AIIntegration.Docs

Assembly : DevExpress.AIIntegration.Docs.v25.2.dll

NuGet Package : DevExpress.AIIntegration.Docs

Declaration

csharp
Task<string> AskAIAsync(
    PdfDocumentProcessor processor,
    string question,
    RagOptions options,
    CancellationToken cancellationToken = default(CancellationToken)
)
vb
Function AskAIAsync(
    processor As PdfDocumentProcessor,
    question As String,
    options As RagOptions,
    cancellationToken As CancellationToken = Nothing
) As Task(Of String)

Parameters

NameTypeDescription
processorPdfDocumentProcessor

The PdfDocumentProcessor instance that contains the content to be questioned.

| | question | String |

The question about the document content.

| | options | RagOptions |

An object that contains retrieval-augmented generation (RAG) options.

|

Optional Parameters

NameTypeDefaultDescription
cancellationTokenCancellationTokennull

The token that cancels the task.

|

Returns

TypeDescription
Task<String>

The response that contains AI-generated answer.

|

Example

How to: Ask Contextual Question about a PDF File

The following code snippet asks a contextual question about the PDF file, configures RAG options (chunk size, collection name, chunk count), and adds the answer as a sticky note annotation:

csharp
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.Docs;
using DevExpress.Pdf;
using Microsoft.Extensions.AI;

// See "Register AI extension service" section for implementation code
var docProcessingService = 
    defaultAIExtensionsContainer.CreateAIDocProcessingService();

var options = new RagOptions {
    VectorCollectionName = "knowledge_base_vectors",
    ChunkSize = 600,
    AugmentationChunkCount = 7
}; 

using (var pdfDocumentProcessor = new PdfDocumentProcessor()) {
    FileStream fs = File.OpenRead(
        Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory, 
            @"Documents/Document1.pdf")); 
        pdfDocumentProcessor.LoadDocument(fs, true);
    fs.Close();

    string result = 
        await docProcessingService.AskAIAsync(
            pdfDocumentProcessor, 
            "What terms does this document contain?",
            options);

    // Access the first page properties
    PdfPageFacade page = pdfDocumentProcessor.DocumentFacade.Pages[0];

    // Add sticky note at the specified point
    PdfTextAnnotationFacade textAnnotation =
       page.AddTextAnnotation(
           new PdfPoint(64, 65), 
           PdfTextAnnotationIconName.Comment);

    // Specify annotation parameters
    textAnnotation.Author = "AI-Generated";
    textAnnotation.Color = new PdfRGBColor(0.8, 0.2, 0.1);
    textAnnotation.Contents = result;

    // Save the modified document
    string outputFilePath = 
        Path.Combine(Environment.CurrentDirectory, $"Document1_Annotated.pdf");
    pdfDocumentProcessor.SaveDocument(outputFilePath);
}
vb
Imports DevExpress.AIIntegration
Imports DevExpress.AIIntegration.Docs
Imports DevExpress.Pdf
Imports Microsoft.Extensions.AI
Imports System.IO

' See "Register AI extension service" section for implementation code
Dim docProcessingService = defaultAIExtensionsContainer.CreateAIDocProcessingService()

Dim options As New RagOptions With {
    .VectorCollectionName = "knowledge_base_vectors",
    .ChunkSize = 600,
    .AugmentationChunkCount = 7
}

Using pdfDocumentProcessor As New PdfDocumentProcessor()
    Dim fs As FileStream = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Documents\Document1.pdf"))
    pdfDocumentProcessor.LoadDocument(fs, True)
    fs.Close()

    Dim result As String = Await docProcessingService.AskAIAsync(pdfDocumentProcessor, "What terms does this document contain?", options)

    ' Access the first page properties
    Dim page As PdfPageFacade = pdfDocumentProcessor.DocumentFacade.Pages(0)

    ' Add sticky note at the specified point
    Dim textAnnotation As PdfTextAnnotationFacade = page.AddTextAnnotation(New PdfPoint(64, 65), PdfTextAnnotationIconName.Comment)

    ' Specify annotation parameters
    textAnnotation.Author = "AI-Generated"
    textAnnotation.Color = New PdfRGBColor(0.8, 0.2, 0.1)
    textAnnotation.Contents = result

    ' Save the modified document
    Dim outputFilePath As String = Path.Combine(Environment.CurrentDirectory, "Document1_Annotated.pdf")
    pdfDocumentProcessor.SaveDocument(outputFilePath)
End Using

See Also

IAIDocProcessingService Interface

IAIDocProcessingService Members

DevExpress.AIIntegration.Docs Namespace