Overview

5 Building a ChatGPT clone with Aspire, Ollama, and Semantic Kernel

The chapter introduces how large language models can be incorporated into distributed .NET applications, showing that modern AI features such as chat, retrieval-augmented generation, and agent-like behavior are accessible beyond cloud-only services. It frames the goal as building a domain-specific ChatGPT-style experience for an enterprise app, then positions Semantic Kernel as the bridge between application code and model capabilities. The discussion emphasizes that this approach lets developers create intelligent, customized user experiences while retaining control over behavior and deployment.

To host the model itself, the chapter uses Ollama inside Aspire as a lightweight, portable way to run open source LLMs locally or in controlled environments. It explains the advantages of self-hosting: better privacy, lower latency, no recurring API fees, and freedom from vendor lock-in. In the Aspire host, Ollama is configured as a persistent container with stored data, an optional web UI, and a small model such as phi3.5; the application is wired so dependent services wait until the model is available, which makes the overall system reliable but resource-intensive.

After the model layer is in place, the chapter adds Semantic Kernel to power chat completion in the web frontend. It shows how to resolve the Ollama endpoint through Aspire service discovery, register the kernel and chat completion service, and maintain per-connection chat history with a singleton service. A SignalR hub then streams model output back to the browser token by token, while a Blazor page and JavaScript client provide the interface for sending prompts, stopping generation, clearing output, and copying responses. The result is a functional, locally hosted ChatGPT clone with a custom UI, demonstrating how Aspire, Ollama, and Semantic Kernel work together in a distributed AI application.

Hosting Ollama in Aspire
figure
Ollama user interface
figure
Custom chat interface
figure

Summary

  • Popular AI applications used for conversations, such as ChatGPT, rely on LLM technology.
  • Ollama allows anyone to host LLMs anywhere, free of charge. It can run in Docker containers and can host a wide range of LLMs, including Meta’s Llama.
  • Aspire has a community toolkit for hosting Ollama. The Ollama community toolkit for Aspire comes with its own frontend, which is similar to ChatGPT and serves the same purpose.
  • Semantic Kernel is a Microsoft framework for building AI agents that allows any .NET code to interact with LLMs. It can stream the LLM response letter by letter.
  • The official name for the LLM functionality used in chats is chat completion.
  • You can use SignalR to build a custom chat interface that supports answer streaming.

FAQ

What is the main goal of this chapter?The chapter shows how to build a locally hosted ChatGPT-like application in Aspire using Ollama for the LLM, Semantic Kernel for orchestration, and a custom UI for chatting with the model.
Why does the chapter use Ollama instead of a cloud AI API?Ollama lets you run open-source language models locally or in controlled environments, giving you more privacy, lower latency, no recurring API costs, and full control over the model and data.
How do you host an LLM inside an Aspire application?You add an Ollama container to the Aspire host, configure it with persistent storage, attach a model such as phi3.5, and then reference that model from the web application.
Why is the model configured with a persistent lifetime and data volume?A persistent lifetime keeps the Ollama container from restarting unnecessarily, and a data volume prevents the model from having to download again every time the app restarts.
What role does Semantic Kernel play in the chatbot?Semantic Kernel acts as the .NET client and orchestration layer that connects the application to the LLM and enables chat-completion functionality in a clean, extensible way.
Why does the web app need service discovery for Ollama?Because the Ollama endpoint is resolved at runtime inside Aspire, the app needs service discovery to locate the container’s actual address instead of hardcoding a fixed URL.
How is chat history handled in the backend?A singleton chat-history service stores a separate ChatHistory object for each SignalR connection ID, so each user session keeps its own conversation context.
Why does the SignalR hub stream responses instead of returning the whole answer at once?Streaming makes the chatbot feel faster and more natural, similar to commercial chat systems, because the response appears incrementally as it is generated.
How is the custom chat UI loaded in the Blazor page?The chat page uses JavaScript interop to dynamically import the chat.js module during the first render, then calls its init function to wire up the SignalR client.
What user interface features does the chat page include?The page includes a prompt input, Send, Stop, Clear, and Copy buttons, a connection status badge, and an output area that displays the streamed response and token count.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free