Method ImportTextFromFileAsync
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
pathstringThe file path of the text file to import.
encodingEncodingThe character encoding used in the file.
dataSourceIdentifierstringThe 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.
sectionIdentifierstringThe identifier for the new section.
cancellationTokenCancellationTokenA 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
pathordataSourceIdentifierisnullor 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
pathstringThe file path of the text file to import.
encodingEncodingThe character encoding used in the file.
chunkerIChunkingThe chunking strategy for splitting the text. If
null, DefaultIChunking is used.dataSourceIdentifierstringThe 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.
sectionIdentifierstringThe identifier for the new section.
cancellationTokenCancellationTokenA 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
pathordataSourceIdentifierisnullor 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
pathstringThe file path of the text file to import.
encodingEncodingThe character encoding used in the file.
dataSourceIdentifierstringThe 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.
sectionIdentifierstringThe identifier for the new section.
additionalMetadataMetadataCollectionMetadata to associate with the imported section.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<DataSource>
A task that resolves to the DataSource containing the imported text.
Exceptions
- ArgumentNullException
Thrown if
pathordataSourceIdentifierisnullor 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
pathstringThe file path of the text file to import.
encodingEncodingThe character encoding used in the file.
chunkerIChunkingThe chunking strategy for splitting the text. If
null, DefaultIChunking is used.dataSourceIdentifierstringThe 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.
sectionIdentifierstringThe identifier for the new section.
additionalMetadataMetadataCollectionMetadata to associate with the imported section.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<DataSource>
A task that resolves to the DataSource containing the imported text.
Exceptions
- ArgumentNullException
Thrown if
pathordataSourceIdentifierisnullor empty.- OperationCanceledException
Thrown if the operation is canceled.