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>, IList<string>, string, bool, CancellationToken)
Classifies the specified plain text into one of the predefined categories.
public int GetBestCategory(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
- int
The index of the best matching category or -1 if none match.
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.
normalize
boolSpecifies whether input parameters should be normalized. Default is
true
.cancellationToken
CancellationTokenCancellationToken for cancelling the operation.
Returns
- int
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.
GetBestCategory(IList<string>, IList<string>, Attachment, bool, CancellationToken)
Classifies the content provided as an attachment into one of the predefined categories.
public int GetBestCategory(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
- int
The index of the best matching category or -1 if none match.