🚀 LLM2Vec: 大規模言語モデルは秘密兵器となるテキストエンコーダー
LLM2Vecは、デコーダー専用の大規模言語モデル(LLM)をテキストエンコーダーに変換するシンプルな方法です。これは3つの簡単なステップで構成されています:1) 双方向注意の有効化、2) マスクされた次のトークン予測、3) 教師なし対照学習。このモデルは、最先端の性能を達成するためにさらに微調整することができます。
- リポジトリ: https://github.com/McGill-NLP/llm2vec
- 論文: https://arxiv.org/abs/2404.05961
🚀 クイックスタート
LLM2Vecを使って、デコーダー専用の大規模言語モデルをテキストエンコーダーに変換し、テキストの埋め込みや類似度計算を行うことができます。
✨ 主な機能
- デコーダー専用のLLMをテキストエンコーダーに変換する機能。
- 双方向注意、マスクされた次のトークン予測、教師なし対照学習を用いた学習方法。
- 微調整により最先端の性能を達成できる。
📦 インストール
pip install llm2vec
💻 使用例
基本的な使用法
from llm2vec import LLM2Vec
import torch
from transformers import AutoTokenizer, AutoModel, AutoConfig
from peft import PeftModel
tokenizer = AutoTokenizer.from_pretrained(
"McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntp"
)
config = AutoConfig.from_pretrained(
"McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntp", trust_remote_code=True
)
model = AutoModel.from_pretrained(
"McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntp",
trust_remote_code=True,
config=config,
torch_dtype=torch.bfloat16,
device_map="cuda" if torch.cuda.is_available() else "cpu",
)
model = PeftModel.from_pretrained(
model,
"McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntp",
)
l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
instruction = (
"Given a web search query, retrieve relevant passages that answer the query:"
)
queries = [
[instruction, "how much protein should a female eat"],
[instruction, "summit define"],
]
q_reps = l2v.encode(queries)
documents = [
"As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
"Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments.",
]
d_reps = l2v.encode(documents)
q_reps_norm = torch.nn.functional.normalize(q_reps, p=2, dim=1)
d_reps_norm = torch.nn.functional.normalize(d_reps, p=2, dim=1)
cos_sim = torch.mm(q_reps_norm, d_reps_norm.transpose(0, 1))
print(cos_sim)
"""
tensor([[0.7740, 0.5580],
[0.4845, 0.4993]])
"""
📚 ドキュメント
もしコードに関する質問があれば、Parishad (parishad.behnamghader@mila.quebec
) またはVaibhav (vaibhav.adlakha@mila.quebec
) にメールで問い合わせてください。
📄 ライセンス
このプロジェクトはMITライセンスの下でライセンスされています。