DEV Community

Cover image for 4 Essential LLM Tasks in .NET 10 Using GitHub Models ๐Ÿš€
Rahul Kumar Jha
Rahul Kumar Jha

Posted on

4 Essential LLM Tasks in .NET 10 Using GitHub Models ๐Ÿš€

4 Essential LLM Tasks in .NET 10 Using GitHub Models ๐Ÿš€

Ever wondered how to integrate AI into your .NET applications? In this post, I'll show you 4 fundamental LLM tasks that you can implement in .NET 10 using GitHub Models - and the best part? It's surprisingly simple!

๐ŸŽฏ What We'll Build

  1. ๐Ÿ’ฌ Chat Completion - Interactive AI conversations
  2. ๐Ÿท๏ธ Text Classification - Automatic categorization of user feedback
  3. ๐Ÿ“ Text Summarization - Condensing long articles into bullet points
  4. ๐Ÿ˜Š Sentiment Analysis - Understanding customer emotions from reviews

๐Ÿ’ก All code is available on GitHub: genai-dotnet-basic_llm_tasks


๐Ÿ”ง Prerequisites

Before we dive in, make sure you have:

  • โœ… .NET 10 SDK installed
  • โœ… Visual Studio 2022 or VS Code
  • โœ… GitHub Personal Access Token with access to GitHub Models

Getting Started with GitHub Models

GitHub Models provides free access to powerful AI models! Here's how to get started:

  1. Get a GitHub PAT (Personal Access Token)

  2. Configure User Secrets (Keep your token safe!)

cd YourProject
dotnet user-secrets set "GitHubModels:Token" "your-github-token-here"
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“š Learn more: Check out the official GitHub Models documentation for detailed setup instructions.


1๏ธโƒฃ Chat Completion - Your AI Assistant ๐Ÿ’ฌ

Let's start with the basics: having a conversation with AI. This project demonstrates both non-streaming (get complete response) and streaming (real-time, token-by-token) responses.

Architecture at a Glance

Configuration โ†’ Authentication โ†’ AI Client โ†’ Chat Response
Enter fullscreen mode Exit fullscreen mode

The Code

using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using OpenAI;
using System.ClientModel;

// Load configuration
IConfigurationRoot configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false)
    .AddUserSecrets<Program>()
    .Build();

// Authenticate with GitHub Models
ApiKeyCredential credential = new ApiKeyCredential(
    configuration["GitHubModels:Token"] 
    ?? throw new InvalidOperationException("Token not found!"));

// Initialize AI client
IChatClient chatClient = new OpenAIClient(credential, new OpenAIClientOptions
{
    Endpoint = new Uri("https://models.inference.ai.azure.com")
})
.GetChatClient("gpt-4o-mini")
.AsIChatClient();

// Get streaming response
var responseStream = chatClient.GetStreamingResponseAsync(
    "Explain quantum computing in simple terms", 
    new ChatOptions { Temperature = 0.7f, MaxOutputTokens = 300 }
);

