AI Development
in .NET
Mert Metin
#dotnetkonf25
Microsoft.Extensions.AI
Slide 2
Slide 2 text
Interests
Microsoft / .NET
software architecture,
secure software, clean code
infrastructure, system design
Senior Software Engineer
Blogger, speaker
MERT METİN
QR to reach me
Slide 3
Slide 3 text
Agenda
Basic Concepts of GenAI
Exploring the .NET Package - Microsoft.Extensions.AI
Slide 4
Slide 4 text
Brief Recap
of the
GenAI
Concepts
Generative AI, LLM, Prompt, Roles, RAG ,Agent, Token, Embedding, Tools
Slide 5
Slide 5 text
Generative AI
Subset of AI
Creates new content such as
text, code, audio, image based on user
prompts and learned data.
Slide 6
Slide 6 text
Large Language Model
LLMs are optimized for complex tasks,
trained on huge datasets, and capable of
understanding and interpreting human
language.
Slide 7
Slide 7 text
Prompt
An input that is used to communicate with the
model to generate desired output.
Prompt Engineering is the practice of designing
effective prompts.
Prompts must be;
clear, specific,
goal oriented,
have contextual information
Slide 8
Slide 8 text
System
Specify “How will the model behave?”
Roles
User
Prompt and query by user.
Asistant
Model responds using user’s prompt.
It is used to ensure focus on a specific task or behavior pattern.
Slide 9
Slide 9 text
Agents
They are capable of reasoning, planning,
and interacting with their environment,
using generative AI to perform tasks
autonomously.
Slide 10
Slide 10 text
Tool
Functions provided to LLMs
must have clear objectives.
Tools enable agents to
perform actions such as
web search, retrieving
information from external
sources and API calls.
Slide 11
Slide 11 text
Token
Meaningful unit processed by LLM.
Token is not only a word.
tokenized by gpt-4o
Slide 12
Slide 12 text
Embedding - Vector Database
Embedding is an operation that converts
text, images into numerical data (vectors)
that machines can understand.
A vector database stores numerical data as
vectors, enabling efficient similarity search
Slide 13
Slide 13 text
Retrieval
Augmented
Generation
(RAG)
Enhances LLMs with external
data sources (databases,
documents, APIs).
Improves response relevance and
accuracy.
Slide 14
Slide 14 text
Exploring
the
.NET
Package
Microsoft.Extensions.AI
Provides utilities for working with generative AI
components.
Slide 15
Slide 15 text
Why
Support for switching between different LLM providers
Manageable, Simple, and Rapid Development
Provide implementations for dependency injection, caching,
telemetry and other cross-cutting concerns.
Implement chat and embedding features using any LLM
provider.
Slide 16
Slide 16 text
IChatClient
Connecting to LLM providers such as Open AI, Ollama, Azure Open AI
Slide 17
Slide 17 text
Microsoft.Extensions.AI.OpenAI
LLM provider for OpenAI
Working With Model Providers
Slide 18
Slide 18 text
Microsoft.Extensions.AI.Ollama
LLM provider for Ollama
Useful for working on local machine
Working With Model Providers
Note: This package is deprecated and the OllamaSharp package
is recommended.
Slide 19
Slide 19 text
OllamaSharp
The eaisest way to use the Ollama in .NET
Working With Model Providers
Slide 20
Slide 20 text
GetResponseAsync
Sends a user chat text message and returns the response messages.
Return type is ChatResponse which represents the response to a chat request.
Slide 21
Slide 21 text
GetStreamingResponseAsync
Sends chat messages and streams the response.
Return type is IAsyncEnumerable which provides a stream of
updated chat response.
Slide 22
Slide 22 text
Case - Sentiment Analysis
Customer’s order reviews of the restaurant
Slide 23
Slide 23 text
Output - GetResponseAsync
Slide 24
Slide 24 text
Output - GetStreamingResponseAsync
Slide 25
Slide 25 text
Conversation
Application needs to keep
conversation context.
So, send conversation history
to the model using roles.
Assistant role is set by
AddMessages.
Case - git commit generator
Slide 26
Slide 26 text
History and Roles
ChatMessage list often represents the history of whole chat messages that are part
of the conversation.
Chat result
Slide 27
Slide 27 text
Tool Calling
UseFunctionInvocation - set method name as
delegate type
Slide 28
Slide 28 text
Tool
Slide 29
Slide 29 text
Output
Usage
Informations
Response
Slide 30
Slide 30 text
Embeddings
GenerateEmbeddingAsync has more detailed information about embeddings and
Vector collection is also included.
GenerateEmbeddingVectorAsync returns the vector collection.
Slide 31
Slide 31 text
Case - RelatedNews
Slide 32
Slide 32 text
Output
Slide 33
Slide 33 text
Dependency Injection
Inject as ChatClient, EmbeddingClient
OpenAI
Ollama
Slide 34
Slide 34 text
Dependency Injection
Call from API Controller
Slide 35
Slide 35 text
Dependency Injection
Using Keyed Services
Slide 36
Slide 36 text
Caching
Caching Alternatives: Memory or Redis
Slide 37
Slide 37 text
Caching
UseDistributedCache
pipeline for caching in
IChatClient