.NET AI Samples
repository·main·Indexed 21 days ago
https://github.com/dotnet/ai-samplesA collection of .NET samples demonstrating the integration of AI capabilities (LLMs, SLMs, and embeddings) using unified abstractions and SDKs. It features Microsoft.Extensions.AI for provider-agnostic C# abstractions, Microsoft.Extensions.AI.Evaluation for response quality tooling, and practical implementations using OpenAI, Azure OpenAI, Ollama, and Semantic Kernel. Samples include text summarization, chat applications, function calling, image generation with DALL-E 3, and Semantic Kernel integration in web apps.
What's inside dotnet-ai-samples
- ML.NET GenAI is a preview set of packages designed for Generative AI tasks in .NET. It provides support for popular GenAI models for both inference and fine-tuning. The implementation is designed to be compatible with model weights available on the Hugging Face Model Hub.
Overview of Microsoft.Extensions.AI OpenAI Examples
mainThis repository provides samples demonstrating how to use the OpenAI reference implementation via the
Microsoft.Extensions.AI.OpenAINuGet package.Available sample types include:
- Console Applications: Found in the
OpenAIExamplesdirectory, these provide a set of samples for interacting with the OpenAI implementation. - Web API Applications: Found in the
OpenAIWebAPIdirectory, these demonstrate a minimal Web API implementation using the same package.
- Console Applications: Found in the
Overview of Microsoft.Extensions.AI - Ollama Examples
mainThis repository provides sample implementations demonstrating how to use the Ollama reference implementation via theMicrosoft.Extensions.AI.OllamaNuGet package. It includes both console applications and Web API implementations to show how to integrate local LLMs into .NET applications using theMicrosoft.Extensions.AIabstractions.Overview of Microsoft.Extensions.AI.Evaluation Reporting API Examples
mainThis project provides practical examples of how to use the
Microsoft.Extensions.AI.Evaluationecosystem to perform 'offline' evaluations. It demonstrates how to combine evaluators (Quality, Safety) with reporting concepts like response caching, result storage, and report generation.Key capabilities demonstrated include:
- Orchestrating Offline Evaluations: Using unit testing frameworks and
dotnet testto run evaluations locally or in CI/CD pipelines. - Response Caching: Using
Microsoft.Extensions.AI.Evaluation.Reportingto store LLM responses in a cache. This reduces costs and improves performance by reusing responses for identical request parameters (model, endpoint, prompts, context). - Result Storage & Reporting: Storing evaluation results over time and generating reports based on that historical data.
- Extensibility: Implementing custom storage providers (e.g., using SQLite) for caching and result storage.
- Orchestrating Offline Evaluations: Using unit testing frameworks and
Overview of the Add Chat History sample
mainThis sample demonstrates how to implement and manage chat history within a .NET AI application. It provides a practical implementation of maintaining context across multiple turns in a conversation.
For complete, step-by-step documentation, including implementation details and exercise instructions, refer to the full exercise guide.
Overview of Microsoft.Extensions.AI
mainMicrosoft.Extensions.AI is a set of core .NET libraries (currently in preview) that provides a unified layer of C# abstractions for interacting with AI services. It is designed to work with the .NET ecosystem, including Semantic Kernel, to provide a consistent way to interact with Small Language Models (SLMs), Large Language Models (LLMs), and embeddings.
Key benefits include:
- Unified API: Standardized APIs and conventions for AI service integration.
- Flexibility: Allows library authors to use AI services without being locked into a specific provider.
- Ease of Use: Enables developers to switch between different AI packages using the same underlying abstractions.
- Componentization: Simplifies testing and adding new capabilities to applications.
Overview of Hiking Benefits Summary sample
mainThe Hiking Benefits Summary sample is a .NET 8.0 console application that demonstrates how to use Azure OpenAI with agpt-35-turbomodel. The application reads a local file namedbenefits.mdand sends a request to an Azure OpenAI Service to summarize the text into a few words.Overview of the Customer Support Ticket Summarizer sample
mainThe Customer Support sample is a C# console application designed to demonstrate AI-driven workflows for customer service. It performs three primary tasks:
- Ticket Summarization: Uses an AI model to generate concise summaries of customer support tickets.
- Manual Ingestion: Extracts data from product manual PDF files, chunks the text, generates embeddings, and saves them to a JSON file.
- Semantic Search: Uses embeddings and a vector store to perform semantic searches over product manuals.
Core Components
ManualIngestor.cs: Handles PDF data extraction, text chunking, embedding generation, and JSON storage.ProductManualService.cs: A storage service utilizing anIVectorStoreto manage and query product manual embeddings.TicketSummarizer: An AI service that interfaces with models to summarize support tickets.
Overview of OpenAI Semantic Kernel Samples
mainThese samples demonstrate how to integrate OpenAI models (
gpt-3.5-turboanddall-e-3) into .NET 8.0 console applications using the Semantic Kernel (SK) library. Semantic Kernel is an extensible SDK that allows you to build generative AI solutions and can be used with various model providers including OpenAI, Azure OpenAI, and Hugging Face.Available sample scenarios include:
- Hike Benefits Summary: Summarizing long text.
- Hiker AI: Chatting with an AI for hike recommendations.
- Chatting About my Previous Hikes: Contextual chatting about past experiences.
- Hiker AI Pro: Extending AI models using local functions via Function Tool.
- Hike Images: Generating images using DALL-E 3.
Overview of Microsoft.Extensions.AI Examples
mainThis repository provides reference implementations and samples for the interfaces defined in theMicrosoft.Extensions.AI.AbstractionsandMicrosoft.Extensions.AINuGet packages. It is intended to help developers understand how to implement these abstractions or use them within their own applications.Overview of Using Semantic Kernel in WebApp
mainThis sample project demonstrates how to integrate Semantic Kernel into a Web Application. It serves as a practical guide for infusing .NET applications with AI capabilities using the Semantic Kernel orchestration framework.Overview of Microsoft.Extensions.AI.Evaluation packages
mainThe
Microsoft.Extensions.AI.Evaluationlibraries provide tooling to evaluate the quality and efficacy of LLM responses. The ecosystem is divided into the following functional areas:- Core Abstractions:
Microsoft.Extensions.AI.Evaluationdefines the base types and interfaces. - Quality Evaluation:
Microsoft.Extensions.AI.Evaluation.Qualityprovides evaluators for metrics like Relevance, Truth, Completeness, Fluency, Coherence, Retrieval, Equivalence, and Groundedness. - Safety Evaluation:
Microsoft.Extensions.AI.Evaluation.Safetyuses Azure AI Foundry Evaluation service to check for Protected Material, Groundedness Pro, Ungrounded Attributes, Hate and Unfairness, Self Harm, Violence, Sexual content, Code Vulnerability, and Indirect Attack. - Reporting & Storage:
Microsoft.Extensions.AI.Evaluation.Reportinghandles caching LLM responses and storing results.Microsoft.Extensions.AI.Evaluation.Reporting.Azureprovides an implementation for storing these in Azure Storage containers. - CLI Tooling:
Microsoft.Extensions.AI.Evaluation.Consoleis a dotnet tool for managing evaluation data and generating reports.
- Core Abstractions: