-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rag langchain demo with retrievals
- Loading branch information
1 parent
556aa96
commit 0429d4e
Showing
29 changed files
with
606 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import torch.nn as nn | ||
from datasets import load_dataset | ||
from transformers import ( | ||
AdamW, | ||
AutoTokenizer, | ||
TrainingArguments, | ||
get_linear_schedule_with_warmup, | ||
) | ||
|
||
from src.retrievals import ( | ||
AutoModelForEmbedding, | ||
PairCollator, | ||
RetrievalTrainer, | ||
TripletCollator, | ||
) | ||
from src.retrievals.losses import ( | ||
ArcFaceAdaptiveMarginLoss, | ||
InfoNCE, | ||
SimCSE, | ||
TripletLoss, | ||
) | ||
|
||
model_name_or_path: str = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2" | ||
batch_size: int = 128 | ||
epochs: int = 3 | ||
|
||
train_dataset = load_dataset('shibing624/nli_zh', 'STS-B')['train'] | ||
train_dataset = train_dataset.rename_columns({'sentence1': 'query', 'sentence2': 'positive'}) | ||
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=False) | ||
model = AutoModelForEmbedding.from_pretrained(model_name_or_path, pooling_method="cls") | ||
model = model.set_train_type('pairwise') | ||
|
||
optimizer = AdamW(model.parameters(), lr=5e-5) | ||
num_train_steps = int(len(train_dataset) / batch_size * epochs) | ||
scheduler = get_linear_schedule_with_warmup( | ||
optimizer, num_warmup_steps=0.05 * num_train_steps, num_training_steps=num_train_steps | ||
) | ||
|
||
training_arguments = TrainingArguments( | ||
output_dir='./checkpoints', | ||
num_train_epochs=epochs, | ||
per_device_train_batch_size=batch_size, | ||
remove_unused_columns=False, | ||
) | ||
trainer = RetrievalTrainer( | ||
model=model, | ||
args=training_arguments, | ||
train_dataset=train_dataset, | ||
data_collator=PairCollator(tokenizer, max_length=512), | ||
loss_fn=InfoNCE(nn.CrossEntropyLoss(label_smoothing=0.05)), | ||
) | ||
trainer.optimizer = optimizer | ||
trainer.scheduler = scheduler | ||
trainer.train() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.