Method GetBestCategoryAsync
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
GetBestCategoryAsync(IList<string>, string, bool, CancellationToken)
Asynchronously classifies the specified plain text into one of the predefined categories.
public Task<int> GetBestCategoryAsync(IList<string> categories, string content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names. Each entry should describe the corresponding category.
contentstringThe plain text to be classified.
normalizeboolOptional. Indicates whether input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- Task<int>
A task representing the asynchronous operation. The task result contains the index of the best matching category in the
categorieslist, or-1if no category can be associated with the provided text.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
class Example
{
static async Task Main()
{
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
Categorization categorization = new Categorization(model);
var categories = new List<string> { "animals", "technology", "music" };
string text = "A newly discovered frog species can glow in the dark.";
int bestCategoryIndex = await categorization.GetBestCategoryAsync(categories, text);
if (bestCategoryIndex != -1)
{
Console.WriteLine($"Category: {categories[bestCategoryIndex]}");
}
else
{
Console.WriteLine("No suitable category found.");
}
Console.WriteLine($"Confidence: {categorization.Confidence}");
}
}
Exceptions
- OperationCanceledException
Thrown if the operation is canceled via the
cancellationToken.
GetBestCategoryAsync(IList<string>, IList<string>, string, bool, CancellationToken)
Asynchronously classifies the specified plain text into one of the predefined categories.
public Task<int> GetBestCategoryAsync(IList<string> categories, IList<string> categoryDescriptions, string content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined categories.
categoryDescriptionsIList<string>Optional descriptions for each category.
contentstringThe plain text to be classified.
normalizeboolSpecifies whether normalization should occur.
cancellationTokenCancellationTokenA CancellationToken for canceling the operation.
Returns
GetBestCategoryAsync(IList<string>, Attachment, bool, CancellationToken)
Asynchronously classifies the content provided as an attachment into one of the predefined categories.
public Task<int> GetBestCategoryAsync(IList<string> categories, Attachment content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names.
contentAttachmentThe attachment to be classified. Supports various file formats including images, PDF documents, HTML files, and Microsoft Office formats. See Attachment for the complete list of supported formats.
normalizeboolSpecifies whether input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenA CancellationToken for canceling the operation.
Returns
- Task<int>
A task representing the asynchronous operation. The task result contains the index of the best matching category, or
-1if none match.
Exceptions
- InvalidModelException
Thrown when the attachment contains visual content requiring vision input but the underlying language model does not support vision.
GetBestCategoryAsync(IList<string>, IList<string>, ImageBuffer, bool, CancellationToken)
Asynchronously classifies the given image into one of the predefined categories. The underlying language model must support vision input.
public Task<int> GetBestCategoryAsync(IList<string> categories, IList<string> categoryDescriptions, ImageBuffer image, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names.
categoryDescriptionsIList<string>Optional descriptions for each category.
imageImageBufferThe image to classify.
normalizeboolSpecifies whether input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenA CancellationToken used to cancel the operation.
Returns
- Task<int>
A Task<TResult> that resolves to the zero-based index of the best matching category, or
-1if none match.
Exceptions
- InvalidModelException
Thrown if the underlying model does not support vision.
GetBestCategoryAsync(IList<string>, IList<string>, Attachment, bool, CancellationToken)
Asynchronously classifies the content provided as an attachment into one of the predefined categories.
public Task<int> GetBestCategoryAsync(IList<string> categories, IList<string> categoryDescriptions, Attachment content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names.
categoryDescriptionsIList<string>Optional descriptions for each category.
contentAttachmentThe attachment to be classified. Supports various file formats including images, PDF documents, HTML files, and Microsoft Office formats.
normalizeboolSpecifies whether input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenA CancellationToken for canceling the operation.
Returns
Exceptions
- InvalidModelException
Thrown when the attachment contains visual content requiring vision input but the underlying language model does not support vision.