Table of Contents

Method RecognizeAsync

Namespace
LMKit.TextAnalysis
Assembly
LM-Kit.NET.dll

RecognizeAsync(Attachment, CancellationToken)

Asynchronously recognizes named entities in the given attachment. For multi-page documents (e.g. PDF), all pages are analyzed.

public Task<List<NamedEntityRecognition.ExtractedEntity>> RecognizeAsync(Attachment content, CancellationToken cancellationToken = default)

Parameters

content Attachment

The attachment to analyze. Can be an image or a multi-page document.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<List<NamedEntityRecognition.ExtractedEntity>>

A task that resolves to a list of detected NamedEntityRecognition.ExtractedEntity instances.

Examples

using LMKit.Model;
using LMKit.TextAnalysis;
using LMKit.Data;
using System;
using System.Threading.Tasks;

async Task ExtractFromDocumentAsync()
{
    LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
    NamedEntityRecognition ner = new NamedEntityRecognition(model);

    var pdfDocument = new Attachment("contract.pdf");
    var entities = await ner.RecognizeAsync(pdfDocument);

    Console.WriteLine($"Found {entities.Count} entities:");
    foreach (var entity in entities)
    {
        Console.WriteLine($"  [{entity.Type}] {entity.Value}");
    }
}

Exceptions

ArgumentNullException

Thrown when the attachment is null.

InvalidModelException

Thrown when the underlying language model does not support the required inference modality.

RecognizeAsync(Attachment, string, CancellationToken)

Asynchronously recognizes named entities in the specified page range of the given attachment.

public Task<List<NamedEntityRecognition.ExtractedEntity>> RecognizeAsync(Attachment content, string pageRange, CancellationToken cancellationToken = default)

Parameters

content Attachment

The attachment to analyze. Can be an image or a multi-page document.

pageRange string

A page range string using 1-based page numbers (e.g., "1-5, 7, 9-12"). If null, empty, or "*", all pages are analyzed.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<List<NamedEntityRecognition.ExtractedEntity>>

A task that resolves to a list of detected NamedEntityRecognition.ExtractedEntity instances.

Exceptions

ArgumentNullException

Thrown when the attachment is null.

InvalidModelException

Thrown when the underlying language model does not support the required inference modality.

RecognizeAsync(string, CancellationToken)

Asynchronously recognizes named entities in the given content.

public Task<List<NamedEntityRecognition.ExtractedEntity>> RecognizeAsync(string content, CancellationToken cancellationToken = default)

Parameters

content string

The non-null, non-empty input text to analyze.

cancellationToken CancellationToken

Returns

Task<List<NamedEntityRecognition.ExtractedEntity>>

A task that resolves to a list of NamedEntityRecognition.ExtractedEntity objects representing each detected entity.

Examples

using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Threading.Tasks;

async Task RecognizeEntitiesAsync()
{
    LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
    NamedEntityRecognition ner = new NamedEntityRecognition(model);

    string document = "The contract between Google and the European Commission was signed on March 1, 2024.";
    var entities = await ner.RecognizeAsync(document);

    foreach (var entity in entities)
    {
        Console.WriteLine($"[{entity.Type}] {entity.Value}");
    }
}

Exceptions

ArgumentException

Thrown if content is null or whitespace.

Share