Home >Backend Development >Python Tutorial >Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure
Large-scale language models (LLMs) have received widespread attention for their ability to understand and generate human-like text. However, many organizations have unique, domain-specific data sets and vocabularies that may not be fully captured by generic models. Fine-tuning enables developers to adapt these large models to specific environments or industries, improving accuracy and relevancy.
This article explores how to fine-tune an LLM using Python, then integrate and deploy the resulting model into a .NET Core C# application, all done on Microsoft Azure for scalability and Convenience.
Domain Specificity: LLM can be fine-tuned to use industry-specific terminology, product names, or jargon.
Performance improvements: Fine-tuning often reduces errors and improves relevancy in use cases such as customer service, research, and analytics.
Reduce costs: Instead of building a model from scratch, you can customize an existing powerful LLM.
Improving efficiency: You take advantage of pre-trained weights and only adjust the final layer or parameters, thus speeding up the process.
Python for fine-tuning
.NET Core C# for integration
Azure Services
This example uses Hugging Face Transformers - one of the most widely adopted LLM fine-tuning libraries.
<code>python -m venv venv source venv/bin/activate # 在 Windows 上:venv\Scripts\activate</code>
<code>pip install torch transformers azureml-sdk</code>
<code> az group create --name LLMFinetuneRG --location eastus az ml workspace create --name LLMFinetuneWS --resource-group LLMFinetuneRG</code>
<code>import os import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer from azureml.core import Workspace, Run # 连接到 Azure ML ws = Workspace.from_config() run = Run.get_context() model_name = "gpt2" # 示例模型 tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) # 加载自定义数据集(本地或来自 Azure 存储) # 示例:Azure ML 中的文本文件或数据集 train_texts = ["此处输入您的特定领域文本..."] # 简化版 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() # 保存微调后的模型 trainer.save_model("./fine_tuned_model") tokenizer.save_pretrained("./fine_tuned_model")</code>
<code>from azureml.core.model import Model model = Model.register( workspace=ws, model_path="./fine_tuned_model", model_name="myFineTunedLLM" )</code>
At this point, your fine-tuned model is stored in Azure Machine Learning for easy access and version control.
<code>dotnet new webapi -n FineTunedLLMApi cd FineTunedLLMApi</code>
<code>dotnet add package Microsoft.Extensions.Http dotnet add package Microsoft.Azure.Storage.Blob dotnet add package Newtonsoft.Json</code>
Assume you have deployed your fine-tuned model as a web service (for example, using Azure Container Instance or a custom endpoint in Azure ML). The following code snippet calls the service to get completion results.
<code>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(); } }</code>
<code>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; } }</code>
In Program.cs or Startup.cs:
<code>var builder = WebApplication.CreateBuilder(args); // 注册 HttpClient builder.Services.AddHttpClient("FineTunedModel", client => { client.BaseAddress = new Uri("https://your-model-endpoint/"); }); // 注册 ModelConsumerService builder.Services.AddTransient<ModelConsumerService>(); builder.Services.AddControllers(); var app = builder.Build(); app.MapControllers(); app.Run();</code>
Azure App Service:
<code>python -m venv venv source venv/bin/activate # 在 Windows 上:venv\Scripts\activate</code>
Azure Function (optional):
Azure Kubernetes Service (AKS) (Advanced):
Data Privacy: Ensure responsible handling of sensitive or proprietary data, especially during model training.
Monitoring and Logging: Integrate with Azure Application Insights to monitor performance, track usage, and detect anomalies.
Security: Use Azure Key Vault to store keys (API keys, connection strings).
Model Versioning: Track different fine-tuned versions of your model in Azure ML; rollback to older versions if needed.
Hint Engineering: Refine your hints to get the best results from your fine-tuned model.
Fine-tune LLM using Python and Azure Machine Learning and then integrate them into .NET Core applications, allowing you to build powerful domain-specific AI solutions. This combination is an excellent choice for organizations looking to take advantage of Python’s AI ecosystem and the enterprise capabilities of .NET, all powered by the extensibility of Azure.
With careful planning for security, data governance, and DevOps, you can launch a production-ready solution that meets real-world needs, delivering accurate domain-specific language functionality in a powerful and easy-to-maintain framework.
The above is the detailed content of Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure. For more information, please follow other related articles on the PHP Chinese website!