Files
UA-.NETStandard/docs/AI.md
Marc Schier 7e25e1a3d7 Extract remaining shared companion, rendering and packaging changes (#4806)
# Description

The shared AI deployment contracts and instance registration; DI
software-upload outcomes, digest handling and simulator; bounded
Positioning/Pumps reads; PubSub raw-data/matrix and security-provider
handling; Vision read-quality contracts; xRegistry resource writes and
instance projection; OpenUSD authoring, embedded viewport and
retained-frame capture APIs; standalone sample CLI controls; and the
generic Debug/RID package-policy and fork/Dependabot coverage-comment
fixes. The Linux native prerequisite step is retained for **Vision tests
only**.

## Related Issues

- Related tracking issue:
[#4461](https://github.com/OPCFoundation/UA-.NETStandard/issues/4461),
without automatic closure.
- Source work:
[#4462](https://github.com/OPCFoundation/UA-.NETStandard/pull/4462),
which remains intact for the coordinator's later integration.
- Immediate stacked predecessor:
[#4804](https://github.com/OPCFoundation/UA-.NETStandard/pull/4804).

## Checklist

_Put an `x` in the boxes that apply. You can complete these step by step
after opening the PR._

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [ ] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [ ] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.
2026-10-17 07:52:02 +02:00

12 KiB

AI Model Management developer guide

The Opc.Ua.AI, Opc.Ua.AI.Server, Opc.Ua.AI.Client, and Opc.Ua.AI.Inference packages implement the draft OPC UA — AI Model Management and Inference companion specification in .NET.

Draft. The namespace http://opcfoundation.org/UA/AI/ and every NodeId in it are provisional until the working group publishes the specification.

AI Model Management exposes model sources, models, datasets, deployments, and inference methods through OPC UA. Clients can discover available resources and read provenance and trust-boundary metadata. They can also invoke Invoke or InvokeAsync and transfer large artefacts with standard file-transfer types.

Contents

Packages

Package What it gives you Depends on
OPCFoundation.NetStandard.Opc.Ua.AI Source-generated AI model — ObjectTypes, ReferenceTypes, DataTypes, enums, node states and model loader Opc.Ua.Core
OPCFoundation.NetStandard.Opc.Ua.AI.Server AINodeManager, AIOptions, fallback reporting, transfer and job support, and AddAI hosting extensions Opc.Ua.AI, Opc.Ua.Server, Opc.Ua.AI.Inference
OPCFoundation.NetStandard.Opc.Ua.AI.Client AIClient, AIClientFactory and AddAIClient() DI registration Opc.Ua.AI, Opc.Ua.Client
OPCFoundation.NetStandard.Opc.Ua.AI.Inference IInferenceBackend, ChatClientInferenceBackend, RestChatCompletionsBackend, credential resolvers and backend options Opc.Ua.AI, Microsoft.Extensions.AI

The AI libraries target modern .NET TFMs used by the sample (net8.0, net9.0 and net10.0). The inference assembly intentionally has no Azure, OpenAI or other vendor SDK dependency.

Model

The Server publishes one AI root below the Server object. The root contains:

  • Model sources
  • Deployments
  • Models
  • Datasets
  • Jobs

Each deployment describes where inference runs and identifies its model source. It also records egress permission, input-retention status, and the maximum inline payload size.

Two properties are especially important:

  • An inference result includes ModelUsed, so callers can tell whether the primary model or a degraded fallback answered.
  • CredentialReference is a name only. An ICredentialResolver resolves the credential value inside the Server process. The address space contains only the reference name, never the credential value.

Large payloads use the standard Part 5 FileType transfer flow. Asynchronous inference jobs use the Part 10 program lifecycle so clients can monitor state instead of polling a private API.

Minimal hosted server

AddAI registers the node manager, options and default backend composition. The host supplies the IChatClient through IChatClientFactory; that keeps vendor packages in the host and out of Opc.Ua.AI.Inference.

using Microsoft.Extensions.Hosting;
using Opc.Ua.AI.Inference;
using Opc.Ua.AI.Server;
using Opc.Ua.AI.Server.Hosting;
using Opc.Ua.Server.Fluent;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.Services.AddRestChatCompletionsAIChatClientFactory();

builder.Services
    .AddOpcUa()
    .AddServer(options =>
    {
        options.ApplicationName = "AIServer";
        options.ApplicationUri = "urn:localhost:OPCFoundation:AIServer";
        options.AutoAcceptUntrustedCertificates = true;
        options.EndpointUrls.Add("opc.tcp://localhost:62640/AIServer");
    })
    .AddAI(
        ai => builder.Configuration.GetSection(AIOptions.SectionName).Bind(ai),
        backend => builder.Configuration
            .GetSection(InferenceBackendOptions.SectionName)
            .Bind(backend),
        fallback => builder.Configuration
            .GetSection(InferenceBackendOptions.FallbackSectionName)
            .Bind(fallback));

using IHost app = builder.Build();
await app.RunAsync().ConfigureAwait(false);

AddRestChatCompletionsAIChatClientFactory() is the sample-friendly factory: it creates an IChatClient over the configured OpenAI-compatible endpoint without adding a vendor SDK. A production host can instead register its own IChatClientFactory. That factory can create IChatClient instances for Azure, OpenAI, Ollama, or an on-device runtime.

The direct construction path remains available for hosts that do not use the generic hosting stack:

var backends = new InferenceBackends(primaryBackend, fallbackBackend);
var factory = new AINodeManagerFactory(
    backends,
    Options.Create(new AIOptions()),
    Options.Create(new InferenceBackendOptions()));

Hosting API

The extension method on IOpcUaServerBuilder is:

Method Purpose
AddAI(Action<AIOptions>?, Action<InferenceBackendOptions>?, Action<InferenceBackendOptions>?) Registers AINodeManagerFactory, AIOptions, primary and fallback InferenceBackendOptions, an InferenceBackends singleton, and the OPC UA node-manager registration

AddAI composes the backend from InferenceBackendOptions.Kind:

  • ChatClient (default) creates ChatClientInferenceBackend from IChatClientFactory.
  • RestChatCompletions creates RestChatCompletionsBackend directly for endpoints whose wire contract is the OpenAI-compatible REST shape.

AIOptions

Property Purpose
PrimaryDeploymentId / FallbackDeploymentId Deployment identifiers published in the address space
EnableFallback Publishes the fallback deployment and FallsBackTo reference
EnableCatalogue Publishes catalogue and import-job nodes
EnableLearningLoop Publishes a LearningJobType node for ground-truth sample accounting
TransferExpiry, MaxTransferSize, MaxConcurrentTransfers, TransferInferenceTimeout Bounds for chunked transfers
AsyncInferenceDelay, MaxRetainedJobs Bounds and timing for asynchronous inference jobs
SourceId Identifier of the model source

When EnableLearningLoop is true, AINodeManager publishes one LearningJobType under LearningJobs. Host-level coordinators report ground-truth corrections through RecordLearningSampleAsync(sampleId, sampleKind). The stable sampleId makes retries idempotent, and AILearningSampleKind.Negative counts empty or retracted observations exactly as positive examples count.

InferenceBackendOptions

Property Purpose
Enabled Enables the backend; most useful for disabling fallback
Kind ChatClient or RestChatCompletions
EndpointUri, ChatCompletionsPath, ProbePath Endpoint and paths for REST-shaped clients
Authentication, CredentialReference, ApiKeyHeader, CredentialDirectory, TokenAudience Server-to-backend authentication
Site, DataJurisdiction, EgressPermitted, RetainsInput Trust-boundary metadata published to clients
MaxInlinePayloadSize UInt32 inline Invoke payload limit; zero permits only an explicitly chosen transfer
Models Configured model catalogue entries

Client surface

AddAIClient() mirrors the other companion-family client extensions. It registers an AIClientFactory and a Func<CancellationToken, Task<AIClient?>> over the managed session.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Opc.Ua;
using Opc.Ua.AI.Client;
using Opc.Ua.Client;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.Services
    .AddOpcUa()
    .AddClient(options =>
    {
        options.ApplicationName = "AIClient";
        options.ApplicationUri = "urn:localhost:OPCFoundation:AIClient";
        options.AutoAcceptUntrustedCertificates = true;
    })
    .AddDiscoveryAndConnect(options =>
    {
        options.DiscoveryUrl = "opc.tcp://localhost:62640/ModelManagementServer";
        options.SecurityMode = MessageSecurityMode.SignAndEncrypt;
        options.SecurityPolicyUri = SecurityPolicies.Basic256Sha256;
    })
    .AddAIClient();

using IHost app = builder.Build();
await app.StartAsync().ConfigureAwait(false);

var createClient = app.Services
    .GetRequiredService<Func<CancellationToken, Task<AIClient?>>>();
AIClient? client = await createClient(CancellationToken.None)
    .ConfigureAwait(false);

if (client is null)
{
    Console.WriteLine("The Server does not implement AI Model Management.");
}

For one-off code, AIClient.TryCreate(session) is the direct fallback when you already own an ISession.

Inference backends

IInferenceBackend is the server-side contract: list models, invoke a model and probe reachability. Two implementations ship:

  • ChatClientInferenceBackend wraps Microsoft.Extensions.AI.IChatClient. This is the default. Hosts can pair it with any SDK or local runtime that implements the abstraction. The OPC UA address space stays unchanged.
  • RestChatCompletionsBackend speaks the OpenAI-compatible REST chat-completions contract directly. Use it when that REST shape is the actual wire contract and no IChatClient is available.

Both hosted and on-device deployments use the same OPC UA nodes. Their configuration identifies the endpoint, credentials, data jurisdiction, and egress.

Invocation inputs and parameters

Invoke and InvokeAsync accept an inline Payload or a PayloadUri, but the current server accepts inline payloads only. It returns BadNotSupported for URI-only input rather than accepting it and running a backend with no request body.

Both built-in backends accept these parameters:

  • temperature: 0 through 2
  • max_tokens: positive integer
  • top_p: 0 through 1

Clients can provide values as strings, built-in integer or floating-point values, or booleans. Both backends convert values using the invariant culture and forward them through these execution paths:

  • Synchronous and asynchronous calls
  • Fallback execution
  • Oversized-transfer execution

Both backends reject malformed or unsupported parameters. In the REST chat-completions backend, an explicit OPC UA parameter overrides the matching field in the JSON request body. The backend preserves body fields without a matching parameter.

The server sets SpecificationVersion from the source-generated Opc.Ua.AI.ModelVersions.Target constant. The AI NodeSet's target model version determines this constant, so updating the NodeSet also updates the published version without a separately maintained literal.

Example

The sample in samples/AI/ModelManagementServer hosts the node manager with AddAI. By default it uses the ChatClient path and the sample composition root supplies a small IChatClient over the OpenAI-compatible endpoint. verify_backend.py is a throwaway endpoint that speaks enough of that contract for local validation:

python samples/AI/verify_backend.py 5273
dotnet run --project samples/AI/ModelManagementServer
dotnet run --project samples/AI/ModelManagementClient

Set InferenceBackend__Kind=RestChatCompletions to select the REST backend for an endpoint that requires direct REST access. Configure FallbackInferenceBackend__Kind independently when the fallback uses a different wire contract from the primary.

Limitations

  • The companion specification is a draft, so namespace URIs and NodeIds can change.
  • The sample publishes a real LearningJobType instance and a real SamplesCollected counter. Host-level code can call the server-side accounting API when ground-truth corrections arrive, including empty or retracted observations. The sample does not simulate retraining, candidate generation, or promotion.
  • IChatClient has no standard model-enumeration method, so hosts that need a catalogue should configure InferenceBackendOptions.Models.
  • The libraries do not reference vendor SDKs. If your hosting application needs a provider package, add it there and expose it through IChatClientFactory.
  • The AI inference and sample projects disable Native AOT because the Microsoft.Extensions.AI ecosystem uses reflection in areas this repository builds with warnings as errors.

See also