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.

llm Article's
30 articles in total
Favicon
Streaming input and output using WebSockets
Favicon
Create an agent and build a deployable notebook from it in watsonx.ai — Part 2
Favicon
How RAG works? Retrieval Augmented Generation Explained
Favicon
Create an agent and build a Notebook from it in watsonx.ai — Part 1
Favicon
Using LLM to translate in Microsoft Word locally
Favicon
AI Workflows vs AI Agents — What’s the Difference?
Favicon
Using Mistral NeMo to summarize 10+ pages in Microsoft Word locally
Favicon
Using Cloudflare Tunnel to public Ollama on the Internet
Favicon
Integrating Azure OpenAI with .NET Applications Using Microsoft.Extensions.AI
Favicon
Best Large Language Model (LLM) of 2024: ChatGPT, Gemini, and Copilot — A Comprehensive Comparison
Favicon
Empowering Your Team with Phi-4 in Microsoft Word within Your Intranet
Favicon
A Magic Line That Cuts Your LLM Latency by >40% on Amazon Bedrock
Favicon
Build an AI code review assistant with v0.dev, litellm and Agenta
Favicon
Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure
Favicon
How are LLMs Transforming Search Algorithms, and How Can You Adapt Your SEO Strategy?
Favicon
Using OpenLLM in Microsoft Word locally
Favicon
Using Xinference in Microsoft Word locally
Favicon
Using Ollama in Microsoft Word locally
Favicon
Using LocalAI in Microsoft Word locally
Favicon
Using llama.cpp in Microsoft Word locally
Favicon
Using LM Studio in Microsoft Word locally
Favicon
Using AnythingLLM in Microsoft Word locally
Favicon
Using LiteLLM in Microsoft Word, locally or remotely
Favicon
Evaluation as a Business Imperative: The Survival Guide for Large Model Application Development
Favicon
Truth Tables: Foundations and Applications in Logic and Neural Networks
Favicon
I gem-packed this with how I'm leveraging LLMs in my workflow!
Favicon
Binary embedding: shrink vector storage by 95%
Favicon
Using Phi-4 in Microsoft Word locally
Favicon
Converting documents for LLM processing — A modern approach
Favicon
Atrium.st - Vercel for AI agents

Featured ones: