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. です。



