Table of Contents

Method LoadDocument

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

LoadDocument(string, DocumentMetadata, CancellationToken)

Loads a PDF document from the specified file path.

public DocumentIndexingResult LoadDocument(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

DocumentIndexingResult

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

Examples

using var chat = new PdfChat(chatModel, embeddingModel);

var result = chat.LoadDocument("report.pdf");
Console.WriteLine($"Loaded: {result.Name}, Mode: {result.IndexingMode}");

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.

LoadDocument(Stream, string, DocumentMetadata, CancellationToken)

Loads a PDF document from a stream.

public DocumentIndexingResult LoadDocument(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

DocumentIndexingResult

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

Examples

using var chat = new PdfChat(chatModel, embeddingModel);
using var stream = File.OpenRead("contract.pdf");

var result = chat.LoadDocument(stream, "contract.pdf");
Console.WriteLine($"Loaded: {result.Name}, Pages: {result.PageCount}");

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.

LoadDocument(Attachment, string, DocumentMetadata, CancellationToken)

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

public DocumentIndexingResult LoadDocument(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

DocumentIndexingResult

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