Large Language Models (LLMs) often lack the specific knowledge and structural formatting required for enterprise legal applications. This thesis compares different architectural solutions to adapt open-source LLMs to the Italian energy trading regulations domain. The primary scientific question addresses whether retrieving external documents or updating internal parameters provides a safer and more accurate legal assistant. The research conducts an ablation study to compare four configurations: a Baseline model, Retrieval-Augmented Generation (RAG), Fine-Tuning (FT), and a Hybrid (RAG+FT) approach. The methodology implements a hybrid computing infrastructure. It utilizes a commercial cloud provider environment for RAG orchestration and centralized evaluation. Simultaneously, it leverages a supercomputer infrastructure to execute parameter-efficient fine-tuning. A synthetic Question-Answer (Q\&A) dataset, generated by a model, simulates interactions with a corporate domain expert. The evaluation framework employs an LLM-as-a-judge methodology and a static Entity Recall metric to assess performances. The experiments demonstrate distinct operational trade-offs. RAG successfully grounds the models in factual reality and significantly reduces hallucinations on specific legal queries. FT effectively adapts the stylistic output, teaching the models to follow strict formatting rules and adopt a professional persona. However, integrating both techniques exposes a critical safety tax. Classic fine-tuned models develop an over-reliance on their parametric memory. They frequently ignore the explicitly provided RAG context to force an answer. Lastly, the analysis reveals that massive models seamlessly process large context windows, while smaller architectures struggle. These findings emphasize that organizations must carefully balance factual rigidity and stylistic adherence. The results provide a practical framework for deploying cost-effective, specialized enterprise legal assistants.
I Large Language Models (LLMs) spesso non possiedono la conoscenza specifica e il linguaggio tecnico per applicazioni legali. Questa tesi confronta soluzioni architetturali per adattare LLM open-source al dominio delle normative italiane sul commercio di energia. L'analisi principale ha lo scopo di capire se sia meglio utilizzare documenti esterni o aggiornare i parametri interni per ottenere un assistente legale più sicuro e preciso. La ricerca conduce uno studio per confrontare quattro configurazioni: un modello non modificato, la Retrieval-Augmented Generation (RAG), il Fine-Tuning (FT) e un approccio ibrido (RAG+FT). Lo studio utilizza un ambiente di cloud computing commerciale per gestire la RAG e la valutazione delle risposte. In aggiunta, sfrutta un'infrastruttura di supercalcolo per eseguire un fine-tuning dei parametri. Un dataset sintetico di Domande-Risposte (Q\&A), generato da un modello, simula le interazioni con un esperto di dominio aziendale. Il framework di valutazione impiega una metodologia LLM-as-a-judge e una metrica statica di Entity Recall per valutare le prestazioni. Gli esperimenti dimostrano chiari compromessi operativi. La RAG àncora con successo i modelli alla realtà fattuale e riduce significativamente le allucinazioni su specifiche domande legali. Il FT adatta efficacemente lo stile delle risposte, insegnando ai modelli a seguire regole di formattazione rigorose e ad adottare un tono professionale. Tuttavia, l'integrazione di entrambe le tecniche espone a un problema in termini di sicurezza. I classici modelli sottoposti a fine-tuning sviluppano un'eccessiva dipendenza dalla loro memoria parametrica. Spesso ignorano il contesto RAG fornito esplicitamente per forzare una risposta. Infine, l'analisi rivela che i modelli massivi elaborano senza problemi ampie finestre di contesto, mentre le architetture più piccole faticano. Queste scoperte sottolineano che le organizzazioni devono bilanciare attentamente il rigore fattuale e l'aderenza stilistica. I risultati forniscono un framework pratico per l'implementazione di assistenti legali aziendali specializzati e vantaggiosi in termini di costi.
Studying RAG architectures and fine-tuned LLMs for the energy trading regulations domain
PELLEGRINI, FRANCESCO
2025/2026
Abstract
Large Language Models (LLMs) often lack the specific knowledge and structural formatting required for enterprise legal applications. This thesis compares different architectural solutions to adapt open-source LLMs to the Italian energy trading regulations domain. The primary scientific question addresses whether retrieving external documents or updating internal parameters provides a safer and more accurate legal assistant. The research conducts an ablation study to compare four configurations: a Baseline model, Retrieval-Augmented Generation (RAG), Fine-Tuning (FT), and a Hybrid (RAG+FT) approach. The methodology implements a hybrid computing infrastructure. It utilizes a commercial cloud provider environment for RAG orchestration and centralized evaluation. Simultaneously, it leverages a supercomputer infrastructure to execute parameter-efficient fine-tuning. A synthetic Question-Answer (Q\&A) dataset, generated by a model, simulates interactions with a corporate domain expert. The evaluation framework employs an LLM-as-a-judge methodology and a static Entity Recall metric to assess performances. The experiments demonstrate distinct operational trade-offs. RAG successfully grounds the models in factual reality and significantly reduces hallucinations on specific legal queries. FT effectively adapts the stylistic output, teaching the models to follow strict formatting rules and adopt a professional persona. However, integrating both techniques exposes a critical safety tax. Classic fine-tuned models develop an over-reliance on their parametric memory. They frequently ignore the explicitly provided RAG context to force an answer. Lastly, the analysis reveals that massive models seamlessly process large context windows, while smaller architectures struggle. These findings emphasize that organizations must carefully balance factual rigidity and stylistic adherence. The results provide a practical framework for deploying cost-effective, specialized enterprise legal assistants.| File | Dimensione | Formato | |
|---|---|---|---|
|
Tesi_Pellegrini_Francesco_New.pdf
accessibile in internet per tutti
Descrizione: Tesi Pellegrini Francesco final version
Dimensione
732.94 kB
Formato
Adobe PDF
|
732.94 kB | Adobe PDF | Visualizza/Apri |
I documenti in POLITesi sono protetti da copyright e tutti i diritti sono riservati, salvo diversa indicazione.
https://hdl.handle.net/10589/251781