Back to Devexpress

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

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

latest4.9 KB
Original Source

IAIDocProcessingService.AskAIAsync(RichEditDocumentServer, 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(
    RichEditDocumentServer wordProcessor,
    string question,
    RagOptions options,
    CancellationToken cancellationToken = default(CancellationToken)
)
vb
Function AskAIAsync(
    wordProcessor As RichEditDocumentServer,
    question As String,
    options As RagOptions,
    cancellationToken As CancellationToken = Nothing
) As Task(Of String)

Parameters

NameTypeDescription
wordProcessorRichEditDocumentServer

The RichEditDocumentServer 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 an AI-generated answer.

|

Example

How to: Ask Contextual Question about a Word Document

The following code snippet asks a question about the document content, configures RAG options (chunk size, collection name, chunk count), and inserts the AI‑generated answer as a comment about the first paragraph:

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

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

var options = new RagOptions {
    VectorCollectionName = "document_embeddings",
    ChunkSize = 800,
    AugmentationChunkCount = 8
};

using (var wordProcessor = new RichEditDocumentServer()) {
    wordProcessor.LoadDocument(@"Documents/Document1.docx");
    string answer = await docProcessingService.AskAIAsync(
        wordProcessor,
        "Does this document contain any confidential information?",
        options
    );

    wordProcessor.Document.Comments.Create(
        wordProcessor.Document.Paragraphs[0].Range, 
        "AI Summary:\n" + answer);
    wordProcessor.SaveDocument("Documents/Document_commented.docx", DocumentFormat.Docx);
}
vb
vbnet
Imports DevExpress.AIIntegration
Imports DevExpress.AIIntegration.Docs
Imports DevExpress.XtraRichEdit
Imports Microsoft.Extensions.AI

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

Dim options As New RagOptions With {
    .VectorCollectionName = "document_embeddings",
    .ChunkSize = 800,
    .AugmentationChunkCount = 8
}

Using wordProcessor As New RichEditDocumentServer()
    wordProcessor.LoadDocument("Documents/Document1.docx")
    Dim answer As String = Await docProcessingService.AskAIAsync(
        wordProcessor,
        "Does this document contain any confidential information?",
        options
    )

    wordProcessor.Document.Comments.Create(
        wordProcessor.Document.Paragraphs(0).Range,
        "AI Summary:" & vbLf & answer)
    wordProcessor.SaveDocument("Documents/Document_commented.docx", DocumentFormat.Docx)
End Using

See Also

IAIDocProcessingService Interface

IAIDocProcessingService Members

DevExpress.AIIntegration.Docs Namespace