English
Summary
Conclusions
Large language models generate text token by token within a context window, do not remember previous requests, and can be confidently wrong. The Microsoft.Extensions.AI libraries provide a common IChatClient interface for local (Ollama) and cloud models: an application sends a list of messages with roles, receives the response whole or as a stream, and through GetResponseAsync<T> gets a typed result that it checks in code. Function calling lets the model use the application's data, and ChatClientBuilder adds logging, caching, limits, and other middleware layers to the client and registers it in DI, so the provider is changed in only one place. Embeddings and cosine similarity underlie semantic search and RAG, which answers from your own documents with references to the sources. API keys are stored outside the code, personal data is not sent to the model without need, costs are limited, and code that uses a model is tested with a fake client.
Self-check questions
- What are a large language model and a prompt?
- What are a token and a context window? Why does a long conversation become more expensive?
- How does temperature affect the response? Which values should you choose for data extraction?
- What are model hallucinations, and how do you reduce them?
- How do cloud and local models differ? What is rate limiting?
- What is the purpose of the
Microsoft.Extensions.AI.Abstractions,Microsoft.Extensions.AI, andOllamaSharppackages? - What roles do
ChatMessagemessages have? Why is a system instruction needed? - How do you store the conversation history, and why must it be trimmed?
- How does
GetStreamingResponseAsyncdiffer fromGetResponseAsync? How do you cancel generation? - How does
GetResponseAsync<T>work? Why must the result be checked? - How do you describe a C# method as a tool for the model? Who actually executes the method?
- What security rules apply to functions that the model calls?
- How does
ChatClientBuilderdetermine the order of middleware clients? How do you create your own? - How do you change the model provider without changing the rest of the code? Where do you store the API key?
- What are an embedding and cosine similarity?
- What stages does RAG consist of? Why does the response include references to the sources?
- How do you test code that uses
IChatClientwithout a real model?
Useful links
- Microsoft.Extensions.AI: https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai
- The
IChatClientinterface: https://learn.microsoft.com/dotnet/ai/ichatclient - The
IEmbeddingGeneratorinterface: https://learn.microsoft.com/dotnet/ai/iembeddinggenerator - Tokens: https://learn.microsoft.com/dotnet/ai/conceptual/understanding-tokens
- Embeddings: https://learn.microsoft.com/dotnet/ai/conceptual/embeddings
- RAG: https://learn.microsoft.com/dotnet/ai/conceptual/rag
- Vector stores: https://learn.microsoft.com/dotnet/ai/vector-stores/overview
- A local model in .NET: https://learn.microsoft.com/dotnet/ai/quickstarts/chat-local-model
- Ollama: https://docs.ollama.com, models: https://ollama.com/library
- User secrets: https://learn.microsoft.com/aspnet/core/security/app-secrets
- Microsoft Agent Framework: https://learn.microsoft.com/agent-framework/overview/
- Model Context Protocol: https://modelcontextprotocol.io