Skip to content

Added embedding API to C# SDK#581

Open
phanindraraja wants to merge 9 commits intomicrosoft:mainfrom
phanindraraja:user/rchava/embedding-support
Open

Added embedding API to C# SDK#581
phanindraraja wants to merge 9 commits intomicrosoft:mainfrom
phanindraraja:user/rchava/embedding-support

Conversation

@phanindraraja
Copy link
Copy Markdown
Member

@phanindraraja phanindraraja commented Apr 3, 2026

Summary

Adds embedding support to the Foundry Local C# SDK, enabling text embedding generation through the OpenAIEmbeddingClient via Foundry Local Core.

Changes

SDK

  • OpenAIEmbeddingClient — Public client for generating embeddings. Exposes GenerateEmbeddingAsync(string input) which sends the "embeddings" command to FoundryLocalCore via ICoreInterop and returns an EmbeddingCreateResponse. Configurable via EmbeddingSettings (optional Dimensions and EncodingFormat fields).
  • EmbeddingRequestResponseTypes.cs — Request DTO (EmbeddingCreateRequestExtended) with FromUserInput() factory, and response deserialization/error handling extensions. Logs only payload length on deserialization failure to avoid large log output. Validates response.Data is non-null before dereferencing.
  • IModel.GetEmbeddingClientAsync() — New interface method for obtaining an embedding client from a loaded model.
  • Model / ModelVariant — Implementation that checks the model is loaded and returns a new OpenAIEmbeddingClient instance.
  • JsonSerializationContext — Registered EmbeddingCreateRequestExtended and EmbeddingCreateResponse for AOT-compatible serialization.

Tests

  • EmbeddingClientTests.cs — Tests against qwen3-0.6b-embedding-generic-cpu:1:
    • Basic request succeeds with correct response structure and 1024 dimensions
    • Embedding is L2-normalized (norm ≈ 1.0, all values within [-1, 1]) across multiple inputs
    • Different inputs produce different embeddings (cosine similarity < 0.99)
    • Same input produces identical embeddings (determinism)
    • Known golden values for first and last embedding components (with tolerance)

Documentation

  • README.md — Added embeddings to features list, usage example with GetEmbeddingClientAsync() and settings configuration.
  • docs/api/index.md — Added OpenAIEmbeddingClient to class index.
  • docs/api/openaiembeddingclient.md — New API reference page with properties and methods.
  • docs/api/imodel.md, model.md, modelvariant.md — Added GetEmbeddingClientAsync() method documentation.

Test plan

  • dotnet build succeeds
  • All SDK tests pass (embedding + existing chat/audio)
  • Embedding response from SDK matches FoundryLocal Core web service curl output (1024-dim, normalized)

Dependencies

This PR depends on the neutron-server PR that adds the "embeddings" functionality and /v1/embeddings endpoint to Foundry Local core

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 3, 2026

Someone is attempting to deploy a commit to the MSFT-AIP Team on Vercel.

A member of the Team first needs to authorize it.

@phanindraraja phanindraraja changed the title Added embedding API to SDK Added embedding API to C# SDK Apr 3, 2026
@phanindraraja phanindraraja marked this pull request as ready for review April 3, 2026 07:03
Copilot AI review requested due to automatic review settings April 3, 2026 07:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an embeddings capability to the Foundry Local C# SDK, aligning it with the existing OpenAI-compatible chat and audio clients and exposing it through the model abstraction.

Changes:

  • Introduces OpenAIEmbeddingClient with request/response serialization helpers and JSON source-gen registrations.
  • Exposes embeddings via IModel.GetEmbeddingClientAsync() and implements it on Model / ModelVariant with loaded-model enforcement.
  • Adds test coverage plus README/API docs updates for the new embeddings surface.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
sdk/cs/src/OpenAI/EmbeddingClient.cs Adds OpenAIEmbeddingClient and the public embeddings API surface.
sdk/cs/src/OpenAI/EmbeddingRequestResponseTypes.cs Adds embeddings request DTO + response parsing/serialization helpers.
sdk/cs/src/IModel.cs Adds GetEmbeddingClientAsync to the public model interface.
sdk/cs/src/Detail/Model.cs Wires GetEmbeddingClientAsync through to the selected variant.
sdk/cs/src/Detail/ModelVariant.cs Implements GetEmbeddingClientAsync with “must be loaded” validation.
sdk/cs/src/Detail/JsonSerializationContext.cs Registers embeddings request/response types for STJ source generation.
sdk/cs/test/FoundryLocal.Tests/EmbeddingClientTests.cs Adds embeddings integration tests (basic, normalization, determinism, known values).
sdk/cs/README.md Documents embeddings feature and shows usage + settings.
sdk/cs/docs/api/microsoft.ai.foundry.local.openaiembeddingclient.md Adds generated API docs for OpenAIEmbeddingClient.
sdk/cs/docs/api/microsoft.ai.foundry.local.imodel.md Updates generated API docs for IModel with embeddings accessor.
sdk/cs/docs/api/microsoft.ai.foundry.local.model.md Updates generated API docs for Model with embeddings accessor.
sdk/cs/docs/api/microsoft.ai.foundry.local.modelvariant.md Updates generated API docs for ModelVariant with embeddings accessor.
sdk/cs/docs/api/index.md Adds OpenAIEmbeddingClient to the API docs index.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants