Llama3.1 8B Legal ThaiCCL Combine
模型概述
模型特點
模型能力
使用案例
🚀 Llama-3.1-Legal-ThaiCCL-8B
Llama-3.1-Legal-ThaiCCL-8B 是一個基於 Llama-3.1-8B 構建的大語言模型,旨在回答泰語法律問題。它使用 WangchanX 微調管道 在 WangchanX 泰語法律數據集 上進行了全量微調。該模型旨在與支持的檢索增強生成(RAG)系統一起使用,該系統會查詢相關的支持性法律文件,以便模型在回答問題時進行參考。
🚀 快速開始
本模型可用於回答泰語法律問題,使用時需結合支持的檢索增強生成(RAG)系統,該系統會為模型查詢相關的法律文件作為參考。
✨ 主要特性
- 專業法律問答:專門針對泰語法律問題進行訓練,能夠提供準確的法律解答。
- 結合 RAG 系統:藉助檢索增強生成(RAG)系統,查詢相關法律文件,使回答更具依據。
- 友好交互風格:以友好、清晰和易懂的方式提供法律建議,讓用戶感覺像在諮詢一位值得信賴的朋友。
📦 安裝指南
文檔未提供安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
from transformers import pipeline
import torch
EN_QA_TEMPLATE = "Given the user's query in the context of Thai legal matters, the RAG system retrieves the top_n related documents. From these documents, it's crucial to identify and utilize only the most relevant ones to craft an accurate and informative response.Context information is below.\n\n---------------------\nContext: Thai legal domain\nQuery: {query_str}\nRetrieved Documents: {context_str}\n---------------------\n\n Using the provided context information and the list of retrieved documents, you will focus on selecting the documents that are most relevant to the user's query. This selection process involves evaluating the content of each document for its pertinency to the query, ensuring that the response is based on accurate and contextually appropriate information.Based on the selected documents, you will synthesize a response that addresses the user's query, drawing directly from the content of these documents to provide a precise, legally informed answer.You must answer in Thai.\nAnswer:"
EN_SYSTEM_PROMPT_STR = """You are a legal assistant named Sommai (สมหมาย in Thai). You provide legal advice in a friendly, clear, and approachable manner. When answering questions, you reference the relevant law sections, including the name of the act or code they are from. You explain what these sections entail, including any associated punishments, fees, or obligations. Your tone is polite yet informal, making users feel comfortable, like consulting a trusted friend. If a question falls outside your knowledge, you must respond with the exact phrase: 'สมหมายไม่สามารถตอบคำถามนี้ได้ครับ'. You avoid making up information and guide users based on accurate legal references relevant to their situation. Where applicable, you provide practical advice, such as preparing documents, seeking medical attention, or contacting authorities. If asked about past Supreme Court judgments, you must state that you do not have information on those judgments at this time."""
query = "การร้องขอให้ศาลสั่งให้บุคคลเป็นคนไร้ความสามารถมีหลักเกณฑ์การพิจารณาอย่างไร"
context = """ประมวลกฎหมายแพ่งและพาณิชย์ มาตรา 33 ในคดีที่มีการร้องขอให้ศาลสั่งให้บุคคลใดเป็นคนไร้ความสามารถเพราะวิกลจริต ถ้าทางพิจารณาได้ความว่าบุคคลนั้นไม่วิกลจริต แต่มีจิตฟั่นเฟือนไม่สมประกอบ เมื่อศาลเห็นสมควรหรือเมื่อมีคำขอของคู่ความหรือของบุคคลตามที่ระบุไว้ในมาตรา 28 ศาลอาจสั่งให้บุคคลนั้นเป็นคนเสมือนไร้ความสามารถก็ได้ หรือในคดีที่มีการร้องขอให้ศาลสั่งให้บุคคลใดเป็นคนเสมือนไร้ความสามารถเพราะมีจิตฟั่นเฟือนไม่สมประกอบ ถ้าทางพิจารณาได้ความว่าบุคคลนั้นวิกลจริต เมื่อมีคำขอของคู่ความหรือของบุคคลตามที่ระบุไว้ในมาตรา 28 ศาลอาจสั่งให้บุคคลนั้นเป็นคนไร้ความสามารถก็ได้"""
model_id = "airesearch/LLaMa3.1-8B-Legal-ThaiCCL-Combine"
pipeline = pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
sample = [
{"role": "system", "content": EN_SYSTEM_PROMPT_STR},
{"role": "user", "content": EN_QA_TEMPLATE.format(context_str=context, query_str=query)},
]
prompt = pipeline.tokenizer.apply_chat_template(sample,
tokenize=False,
add_generation_prompt=True)
outputs = pipeline(
prompt,
max_new_tokens = 512,
eos_token_id = terminators,
do_sample = True,
temperature = 0.6,
top_p = 0.9
)
print(outputs[0]["generated_text"][-1])
高級用法
EN_QA_TEMPLATE = "Given the user's query in the context of Thai legal matters, the RAG system retrieves the top_n related documents. From these documents, it's crucial to identify and utilize only the most relevant ones to craft an accurate and informative response.Context information is below.\n\n---------------------\nContext: Thai legal domain\nQuery: {query_str}\nRetrieved Documents: {context_str}\n---------------------\n\n Using the provided context information and the list of retrieved documents, you will focus on selecting the documents that are most relevant to the user's query. This selection process involves evaluating the content of each document for its pertinency to the query, ensuring that the response is based on accurate and contextually appropriate information.Based on the selected documents, you will synthesize a response that addresses the user's query, drawing directly from the content of these documents to provide a precise, legally informed answer.You must answer in Thai.\nAnswer:"
EN_SYSTEM_PROMPT_STR = """You are a legal assistant named Sommai (สมหมาย in Thai). You provide legal advice in a friendly, clear, and approachable manner. When answering questions, you reference the relevant law sections, including the name of the act or code they are from. You explain what these sections entail, including any associated punishments, fees, or obligations. Your tone is polite yet informal, making users feel comfortable, like consulting a trusted friend. If a question falls outside your knowledge, you must respond with the exact phrase: 'สมหมายไม่สามารถตอบคำถามนี้ได้ครับ'. You avoid making up information and guide users based on accurate legal references relevant to their situation. Where applicable, you provide practical advice, such as preparing documents, seeking medical attention, or contacting authorities. If asked about past Supreme Court judgments, you must state that you do not have information on those judgments at this time."""
def format(example):
if "คำตอบ: " in example["positive_answer"]:
example["positive_answer"] = example["positive_answer"].replace("คำตอบ: ", "")
if example['positive_contexts']:
context = ''.join([v['text'] for v in example['positive_contexts'][:5]])
message = [
{"content": EN_SYSTEM_PROMPT_STR, "role": "system"},
{"content": EN_QA_TEMPLATE.format(query_str=example['question'], context_str=context), "role": "user"},
]
else:
message = [
{"content": EN_SYSTEM_PROMPT_STR, "role": "system"},
{"content": EN_QA_TEMPLATE.format(query_str=example['question'], context_str=" "), "role": "user"},
]
return dict(messages=message)
dataset = dataset.map(format, batched=False)
📚 詳細文檔
模型描述
屬性 | 詳情 |
---|---|
基礎模型 | Meta Llama 3.1 8B |
訓練倉庫 | WangchanX 微調管道 |
訓練數據集 | WangchanX 泰語法律數據集 |
許可證 | Meta 的 Llama 3.1 社區許可協議 |
訓練數據
該模型在 WangchanX 法律泰語 CCL RAG 數據集 上進行訓練,這是一個泰語法律問答數據集,使用 RAG 系統根據問題查詢相關的支持性法律數據集,供大語言模型在回答中參考。有關該數據集的創建方式的更多信息,請參考此 博客。
為了模擬現實世界的使用場景,在訓練過程中,我們將正上下文和負上下文(如果可用)都納入了提示中。我們發現,這樣訓練出的模型對於 RAG 系統傳入包含無關上下文和正確參考上下文的情況更具魯棒性(有關結果,請參閱 評估 部分)。
提示格式
我們建議在使用提供的權重時使用相同的聊天模板(系統提示以及上下文、查詢和檢索到的文檔的問題模板),因為模型是使用特定的系統提示和問題模板進行訓練的。示例輸入提示如下:
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a legal assistant named Sommai (สมหมาย in Thai), you provide legal advice to users in a friendly and understandable manner. When answering questions, you specifically reference the law sections relevant to the query, including the name of the act or code they originated from, an explanation of what those sections entail, and any associated punishments or fees. Your tone is approachable and informal yet polite, making users feel as if they are seeking advice from a friend. If a question arises that does not match the information you possess, you must acknowledge your current limitations by stating this exactly sentence: 'สมหมายไม่สามารถตอบคำถามนี้ได้ครับ'. You will not fabricate information but rather guide users based on actual law sections relevant to their situation. Additionally, you offer practical advice on next steps, such as gathering required documents, seeking medical attention, or visiting a police station, as applicable. If inquired about past Supreme Court judgments, you must reply that you do not have information on those judgments yet.<|eot_id|>
<|start_header_id|>user<|end_header_id|>
Given the user's query in the context of Thai legal matters, the RAG system retrieves the top_n related documents. From these documents, it's crucial to identify and utilize only the most relevant ones to craft an accurate and informative response.
Context information is below.
---------------------
Context: Thai legal domain
Query: {question}
Retreived Documents: {retreived legal documents}
---------------------
Using the provided context information and the list of retrieved documents, you will focus on selecting the documents that are most relevant to the user's query. This selection process involves evaluating the content of each document for its pertinency to the query, ensuring that the response is based on accurate and contextually appropriate information.
Based on the selected documents, you will synthesize a response that addresses the user's query, drawing directly from the content of these documents to provide a precise, legally informed answer.
You must answer in Thai.
Answer:
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
訓練超參數
我們使用以下超參數對 Llama-3.1-8B 進行了全量微調:
- 學習率:0.0002
- 訓練批次大小:4
- 評估批次大小:4
- 隨機種子:42
- 分佈式類型:多 GPU
- 設備數量:4
- 梯度累積步數:16
- 總訓練批次大小:256
- 總評估批次大小:16
- 優化器:Adam,β=(0.9, 0.999),ε=1e-08
- 學習率調度器類型:餘弦
- 學習率調度器預熱比例:0.1
- 訓練輪數:4
總訓練時間:2:15:14.66
評估
我們基於 WangchanX 法律泰語 CCL 數據集的測試集對模型進行了測試,使用了傳統(MRC)指標和基於論文 CHIE: Generative MRC Evaluation for in-context QA with Correctness, Helpfulness, Irrelevancy, and Extraneousness Aspects 的大語言模型評判技術。
注意:LLaMa3.1-8B-Legal-ThaiCCL
僅在正上下文上進行訓練,而 LLaMa3.1-8B-Legal-ThaiCCL-Combine
在正上下文和負上下文上進行訓練。
表 1:MRC 結果
模型 | 上下文類型 | 答案類型 | ROUGE-L | 字符錯誤率 (CER) | 單詞錯誤率 (WER) | BERT 分數 | F1 分數 XQuAD | 精確匹配 XQuAD |
---|---|---|---|---|---|---|---|---|
Zero-shot LLaMa3.1-8B-Instruct | 黃金段落 | 僅正例 | 0.553 | 1.181 | 1.301 | 0.769 | 48.788 | 0.0 |
LLaMa3.1-8B-Legal-ThaiCCL | 黃金段落 | 僅正例 | 0.603 | 0.667 | 0.736 | 0.821 | 60.039 | 0.053 |
LLaMa3.1-8B-Legal-ThaiCCL-Combine | 黃金段落 | 僅正例 | 0.715 | 0.695 | 0.758 | 0.833 | 64.578 | 0.614 |
Zero-shot LLaMa3.1-70B-Instruct | 黃金段落 | 僅正例 | 0.830 | 0.768 | 0.848 | 0.830 | 61.497 | 0.0 |
Zero-shot LLaMa3.1-8B-Instruct | 檢索段落 | 僅正例 | 0.422 | 1.631 | 1.773 | 0.757 | 39.639 | 0.0 |
LLaMa3.1-8B-Legal-ThaiCCL | 檢索段落 | 僅正例 | 0.366 | 1.078 | 1.220 | 0.779 | 44.238 | 0.03 |
LLaMa3.1-8B-Legal-ThaiCCL-Combine | 檢索段落 | 僅正例 | 0.516 | 0.884 | 0.884 | 0.816 | 54.948 | 0.668 |
Zero-shot LLaMa3.1-70B-Instruct | 檢索段落 | 僅正例 | 0.616 | 0.934 | 1.020 | 0.816 | 54.930 | 0.0 |
表 2:CHIE 結果
模型 | 上下文類型 | 答案類型 | Q1: 正確性 [H] | Q2: 有用性 [H] | Q3: 無關性 [L] | Q4: 上下文外 [L] |
---|---|---|---|---|---|---|
Zero-shot LLaMa3.1-8B-Instruct | 黃金段落 | 僅正例 | 0.740 | 0.808 | 0.480 | 0.410 |
LLaMa3.1-8B-Legal-ThaiCCL | 黃金段落 | 僅正例 | 0.705 | 0.486 | 0.294 | 0.208 |
LLaMa3.1-8B-Legal-ThaiCCL-Combine | 黃金段落 | 僅正例 | 0.565 | 0.468 | 0.405 | 0.325 |
Zero-shot LLaMa3.1-70B-Instruct | 黃金段落 | 僅正例 | 0.870 | 0.658 | 0.316 | 0.247 |
Zero-shot LLaMa3.1-8B-Instruct | 檢索段落 | 僅正例 | 0.480 | 0.822 | 0.557 | 0.248 |
LLaMa3.1-8B-Legal-ThaiCCL | 檢索段落 | 僅正例 | 0.274 | 0.470 | 0.720 | 0.191 |
LLaMa3.1-8B-Legal-ThaiCCL-Combine | 檢索段落 | 僅正例 | 0.532 | 0.445 | 0.508 | 0.203 |
Zero-shot LLaMa3.1-70B-Instruct | 檢索段落 | 僅正例 | 0.748 | 0.594 | 0.364 | 0.202 |
🔧 技術細節
文檔未提供足夠詳細的技術實現細節,故跳過此章節。
📄 許可證
該模型根據 Meta 的 Llama 3.1 社區許可協議 發佈。Llama 3.1 遵循 Llama 3.1 社區許可協議,版權所有 © Meta Platforms, Inc.



