Semantic Kernel: Creating a Custom Webchat Agent Using Open AI GPT-4, and ASP.NET
Note: This post covers Semantic Kernel, which I’ve since moved on from. My new work is built on Microsoft Agent Framework. If you’re starting fresh, I’d recommend the Microsoft Agent Framework: The Complete Series instead, or see the section What Does It Mean For Your Current Semantic Kernel project in this blog.
In the previous blog post, we saw how to use logging to examine planners and help debug your Semantic Kernel solution.
In this blog post, you will see how to expose the customer service agent we’ve been building in a custom webchat experience using ASP.NET Core.
This blog ties together many of the concepts from earlier posts and demos.
All previous posts in the series can be found here:
- First Look, Integrating Semantic Kernel with Open AI (Part 0) – An introduction to Semantic Kernel, who is it for, why use it, key components and creating a simple agent.
- Working with Inline Prompt Functions (Part 1) – Learn about leveraging prompt functions to ensure predictability, refine LLM prompt output, few shot prompting, and handling unexpected output
- Working File System Prompt Functions (Part 2) – See how prompt definitions can be loaded from the file-system and used within Semantic Kernel. You will also learn why you might want to do this.
- Native Functions and Plugins (Part 3) – Learn how to implement a Native Function using Semantic Kernel. You will also learn about the concept of plugins, the architecture of a plugin, prompts, and native functions, how to organise plugins and native functions within your solution
- Function Calling and Planners (Part 4) – Learn about Function Calling and Planners. Specifically, the following is covered: What is Function Calling, How Does Function Calling Work, What is a Planner, How Planners Use Function Calling.
- Using Logging to Examine Planners and Support Debugging (Part 5) – See how to add logging to a Semantic Kernel and OpenAI integration. We also see what logging verbosity is available, examples of logging outputs, the planner component of Semantic Kernel in action
I recommend working through each of the above before proceeding.
~
Technology
In this blog post, the following technology is used to implement the agentic webchat solution:
- NET Core
- C#
- JavaScript
- Open AI GPT-4
- Semantic Kernel
- Semantic Kernel Automatic Function Calling
- Semantic Kernel Plugins
- Semantic Kernel Native Functions
The Semantic Kernel integration uses the Open AI Chat Completions endpoint to process human prompts and generate the relevant response.
Automatic Function calling identifies the relevant plugins and native functions within the solution to execute to process a human prompt:
~
Overview
To recap, the agent that has been developed in previous parts of the series is a customer service agent.
The agent handles basic customer service queries related to bookings. A customer and manager can interact with the agent.
The following business rules are defined:
- A customer can make or cancel a booking.
- A customer can file a complaint.
- A manager can fetch all complaints.
- A customer cannot fetch all complaints
The agent and this behaviour will be accessible from a webchat window within an ASP.NET Core web application.
~
A Recap on Plugins
Two Semantic Kernel Plugins exist. These contain Native Functions which encapsulate discrete functionality.
| Plugin | Native Functions |
| BookingPlugin | 1. GetNextAvailableDate
2. BookAppointment |
| Complaints Plugin | 1. FileComplaint
2. FetchComplaints |
You can read more about Plugins, and Native Functions in earlier parts of this series.
~
Controller
To implement a web chat experience, we need an API or controller endpoint. This end point is used to let the human interact with the Semantic Kernel integration and LLM.
We can see a definition of this controller and point here:
[ApiController]
[Route("api/[controller]")]
public class ChatController : ControllerBase
{
private readonly Kernel _kernel;
private readonly IChatCompletionService _chatCompletionService;
private readonly ChatHistoryService _chatHistoryService;
public ChatController(Kernel kernel, ChatHistoryService chatHistoryService)
{
_kernel = kernel;
_chatCompletionService = _kernel.GetRequiredService<IChatCompletionService>();
_chatHistoryService = chatHistoryService;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] ChatRequest chatRequest)
{
// Use a fixed session ID for simplicity, or generate a unique one per user/session
var sessionId = "default-session";
var history = _chatHistoryService.GetOrCreateHistory(sessionId);
// Add user input
history.AddUserMessage(chatRequest.Message);
var openAIPromptExecutionSettings = new OpenAIPromptExecutionSettings
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
};
// Get the response from the AI
var result = await _chatCompletionService.GetChatMessageContentAsync(history,executionSettings: openAIPromptExecutionSettings,kernel: _kernel);
// Add the message from the agent to the chat history
history.AddAssistantMessage(result.Content ?? string.Empty);
return new JsonResult(new { reply = result.Content });
}
}
This controller handles POST requests for chat interactions. The Open AI completions integration is added using the Semantic Kernel SDK to generate AI LLM responses.
Automatic function calling is setup by the setting the ToolCallBehaviour settings.
An important feature to point out is how chat history is implemented. This is maintained by implementing a chat history service.
~
Chat History Service
The agent has no concept of state or memory. If we were to run the endpoint, the chat history would reset each time the endpoint is hit.
(Remember Dory? – no memory!)
To help manage and maintain the chat history, we create a ChatHistoryService class. You can see a definition of this here:
public class ChatHistoryService
{
private readonly Dictionary<string, ChatHistory> _chatHistories;
public ChatHistoryService()
{
_chatHistories = new Dictionary<string, ChatHistory>();
}
public ChatHistory GetOrCreateHistory(string sessionId) {
if (!_chatHistories.TryGetValue(sessionId, out var history))
{
history = new ChatHistory("You are professional customer service agent that handles bookings." +
"If the person says 'I am a manager', the agent will let them access " +
"fetch all complaints in the ComplaintPlugin.");
_chatHistories[sessionId] = history;
}
return history;
}
}
This is passed in using dependency injection in Program.cs. Key objects such as the Kernel and relevant Plugins are also setup here:
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<Kernel>(sp =>
{
var kernelBuilder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId, apiKey);
kernelBuilder.Plugins.AddFromType<BookingPlugin>();
kernelBuilder.Plugins.AddFromType<ComplaintPlugin>();
return kernelBuilder.Build();
});
builder.Services.AddSingleton<ChatHistoryService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
}
Doing this helps us preserve the chat history between requests. With the controller endpoint defined.
We can turn our attention to the UI and required script to interact with the endpoint.
~
ASP.NET View HTML
The interface for this view is simple. We add some HTML to capture and render the input:
<div class="chat-widget-container"> <div class="chat-container"> <div class="messages" id="messages"></div> <input type="text" id="userInput" placeholder="Type your message here..." /> </div> </div>
Some styling is also applied to position the chat window at the bottom right of the web page.
~
ASP.NET View JavaScript
We need some script to send messages to the endpoint. The script must also manipulate the DOM and inject agent responses.
This script here does that:
<script>
document.getElementById('userInput').addEventListener('keypress', function (event) {
if (event.key === 'Enter') {
const userInput = event.target.value;
if (userInput.trim() === '') return;
const userMessage = document.createElement('div');
userMessage.classList.add('message', 'user');
userMessage.textContent = userInput;
document.getElementById('messages').appendChild(userMessage);
fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
async: true,
body: JSON.stringify({ message: userInput })
})
.then(response => response.json())
.then(data => {
const assistantMessage = document.createElement('div');
assistantMessage.classList.add('message', 'assistant');
assistantMessage.textContent = data.reply;
document.getElementById('messages').appendChild(assistantMessage);
});
event.target.value = '';
}
});
</script>
With the Semantic Kernel implementation complete, HTML and JS written, the integration can now be tested.
~
Testing
We can test the webpage and chat experience by running the web application in Visual Studio and supplying a different prompts.
When asking the agent what it can, Semantic Kernel, automatic function calling, and the LLM are able to discover available plugins and native functions:
We can ask the agent to make a booking:
The agent lets us know what date is available from currently available dates. We can tell the agent we would like to proceed with the booking:
The agent updates the internal bookings list and lets us know the booking has been made successfully.
~
Demo
You can also see this agent in action in the following video demo on YouTube here:
(apologies for the interference, I left my cell phone next to the mic.)
~
Further Thoughts
This agent is relatively straightforward Aunt has several constraints.
For example, this agent Can only work with a single user and session. You could extend the cause to inject a unique session ID for a particular browser session.
The agent chat history does not persist.
You could store this in a database thereby allowing persistent tracking of conversation context across sessions. This can enhance the continuity and relevance of interactions.
Storing chat history enables detailed analysis of the human and machine interaction. This can help you improve AI responses, system prompt design or training models.
~
Summary
In this blog post, you have seen how to create a custom webchat experience using Semantic Kernel, Open AI GPT-4, and ASP.NET Core.
You saw how to create an ASP.NET Core endpoint that can accept human prompts. You learned how to integrate Semantic Kernel and the Open AI Completions endpoint. We learned how to preserve the chat history between requests.
We saw how these components can be surfaced in an ASP.NET web page that lets the user interact with the AI agent.
~
Further Reading and Resources
You can learn more about Semantic Kernel, Planners, and Function Calling here:
- Semantic Kernel – Semantic Kernel documentation
- Plugins and Native Functions – Provide native code to your agents
- Function Calling – Function calling – OpenAI API
- Completions – Completions – OpenAI API
Enjoy what you’ve read, have questions about this content, or would like to see another topic? Drop me a note below.
You can schedule a call using my Calendly link to discuss consulting and development services.