.NET AI Chatbot: Integrate GitHub Models with Microsoft Extensions AI

Updated on Nov 09,2025

The landscape of software development is rapidly evolving, especially for .NET developers venturing into Artificial Intelligence. With advancements in libraries and the seamless integration of Large Language Models (LLMs), building intelligent applications is more accessible than ever before. This article delves into using the Microsoft.Extensions.AI library to create an AI chatbot within a .NET environment, focusing on integrating GitHub Models to enhance language processing capabilities. We'll explore a straightforward, consistent approach that simplifies the integration of AI into your applications. Get ready to boost your AI app skills!

Key Points

Utilize the Microsoft.Extensions.AI library for clean AI abstractions in .NET.

Integrate GitHub Models into your .NET applications for advanced language processing.

Set up the OpenAI adapter to connect to various LLMs.

Configure a chat client using IChatClient interface.

Stream responses in real-time for a more interactive user experience.

Maintain conversation history to provide context-aware interactions.

Deploy a simple .NET 9 console application to demonstrate AI integration.

Leveraging AI in .NET with Microsoft Extensions AI

Why .NET Developers Should Explore AI

Today is the perfect moment for .NET developers to delve into the realm of Artificial Intelligence. The latest advancements in AI libraries and tools have made integrating powerful language models into .NET applications incredibly straightforward and accessible. The ability to create intelligent, context-aware chatbots and applications is no longer a distant dream but a practical reality. By harnessing the power of AI, .NET developers can unlock new possibilities and build cutting-edge solutions that enhance user experiences and solve complex problems. Integrating AI opens doors to a vast array of applications, from automated Customer Service to intelligent data analysis, revolutionizing how software interacts with users and data alike. Become a cutting edge AI developer!

Introducing Microsoft.Extensions.AI

The cornerstone library enabling seamless AI integration is Microsoft.Extensions.AI. This library provides clean and consistent abstractions for working with different AI providers, simplifying the process of incorporating AI functionality into your .NET applications. The key benefit of this library is its consistent approach, allowing developers to switch between different AI providers with minimal code changes. By abstracting away the provider-specific details, it fosters a more modular and maintainable codebase. The library offers a unified interface for performing common AI tasks, such as natural language processing, sentiment analysis, and more. This abstraction promotes code reusability and reduces the complexity of integrating AI into your .NET projects. Key concepts include the IChatClient interface, which facilitates communication with Large Language Models (LLMs). This interface provides a consistent way to send prompts to the model and receive responses.

Microsoft.Extensions.AI is key to next gen .NET AI apps!

GitHub Models Integration: A Practical Approach

This article focuses on integrating GitHub Models with .NET, demonstrating the ease with which you can connect your .NET applications to pre-trained AI models. The process involves setting up an OpenAI adapter, configuring a chat client, and writing a few lines of code to establish communication with the selected model. GitHub Models offers a rich ecosystem of pre-trained AI models ready to be deployed. Integrating them into your .NET applications lets you harness advanced capabilities without the need for extensive training. This integration enables developers to focus on the core functionalities of their applications while relying on pre-built models for AI-driven tasks. Streamlining the development process.

Step-by-Step Guide to Building a .NET AI Chatbot

Prerequisites

Before you begin, ensure that you have the following:

  • .NET 9 SDK installed
  • Visual Studio or Visual Studio Code with the C# extension
  • GitHub account

Step 1: Setting Up a .NET 9 Console App

First, create a new .NET 9 console application. Open your terminal or command Prompt and run the following command:

dotnet new console -o ChatAppWithGithubModel
cd ChatAppWithGithubModel

This creates a new console application project named ChatAppWithGithubModel and navigates into the project directory.

Step 2: Adding Required Libraries via NuGet

Next, add the necessary libraries using the NuGet package manager. This involves installing the Microsoft.Extensions.AI.OpenAI package, which handles communication with OpenAI-compatible endpoints, including GitHub Models. Open the NuGet Package Manager by right-clicking on your project in Visual Studio and selecting "Manage NuGet Packages." Alternatively, you can use the .NET CLI.

Search for Microsoft.Extensions.AI.OpenAI and install the latest stable version . This package provides the necessary components to interact with OpenAI-compatible AI models. It handles the communication and data serialization, allowing you to focus on implementing the core logic of your chatbot.

dotnet add package Microsoft.Extensions.AI.OpenAI

Step 3: Implementing the Chat Client

Now, let's implement the chat client using the IChatClient interface from the Microsoft.Extensions.AI library. This involves configuring the chat client and adding the necessary code to handle user input and model responses.

  1. Clear the Program.cs file: Remove the default code in Program.cs.
  2. Add the Chat Client implementation: Add the following code to your Program.cs file:
using Microsoft.Extensions.AI.Chat;
using OpenAI;

IChatClient chatClient = new ChatClient(
    new ApiKeyCredential("YOUR_GITHUB_API_KEY"),
    new OpenAIClientOptions
    {
        Endpoint = new Uri("https://models.github.ai/inference")
    }).AsChatClient();

Console.WriteLine("GPT 4.1 Mini Chat - Type 'exit' to quit");

