Method ExtractKeywordsAsync
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
ExtractKeywordsAsync(string, CancellationToken)
Asynchronously extracts a set of keywords from the provided text content.
public Task<List<KeywordExtraction.KeywordItem>> ExtractKeywordsAsync(string content, CancellationToken cancellationToken = default)
Parameters
contentstringThe text content from which to extract keywords.
cancellationTokenCancellationTokenA token to cancel the operation if needed.
Returns
- Task<List<KeywordExtraction.KeywordItem>>
A task representing the asynchronous extraction operation that returns a list of extracted KeywordExtraction.KeywordItem instances upon completion.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Threading.Tasks;
async Task ExtractKeywordsExample()
{
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
KeywordExtraction extractor = new KeywordExtraction(model);
var keywords = await extractor.ExtractKeywordsAsync("Renewable energy sources like solar and wind power are essential for sustainable development.");
Console.WriteLine("Extracted keywords:");
foreach (var keyword in keywords)
{
Console.WriteLine($" - {keyword.Value}");
}
}
Exceptions
- ArgumentNullException
Thrown when the content is null or empty.
ExtractKeywordsAsync(Attachment, CancellationToken)
Asynchronously extracts a set of keywords from an Attachment. For multi-page documents (e.g. PDF), all pages are analyzed.
public Task<List<KeywordExtraction.KeywordItem>> ExtractKeywordsAsync(Attachment content, CancellationToken cancellationToken = default)
Parameters
contentAttachmentThe attachment to analyze. Can be an image or a multi-page document.
cancellationTokenCancellationTokenA token to cancel the operation if needed.
Returns
- Task<List<KeywordExtraction.KeywordItem>>
A task representing the asynchronous extraction operation that returns a list of extracted KeywordExtraction.KeywordItem instances upon completion.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using LMKit.Data;
using System;
using System.Threading.Tasks;
async Task ExtractFromImageAsync()
{
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
KeywordExtraction extractor = new KeywordExtraction(model);
var imageAttachment = new Attachment("diagram.png");
var keywords = await extractor.ExtractKeywordsAsync(imageAttachment);
Console.WriteLine("Keywords from image:");
foreach (var keyword in keywords)
{
Console.WriteLine($" - {keyword.Value}");
}
}
Exceptions
- ArgumentNullException
Thrown when the attachment is null.
- InvalidModelException
Thrown when the underlying language model does not support the required inference modality.
ExtractKeywordsAsync(Attachment, string, CancellationToken)
Asynchronously extracts a set of keywords from the specified page range of an Attachment.
public Task<List<KeywordExtraction.KeywordItem>> ExtractKeywordsAsync(Attachment content, string pageRange, CancellationToken cancellationToken = default)
Parameters
contentAttachmentThe attachment to analyze. Can be an image or a multi-page document.
pageRangestringA page range string using 1-based page numbers (e.g.,
"1-5, 7, 9-12"). Ifnull, empty, or"*", all pages are analyzed.cancellationTokenCancellationTokenA token to cancel the operation if needed.
Returns
- Task<List<KeywordExtraction.KeywordItem>>
A task representing the asynchronous extraction operation that returns a list of extracted KeywordExtraction.KeywordItem instances upon completion.
Exceptions
- ArgumentNullException
Thrown when the attachment is null.
- InvalidModelException
Thrown when the underlying language model does not support the required inference modality.