DEV Community

Pawel Janda
Pawel Janda

Posted on

Building AI-Powered .NET applications with Groq Cloud and MaIN.NET

Introduction

In this tutorial, we'll explore how to integrate Groq Cloud with the MaIN.NET framework to build powerful AI applications in .NET. We'll create a console application that demonstrates basic chat functionality and interactive conversations with proper conversation context management.

What is Groq Cloud?

Groq Cloud is a cutting-edge AI platform that specializes in ultra-fast inference for large language models. Unlike traditional cloud AI services that can take several seconds to respond, Groq Cloud delivers responses in milliseconds, making it ideal for real-time applications.

Key features of Groq Cloud:

  • Lightning-fast inference: Responses in milliseconds instead of seconds.
  • Multiple model support: Access to models like Llama 3, Qwen or brand new GPT-OSS.
  • Cost-effective: Competitive pricing for high-speed inference and powerful free plan.
  • Developer-friendly: OpenAI API and MaIN.NET compatibility.

Why Groq Cloud?

Traditional AI services often have some latency, which can break the user experience in real-time applications. Groq Cloud solves this by using specialized hardware and optimized inference engines, making it perfect for:

  • Chatbots and conversational AI
  • Real-time content generation
  • Interactive applications
  • Low-latency AI services

Why MaIN.NET?

Link to the repo

One of the most impressive aspects of this integration is how clean and straightforward the code becomes thanks to MaIN.NET's well-designed architecture. Unlike traditional AI integrations that often require complex HTTP client setup, authentication handling, and response parsing, MaIN.NET abstracts all of this complexity away.

With just a few lines of code, you get:

  • Automatic conversation context management - No need to manually track message history
  • Built-in error handling - Robust error management out of the box
  • Fluent API design - Intuitive method chaining for configuration
  • Multiple backend support - Easy switching between different AI providers

The result? Clean, maintainable code that focuses on your business logic rather than infrastructure concerns. Let's see how this translates into a real application!

Prerequisites

Before we begin, make sure you have:

  • .NET SDK installed
  • A Groq Cloud API key (get one at console.groq.com)
  • Basic knowledge of C# and .NET

Step 1: Setting up the project

First, let's create a new console application and set up the MaIN.NET framework:

# Create a new console application
dotnet new console -n MaINGroqDemo

# Navigate to the project directory
cd MaINGroqDemo

# Add MaIN.NET NuGet package
dotnet add package MaIN.NET
Enter fullscreen mode Exit fullscreen mode

Step 2: Set up environment variables

Set your Groq Cloud API key as an environment variable:

# macOS/Linux
export GROQ_API_KEY=your_groq_api_key_here

# Windows (PowerShell)
$env:GROQ_API_KEY="your_groq_api_key_here"

# Windows (CMD)
set GROQ_API_KEY=your_groq_api_key_here
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the basic application

Replace the contents of Program.cs with the following code:

using MaIN.Core;
using MaIN.Core.Hub;
using MaIN.Core.Hub.Contexts;
using MaIN.Domain.Configuration;

Console.WriteLine("πŸš€ MaIN.NET Groq Cloud Integration Demo");
Console.WriteLine("=====================================");

// Check if API key is available
var apiKey = Environment.GetEnvironmentVariable("GROQ_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
    Console.WriteLine("❌ GROQ_API_KEY is not set in environment variables.");
    Console.WriteLine("   Set the GROQ_API_KEY environment variable with your Groq API key.");
    Console.WriteLine("   Example: export GROQ_API_KEY=your_api_key_here");
    return;
}

try
{
    // Initialize MaIN.NET with Groq Cloud configuration
    Console.WriteLine("πŸ”§ Initializing MaIN.NET with Groq Cloud...");
    MaINBootstrapper.Initialize(configureSettings: (options) =>
    {
        options.BackendType = BackendType.GroqCloud;
        options.GroqCloudKey = apiKey;
    });

    Console.WriteLine("βœ… MaIN.NET initialized successfully!");

    // Run interactive chat
    await RunInteractiveChat();
}
catch (Exception ex)
{
    Console.WriteLine($"❌ Error: {ex.Message}");
}

