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 -1 if no category can be associated with the provided text.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Collections.Generic;
using 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>List of predefined categories.
categoryDescriptionsIList<string>Optional descriptions for each category.
contentstringThe plain text to be classified.
normalizeboolSpecifies whether normalization should occur.
cancellationTokenCancellationTokenCancellationToken for cancelling the operation.
Returns
GetBestCategoryAsync(IList<string>, Attachment, bool, CancellationToken)
Asynchronously classifies the content provided as an attachment into one of the predefined categories. The attachment may contain either plain text or image data. For image attachments that require vision processing, ensure that the underlying language model supports vision input.
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.
normalizeboolSpecifies whether input parameters should be normalized. Default is
true.cancellationTokenCancellationTokenCancellationToken for cancelling the operation.
Returns
- Task<int>
A task representing the asynchronous operation. The task result contains the index of the best matching category or -1 if none match.
Exceptions
- InvalidModelException
Thrown when the attachment is an image 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, assuming the underlying language model supports 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 (default is
true).cancellationTokenCancellationTokenA
CancellationTokenused 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.
normalizeboolSpecifies whether input parameters should be normalized. Default is
true.cancellationTokenCancellationTokenCancellationToken for cancelling the operation.
Returns
Exceptions
- InvalidModelException
Thrown when the attachment is an image requiring vision input but the underlying language model does not support vision.