---
title: EmbeddingGemma-300M-LawVault
canonical_url: "https://www.modelscope.cn/models/ByronLeeee/EmbeddingGemma-300M-LawVault"
md_url: "https://www.modelscope.cn/models/ByronLeeee/EmbeddingGemma-300M-LawVault.md"
repository: ByronLeeee/EmbeddingGemma-300M-LawVault
chinese_name: EmbeddingGemma-300M-LawVault
last_updated: 2025-12-06
license: "Apache License 2.0"
model_type:
  - gemma3_text
architectures:
  - Gemma3TextModel
base_model:
  - google/embeddinggemma-300m
base_model_relation: finetune
parameters: 4.7M
tensor_type:
  - F32
library_name:
  - sentence-transformers
  - safetensors
language:
  - zh
downloads: 88
stars: 2
tags:
  - sentence-transformers
  - sentence-similarity
  - feature-extraction
  - dense
  - generated_from_trainer
  - "dataset_size:65783"
  - "loss:MatryoshkaLoss"
  - "loss:MultipleNegativesRankingLoss"
---

# EmbeddingGemma-300M-LawVault

> EmbeddingGemma-300M-LawVault - ByronLeeee 在 ModelScope 开源的模型。EmbeddingGemma-300M-LawVault (Chinese Legal RAG)

ByronLeeee/EmbeddingGemma-300M-LawVault 是 ModelScope 魔搭社区上的 4.7M 参数机器学习模型，采用 Apache License 2.0 许可，基于 google/embeddinggemma-300m 构建。

- **Repository**: ByronLeeee/EmbeddingGemma-300M-LawVault
- **License**: Apache License 2.0
- **Parameters**: 4.7M
- **Base model**: google/embeddinggemma-300m
- **Tags**: sentence-transformers, sentence-similarity, feature-extraction, dense, generated_from_trainer, dataset_size:65783, loss:MatryoshkaLoss, loss:MultipleNegativesRankingLoss
- **Downloads**: 88
- **Stars**: 2
- **Last updated**: 2025-12-06

Source: https://www.modelscope.cn/models/ByronLeeee/EmbeddingGemma-300M-LawVault

---

# EmbeddingGemma-300M-LawVault (Chinese Legal RAG)

## 📖 模型简介 (Model Introduction)

**EmbeddingGemma-300M-LawVault** 是一个专为**中国法律领域 RAG (检索增强生成)** 场景微调的高性能向量模型。

该模型基于 Google 的 [embeddinggemma-300m](https://huggingface.co/google/embeddinggemma-300m) 进行全量参数微调。通过构建超过6万条高质量的 `(Query, Positive, Hard Negative)` 三元组[数据](https://www.modelscope.cn/datasets/ByronLeeee/CN-Law-Query-Retrieval-Dataset)，并结合 **MultipleNegativesRankingLoss** 与 **MatryoshkaLoss** 进行训练，该模型在法律条文检索、口语化法律咨询理解以及抗干扰能力上相比原始模型有显著提升。

### 核心亮点
*   **针对性强**：专门解决了通用模型无法区分“国家法律”与“地方法规/行政规章”相似文本的痛点。
*   **抗干扰能力**：在训练中通过引入“同源难负例”，以原模型基于同一问题的错误召回法条作为难负例进行训练，模型能精准识别并排除内容相似但出处错误的干扰项。
*   **支持口语化**：训练集包含了由 LLM 生成的真实用户口语化提问，弥补了法言法语与日常用语之间的语义鸿沟。
*   **弹性向量 (Matryoshka)**：支持输出不同维度的向量 (768, 512, 256, 128)，在保持精度的同时大幅降低向量库存储成本。

## 📊 评测表现 (Evaluation)

基于真实法律场景构建的测试集（包含 120 条由 Deepseek V3.2 重新生成的未见过的口语化法律咨询问题）进行端到端 RAG 检索测试，结果如下：

| 指标 (Metric) | 原始模型 (Base) | **微调模型 (Ours)** | 提升 (Improvement) |
| :--- | :---: | :---: | :---: |
| **Hit Rate @ 10** (前10名命中率) | 85.0% | **98.0%** | 显著减少了“查不到”的情况 |
| **Top-1 Accuracy** (首位命中率) | 58.0% | **92.0%** | **巨大提升 (+34%)**，绝大多数问题首条即答案 |
| **MRR @ 10** (平均倒数排名) | 0.78 | **0.96** | 排序质量极高 |

*注：测试环境为 LanceDB 向量库，检索范围覆盖基于全量中国法律法规切片的数据库。*

### 真实案例对比 (Case Study)

| 用户问题 (Query) | 原始模型排名 | **微调模型排名** |
| :--- | :---: | :---: |
| "省里的文物局能不能直接把我们这儿出土的文物调走？" | ❌ 未召回 (10+) | ✅ **第 1 名** |
| "商家在制定商品价格时，法律上要求他们必须做到哪几点？" | ❌ 未召回 (10+) | ✅ **第 1 名** |
| "如果因为修大型水电站征收了土地，补偿标准怎么算？" | 第 2 名 | ✅ **第 1 名** |
| "政府在资金上怎么支持乡村振兴？" | 第 6 名 | ✅ **第 1 名** |

## 🚀 快速使用 (Usage)

### 安装依赖
```bash
pip install -U sentence-transformers
```

### 加载模型
```python
from sentence_transformers import SentenceTransformer

# 加载模型
model_path = "ByronLeeee/EmbeddingGemma-300M-LawVault"
model = SentenceTransformer(model_path, trust_remote_code=True)

# 1. 定义查询 (Query)
query = "抢劫罪一般判几年？"

# 2. 定义文档 (Documents) - 推荐格式：title: {法规名} | text: {内容}
documents = [
    "title: 中华人民共和国刑法 第二百六十三条 | text: 以暴力、胁迫或者其他方法抢劫公私财物的，处三年以上十年以下有期徒刑，并处罚金...",
    "title: 中华人民共和国刑法 第二百六十七条 | text: 抢夺公私财物，数额较大的，或者多次抢夺的，处三年以下有期徒刑、拘役或者管制...",
    "title: 陕西省专利条例 第二十四条 | text: 负责专利执法的部门...可以查封或者扣押。"
]

# 3. 编码
query_vec = model.encode(query)
doc_vecs = model.encode(documents)

# 4. 计算相似度
similarities = model.similarity(query_vec, doc_vecs)
print(similarities)
```

---

## Training Details (Generated by Trainer)

### Dataset
*   **Size**: 65,783 training triplets (Anchor, Positive, Hard Negative)
*   **Source**: Chinese Laws & Regulations (Civil, Criminal, Administrative, etc.)

### Training Hyperparameters
*   **Batch Size**: 24 (Effective Batch Size = 144 with Gradient Accumulation)
*   **Learning Rate**: 2e-05
*   **Epochs**: 3
*   **Precision**: bf16 (BFloat16)
*   **Gradient Accumulation**: 6 steps
*   **Max Sequence Length**: 1024 tokens

### Loss Function
**MatryoshkaLoss** wrapping **MultipleNegativesRankingLoss**:
```json
{
    "matryoshka_dims": [768, 512, 256, 128],
    "matryoshka_weights": [1, 1, 1, 1]
}
```

### Training Logs
<details><summary>Click to expand detailed logs</summary>

| Epoch  | Step | Training Loss |
|:------:|:----:|:-------------:|
| 0.0022 | 1    | 3.5148        |
| ...    | ...  | ...           |
| 1.0    | 457  | 0.2123        |
| 2.0    | 914  | 0.0749        |
| 3.0    | 1371 | 0.0369        |

</details>

### Framework Versions
- Python: 3.13.1
- Sentence Transformers: 5.1.2
- Transformers: 4.57.1
- PyTorch: 2.9.1+cu130
- Accelerate: 1.12.0
- Datasets: 4.4.1
- Tokenizers: 0.22.1

## Citation

If you use this model, please cite the following:

```bibtex
@inproceedings{reimers-2019-sentence-bert,
    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
    author = "Reimers, Nils and Gurevych, Iryna",
    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
    month = "11",
    year = "2019",
    publisher = "Association for Computational Linguistics",
    url = "https://arxiv.org/abs/1908.10084",
}
```