static async Task RunInteractiveChat()
{
    Console.WriteLine("\n🎯 Interactive Chat with Groq Cloud");
    Console.WriteLine("=================================");
    Console.WriteLine("Type 'exit' to quit, 'history' to see chat history, or ask me anything:");
    Console.WriteLine();

    // Create a single chat context that will maintain conversation history
    var chatContext = AIHub.Chat()
        .WithModel("openai/gpt-oss-120b")
        .WithSystemPrompt("You are a helpful AI assistant powered by Groq Cloud. Respond in English. Keep your responses concise and friendly.");

    while (true)
    {
        Console.Write("You: ");
        var userInput = Console.ReadLine()?.Trim();

        if (string.IsNullOrEmpty(userInput))
            continue;

        if (userInput.ToLower() == "exit")
        {
            Console.WriteLine("Goodbye! πŸ‘‹");
            break;
        }

        if (userInput.ToLower() == "history")
        {
            ShowChatHistory(chatContext);
            continue;
        }

        try
        {
            // Add the user message to the conversation
            chatContext.WithMessage(userInput);

            // Get the AI response while maintaining context
            var response = await chatContext.CompleteAsync();

            Console.WriteLine($"AI: {response.Message.Content}");
            Console.WriteLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"❌ Error: {ex.Message}");
            Console.WriteLine();
        }
    }
}

static void ShowChatHistory(ChatContext chatContext)
{
    Console.WriteLine("\nπŸ“œ Chat History:");
    Console.WriteLine("----------------");

    var history = chatContext.GetChatHistory();
    if (history.Count == 0)
    {
        Console.WriteLine("No messages yet.");
    }
    else
    {
        foreach (var message in history)
        {
            var role = message.Role == "User" ? "You" : "AI";
            Console.WriteLine($"{role}: {message.Content}");
        }
    }
    Console.WriteLine();
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Understanding the code

Let's break down the key components of our implementation:

1. MaIN.NET initialization

MaINBootstrapper.Initialize(configureSettings: (options) =>
{
    options.BackendType = BackendType.GroqCloud;
    options.GroqCloudKey = apiKey;
});
Enter fullscreen mode Exit fullscreen mode

This initializes the MaIN.NET framework with Groq Cloud as the backend provider.

2. Chat context management

var chatContext = AIHub.Chat()
    .WithModel("openai/gpt-oss-120b")
    .WithSystemPrompt("You are a helpful AI assistant...");
Enter fullscreen mode Exit fullscreen mode

We create a single ChatContext instance that maintains conversation history throughout the session.

3. Message handling

chatContext.WithMessage(userInput);
var response = await chatContext.CompleteAsync();
Enter fullscreen mode Exit fullscreen mode

We add user messages to the conversation and get AI responses while preserving context.

Step 5: Running the application

Build and run your application:

dotnet build
dotnet run
Enter fullscreen mode Exit fullscreen mode

You should see output like:

Console output

Step 6: Available Groq models

Groq Cloud supports several high-performance models. For the latest list of available models and their capabilities, visit the Groq Cloud Model Documentation.

You can easily switch models by changing the model name in the WithModel() call:

var chatContext = AIHub.Chat()
    .WithModel("openai/gpt-oss-120b")  // Switch to gpt-oss model
    .WithSystemPrompt("...");
Enter fullscreen mode Exit fullscreen mode

Step 7: Advanced features

Custom system prompts

You can customize the AI's behavior with system prompts:

.WithSystemPrompt("You are a coding assistant. Always provide code examples in C# and explain your reasoning.")
Enter fullscreen mode Exit fullscreen mode

Error handling

The application includes robust error handling:

try
{
    var response = await chatContext.CompleteAsync();
    Console.WriteLine($"AI: {response.Message.Content}");
}
catch (Exception ex)
{
    Console.WriteLine($"❌ Error: {ex.Message}");
}
Enter fullscreen mode Exit fullscreen mode

Performance benefits

One of the most impressive aspects of Groq Cloud is its speed. Here's what you can expect:

  • Response times: 100-500ms vs 2-5 seconds with traditional services
  • No queue times: Instant responses without waiting
  • Consistent performance: Reliable speed regardless of load
  • Cost efficiency: Fast responses and low costs per interaction

Next steps

Now that you have a working Groq Cloud integration, you can extend it with:

  1. Web API integration: Create a REST API
  2. Blazor web application: Build interactive web interfaces

For some inspiration, you can check out my previous article: Build a Local ChatGPT-like App with Blazor and MaIN.NET (Part 1).

Conclusion

Integrating Groq Cloud with MaIN.NET opens up exciting possibilities for building high-performance AI applications. The combination of Groq's lightning-fast inference and MaIN.NET's comprehensive AI framework provides a powerful foundation for modern AI development.

The key benefits of this integration include:

  • Speed: Ultra-fast response times for better user experience
  • Simplicity: Easy integration with existing .NET applications
  • Flexibility: Support for multiple models and use cases
  • Cost-effectiveness: Efficient resource utilization

Whether you're building chatbots, content generators, or complex AI workflows, this setup provides the performance and reliability needed for production applications.

Resources


Happy coding! πŸš€

Top comments (1)

Collapse
 
artyprog profile image
ArtyProg

Thank you :-)