Sunday, October 26, 2025
  • About
  • Advertise
  • Privacy & Policy
  • Contact
Unlock Your Potential With AI | AI Mind Center
  • AI News & Trends
    • All
    • AI Model Releases
    Gemini2.5 Pro (I/O edition) First Look: How to Get Access

    Gemini2.5 Pro (I/O edition) First Look: How to Get Access

    May the Fourth be with you: Launching AI MIND CENTER

    May the Fourth be with you: Launching AI MIND CENTER

  • AI Tools for Productivity
    • All
    • AI Personal Assistants
    • AI Productivity Apps
    • AI Time Management
    • AI Workflow Automation
    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Running IA Locally with Ollama with .NET

    Running IA Locally with Ollama with .NET

    ⚡ Best AI Writing Tools 2025: Transform Your Content Creation Game

    ⚡ Best AI Writing Tools 2025: Transform Your Content Creation Game

    AI Workflow Automation: Build Advanced Solutions Without Code (Even Developers Are Impressed)

    AI Workflow Automation: Build Advanced Solutions Without Code (Even Developers Are Impressed)

    Bubble No-Code: Why IT Experts Struggle (And How You Can Profit)

    Bubble No-Code: Why IT Experts Struggle (And How You Can Profit)

    7 AI Game Development Tools that Are Changing the Industry

    7 AI Game Development Tools that Are Changing the Industry

  • AI for Developers
    • All
    • AI APIs & Libraries
    • AI Assisted Coding
    • AI Implementation Projects
    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Running IA Locally with Ollama with .NET

    Running IA Locally with Ollama with .NET

    How to Use GitHub Copilot X: From Beginner to Advanced (Complete 2025 Guide)

    Getting Started with OpenAI’s API: A Practical Guide for Developers (2025 Edition)

    Getting Started with OpenAI’s API: A Practical Guide for Developers (2025 Edition)

    Top 15 AI Tools Every Developer Should Be Using in 2025

    Top 15 AI Tools Every Developer Should Be Using in 2025

  • AI in Game Development
    • All
    • AI for NPCs & Game Characters
    • AI Game Design Tools
    • AI Game Optimization
    • AI Procedural Generation

    The Ultimate Guide to AI-Generated Assets in Unreal Engine 5.3

    7 AI Game Development Tools that Are Changing the Industry

    7 AI Game Development Tools that Are Changing the Industry

  • AI Tools Reviews
No Result
View All Result
  • AI News & Trends
    • All
    • AI Model Releases
    Gemini2.5 Pro (I/O edition) First Look: How to Get Access

    Gemini2.5 Pro (I/O edition) First Look: How to Get Access

    May the Fourth be with you: Launching AI MIND CENTER

    May the Fourth be with you: Launching AI MIND CENTER

  • AI Tools for Productivity
    • All
    • AI Personal Assistants
    • AI Productivity Apps
    • AI Time Management
    • AI Workflow Automation
    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Running IA Locally with Ollama with .NET

    Running IA Locally with Ollama with .NET

    ⚡ Best AI Writing Tools 2025: Transform Your Content Creation Game

    ⚡ Best AI Writing Tools 2025: Transform Your Content Creation Game

    AI Workflow Automation: Build Advanced Solutions Without Code (Even Developers Are Impressed)

    AI Workflow Automation: Build Advanced Solutions Without Code (Even Developers Are Impressed)

    Bubble No-Code: Why IT Experts Struggle (And How You Can Profit)

    Bubble No-Code: Why IT Experts Struggle (And How You Can Profit)

    7 AI Game Development Tools that Are Changing the Industry

    7 AI Game Development Tools that Are Changing the Industry

  • AI for Developers
    • All
    • AI APIs & Libraries
    • AI Assisted Coding
    • AI Implementation Projects
    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Step-by-Step: Setting Up Microsoft.Extensions.AI

    Running IA Locally with Ollama with .NET

    Running IA Locally with Ollama with .NET

    How to Use GitHub Copilot X: From Beginner to Advanced (Complete 2025 Guide)

    Getting Started with OpenAI’s API: A Practical Guide for Developers (2025 Edition)

    Getting Started with OpenAI’s API: A Practical Guide for Developers (2025 Edition)

    Top 15 AI Tools Every Developer Should Be Using in 2025

    Top 15 AI Tools Every Developer Should Be Using in 2025

  • AI in Game Development
    • All
    • AI for NPCs & Game Characters
    • AI Game Design Tools
    • AI Game Optimization
    • AI Procedural Generation

    The Ultimate Guide to AI-Generated Assets in Unreal Engine 5.3

    7 AI Game Development Tools that Are Changing the Industry

    7 AI Game Development Tools that Are Changing the Industry

  • AI Tools Reviews
