I'm always excited to take on new projects and collaborate with innovative minds.

Social Links

Build a Local AI Code Assistant with Blazor and Ollama — No Cloud, No API Costs

A step-by-step guide to building a fully local, cloud-free AI code assistant with Blazor .NET 8 and Ollama — featuring an interactive chat UI, real-time connection status, and one-click code copying with zero API costs.

AI code assistants have become central to modern development workflows. They translate natural language into functioning code, assist in debugging complex logic, and automate routine boilerplate. However, relying on cloud-based AI services introduces potential data privacy risks, as code snippets sent to external endpoints may be retained or used for model training.

Running open-source models locally provides a secure alternative. By combining Ollama as a local LLM runtime with a Blazor frontend, you can build a fully private, self-hosted AI coding assistant that runs entirely on your own hardware—requiring no subscriptions, external API keys, or cloud data transfers.

This article details how to build and run a local AI code assistant using .NET 8 and Ollama.

Architecture Overview

The solution uses a Blazor architecture configured with .NET 8 Interactive Server rendering. It connects directly to a locally running Ollama instance via HTTP to process prompts and return code completions.

┌─────────────────────────────────────────────────────────────┐
│                       Blazor UI                             │
│       (Interactive Server / Bootstrap 5 / Font Awesome)      │
└──────────────────────────────┬──────────────────────────────┘
                               │
                               │  HTTP Requests
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                      OllamaService                          │
│         (Prompt Engineering / Health Checks / DTOs)         │
└──────────────────────────────┬──────────────────────────────┘
                               │
                               │  REST API (localhost:11434)
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                   Ollama Engine (Local)                     │
│                (llama3.1:8b-instruct or similar)            │
└─────────────────────────────────────────────────────────────┘

Tech Stack

Layer

Technology

Framework

Blazor (.NET 8, Interactive Server rendering)

Language

C#

AI Backend

Ollama (Local LLM Runtime)

Model

Llama 3.1 8B (Configurable)

UI Framework

Bootstrap 5 + Font Awesome

Configuration

appsettings.json

Key Capabilities

  • Local Code Generation: Supports multi-language code generation (Python, C#, JavaScript, Java, Go, Rust, SQL, and CSS) running fully offline.

  • Conversational Interface: A responsive chat UI designed for developer interaction.

  • Clipboard Integration: Single-click copying for generated code blocks.

  • Service Monitoring: Real-time health checking to verify the status of the local Ollama daemon before sending queries.

  • Configurable Model Settings: Endpoint URLs and target models are fully managed via standard .NET configuration files.

Application Architecture

The solution follows a clean three-layer separation of concerns:

1. Service Layer (Services/OllamaService.cs)

The OllamaService encapsulates interaction with Ollama's REST API. It handles three primary operations:

  • GenerateCodeAsync(query): Formats prompts with custom system instructions and POSTs payloads to /api/generate. Default execution parameters are tuned for deterministic code generation:

    • temperature: 0.2 (reduces randomness)

    • num_predict: 512 (sets maximum token generation per request)

    • repetition_penalty: 1.13 (prevents repetitive loop outputs)

  • IsOllamaRunningAsync(): Performs health checks against the /api/tags endpoint to confirm daemon availability.

  • GetAvailableModelsAsync(): Queries installed local models.

The registered HttpClient includes a 10-minute timeout to accommodate intensive code generation tasks on hardware with varying compute constraints.

2. Model Layer (Models/)

Data Transfer Objects (DTOs) map JSON payloads between the application and the Ollama REST API:

  • ChatMessage: Represents conversation state in the UI.

  • OllamaResponse / OllamaModelsResponse: Deserializes Ollama's execution outputs and model listings.

3. UI Layer (Components/Pages/Home.razor)

A single-page component managing application interaction:

  • Status Banners: Visual indicators confirming active connectivity to Ollama, complete with manual reconnect controls.

  • Starter Prompts: One-click template queries covering common generation tasks across multiple languages.

  • Async Processing States: Visual loading spinners during model inference.

  • History Management: Controls for clearing active session history and copying responses to clipboard with fallback handling for legacy browsers.

3 min read
Aug 16, 2025
By Dheer Gupta
Share

Leave a comment

Your email address will not be published. Required fields are marked *