Skip to main content
All docs
V25.2
  • 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

    Task<string> AskAIAsync(
        PdfDocumentProcessor processor,
        string question,
        RagOptions options,
        CancellationToken cancellationToken = default(CancellationToken)
    )

    Parameters

    Name Type Description
    processor PdfDocumentProcessor

    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

    Name Type Default Description
    cancellationToken CancellationToken null

    The token that cancels the task.

    Returns

    Type Description
    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:

    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);
    }
    
    See Also