Logo

dev-resources.site

for different kinds of informations.

Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure

Published at
1/13/2025
Categories
dotnet
azure
python
llm
Author
paulotorrestech
Categories
4 categories in total
dotnet
open
azure
open
python
open
llm
open
Author
15 person written this
paulotorrestech
open
Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure

Table of Contents

  1. Introduction
  2. Why Fine-Tune Large Language Models?
  3. Solution Overview
  4. Setting Up the Environment
  5. Training and Fine-Tuning with Python
  6. Integrating the Fine-Tuned Model in .NET Core
  7. Deploying to Azure
  8. Best Practices
  9. Conclusion

1. Introduction

Large Language Models (LLMs) have gained significant traction for their ability to understand and generate human-like text. However, many organizations have unique datasets and domain-specific vocabularies that general-purpose models may not fully capture. Fine-tuning allows developers to tailor these large models to a specific context or industry, improving accuracy and relevance.

In this article, we’ll explore how to fine-tune an LLM using Python, then integrate and deploy the resulting model into a .NET Core C# application, all on Microsoft Azure for scalability and convenience.


2. Why Fine-Tune Large Language Models?

  1. Domain Specificity: LLMs can be fine-tuned to use industry-specific jargon, product names, or specialized terminology.
  2. Performance Boost: Fine-tuning often reduces errors and increases relevance in use cases like customer service, research, and analytics.
  3. Reduced Costs: Rather than building a model from scratch, you customize an existing powerful LLM.
  4. Time Efficiency: You leverage pre-trained weights and only adjust final layers or parameters, expediting the process.

3. Solution Overview

Components and Technologies

  1. Python for Fine-Tuning

    • Popular libraries (e.g., Hugging Face Transformers, PyTorch)
    • Streamlined process for loading and adapting pre-trained models
  2. .NET Core C# for Integration

    • Back-end service or API exposing the fine-tuned model
    • Strongly typed language, familiar to many enterprise developers
  3. Azure Services

    • Azure Machine Learning for training and model management
    • Azure Storage for data and model artifacts
    • Azure App Service or Azure Functions for hosting the .NET Core application
    • Azure Key Vault (optional) to secure credentials

4. Setting Up the Environment

Prerequisites

  • Azure Subscription: Required to create resources such as Machine Learning workspaces and App Services.
  • Python 3.8+: Installed locally for model fine-tuning.
  • .NET 6/7/8 SDK: To create and run the .NET Core C# application.
  • Visual Studio 2022 or Visual Studio Code: Recommended IDEs.
  • Azure CLI: For provisioning and managing Azure services via the terminal.
  • Docker (optional): Useful for containerizing your application, if desired.

5. Training and Fine-Tuning with Python

This example uses Hugging Face Transformers—one of the most widely adopted libraries for LLM fine-tuning.

5.1 Set Up a Virtual Environment

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

5.2 Install Dependencies

pip install torch transformers azureml-sdk
Enter fullscreen mode Exit fullscreen mode

5.3 Create an Azure Machine Learning Workspace

  1. Resource Group and Workspace:
   az group create --name LLMFinetuneRG --location eastus
   az ml workspace create --name LLMFinetuneWS --resource-group LLMFinetuneRG
Enter fullscreen mode Exit fullscreen mode
  1. Configure local environment to connect to the workspace (using a config.json file or environment variables).

5.4 Fine-Tuning Script (train.py)

import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
from azureml.core import Workspace, Run

# Connect to Azure ML
ws = Workspace.from_config()
run = Run.get_context()

model_name = "gpt2"  # Example model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Load your custom dataset (local or from Azure Storage)
# Example: a text file or a dataset in Azure ML
train_texts = ["Your domain-specific text here..."]  # Simplified
train_encodings = tokenizer(train_texts, truncation=True, padding=True)

class CustomDataset(torch.utils.data.Dataset):
    def __init__(self, encodings):
        self.encodings = encodings
    def __len__(self):
        return len(self.encodings["input_ids"])
    def __getitem__(self, idx):
        return {k: torch.tensor(v[idx]) for k, v in self.encodings.items()}

train_dataset = CustomDataset(train_encodings)

training_args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=3,
    per_device_train_batch_size=2,
    save_steps=100,
    logging_steps=100
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
)

trainer.train()

# Save the fine-tuned model
trainer.save_model("./fine_tuned_model")
tokenizer.save_pretrained("./fine_tuned_model")
Enter fullscreen mode Exit fullscreen mode

5.5 Register the Model in Azure

from azureml.core.model import Model

model = Model.register(
    workspace=ws,
    model_path="./fine_tuned_model",
    model_name="myFineTunedLLM"
)
Enter fullscreen mode Exit fullscreen mode

At this point, your fine-tuned model is stored in Azure Machine Learning for easy access and version control.


6. Integrating the Fine-Tuned Model in .NET Core

6.1 Create a .NET Core Web API Project

dotnet new webapi -n FineTunedLLMApi
cd FineTunedLLMApi
Enter fullscreen mode Exit fullscreen mode

6.2 Add Dependencies

  • HttpClient for calling Azure endpoints or local inference API
  • Newtonsoft.Json (if you prefer JSON.NET for serialization)
  • Azure.Storage.Blobs or Azure.Identity for secure access to Azure resources
dotnet add package Microsoft.Extensions.Http
dotnet add package Microsoft.Azure.Storage.Blob
dotnet add package Newtonsoft.Json
Enter fullscreen mode Exit fullscreen mode

6.3 ModelConsumerService.cs

