Table of Contents

Method ImportTextFromFile

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

ImportTextFromFile(string, Encoding, string, string, CancellationToken)

Imports text from a file into a DataSource by creating a new section.

public DataSource ImportTextFromFile(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

DataSource

The DataSource containing the imported text.

Examples

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

DataSource dataSource = ragEngine.ImportTextFromFile(
    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.

ImportTextFromFile(string, Encoding, IChunking, string, string, CancellationToken)

Imports text from a file into a DataSource by creating a new section, using a custom chunking strategy.

public DataSource ImportTextFromFile(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

DataSource

The DataSource containing the imported text.

Examples

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

DataSource dataSource = ragEngine.ImportTextFromFile(
    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.

ImportTextFromFile(string, Encoding, string, string, MetadataCollection, CancellationToken)

Imports text from a file into a DataSource by creating a new section, and attaches additional metadata.

public DataSource ImportTextFromFile(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

DataSource

The DataSource containing the imported text.

Exceptions

ArgumentNullException

Thrown if path or dataSourceIdentifier is null or empty.

OperationCanceledException

Thrown if the operation is canceled.

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

Imports text from a file into a DataSource by creating a new section, using a custom chunking strategy and attaching additional metadata.

public DataSource ImportTextFromFile(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

DataSource

The DataSource containing the imported text.

Exceptions

ArgumentNullException

Thrown if path or dataSourceIdentifier is null or empty.

OperationCanceledException

Thrown if the operation is canceled.