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 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.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- Task<DataSource>
A task that resolves to the DataSource containing the imported text.
Examples
// Example: Asynchronously import text from a file.
string filePath = "sample.txt";
LM embeddingModel = LM.LoadFromModelID("nomic-embed-text");
RagEngine ragEngine = new RagEngine(embeddingModel);
DataSource dataSource = await ragEngine.ImportTextFromFileAsync(
path: filePath,
encoding: Encoding.UTF8,
dataSourceIdentifier: "myDataSource",
sectionIdentifier: "intro"
);
Console.WriteLine($"Imported text into DataSource: {dataSource.Identifier}");
Exceptions
- ArgumentNullException
Thrown if
pathordataSourceIdentifieris null or empty.- OperationCanceledException
Thrown if the operation is canceled.
ImportTextFromFileAsync(string, Encoding, TextChunking, string, string, CancellationToken)
Asynchronously imports text from a file into a DataSource, creating a new section.
public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, TextChunking textChunking, string dataSourceIdentifier, string sectionIdentifier, CancellationToken cancellationToken = default)
Parameters
pathstringThe file path of the text to import.
encodingEncodingThe character encoding used in the file.
textChunkingTextChunkingSpecifies how to split the text into chunks.
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.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- Task<DataSource>
A task that resolves to the DataSource containing the imported text.
Examples
// Example: Asynchronously import text from a file.
string filePath = "sample.txt";
LM embeddingModel = LM.LoadFromModelID("nomic-embed-text");
RagEngine ragEngine = new RagEngine(embeddingModel);
DataSource dataSource = await ragEngine.ImportTextFromFileAsync(
path: filePath,
encoding: Encoding.UTF8,
textChunking: new TextChunking() { MaxChunkSize = 500 },
dataSourceIdentifier: "myDataSource",
sectionIdentifier: "intro"
);
Console.WriteLine($"Imported text into DataSource: {dataSource.Identifier}");
Exceptions
- ArgumentNullException
Thrown if
pathordataSourceIdentifieris 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 information.
public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, string dataSourceIdentifier, string sectionIdentifier, MetadataCollection additionalMetadata, CancellationToken cancellationToken = default)
Parameters
pathstringThe file path of the text 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.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- Task<DataSource>
A task that resolves to the DataSource containing the imported text.
ImportTextFromFileAsync(string, Encoding, TextChunking, string, string, MetadataCollection, CancellationToken)
Asynchronously imports text from a file into a DataSource, creating a new section and attaching additional metadata information.
public Task<DataSource> ImportTextFromFileAsync(string path, Encoding encoding, TextChunking textChunking, string dataSourceIdentifier, string sectionIdentifier, MetadataCollection additionalMetadata, CancellationToken cancellationToken = default)
Parameters
pathstringThe file path of the text to import.
encodingEncodingThe character encoding used in the file.
textChunkingTextChunkingSpecifies how to split the text into chunks.
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.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- Task<DataSource>
A task that resolves to the DataSource containing the imported text.