In previous lessons, we introduced Microsoft.Extensions.AI and IChatClient and started setting up our AiLearningApi project using .NET 10.
Now let's understand what actually happens when our Web API sends a request to an AI model.
In this lesson, we will understand the relationship between an AI provider, an AI model, a chat completion API, and the messages we send to it.
These terms are often confused, so let's separate them.
An AI provider is the company or platform that gives you access to AI models through APIs.
Examples:
OpenAI
Groq
Microsoft Azure AI
Think of the provider as the service that hosts or makes models available.
An AI model is the actual trained model that processes your input and generates a response.
Examples include GPT models, Gemini models, and Llama models.
Different models can have different:
Capabilities
Context window sizes
Response speeds
Reasoning abilities
Costs and usage limits
Important: Groq is a provider, not an AI model. You select a model that Groq makes available.
A chat completion API is an endpoint that accepts conversation messages and model options, sends them to a model, and returns a generated response.
For example, our application sends:
The provider processes the request and returns an assistant response.
The important distinction is that IChatClient is our .NET abstraction, while the provider and model perform the actual AI processing.
A chat completion is the response generated by an AI model based on the messages supplied to it.
For example:
Input
User: What is an LLM?
Output
Assistant: An LLM (Large Language Model) is an AI model trained on large amounts of text data to understand and generate human-like language.
The model doesn't automatically know your entire previous conversation. We must provide the relevant conversation messages in each request if we want it to consider that history.
The model doesn't automatically know your entire previous conversation. We must provide the relevant conversation messages in each request if we want it to consider that history.
This is why conversation management will become important in a later lesson.
Remember Lesson 4, where we learned about message roles?
A typical chat request contains messages with roles such as:
Role Purpose
System Sets the model's overall behavior or instructions
User Contains the user's question or input
Assistant Contains previous AI responses
Tool Contains results returned by an executed tool
For example:
The system message guides the response, and the user message contains the actual question.
When calling an AI model, we can provide additional settings that influence how it generates responses.
In .NET, ChatOptions allows us to specify these settings.
Some important options:
Option Purpose
Temperature Controls response variability
MaxOutputTokens Limits the generated response length
TopP Controls nucleus sampling
StopSequences Specifies sequences at which generation should stop
Tools Provides tools the model may be allowed to call
Remember Lesson 3? We already studied Temperature and Top-P. Now we're seeing where those concepts fit into actual application code.
For example:
These options are passed when requesting a response:
Not every provider or model supports every option. Always check the provider's supported capabilities.
In our earlier implementation, we sent only a plain string. Now let's explicitly send messages with roles and configure the model.
Update ChatService.cs:
using Microsoft.Extensions.AI;
namespace AiLearningApi.Services;
public class ChatService
{
private readonly IChatClient _chatClient;
public ChatService(IChatClient chatClient)
{
_chatClient = chatClient;
}
public async Task<string> GetResponseAsync(
string message,
CancellationToken cancellationToken)
{
var messages = new List<ChatMessage>
{
new ChatMessage(
ChatRole.System,
"You are a helpful AI assistant. " +
"Explain technical concepts in simple language."),
new ChatMessage(
ChatRole.User,
message)
};
var options = new ChatOptions
{
Temperature = 0.7f,
MaxOutputTokens = 500
};
var response = await _chatClient.GetResponseAsync(
messages,
options,
cancellationToken);
return response.Text ?? string.Empty;
}
}
Previously:
We sent a simple string.
We relied on the default model behavior.
We didn't explicitly configure generation options.
Now:
We send structured chat messages.
We define system instructions.
We configure response generation.
We pass cancellation through to the AI request.
Notice: We haven't changed our controller. This is one benefit of separating the AI logic into ChatService.
Suppose today we use:
Current configuration
Provider: Groq
Model: A model supported by Groq
Application interface: IChatClient
Tomorrow, the organization wants to use Azure OpenAI.
We can replace the provider-specific client registration and configuration while retaining much of our ChatService implementation.
However, we should still test:
Whether the replacement model supports our required options.
Whether tool calling and structured outputs behave as expected.
Whether response quality and latency meet requirements.
This is an important principle for enterprise AI architecture: keep provider-specific integration at the infrastructure boundary and keep business logic independent wherever practical.
By now, you should understand these relationships:
Concept Responsibility
Provider Makes AI models available
Model Processes input and generates output
Chat completion API Provides access to conversational model capabilities
Message Represents one part of the conversation
Role Identifies who sent the message
ChatOptions Configures response generation
IChatClient Common .NET abstraction for chat interaction