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
- ๐ฌ Chat Completion - Interactive AI conversations
- ๐ท๏ธ Text Classification - Automatic categorization of user feedback
- ๐ Text Summarization - Condensing long articles into bullet points
- ๐ 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:
-
Get a GitHub PAT (Personal Access Token)
- Go to GitHub Settings โ Developer Settings โ Tokens
- Generate a token with
repoaccess - Save it securely!
Configure User Secrets (Keep your token safe!)
cd YourProject
dotnet user-secrets set "GitHubModels:Token" "your-github-token-here"
๐ 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
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);
}
๐จ 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
๐ก Pro Tips
- Use low temperature (0.1) for consistent classifications
- Keep
MaxOutputTokensminimal 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}");
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
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}");
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"
}
๐ฏ 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
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-minifor general tasks (10x cheaper!) - Set
MaxOutputTokensappropriately - 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
.gitignorefor 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:
- Combine tasks: Classify โ Analyze Sentiment โ Summarize
- Add conversation history for multi-turn chats
- Build a web API with ASP.NET Core
- Create a Blazor UI for interactive demos
- Add function calling for tool integration
๐ Resources
- ๐ฆ GitHub Repo: genai-dotnet-basic_llm_tasks
- ๐ค GitHub Models: Official Documentation
- ๐ง Microsoft.Extensions.AI: NuGet Package
- ๐ .NET 10: What's New
๐ฌ 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
Top comments (0)