Zeeshan Ayyub
Zeeshan Ayyub
  • Home
  • About Us
  • Services
  • Projects
  • Blog
  • Contact Us

Build an AI chat app with .NET - Zeeshan Ayyub

    Home / Blog / Build an AI chat app with .NET
  • September 25, 2025
  • linkzeeshan.ayyub

Build an AI chat app with .NET

Create the app

Complete the following steps to create a .NET console app to connect to an AI model.

  1. In an empty directory on your computer, use the dotnet new command to create a new console app:

    dotnet new console -o ChatAppAI
3. Install the required packages:  
dotnet add package OpenAI
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets

Add the app code

This app uses the Microsoft.Extensions.AI package to send and receive requests to the AI model. The app provides users with information about hiking trails.

  1. In the Program.cs file, add the following code to connect and authenticate to the AI model.

// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using OpenAI;

Console.WriteLine("Hello, World!");

var config = new ConfigurationBuilder().AddUserSecrets().Build();
string model = config["ModelName"]; //"gpt-5"; //
string key = config["OpenAIKey"];

// Create the IChatClient
IChatClient chatClient =
    new OpenAIClient(key).GetChatClient(model).AsIChatClient();

//ChatClient chatClient = new OpenAIClient(key).GetChatClient(model);
// Start the conversation with context for the AI model
List chatHistory =
    [
        new ChatMessage(ChatRole.System, """
            You are a friendly hiking enthusiast who helps people discover fun hikes in their area.
            You introduce yourself when first saying hello.
            When helping people out, you always ask them for this information
            to inform the hiking recommendation you provide:

            1. The location where they would like to hike
            2. What hiking intensity they are looking for

            You will then provide three suggestions for nearby hikes that vary in length
            after you get that information. You will also share an interesting fact about
            the local nature on the hikes when making a recommendation. At the end of your
            response, ask if there is anything else you can help with.
        """)
    ];
// Loop to get user input and stream AI response
while (true)
{
    // Get user prompt and add to chat history
    Console.WriteLine("Your prompt:");
    string? userPrompt = Console.ReadLine();
    chatHistory.Add(new ChatMessage(ChatRole.User, userPrompt));

    // Stream the AI response and add to chat history
    Console.WriteLine("AI Response:");
    string response = "";
    await foreach (ChatResponseUpdate item in
        chatClient.GetStreamingResponseAsync(chatHistory))
    {
        Console.Write(item.Text);
        response += item.Text;
    }
    chatHistory.Add(new ChatMessage(ChatRole.Assistant, response));
    Console.WriteLine();
}




Previous Post
Next Post

One Comment

Abhiroop Santra
26 Sep 2025

👌

Reply

Leave a comment

Cancel reply

Recent Posts

  • Build a .NET AI vector search app
  • Build an AI chat app with .NET
  • Executive Brief .NET 10 for Web & APIs
  • Executive Brief: .NET 10 for Web & APIs
  • Single vs. Split Queries

Recent Comments

  1. Abhiroop Santra on Build an AI chat app with .NET

Recent Post

  • October 1, 2025
    Build a .NET AI vector search app
  • September 25, 2025
    Build an AI chat app with .NET
  • September 22, 2025
    Executive Brief .NET 10 for Web & APIs

Categories

  • Blog

Tags

There’s no content to show here yet.

Archives

  • October 2025
  • September 2025
  • February 2024

Excellence decisively nay man yet impression for contrasted remarkably. There spoke happy for you are out. Fertile how old address did showing.

Solutions

  • IT Management
  • Cloud Service
  • Data Center
  • Software Development
  • Machine Learning

Contact Info

  • 5919 Trussville Crossings Pkwy, new Dusting town, Austria
  • Opening Hours: 8:00 AM – 7:45 PM
  • Phone: +123 34598768

Subscribe to Newsletter

Join our subscribers list to get the latest news and special offers.

Copyright 2024 Zeeshan Ayyub. All Rights Reserved by Trancept