Method GetBestCategory
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
GetBestCategory(IList<string>, string, bool, CancellationToken)
Classifies the specified plain text into one of the predefined categories.
public int GetBestCategory(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. Specifies whether the input parameters should be normalized. The default is
true
.cancellationToken
CancellationTokenOptional. A CancellationToken that can be used to cancel the operation.
Returns
- int
The index of the best matching category from the
categories
list, or -1 if no suitable category is found.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
LM model = LM.LoadFromModelID("llama-3.1");
Categorization categorization = new Categorization(model);
var categories = new List<string> { "finance", "travel", "entertainment" };
string text = "Stock prices soared on Wall Street.";
int bestCategoryIndex = categorization.GetBestCategory(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
.
GetBestCategory(IList<string>, Attachment, bool, CancellationToken)
Classifies the content provided as an attachment into one of the predefined categories. The attachment can represent plain text or an image. If the attachment is an image requiring vision processing, the underlying language model must support vision input.
public int GetBestCategory(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. The attachment may contain either plain text or image data.
normalize
boolOptional. Specifies whether the input parameters should be normalized. The default is
true
.cancellationToken
CancellationTokenOptional. A CancellationToken to cancel the operation if needed.
Returns
- int
The index of the best matching category from the
categories
list, or -1 if no suitable category is found.
Exceptions
- InvalidModelException
Thrown when the attachment is an image that requires vision input but the underlying language model does not support vision.