Integrating a language model into a .NET application used to mean tying yourself to a single provider's library. Microsoft.Extensions.AI introduces common abstractions: you write the code once and switch provider by changing configuration.
IChatClient
IChatClient client = CreateClient(); // OpenAI, Azure, a local model...
ChatResponse response = await client.GetResponseAsync(
"Summarise in three points the benefits of .NET 10 for a company.");
Console.WriteLine(response.Text);
Each provider offers a package that implements the interface; models running locally, for example with Ollama, are used the same way. For long answers there is a streaming version, which returns the text as it is generated.
Composable middleware
As in ASP.NET Core, cross-cutting features are added as layers around the client:
IChatClient client = new ChatClientBuilder(baseClient)
.UseFunctionInvocation() // the model can call C# functions
.UseOpenTelemetry() // traces and metrics for calls
.Build();
Calling application functions
With function invocation the model can ask to run methods exposed by the application, for example "look up order 4521", and use the result in its answer. Every exposed function must be treated like a public endpoint: argument validation, permissions and limits.
Be careful with data
- Do not send personal or confidential data to an external service without a legal basis and a proper contract.
- Consider local models when data must not leave the company.
- Treat generated text as untrusted input: never execute it as code or insert it into a page without escaping.
Comments (0)
No comments yet.