CoolFace
Modelpublic

Nextorage/Llama-3.1-Swallow-8B-LawInstruct-NLI-LoRA

sourceHugging Facellama3updated 4mo agoView on Hugging Face
0likes8downloads
Model Card

Llama-3.1-Swallow-8B LawInstruct NLI LoRA (T2T)

概要 / Overview

Nextorage AiDAPTIV+ プラットフォーム上で Llama-3.1-Swallow-8B に LoRA ファインチューニングを施した、英語法律文書 NLI(含意関係分類)モデルです。

Accuracy 88.0%、Macro F1 0.827 を達成し、GPT-4o few_shot(Acc 86.0%)を上回りました。LoRA アダプターサイズは約 75MB で、フルモデル(~16GB)に対して約 0.5% のパラメータ更新で実現されています。

A LoRA fine-tuned version of Llama-3.1-Swallow-8B for legal NLI (Natural Language Inference) classification, trained on AiDAPTIV+ platform by Nextorage. Achieves Accuracy 88.0% and Macro F1 0.827 — surpassing GPT-4o few_shot (86.0%) on the same test set, with only a ~75MB LoRA adapter.


性能 / Performance

テストセット: LawInstruct NLI 英語版 100件(entailment/contradiction/neutral の3値分類)

モデルAccuracyMacro F1
Llama-3.1-Swallow-8B(未FT ベースライン)71.0%0.655
Claude Sonnet (zero_shot)80.0%0.738
Claude Sonnet (few_shot)85.0%0.824
GPT-4o (zero_shot)84.0%0.819
GPT-4o (few_shot)86.0%0.840
本モデル(LoRA, 1,565件学習)88.0%0.827

クラス別精度

クラスPrecisionRecallF1Support
entailment0.6670.8420.74419
contradiction0.9330.6670.77821
neutral0.9510.9670.95960
注: contradiction クラスの Recall がベースライン 0.476 から 0.667 へ大幅改善(FullFT では 0.857)。

使い方 / Usage

LoRA アダプターとして使用(推奨)

python
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# ベースモデルのロード(要: tokyotech-llm/Llama-3.1-Swallow-8B)
base_model_id = "tokyotech-llm/Llama-3.1-Swallow-8B"
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
base_model = AutoModelForCausalLM.from_pretrained(
    base_model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

# LoRA アダプターのロード
model = PeftModel.from_pretrained(
    base_model,
    "Nextorage/Llama-3.1-Swallow-8B-LawInstruct-NLI-LoRA"
)

SYSTEM_PROMPT = """You are an expert AI assistant specializing in analyzing logical relationships in legal documents.
Classify the relationship between two legal case passages as one of:
  entailment   – Passage 1 logically implies Passage 2
  contradiction – Passage 1 and Passage 2 are logically contradictory
  neutral      – No logical entailment or contradiction between the passages

Respond with exactly one word: entailment, contradiction, or neutral."""

question = """Analyze these two passages from a US legal case:
do they entail, contradict, or remain neutral to each other?

Passage 1: We agree with the trial court that under the clear terms of
           the contract, the seller's obligation was discharged upon
           delivery of the goods.

Passage 2: The appellate court held that the seller retained liability
           for latent defects regardless of the delivery terms agreed
           upon by the parties."""

messages = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": question},
]
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
output = model.generate(input_ids, max_new_tokens=10)
print(tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True))
# 期待出力: contradiction

推論スクリプト

bash
# 依存パッケージのインストール
pip install transformers peft torch

# 評価・推論の実行
python run_inference.py \
    --model_path /path/to/this/adapter \
    --original_model_path /path/to/Llama-3.1-Swallow-8B \
    --test_data /path/to/nli_test.json \
    --output_dir ./results

学習設定 / Training Configuration

パラメータ値
ベースモデルtokyotech-llm/Llama-3.1-Swallow-8B
手法LoRA
LoRA rank (r)16
LoRA alpha32
LoRA dropout0.0
LoRA target modulesqproj, vproj, oproj, gateproj, upproj, downproj
学習データLawInstruct NLI 英語版(1,565件)
Learning Rate2e-4
LR Schedulercosine
EarlyStoppatience=3, Epoch 1 で停止
Batch Size (effective)32 (perdevice=2, gradaccum=16)
Max Seq Length2048
Weight Decay0.01
プラットフォームNextorage AiDAPTIV+ (phisonai2)

データセット詳細

  • —元データ: LawInstruct(全 20,000 件)から NLI サブセットを抽出
  • —英語フィルタ(非ASCII比率 > 10% を除外)後、3値ラベル(entailment/contradiction/neutral)に絞り込み
  • —層別分割(stratified split, seed=42): Train 1,565件 / Val 283件 / Test 100件

制限事項 / Limitations

  • —英語法律文書の NLI タスク(entailment / contradiction / neutral の 3 値分類)に特化
  • —使用には ベースモデル `tokyotech-llm/Llama-3.1-Swallow-8B` が別途必要
  • —テストセット中約 12% に多言語(ルーマニア語・フィンランド語等)の指示文が残存しており、これらのサンプルでは精度が低下する可能性がある
  • —日本語翻訳版データでの学習・評価では Accuracy が大幅に低下(最良 66%)するため、英語法律文書での使用を推奨

ライセンス / License

本モデルは Llama 3.1 Community License に基づいています。 商用利用は条件付きで許可されています。詳細はライセンス全文を参照してください。


引用 / Citation

bibtex
@misc{nextorage-lawinstruct-nli-lora-2026,
  title        = {Llama-3.1-Swallow-8B LawInstruct NLI LoRA},
  author       = {Nextorage Inc.},
  year         = {2026},
  howpublished = {\url{https://huggingface.co/Nextorage/Llama-3.1-Swallow-8B-LawInstruct-NLI-LoRA}},
  note         = {LoRA fine-tuned model for legal NLI classification on AiDAPTIV+ platform}
}