No Result
View All Result
Unlock Your Potential With AI | AI Mind Center
No Result
View All Result
Home AI for Developers

Step-by-Step: Setting Up Microsoft.Extensions.AI

Felipe Machado by Felipe Machado
September 25, 2025
in AI for Developers, AI Productivity Apps, AI Workflow Automation
10
0
Step-by-Step: Setting Up Microsoft.Extensions.AI
18
SHARES
150
VIEWS
Share on FacebookShare on XShare on Linkedin

Introduction

Imagine effortlessly switching between AI models like OpenAI or a local Ollama instance to generate blog titles or analyze data, all within a single .NET app. That’s what I’ve accomplished at AI Mind Center, using Microsoft.Extensions.AI to streamline AI workflows with flexibility and ease. Whether you’re a developer building apps, a tester ensuring quality, or a business analyst optimizing processes, this library simplifies AI integration for tasks like crafting SEO-friendly titles or automating reports. In this guide, I’ll walk you through setting up a .NET minimal API with Microsoft.Extensions.AI to interact with AI, using my blog as a practical example. Perfect for tech enthusiasts, this post is your starting point for flexible AI solutions.

Why Microsoft.Extensions.AI?

Microsoft.Extensions.AI is a .NET library that abstracts AI services, acting like a universal adapter for providers like OpenAI, Azure, or Ollama. It’s ideal for tasks like generating blog content or processing data, offering a consistent interface regardless of the AI backend. For AI Mind Center, I use it to create SEO-optimized blog titles. Key benefits include:

  • Flexibility: Switch between providers without code changes, ideal for experimenting.
  • Caching: Store responses to reduce latency and API costs.
  • Simplicity: Integrates with .NET’s dependency injection, familiar to developers.
  • Scalability: Supports local and cloud AI for small or large projects.

Think of it as a power strip that lets you plug in any AI model, streamlining development for any tech project.

Prerequisites

To get started with Microsoft.Extensions.AI and .NET, you’ll need:

  • Hardware: A machine with at least 8GB RAM and a decent CPU or GPU. I use an NVIDIA RTX-4060 with 8GB VRAM, which handles models like llama3.2-vision:11b smoothly. Most modern laptops or desktops (e.g., 16GB RAM) work well.
  • Operating System: Windows, Linux, or macOS. I tested this on Windows 11, but it’s cross-platform.
  • .NET Environment: .NET 8 SDK, installed via Visual Studio or the .NET CLI.
  • Ollama: Download from ollama.com. Installation is simple—download the installer for your OS and run it, no complex configuration needed.
  • OpenAI API Key: Required for OpenAI integration (store securely, e.g., in environment variables).
  • NuGet Packages: Installed via the CLI or Visual Studio (detailed below).
  • Optional Database: For advanced integrations, a database like SQL Server or PostgreSQL can store data (e.g., blog metadata), but this guide starts with a basic API.

Verify Ollama installation by opening a terminal and typing:

ollama

This shows Ollama’s command-line help. Check the API endpoint at http://localhost:11434 in your browser; it should display “Ollama is running”.

Step-by-Step: Setting Up Microsoft.Extensions.AI

