Table of Contents

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 string

The plain text to be classified.

normalize bool

Optional. Indicates whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

Optional. 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 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

categories IList<string>

A list of predefined categories.

categoryDescriptions IList<string>

Optional descriptions for each category.

content string

The plain text to be classified.

normalize bool

Specifies whether normalization should occur.

cancellationToken CancellationToken

A CancellationToken for canceling the operation.

Returns

Task<int>

The index of the best matching category, or -1 if none match.

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

categories IList<string>

A list of predefined category names.

content Attachment

The 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.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A 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 -1 if 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

categories IList<string>

A list of predefined category names.

categoryDescriptions IList<string>

Optional descriptions for each category.

image ImageBuffer

The image to classify.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A 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 -1 if 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

categories IList<string>

A list of predefined category names.

categoryDescriptions IList<string>

Optional descriptions for each category.

content Attachment

The attachment to be classified. Supports various file formats including images, PDF documents, HTML files, and Microsoft Office formats.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A CancellationToken for canceling the operation.

Returns

Task<int>

The index of the best matching category, or -1 if none match.

Exceptions

InvalidModelException

Thrown when the attachment contains visual content requiring vision input but the underlying language model does not support vision.