Console.Write("AI: ");
await foreach (var message in responseStream)
{
    Console.Write(message.Text);
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽจ What Makes It Cool?

  • Real-time streaming for better UX
  • Token usage tracking for cost management
  • Configurable temperature (0.0 = focused, 1.0 = creative)

Use cases: Chatbots, Q&A systems, code assistants


2๏ธโƒฃ Text Classification - Smart Categorization ๐Ÿท๏ธ

Automatically categorize user feedback into Complaint, Suggestion, Praise, or Other. Perfect for customer support automation!

The Magic Prompt

var classificationPrompt = @"Classify the text into one category: 
Complaint, Suggestion, Praise, or Other.
Respond with only the category name.
Text: '{0}'";

var userFeedback = "The app keeps crashing when I upload photos!";

var response = await chatClient.GetResponseAsync(
    string.Format(classificationPrompt, userFeedback),
    new ChatOptions 
    { 
        Temperature = 0.1f,  // Low temp for consistency
        MaxOutputTokens = 50 
    }
);

Console.WriteLine($"Classification: {response.Text}");
// Output: Complaint
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pro Tips

  • Use low temperature (0.1) for consistent classifications
  • Keep MaxOutputTokens minimal since you only need one word
  • Perfect for routing support tickets!

Real-world impact: Route 1000s of customer messages automatically ๐Ÿ“ฎ


3๏ธโƒฃ Text Summarization - TL;DR Generator ๐Ÿ“

Turn lengthy articles into concise bullet points. Save reading time and improve information accessibility!

Quick Example

var summaryPrompt = @"Summarize the following text as bullet points.
Provide 2-5 key points.
Text: '{0}'";

var longText = @"The quarterly earnings report shows our company 
exceeded expectations with a 15% revenue increase. Growth was driven 
by Asia-Pacific sales and new product launches. Profit margin 
improved from 12% to 14%, and the board approved a 5% dividend increase.";

var summary = await chatClient.GetResponseAsync(
    string.Format(summaryPrompt, longText),
    new ChatOptions { Temperature = 0.1f, MaxOutputTokens = 150 }
);

Console.WriteLine($"Summary:\n{summary.Text}");
Enter fullscreen mode Exit fullscreen mode

Output

โ€ข Company revenue increased 15% year-over-year, beating expectations
โ€ข Growth driven by Asia-Pacific sales and new product launches  
โ€ข Profit margin improved from 12% to 14%
โ€ข Board approved 5% dividend increase for shareholders
Enter fullscreen mode Exit fullscreen mode

Use cases: News aggregation, document processing, meeting notes


4๏ธโƒฃ Sentiment Analysis - Understand Customer Emotions ๐Ÿ˜Š

Go beyond simple positive/negative! This returns structured JSON with sentiment breakdown, positive/negative aspects, and emotional tone.

The Smart Prompt

var sentimentPrompt = @"Analyze the sentiment of this product review.
Return as JSON with this structure:
{
  ""overallSentiment"": ""Positive/Negative/Neutral/Mixed"",
  ""positiveAspects"": [""list of good things""],
  ""negativeAspects"": [""list of bad things""],
  ""emotionalTone"": ""description of tone""
}
Review: '{0}'";

var review = @"This laptop is amazing! Battery lasts all day, 
screen is crystal clear. Best purchase this year!";

var analysis = await chatClient.GetResponseAsync(
    string.Format(sentimentPrompt, review),
    new ChatOptions { Temperature = 0.1f, MaxOutputTokens = 200 }
);

Console.WriteLine($"Analysis:\n{analysis.Text}");
Enter fullscreen mode Exit fullscreen mode

Sample Output

{
  "overallSentiment": "Positive",
  "positiveAspects": [
    "Excellent battery life lasting all day",
    "Crystal clear screen quality",
    "Overall satisfaction with purchase"
  ],
  "negativeAspects": [],
  "emotionalTone": "Enthusiastic and highly satisfied"
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Why JSON?

  • Easy to parse programmatically
  • Structured data for dashboards
  • Multi-dimensional insights in one request

Business value: Build customer sentiment dashboards, track product issues, identify improvement areas ๐Ÿ“Š


๐Ÿš€ Getting Started

Ready to try it yourself?

# Clone the repo
git clone https://github.com/Rahul1994jh/genai-dotnet-basic_llm_tasks
cd genai-dotnet-basic_llm_tasks

# Setup your GitHub token
dotnet user-secrets set "GitHubModels:Token" "your-token-here"

# Run any project
dotnet run --project TextCompletion
dotnet run --project Classification
dotnet run --project Summarization
dotnet run --project SentimentAnalysis
Enter fullscreen mode Exit fullscreen mode

Each project has a detailed README with:

  • Architecture diagrams
  • Customization guides
  • Troubleshooting tips
  • Best practices

๐ŸŽ“ Key Takeaways

Task Best For Temperature Model
Chat Completion Conversations, Q&A 0.7 gpt-4o-mini
Classification Categorization 0.1 gpt-4o-mini
Summarization Content condensing 0.1 gpt-4o-mini
Sentiment Analysis Emotion detection 0.1 gpt-4o-mini

๐Ÿ’ฐ Cost Optimization Tips

  • Use gpt-4o-mini for general tasks (10x cheaper!)
  • Set MaxOutputTokens appropriately
  • Use low temperature for predictable tasks
  • Monitor token usage in production

๐Ÿ” Security Best Practices

โœ… DO:

  • Store tokens in User Secrets (development)
  • Use Azure Key Vault (production)
  • Use .gitignore for secrets
  • Rotate tokens regularly

โŒ DON'T:

  • Hardcode API keys in code
  • Commit secrets to Git
  • Share tokens publicly

๐ŸŽฏ What's Next?

Now that you've mastered the basics, here are some ideas to level up:

  1. Combine tasks: Classify โ†’ Analyze Sentiment โ†’ Summarize
  2. Add conversation history for multi-turn chats
  3. Build a web API with ASP.NET Core
  4. Create a Blazor UI for interactive demos
  5. Add function calling for tool integration

๐Ÿ“š Resources


๐Ÿ’ฌ Let's Connect!

Found this helpful? Have questions or ideas?

  • โญ Star the repo on GitHub
  • ๐Ÿ’ฌ Drop a comment below
  • ๐Ÿฆ Share with your network

Happy coding! ๐ŸŽ‰


Built with โค๏ธ using .NET 10, Microsoft.Extensions.AI, and GitHub Models

dotnet #ai #machinelearning #csharp #github #llm #artificialintelligence #programming

Top comments (0)