Here’s how I set up a .NET minimal API with Microsoft.Extensions.AI for AI Mind Center, keeping it simple for your first AI experience:

  1. Install Ollama
    Download the installer from ollama.com and run it. The process is straightforward—follow the prompts, and Ollama sets up in minutes. Verify with:ollamaCheck http://localhost:11434 in your browser to see “Ollama is running”.
  2. Choose and Manage Models
    I chose the llama3.2-vision:11b model for its text and vision capabilities, perfect for generating blog titles. Explore other models at ollama.com/library. Manage models with:
    • List Models: View installed models:ollama list
    • Download Model: Pull llama3.2-vision:11b:ollama pull llama3.2-vision:11b
    • Run Model: Start the model:ollama run llama3.2-vision

Create a .NET Project
In a terminal, create a .NET 8 console app named MsExtensionsAi:

dotnet new console -o MsExtensionsAi

This creates a directory MsExtensionsAi with a basic console app. Add required NuGet packages:

dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.Abstractions --prerelease
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
dotnet add package OllamaSharp

Configure Launch Settings
Create a Properties/launchSettings.json file to define profiles for Ollama and OpenAI:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "Ollama": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "OpenAI": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

Write the Code
Replace the default Program.cs with this minimal API code:

using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using OllamaSharp;

const string openAiKey = "OPENAI_KEY";
var ollamaUrl = new Uri("http://localhost:11434");

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));

var client = app.Environment.IsDevelopment()
    ? new OllamaApiClient(ollamaUrl, "llama3.2-vision:latest")
    : new OpenAI.Chat.ChatClient("gpt-4o-mini", openAiKey)
        .AsIChatClient();

var cachedClient = new ChatClientBuilder(client)
    .UseDistributedCache(cache)
    .Build();

app.MapPost("/v1", async (Question question) =>
{
    var response = await client.GetResponseAsync(question.prompt);
    return Results.Ok(response.Text);
});

app.MapPost("/v2", async (Question question) =>
{
    var response = await client.GetResponseAsync(
        [
            new ChatMessage(ChatRole.System, "You are an expert in games and technology, answer me in just one sentence, with 50 words."), 
            new ChatMessage(ChatRole.User, question.prompt)
        ]
        );
    return Results.Ok(response.Text);
});

app.MapPost("/v3", async (Question question) =>
{
    var response = await cachedClient.GetResponseAsync(
        [
            new ChatMessage(ChatRole.System, "You are an expert in games and technology, answer me in just one sentence, with 50 words."),
            new ChatMessage(ChatRole.User, question.prompt)
        ]
        );
    return Results.Ok(response.Text);
});

app.Run();

public record Question(string prompt);

This code sets up a minimal API with three endpoints:

  • /v1: A simple endpoint that sends a user prompt to the AI and returns a response. It’s straightforward, ideal for quick tests.
  • /v2: An evolution of /v1, using ChatRole.System to define the AI’s behavior (e.g., “expert in games and technology”) and ChatRole.User for the user’s prompt, ensuring more controlled, context-aware responses.
  • /v3: Builds on /v2 by adding memory caching. Identical prompts reuse cached responses, saving API tokens and improving performance, even if the AI rephrases the same answer.

Run and Test
Run the app with either profile:

  • For Ollama:dotnet run --launch-profile Ollama
  • For OpenAI:dotnet run --launch-profile OpenAI

