Table of Contents

Method LoadDocumentAsync

Namespace
LMKit.Retrieval
Assembly
LM-Kit.NET.dll

LoadDocumentAsync(string, DocumentMetadata, CancellationToken)

Asynchronously loads a PDF document from the specified file path.

public Task<DocumentIndexingResult> LoadDocumentAsync(string filePath, DocumentRag.DocumentMetadata documentMetadata = null, CancellationToken cancellationToken = default)

Parameters

filePath string

Path to the PDF file.

documentMetadata DocumentRag.DocumentMetadata

Optional metadata to associate with the document. If null, default metadata is created using the file name. Use this to specify a custom document name, reference URL, or additional metadata fields for source attribution in query results.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DocumentIndexingResult>

A task containing a DocumentIndexingResult describing how the document was processed, including the indexing mode, page count, and token count.

Examples

// Load with custom metadata for source tracking
var metadata = new DocumentMetadata("Q4 Financial Report")
{
    SourceUri = "https://intranet.example.com/docs/report.pdf",
    AdditionalMetadata = new MetadataCollection
    {
        { "author", "Finance Team" },
        { "confidentiality", "Internal" }
    }
};
var result = await chat.LoadDocumentAsync("report.pdf", metadata);

Remarks

Documents can be added at any time, including after the conversation has started. A document loaded mid-conversation is always indexed for passage retrieval; the full-document context mode is only available before the first question.

Exceptions

FileNotFoundException

Thrown if the specified file does not exist.

ObjectDisposedException

Thrown if this instance has been disposed.

LoadDocumentAsync(Stream, string, DocumentMetadata, CancellationToken)

Asynchronously loads a PDF document from a stream.

public Task<DocumentIndexingResult> LoadDocumentAsync(Stream stream, string documentName, DocumentRag.DocumentMetadata documentMetadata = null, CancellationToken cancellationToken = default)

Parameters

stream Stream

Stream containing PDF data.

documentName string

Display name for the document.

documentMetadata DocumentRag.DocumentMetadata

Optional metadata to associate with the document. If null, default metadata is created using documentName. Use this to specify a custom document name, reference URL, or additional metadata fields for source attribution in query results.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DocumentIndexingResult>

A task containing a DocumentIndexingResult describing how the document was processed, including the indexing mode, page count, and token count.

Examples

using var stream = File.OpenRead("contract.pdf");

// Create metadata with reference URL and custom fields
var metadata = new DocumentMetadata("contract.pdf")
{
    SourceUri = "file:///contracts/2024/contract-001.pdf",
    AdditionalMetadata = new MetadataCollection
    {
        { "client", "Acme Corp" },
        { "status", "Active" }
    }
};

var result = await chat.LoadDocumentAsync(stream, "contract.pdf", metadata);

// Later, when querying, source references include the metadata
var response = await chat.SubmitAsync("What are the payment terms?");
foreach (var source in response.SourceReferences)
{
    if (source.Metadata.TryGet("client", out var clientMeta))
        Console.WriteLine($"From client: {clientMeta.Value}");
}

Exceptions

InvalidOperationException

Thrown if PageProcessingMode is DocumentUnderstanding and DocumentVisionParser is not set.

ArgumentNullException

Thrown if stream or documentName is null or empty.

ObjectDisposedException

Thrown if this instance has been disposed.

LoadDocumentAsync(Attachment, string, DocumentMetadata, CancellationToken)

Asynchronously loads a document from an existing Attachment, taking ownership of it.

public Task<DocumentIndexingResult> LoadDocumentAsync(Attachment document, string documentName = null, DocumentRag.DocumentMetadata documentMetadata = null, CancellationToken cancellationToken = default)

Parameters

document Attachment

The attachment to load. Ownership transfers to this PdfChat: the attachment is retained for the session and disposed when ClearDocuments() is called or this instance is disposed (and is also disposed if this call fails). The caller must not dispose it.

documentName string

Display name for the document. When null, the attachment's Name is used.

documentMetadata DocumentRag.DocumentMetadata

Optional metadata to associate with the document. If null, default metadata is created from the resolved document name.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DocumentIndexingResult>

A task containing a DocumentIndexingResult describing how the document was processed.

Remarks

This overload performs no copy of the document content, making it the most memory-efficient way to load a document whose bytes are already wrapped in an Attachment (for example a file-backed attachment that streams pages from disk on demand). The PDF renderer reads pages lazily during vision / full-context inference, so the attachment's backing stream must stay valid for the session; transferring ownership guarantees that.

Documents can be added at any time, including after the conversation has started. A document loaded mid-conversation is always indexed for passage retrieval; the full-document context mode is only available before the first question.

Exceptions

ArgumentNullException

Thrown if document is null.

ObjectDisposedException

Thrown if this instance has been disposed.

Share