DEV Community

Cover image for How to Build Your First AI Agent with Semantic Kernel & DeepSeek R1 Locally with Ollama
CodeStreet
CodeStreet

Posted on

How to Build Your First AI Agent with Semantic Kernel & DeepSeek R1 Locally with Ollama

In this post, we will create our first AI Agent using the Microsoft Agentic Framework of Semantic Kernel and DeepSeek R1, running locally on Ollama.

We’ll walk step by step through setting up a Semantic Kernel agent that leverages DeepSeek R1 via Ollama, showing you how to install the tools, configure the model locally, and connect everything together. By the end, you’ll have a working AI YouTube Title Generator agent running entirely on your machine locally—no cloud subscription required.

What You Will Learn

  • How to Get Started with DeepSeek R1
  • How to Use Ollama for running local models
  • How to install and start running the DeepSeek R1 model
  • How to Use Semantic Kernel in C#

1. Prerequisites

  • Visual Studio 2022+ (with .NET 9 SDK installed) .NET 9 is still in preview, so ensure that you have the preview SDK installed.
  • Ollama (for managing and running local models)
  • DeepSeek1.5b Model

2. Installing Ollama

Ollama is a platform or tool (specific details may vary depending on the context) that allows users to interact with large language models (LLMs) locally. It simplifies the process of deploying and running LLMs like LLaMA, Phi, DeepSeek R1, or other open-source models.

To install Ollama, visit its official website https://ollama.com/download and install it on your machine.

3. Installing DeepSeek R1

DeepSeek's first-generation reasoning models have comparable performance to OpenAI-o1, including six dense models distilled from DeepSeek-R1 based on Llama and Qwen.

On the Ollama website click on Models and click on deepseek-r1 and choose 1.5b parameter option

How To Use DeepSeek With .NET 9 - deepseek-r1:1.5b

Open Command Prompt and run the command below.

ollama run deepseek-r1:1.5b

It will download the model and start running automatically.

Once done, verify that the model is available

ollama list

That’s it! We’re ready to integrate DeepSeek locally.

4. Creating .NET Console Application

  1. Launch Visual Studio
  2. Make sure .NET 9 is installed.
  3. Create a New Project
  4. File → New → Project…
  5. Pick Console App with .NET 9.
  6. Name Your Project
  7. e.g., DeepSeekDemoApp or any name you prefer.
  8. Target Framework Check
  9. Right-click on your project → Properties.
  10. Set Target Framework to .NET 9.

5. Integrating DeepSeek R1 with Semantic Kernel

While you could call DeepSeek via direct HTTP requests to Ollama, using Semantic Kernel offers a powerful abstraction for prompt engineering, orchestration, and more.

  1. Add Necessary NuGet Packages
<ItemGroup>
  <PackageReference Include="Microsoft.SemanticKernel" Version="1.61.0" />
  <PackageReference Include="Microsoft.SemanticKernel.Agents.Core" Version="1.61.0" />
  <PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.61.0-alpha" />
</ItemGroup>

Enter fullscreen mode Exit fullscreen mode

6. Complete code

Program.cs:

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;

var builder = Kernel.CreateBuilder().AddOllamaChatCompletion("deepseek-r1:1.5b",
    new HttpClient
    {
        BaseAddress = new Uri("http://localhost:11434"),

    });

Kernel kernel = builder.Build();

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Welcome to TheCodeStreet - AI YouTube Title Generator!");
Console.WriteLine("------------------------------------------------------");
Console.ResetColor();
Console.WriteLine("Provide the keyword to generate title:");
string keyword = Console.ReadLine()?? "";


string instructions = """
You are a professional YouTube content strategist and SEO expert.

🎯 Keyword or Topic: {{keyword}}

Your job is to generate 5 click-worthy and SEO-friendly YouTube video titles based on a given topic or keyword.

📌 Guidelines for Generating Titles:

-- Keep them under 70 characters if possible.
-- Include emotional triggers, power words, or numbers (e.g., “Top 5”, “You Won’t Believe…”).
-- Use strong action verbs (e.g., “Boost”, “Master”, “Avoid”, “Unlock”).
-- Make titles engaging and curiosity-driven (clickbait style but still relevant).
-- Include the keyword or topic for SEO.
-- Avoid duplicate or overly similar titles.

💡 Return only the titles in a numbered list format like:
1. Title one...
2. Title two...
3. ...
""";

 instructions = instructions
    .Replace("{{keyword}}", keyword);

ChatCompletionAgent agent =
    new()
    {
        Name = "TitleGeneratorAgent",
        Instructions = instructions,
        Kernel = kernel,
    };

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Generating titles...");
Console.WriteLine("------------------------------------------------------");
await foreach (AgentResponseItem<StreamingChatMessageContent> response
    in agent.InvokeStreamingAsync())
{
    Console.Write(response.Message.Content.Replace("**",""));
}
Console.ResetColor();
Console.ReadKey();

Enter fullscreen mode Exit fullscreen mode

7. Running & Testing

  1. Ensure Ollama is Running

-Some systems auto-run Ollama; otherwise, start it

ollama run

  1. Run Your .NET App

-Hit F5 (or Ctrl+F5) in Visual Studio.

-Watch for console output—

YouTube Title Generator Agent

Support me!

If you found this guide helpful, make sure to check out the accompanying YouTube video tutorial where I walk you through the process visually. Don’t forget to subscribe to my channel for more amazing tutorials!

I would appreciate it if you could buy me a coffee.
Buy me a coffee

Feel free to leave your questions, comments, or suggestions below. Happy coding!

Top comments (0)