Table of Contents

Method ImportTextFromFileAsync

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

ImportTextFromFileAsync(string, Encoding, string, string, CancellationToken)

Asynchronously imports text from a file into a DataSource, creating a new section.

public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, string dataSourceIdentifier, string sectionIdentifier, CancellationToken cancellationToken = default)

Parameters

path string

The file path of the text file to import.

encoding Encoding

The character encoding used in the file.

dataSourceIdentifier string

The unique identifier for the target DataSource. If a matching data source exists, the text is added as a new section; otherwise, a new data source is created.

sectionIdentifier string

The identifier for the new section.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<DataSource>

A task that resolves to the DataSource containing the imported text.

Examples

LM embeddingModel = LM.LoadFromModelID("embeddinggemma-300m");
RagEngine ragEngine = new RagEngine(embeddingModel);

DataSource dataSource = await ragEngine.ImportTextFromFileAsync(
    path: "sample.txt",
    encoding: Encoding.UTF8,
    dataSourceIdentifier: "myDataSource",
    sectionIdentifier: "intro");

Console.WriteLine($"Imported text into DataSource: {dataSource.Identifier}");

Exceptions

ArgumentNullException

Thrown if path or dataSourceIdentifier is null or empty.

OperationCanceledException

Thrown if the operation is canceled.

ImportTextFromFileAsync(string, Encoding, IChunking, string, string, CancellationToken)

Asynchronously imports text from a file into a DataSource, creating a new section with a custom chunking strategy.

public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, IChunking chunker, string dataSourceIdentifier, string sectionIdentifier, CancellationToken cancellationToken = default)

Parameters

path string

The file path of the text file to import.

encoding Encoding

The character encoding used in the file.

chunker IChunking

The chunking strategy for splitting the text. If null, DefaultIChunking is used.

dataSourceIdentifier string

The unique identifier for the target DataSource. If a matching data source exists, the text is added as a new section; otherwise, a new data source is created.

sectionIdentifier string

The identifier for the new section.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<DataSource>

A task that resolves to the DataSource containing the imported text.

Examples

LM embeddingModel = LM.LoadFromModelID("embeddinggemma-300m");
RagEngine ragEngine = new RagEngine(embeddingModel);

DataSource dataSource = await ragEngine.ImportTextFromFileAsync(
    path: "sample.txt",
    encoding: Encoding.UTF8,
    chunker: new TextChunking { MaxChunkSize = 500 },
    dataSourceIdentifier: "myDataSource",
    sectionIdentifier: "intro");

Console.WriteLine($"Imported text into DataSource: {dataSource.Identifier}");

Exceptions

ArgumentNullException

Thrown if path or dataSourceIdentifier is null or empty.

OperationCanceledException

Thrown if the operation is canceled.

ImportTextFromFileAsync(string, Encoding, string, string, MetadataCollection, CancellationToken)

Asynchronously imports text from a file into a DataSource, creating a new section and attaching additional metadata.

public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, string dataSourceIdentifier, string sectionIdentifier, MetadataCollection additionalMetadata, CancellationToken cancellationToken = default)

Parameters

path string

The file path of the text file to import.

encoding Encoding

The character encoding used in the file.

dataSourceIdentifier string

The unique identifier for the target DataSource. If a matching data source exists, the text is added as a new section; otherwise, a new data source is created.

sectionIdentifier string

The identifier for the new section.

additionalMetadata MetadataCollection

Metadata to associate with the imported section.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<DataSource>

A task that resolves to the DataSource containing the imported text.

Exceptions

ArgumentNullException

Thrown if path or dataSourceIdentifier is null or empty.

OperationCanceledException

Thrown if the operation is canceled.

ImportTextFromFileAsync(string, Encoding, IChunking, string, string, MetadataCollection, CancellationToken)

Asynchronously imports text from a file into a DataSource, creating a new section with a custom chunking strategy and attaching additional metadata.

public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, IChunking chunker, string dataSourceIdentifier, string sectionIdentifier, MetadataCollection additionalMetadata, CancellationToken cancellationToken = default)

Parameters

path string

The file path of the text file to import.

encoding Encoding

The character encoding used in the file.

chunker IChunking

The chunking strategy for splitting the text. If null, DefaultIChunking is used.

dataSourceIdentifier string

The unique identifier for the target DataSource. If a matching data source exists, the text is added as a new section; otherwise, a new data source is created.

sectionIdentifier string

The identifier for the new section.

additionalMetadata MetadataCollection

Metadata to associate with the imported section.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<DataSource>

A task that resolves to the DataSource containing the imported text.

Exceptions

ArgumentNullException

Thrown if path or dataSourceIdentifier is null or empty.

OperationCanceledException

Thrown if the operation is canceled.