Method ImportTextFromFile
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
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
- 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
pathordataSourceIdentifierisnullor 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
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
- 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
pathordataSourceIdentifierisnullor 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
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
- DataSource
The DataSource containing the imported text.
Exceptions
- ArgumentNullException
Thrown if
pathordataSourceIdentifierisnullor 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
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
- DataSource
The DataSource containing the imported text.
Exceptions
- ArgumentNullException
Thrown if
pathordataSourceIdentifierisnullor empty.- OperationCanceledException
Thrown if the operation is canceled.