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
categories
IList<string>A list of predefined category names. Each entry should describe the corresponding category.
content
stringThe plain text to be classified.
normalize
boolOptional. Indicates whether input parameters should be normalized. The default is
true
.cancellationToken
CancellationTokenOptional. 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
categories
list, 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("llama-3.1");
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
categories
IList<string>List of predefined categories.
categoryDescriptions
IList<string>Optional descriptions for each category.
content
stringThe plain text to be classified.
normalize
boolSpecifies whether normalization should occur.
cancellationToken
CancellationTokenCancellationToken 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
categories
IList<string>A list of predefined category names.
content
AttachmentThe attachment to be classified.
normalize
boolSpecifies whether input parameters should be normalized. Default is
true
.cancellationToken
CancellationTokenCancellationToken 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>, 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
categories
IList<string>A list of predefined category names.
categoryDescriptions
IList<string>Optional descriptions for each category.
content
AttachmentThe attachment to be classified.
normalize
boolSpecifies whether input parameters should be normalized. Default is
true
.cancellationToken
CancellationTokenCancellationToken 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.