Test the endpoints (e.g., http://localhost:5000/v1) using a tool like Postman or curl, sending a JSON payload like {“prompt”: “Explain AI in 50 words”}. For AI Mind Center, I use /v3 to generate SEO titles, cutting prep time by 60%. Testers can verify responses, while analysts can refine prompts.

Optional Database Integration
To manage blog metadata, connect to a database (e.g., SQL Server or PostgreSQL):

var connectionString = "your-database-connection-string";
using var conn = new SqlConnection(connectionString);
await conn.OpenAsync();
var cmd = new SqlCommand("SELECT Description FROM BlogPosts WHERE Id = @Id", conn);
cmd.Parameters.AddWithValue("@Id", 1);
var description = (string)await cmd.ExecuteScalarAsync();
var question = new Question($"Create an SEO title for: {description}");

Send question to /v3 for a cached, efficient response.

Challenges and Solutions

Setting up Microsoft.Extensions.AI had hurdles:

  • API Key Security: Storing OpenAI keys safely was critical. Solution: Use environment variables or .NET’s configuration system.
  • Model Selection: llama3.2-vision:11b balanced speed and quality, but larger models needed more memory. Solution: Stick to smaller models for modest setups; 16GB RAM is ideal.
  • Caching Issues: Stale cache entries caused outdated responses. Solution: Set a 1-hour expiration in MemoryDistributedCacheOptions.
  • Prompt Quality: Generic prompts led to vague titles. Solution: Use specific system prompts (e.g., “expert in games and technology”), which analysts can refine.

Applications for Tech Professionals

This setup benefits various roles:

  • Developers: Build flexible AI-powered APIs for chatbots or content tools, switching providers seamlessly.
  • Testers: Validate AI outputs against expected results, ensuring consistency across endpoints.
  • Business Analysts: Define system prompts to automate tasks like report generation or content ideation.

For AI Mind Center, I use /v3 to generate SEO titles and analyze comments, saving hours. Developers can extend it to app features, testers for quality checks, and analysts for workflows.

Scaling Up

To enhance this:

  • Redis Caching: Use Redis for distributed caching in production.
  • Multiple Providers: Test Azure AI or other providers via Microsoft.Extensions.AI.
  • Monitoring: Log API usage with Serilog for performance tracking.

This approach streamlines AI Mind Center’s content creation and can boost any tech project.

Conclusion

Microsoft.Extensions.AI makes AI integration flexible and simple for .NET developers. For AI Mind Center, it’s transformed my SEO workflow, and for tech enthusiasts, it’s a gateway to smarter apps.

Felipe Machado

Felipe Machado

I'm truly excited about what AI has to offer—especially in how it can unlock our potential across every area of life. I dive deep into a variety of AI-related topics and share the most valuable insights and tools I've discovered that are helping me every day. That way, you don’t have to spend hours researching—I’ve done the hard work for you, so you can save time and focus on what matters most.

Related Posts

Running IA Locally with Ollama with .NET
AI APIs & Libraries

Running IA Locally with Ollama with .NET

September 24, 2025
AI for Developers

How to Use GitHub Copilot X: From Beginner to Advanced (Complete 2025 Guide)

June 10, 2025
⚡ Best AI Writing Tools 2025: Transform Your Content Creation Game
AI Time Management

⚡ Best AI Writing Tools 2025: Transform Your Content Creation Game

May 17, 2025
Next Post
Streamlining Workflows with Semantic Kernel

Streamlining Workflows with Semantic Kernel

Leave a Reply Cancel reply

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

I agree to the Terms & Conditions and Privacy Policy.

Unlock Your Potential With AI | AI Mind Center

Unlock your potential with AI Mind Center! Explore AI tools, tutorials, and inspiration to grow, create, and geek out.

Follow Us

Subscribe to our newsletter!

Browse by Category

  • AI APIs & Libraries
  • AI Assisted Coding
  • AI for Developers
  • AI for NPCs & Game Characters
  • AI Game Design Tools
  • AI Game Optimization
  • AI Implementation Projects
  • AI in Game Development
  • AI Model Releases
  • AI News & Trends
  • AI Personal Assistants
  • AI Procedural Generation
  • AI Productivity Apps
  • AI Time Management
  • AI Tools for Productivity
  • AI Workflow Automation
  • Uncategorized

Recent News

Streamlining Workflows with Semantic Kernel

Streamlining Workflows with Semantic Kernel

September 26, 2025
Step-by-Step: Setting Up Microsoft.Extensions.AI

Step-by-Step: Setting Up Microsoft.Extensions.AI

September 25, 2025
Running IA Locally with Ollama with .NET

Running IA Locally with Ollama with .NET

September 24, 2025
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2025 | Website Made By aimindcenter.com.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • AI News & Trends
  • AI Tools for Productivity
  • AI in Game Development
  • AI for Developers
  • AI Tools Reviews

© 2025 | Website Made By aimindcenter.com.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.