Method LoadDocument
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
filePathstringPath to the PDF file.
documentMetadataDocumentRag.DocumentMetadataOptional 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.cancellationTokenCancellationTokenToken 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
streamStreamStream containing PDF data.
documentNamestringDisplay name for the document.
documentMetadataDocumentRag.DocumentMetadataOptional metadata to associate with the document. If
null, default metadata is created usingdocumentName. Use this to specify a custom document name, reference URL, or additional metadata fields for source attribution in query results.cancellationTokenCancellationTokenToken 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
streamordocumentNameisnullor 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
documentAttachmentThe 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.
documentNamestringDisplay name for the document. When
null, the attachment's Name is used.documentMetadataDocumentRag.DocumentMetadataOptional metadata to associate with the document. If
null, default metadata is created from the resolved document name.cancellationTokenCancellationTokenToken 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
documentisnull.- ObjectDisposedException
Thrown if this instance has been disposed.