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.



