Method ImportTextAsync
ImportTextAsync(string, string, string, CancellationToken)
Asynchronously imports a text string into a DataSource, creating a new section.
public Task<DataSource> ImportTextAsync(string data, string dataSourceIdentifier, string sectionIdentifier, CancellationToken cancellationToken = default)
Parameters
datastringThe text content to import.
dataSourceIdentifierstringThe unique identifier for the target DataSource. If the identifier 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);
string textData = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
DataSource dataSource = await ragEngine.ImportTextAsync(
data: textData,
dataSourceIdentifier: "latinData",
sectionIdentifier: "introduction");
Console.WriteLine($"Data imported into source: {dataSource.Identifier}");
Exceptions
- ArgumentNullException
Thrown if
dataordataSourceIdentifierisnullor empty.- OperationCanceledException
Thrown if the operation is canceled.
ImportTextAsync(string, IChunking, string, string, CancellationToken)
Asynchronously imports a text string into a DataSource, creating a new section with a custom chunking strategy.
public Task<DataSource> ImportTextAsync(string data, IChunking chunker, string dataSourceIdentifier, string sectionIdentifier, CancellationToken cancellationToken = default)
Parameters
datastringThe text content to import.
chunkerIChunkingThe chunking strategy for splitting the text. If
null, DefaultIChunking is used.dataSourceIdentifierstringThe unique identifier for the target DataSource. If the identifier 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);
string textData = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
DataSource dataSource = await ragEngine.ImportTextAsync(
data: textData,
chunker: new TextChunking { MaxChunkSize = 500 },
dataSourceIdentifier: "latinData",
sectionIdentifier: "introduction");
Console.WriteLine($"Data imported into source: {dataSource.Identifier}");
Exceptions
- ArgumentNullException
Thrown if
dataordataSourceIdentifierisnullor empty.- OperationCanceledException
Thrown if the operation is canceled.
ImportTextAsync(string, string, string, MetadataCollection, CancellationToken)
Asynchronously imports a text string into a DataSource, creating a new section and attaching additional metadata.
public Task<DataSource> ImportTextAsync(string data, string dataSourceIdentifier, string sectionIdentifier, MetadataCollection additionalMetadata, CancellationToken cancellationToken = default)
Parameters
datastringThe text content to import.
dataSourceIdentifierstringThe unique identifier for the target DataSource. If the identifier 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
dataordataSourceIdentifierisnullor empty.- OperationCanceledException
Thrown if the operation is canceled.
ImportTextAsync(string, IChunking, string, string, MetadataCollection, CancellationToken)
Asynchronously imports a text string into a DataSource, creating a new section with a custom chunking strategy and attaching additional metadata.
public Task<DataSource> ImportTextAsync(string data, IChunking chunker, string dataSourceIdentifier, string sectionIdentifier, MetadataCollection additionalMetadata, CancellationToken cancellationToken = default)
Parameters
datastringThe text content to import.
chunkerIChunkingThe chunking strategy for splitting the text. If
null, DefaultIChunking is used.dataSourceIdentifierstringThe unique identifier for the target DataSource. If the identifier 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
dataordataSourceIdentifierisnullor empty.- OperationCanceledException
Thrown if the operation is canceled.
ImportTextAsync(IList<string>, string, IList<string>, IList<MetadataCollection>, CancellationToken)
Asynchronously imports multiple text strings into a DataSource, creating a new section for each.
public Task<DataSource> ImportTextAsync(IList<string> data, string dataSourceIdentifier, IList<string> sectionIdentifiers, IList<MetadataCollection> metadataCollections = null, CancellationToken cancellationToken = default)
Parameters
dataIList<string>A list of text strings to import, each representing a separate section.
dataSourceIdentifierstringThe unique identifier for the target DataSource. If the data source exists, new sections are added; otherwise, a new data source is created.
sectionIdentifiersIList<string>A list of unique identifiers for the new sections. The count must match the number of text strings in
data.metadataCollectionsIList<MetadataCollection>An optional list of metadata collections to associate with each imported section. If provided, the count must match the number of text strings.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<DataSource>
A task that resolves to the updated or newly created DataSource.
Exceptions
- ArgumentNullException
Thrown if
data,dataSourceIdentifier, orsectionIdentifiersisnullor empty.- ArgumentOutOfRangeException
Thrown if the count of
sectionIdentifiersdoes not match the count ofdata.
ImportTextAsync(IList<string>, IChunking, string, IList<string>, IList<MetadataCollection>, CancellationToken)
Asynchronously imports multiple text strings into a DataSource, creating a new section for each with a custom chunking strategy.
public Task<DataSource> ImportTextAsync(IList<string> data, IChunking chunker, string dataSourceIdentifier, IList<string> sectionIdentifiers, IList<MetadataCollection> metadataCollections = null, CancellationToken cancellationToken = default)
Parameters
dataIList<string>A list of text strings to import, each representing a separate section.
chunkerIChunkingThe chunking strategy for splitting each text. If
null, DefaultIChunking is used.dataSourceIdentifierstringThe unique identifier for the target DataSource. If the data source exists, new sections are added; otherwise, a new data source is created.
sectionIdentifiersIList<string>A list of unique identifiers for the new sections. The count must match the number of text strings in
data.metadataCollectionsIList<MetadataCollection>An optional list of metadata collections to associate with each imported section. If provided, the count must match the number of text strings.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<DataSource>
A task that resolves to the updated or newly created DataSource.
Examples
LM embeddingModel = LM.LoadFromModelID("embeddinggemma-300m");
RagEngine ragEngine = new RagEngine(embeddingModel);
var pages = new List<string>
{
"Page 1: Introduction to RAG systems.",
"Page 2: Advanced RAG techniques."
};
var sectionIds = new List<string> { "chapter1", "chapter2" };
DataSource ds = await ragEngine.ImportTextAsync(
data: pages,
chunker: new TextChunking { MaxChunkSize = 300 },
dataSourceIdentifier: "ragBook",
sectionIdentifiers: sectionIds);
Console.WriteLine($"Imported {pages.Count} pages into DataSource: {ds.Identifier}");
Exceptions
- ArgumentNullException
Thrown if
data,dataSourceIdentifier, orsectionIdentifiersisnullor empty.- ArgumentOutOfRangeException
Thrown if the count of
sectionIdentifiersdoes not match the count ofdata.