Assume you deployed the fine-tuned model as a web service (e.g., using Azure Container Instances or a custom endpoint in Azure ML). The following snippet calls that service to get completions.

using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

public class ModelConsumerService
{
    private readonly HttpClient _httpClient;

    public ModelConsumerService(IHttpClientFactory httpClientFactory)
    {
        _httpClient = httpClientFactory.CreateClient("FineTunedModel");
    }

    public async Task<string> GetCompletionAsync(string prompt)
    {
        var requestBody = new { prompt = prompt };
        var content = new StringContent(
            JsonConvert.SerializeObject(requestBody),
            Encoding.UTF8, 
            "application/json");

        var response = await _httpClient.PostAsync("/predict", content);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}
Enter fullscreen mode Exit fullscreen mode

6.4 LLMController.cs

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

[ApiController]
[Route("[controller]")]
public class LLMController : ControllerBase
{
    private readonly ModelConsumerService _modelService;

    public LLMController(ModelConsumerService modelService)
    {
        _modelService = modelService;
    }

    [HttpPost("complete")]
    public async Task<IActionResult> CompletePrompt([FromBody] PromptRequest request)
    {
        var result = await _modelService.GetCompletionAsync(request.Prompt);
        return Ok(new { Completion = result });
    }
}

public class PromptRequest
{
    public string Prompt { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

6.5 Configure Your .NET Core App

In Program.cs or Startup.cs:

var builder = WebApplication.CreateBuilder(args);

// Register HttpClient
builder.Services.AddHttpClient("FineTunedModel", client =>
{
    client.BaseAddress = new Uri("https://your-model-endpoint/");
});

// Register the ModelConsumerService
builder.Services.AddTransient<ModelConsumerService>();

builder.Services.AddControllers();
var app = builder.Build();

app.MapControllers();
app.Run();
Enter fullscreen mode Exit fullscreen mode

7. Deploying to Azure

  1. Azure App Service:
    • Easiest route for many .NET Core apps.
    • Create a new Web App from the Azure Portal or via the CLI.
   az webapp create --resource-group LLMFinetuneRG --plan MyPlan --name FineTunedLLMWebApp
Enter fullscreen mode Exit fullscreen mode
  1. Azure Functions (Optional):

    • Ideal for running serverless, event-driven logic if your usage is intermittent or scheduled.
  2. Azure Kubernetes Service (AKS) (Advanced):

    • Ideal for large-scale deployments.
    • Containerize your app using Docker and push it to Azure Container Registry (ACR).

8. Best Practices

  1. Data Privacy: Ensure you handle sensitive or proprietary data responsibly, especially during model training.
  2. Monitoring & Logging: Integrate Azure Application Insights to monitor performance, track usage, and detect anomalies.
  3. Security: Use Azure Key Vault to store secrets (API keys, connection strings).
  4. Model Versioning: Keep track of different fine-tuned versions in Azure ML; roll back to older versions if needed.
  5. Prompt Engineering: Refine your prompts to get the best results from your fine-tuned model.

9. Conclusion

Fine-tuning LLMs with Python and Azure Machine Learning, then integrating them into a .NET Core application, allows you to build powerful domain-specific AI solutions. This combination is an excellent choice for organizations seeking to leverage the best of Python’s AI ecosystem and .NET’s enterprise capabilities, all backed by the scalability of Azure.

With careful planning around security, data governance, and DevOps, you can roll out a production-ready solution that meets real-world needs—delivering accurate, domain-specific language capabilities in a robust, maintainable framework.

python Article's
30 articles in total
Favicon
Contribute to `real-to-sim-to-real` in SmilingRobo Open-Source Sprint!
Favicon
A Complete Beginner’s Guide to Python Training Course
Favicon
Solving Circular Dependencies: A Journey to Better Architecture
Favicon
How to Build a Google Trends Scraper | Scraping Browser Guide 2025
Favicon
Protect Your APIs from Abuse with FastAPI and Redis
Favicon
How to solve the problem of limited access speed of crawlers
Favicon
The Complete Introduction to Time Series Classification in Python
Favicon
Working with Files Asynchronously in Python using aiofiles and asyncio
Favicon
Simplify Python-Informix Connections with wbjdbc
Favicon
Train LLM From Scratch
Favicon
5 Advanced Python Web Crawling Techniques for Efficient Data Collection
Favicon
Vyper - Write your First Python Smart Contract (Series)
Favicon
Extract structured data using Python's advanced techniques
Favicon
🚀 Excited to share my latest project: Local LLM Chat Application!
Favicon
Build an AI code review assistant with v0.dev, litellm and Agenta
Favicon
Building a BLE Real-Time macOS Menu Bar App
Favicon
I am a beginner in Python programming and I want to develop my skills.
Favicon
FastHTML and Heroku
Favicon
Making a Todo API with FastAPI and MongoDB
Favicon
Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure
Favicon
GraphDB for CMDB
Favicon
Exporting Mac OSX Book Highlights into an Obsidian Vault or Markdown Files
Favicon
Getting Started with Python: Installing Python and Writing Your First Program (Day 2 of 100 Days of Python)
Favicon
Typed integers in Rust for safer Python bytecode compilation
Favicon
How do I measure the execution time of Celery tasks?
Favicon
A Developer's Guide to Odoo CRM Customization
Favicon
Build Code-Action AI Agents with freeact
Favicon
The Core of FastAPI: A Deep Dive into Starlette 🌟🌟🌟
Favicon
Pythonizing JavaScript
Favicon
🔧 Generative AI Developer Week 2 - Day 3: Data Preprocessing

Featured ones: