基于
BAAI/bge-m3 微调的
Mathlib4 API 检索模型,专为 Lean 4 形式化证明场景设计。该模型能够根据当前证明目标(goal type)和数学关键词,从 Mathlib4 的 32.6 万条声明中检索最相关的引理/定理/定义。
1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer("YuxuanGong/lean-RAG")
4
5# 查询:Lean goal + 关键词
6query = "x % y < y nat mod lt"
7# 候选:Mathlib4 声明(从 declarations.jsonl 构建的 FAISS 索引)
8passages = [
9 "Keywords: nat mod lt theorem | Name: Nat.mod_lt | Kind: theorem | ...",
10 "Keywords: nat mod add theorem | Name: Nat.ModEq.add | Kind: theorem | ...",
11 # ... 32.6 万条
12]
13
14query_emb = model.encode(query)
15passage_embs = model.encode(passages)
16similarities = model.similarity(query_emb, passage_embs)
1# Skill 中的 mathlib_lookup.sh (Route 4) 使用本模型:
2DENSE_QUERY="${GOAL_TYPE} ${KEYWORDS_TEXT}"
3# 然后调用 search.py 用本模型编码查询 -> FAISS 搜索
SentenceTransformer(
(0): Transformer(XLMRobertaModel, task='feature-extraction')
(1): Pooling(cls pooling, 1024d)
(2): Normalize()
)
1@misc{gong2025mathlib-rag,
2 title={Mathlib-RAG: Fine-tuned BGE-M3 for Lean 4 Mathlib API Retrieval},
3 author={Gong, Yuxuan},
4 year={2025},
5 publisher={Hugging Face},
6 howpublished={\url{https://huggingface.co/YuxuanGong/lean-RAG}}
7}
1@article{chen2024bge,
2 title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
3 author={Chen, Jianlv and Xiao, Shitao and Zhang, Peitian and others},
4 journal={arXiv preprint arXiv:2402.03216},
5 year={2024}
6}