while (true)
{
    Console.Write("You: ");
    string userInput = Console.ReadLine();

    if (string.IsNullOrEmpty(userInput))
    {
        continue;
    }

    if (userInput.Equals("exit", StringComparison.OrdinalIgnoreCase))
    {
        break;
    }

    var messages = new List<ChatMessage>();
    messages.Add(new ChatMessage(ChatRole.User, userInput));

    var chatCompletionsOptions = new ChatCompletionsOptions()
    {
        Messages = messages,
        MaxTokens = 1000,
        Temperature = 0.7f,
        TopP = 0.95f,
        N = 1,
        StopSequences = { "
User:", "
Assistant:" }
    };

    var response = await chatClient.GetChatCompletionsAsync(chatCompletionsOptions);
    var botMessage = response.Choices[0].Message;
    Console.WriteLine($"Assistant: {botMessage.Content}");
}

Replace YOUR_GITHUB_API_KEY with your actual GitHub API key.

Step 4: Generating Your GitHub API Key

To connect to GitHub Models, you'll need to generate a Personal Access Token (API Key). This token authenticates your application and grants access to the desired models.

  1. Navigate to GitHub Models Marketplace: Go to the GitHub Models marketplace.
  2. Select a Model: Choose a model that fits your project needs.
  3. Use this Model: Click the "Use this model" button.
  4. Create Personal Access Token: Click the "Create Personal Access Token" button and follow the instructions to generate your API key. The token will only be visible once, store it securely!

Step 5: Configuring the Model Name and Endpoint

Modify the code to include the correct model name and endpoint for GitHub Models. In the example code, ensure that the Endpoint URI is set to https://models.github.ai/inference. The ModelId depends on which Model you choose.

Modify the IchatClient to new ChatClient and provide the right parameter

IChatClient chatClient = new ChatClient(
    new ApiKeyCredential("YOUR_GITHUB_API_KEY"),
    new OpenAIClientOptions
    {
        Endpoint = new Uri("https://models.github.ai/inference")
    }).AsChatClient();

Step 6: Running the Chat Application

Build and run the application to start interacting with the AI Chatbot. This involves executing the application and entering prompts to test the model's responses. Use the following command to run your application:

dotnet run

The application will start, and you can begin typing your messages. Type exit to quit the chat.

Step 7: Enhancing the Chat Experience (Optional)

To improve the user experience, you can customize the console's text color. This helps distinguish between user input and assistant responses. Modify the code to include the following lines:

Console.ForegroundColor = ConsoleColor.Green; // Assistant's response color
Console.WriteLine($"Assistant: {botMessage.Content}");
Console.ResetColor(); // Reset to default color

Additionally, you can store conversation history to provide context to the model, enabling it to remember previous interactions.

GitHub Models Pricing

GitHub Free Tier and Paid Options

GitHub Models offers a free tier that allows developers to start building AI applications with limited usage.

For higher limits and more advanced features, there are paid options available.

  • Free Tier: Offers free access with simple rate limits, suitable for initial development and testing.
  • Paid Tiers: Provide increased rate limits and additional features, catering to production-level applications and more demanding workloads.

It's important to review the pricing details on the GitHub Models marketplace to choose the tier that best aligns with your project requirements.

Advantages and Disadvantages of Using GitHub Models with .NET

👍 Pros

Easy Integration: Seamlessly integrate AI functionalities into your .NET applications with minimal code.

Cost-Effective: Utilize the free tier for initial development and testing, with scalable paid options for production environments.

Access to Pre-trained Models: Leverage a rich ecosystem of pre-trained AI models ready to be deployed.

Real-time Streaming: Provide an interactive user experience with real-time response streaming.

Conversation History: Maintain context-aware interactions by storing and utilizing conversation history.

👎 Cons

Dependency on External Services: Reliant on GitHub Models for AI functionalities.

API Key Management: Requires secure handling of API keys to prevent unauthorized access.

Rate Limits: Free tier has limitations on usage, which may require upgrading to a paid plan.

Model Selection: Limited by the models available on the GitHub Models marketplace.

Network Dependency: Requires a stable network connection for real-time streaming and API communication.

Frequently Asked Questions

What is Microsoft.Extensions.AI?
Microsoft.Extensions.AI is a library providing clean abstractions for AI integration in .NET applications, offering a consistent interface for working with different AI providers.
How do I generate a GitHub API key?
Navigate to the GitHub Models marketplace, select a model, click 'Use this model', and follow the instructions to create a Personal Access Token.
What is IChatClient?
IChatClient is the primary abstraction from Microsoft.Extensions.AI used to communicate with Large Language Models (LLMs) in .NET.
How can I enable real-time streaming of AI responses?
Use the GetStreamingResponseAsync method in a foreach loop to process responses token by token, displaying the results in real-time.
Is it safe to hardcode the API key in a real app?
It is dangerous to hardcode API Key, instead, save the API Key to a safe place like the enviromental variables.

Understanding AI in .NET: Related Questions

How do I handle API Key security in a production .NET application?
Managing API keys securely is vital, especially in production environments. Avoid hardcoding API keys directly into your source code. This practice can expose your keys to unauthorized access and potential misuse. Instead, leverage environment variables or secure configuration files. Environment variables allow you to store sensitive information outside of your codebase, making it less susceptible to accidental exposure. Secure configuration files offer encryption and access control, further enhancing security. Utilize Azure Key Vault or similar services to manage and protect your API keys. These services provide centralized key management, auditing capabilities, and role-based access control. Regularly rotate your API keys to minimize the impact of any potential breaches. This practice involves generating new keys and updating your application configuration periodically, reducing the window of vulnerability in case of a security incident. Implement robust access controls to limit which components and users can access API keys. Role-based access control (RBAC) allows you to define specific permissions for different roles, ensuring that only authorized personnel can retrieve and manage sensitive information. Monitor API usage and set up alerts for suspicious activities. Anomaly detection can help identify potential security breaches and unauthorized access attempts, allowing you to take proactive measures to mitigate risks.

Most people like