Edit

Share via


Microsoft.Extensions.AI libraries

.NET developers need to integrate and interact with a growing variety of artificial intelligence (AI) services in their apps. The Microsoft.Extensions.AI libraries provide a unified approach for representing generative AI components, and enable seamless integration and interoperability with various AI services. This article introduces the libraries and provides in-depth usage examples to help you get started.

The packages

The 📦 Microsoft.Extensions.AI.Abstractions package provides the core exchange types, including IChatClient and IEmbeddingGenerator<TInput,TEmbedding>. Any .NET library that provides an LLM client can implement the IChatClient interface to enable seamless integration with consuming code.

The 📦 Microsoft.Extensions.AI package has an implicit dependency on the Microsoft.Extensions.AI.Abstractions package. This package enables you to easily integrate components such as automatic function tool invocation, telemetry, and caching into your applications using familiar dependency injection and middleware patterns. For example, it provides the UseOpenTelemetry(ChatClientBuilder, ILoggerFactory, String, Action<OpenTelemetryChatClient>) extension method, which adds OpenTelemetry support to the chat client pipeline.

Which package to reference

To have access to higher-level utilities for working with generative AI components, reference the Microsoft.Extensions.AI package instead (which itself references Microsoft.Extensions.AI.Abstractions). Most consuming applications and services should reference the Microsoft.Extensions.AI package along with one or more libraries that provide concrete implementations of the abstractions.

Libraries that provide implementations of the abstractions typically reference only Microsoft.Extensions.AI.Abstractions.

Install the packages

For information about how to install NuGet packages, see dotnet package add or Manage package dependencies in .NET applications.

APIs and functionality

The IChatClient interface

The IChatClient interface defines a client abstraction responsible for interacting with AI services that provide chat capabilities. It includes methods for sending and receiving messages with multi-modal content (such as text, images, and audio), either as a complete set or streamed incrementally.

For more information and detailed usage examples, see Use the IChatClient interface.

The IEmbeddingGenerator interface

The IEmbeddingGenerator interface represents a generic generator of embeddings. For the generic type parameters, TInput is the type of input values being embedded, and TEmbedding is the type of generated embedding, which inherits from the Embedding class.

For more information and detailed usage examples, see Use the IEmbeddingGenerator interface.

The IImageGenerator interface (experimental)

The IImageGenerator interface represents a generator for creating images from text prompts or other input. This interface enables applications to integrate image generation capabilities from various AI services through a consistent API. The interface supports text-to-image generation (by calling GenerateAsync(ImageGenerationRequest, ImageGenerationOptions, CancellationToken)) and configuration options for image size and format. Like other interfaces in the library, it can be composed with middleware for caching, telemetry, and other cross-cutting concerns.

For more information, see Generate images from text using AI.

Build with Microsoft.Extensions.AI

You can start building with Microsoft.Extensions.AI in the following ways:

  • Library developers: If you own libraries that provide clients for AI services, consider implementing the interfaces in your libraries. This allows users to easily integrate your NuGet package via the abstractions. For examples, see IChatClient implementation examples and IEmbeddingGenerator implementation examples.
  • Service consumers: If you're developing libraries that consume AI services, use the abstractions instead of hardcoding to a specific AI service. This approach gives your consumers the flexibility to choose their preferred provider.
  • Application developers: Use the abstractions to simplify integration into your apps. This enables portability across models and services, facilitates testing and mocking, leverages middleware provided by the ecosystem, and maintains a consistent API throughout your app, even if you use different services in different parts of your application.
  • Ecosystem contributors: If you're interested in contributing to the ecosystem, consider writing custom middleware components.

For more samples, see the dotnet/ai-samples GitHub repository. For an end-to-end sample, see eShopSupport.

See also