Registry / ai-ml / setfit

setfit

JSON →
library1.1.3pypypiunverified

SetFit is a Python library for efficient few-shot learning using Sentence Transformers. It enables training accurate text classifiers with minimal labeled data by finetuning pre-trained Sentence Transformer models. The library is prompt-free, fast to train, and offers multilingual support. The current version is 1.1.3, and the project maintains an active release cadence with frequent patch updates addressing compatibility and minor fixes, alongside larger feature releases.

pip install setfit
INSTALL
IMPORT
SIG · SETFIT
S
setfit
ai-mlpythonv1.1.3
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ timeout
✕ timeout
py 3.11
✕ timeout
✕ timeout
py 3.12
✕ dependency_conflict
✕ timeout
py 3.13
✕ no_wheel
2/8 runs
py 3.9
✕ build_error
✕ timeout
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SetFitModel
from setfit import SetFitModel
from setfit import SetFitModel

This quickstart demonstrates the typical workflow for training a SetFit model for text classification. It covers initializing a `SetFitModel` from the Hugging Face Hub, preparing a dataset (including simulating a few-shot scenario), configuring training parameters via `TrainingArguments`, creating and training a `SetFitTrainer`, evaluating the model, and making predictions. The example uses a small BGE model and the SST-2 dataset, sampling 8 examples per class for training.

from datasets import load_dataset from setfit import SetFitModel, SetFitTrainer, TrainingArguments, sample_dataset from sentence_transformers.losses import CosineSimilarityLoss # 1. Initialize a SetFit model model = SetFitModel.from_pretrained("BAAI/bge-small-en-v1.5") # 2. Load and prepare a dataset (e.g., sst2 for sentiment classification) dataset = load_dataset("SetFit/sst2") # Simulate few-shot regime: 8 examples per class train_dataset = sample_dataset(dataset["train"], label_column="label", num_samples=8) eval_dataset = dataset["validation"] # Optional: Map dataset columns if they are not 'text' and 'label' column_mapping = {"sentence": "text", "label": "label"} # 3. Define TrainingArguments training_args = TrainingArguments( batch_size=16, num_iterations=20, # Number of text pairs to generate for contrastive learning num_epochs=1, # Number of epochs to use for contrastive learning learning_rate=2e-5, seed=42, evaluation_strategy="epoch", save_strategy="epoch" ) # 4. Create SetFitTrainer trainer = SetFitTrainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset, loss_class=CosineSimilarityLoss, # Loss function for contrastive learning metric="accuracy", column_mapping=column_mapping ) # 5. Train the model trainer.train() # 6. Evaluate the model metrics = trainer.evaluate() print(f"Evaluation Metrics: {metrics}") # 7. Make predictions sentences = ["The movie was great!", "I didn't like the food."] predictions = model.predict(sentences) print(f"Predictions: {predictions}") # 8. Push model to Hugging Face Hub (requires `huggingface_hub` login) # trainer.push_to_hub("my-awesome-setfit-model")
Debug
Known issues
breakingSetFit v1.0.0 introduced significant API changes, particularly for the `SetFitTrainer` and how training arguments are handled. The old `model.fit()` method was removed, and training parameters were moved into a `TrainingArguments` dataclass.
fix
Migrate `model.fit()` calls to use `SetFitTrainer` and pass training hyperparameters via a `TrainingArguments` instance. Refer to the v1.0.0 migration guide for details.
affects: <1.0.0
deprecatedPython 3.7 support was deprecated starting from SetFit v1.1.0.
fix
Upgrade to Python 3.9 or newer. SetFit is tested on Python 3.9+.
affects: >=1.1.0
gotchaFrequent compatibility issues can arise with newer versions of core dependencies like `datasets`, `transformers`, and `sentence-transformers`. Patch releases of SetFit (`v1.1.3`, `v1.1.2`) often address these specific issues.
fix
Ensure you are using a compatible set of dependency versions. If encountering errors, try upgrading SetFit to the latest patch release or consult the official documentation/GitHub issues for known compatibility notes.
affects: All versions
gotchaReproducibility of trained models across different SetFit versions (e.g., v0.6.0 vs v1.0.3) can be challenging due to changes in random seed handling for the model's head initialization and different sentence pair sampling methods.
fix
Ensure `transformers.trainer_utils.set_seed()` is called before `SetFitModel.from_pretrained()`. Be aware that minor differences in sampling logic between major versions might still lead to slightly different models.
affects: <1.0.3
gotchaThe `report_to="none"` argument in `TrainingArguments` was sometimes ignored, leading to unexpected logging behavior if Weights & Biases or Tensorboard were installed.
fix
This issue was fixed in v1.1.1. Upgrade to SetFit v1.1.1 or later to ensure `report_to="none"` is respected.
affects: 1.0.0 - 1.1.0
Upgrade
Version history
1.1.3latest on PyPI · released Aug 5, 2025
Audit
Dependencies
pythonrequiredSetFit is tested on Python 3.9+.
datasetsrequiredCore dependency for data handling, frequent compatibility updates required.
sentence-transformersrequiredCore backend for embedding models and finetuning. SetFit defers embedding model finetuning to its Trainer.
transformersrequiredUsed for `TrainingArguments` and `TrainerCallback` instances; compatibility updates are frequent.
acceleraterequiredRequired for multi-GPU training and is a dependency of `sentence-transformers`.
scikit-learnoptionalDefault classification head used by SetFit.
torchoptionalRecommended for GPU acceleration.
Agent activity
29 hits · last 30 days
node
24
OpenAI (training)
1
Resources