Transformer/LLM-based zero and few-shot classification in scikit-learn pipelines
Project description
stormtrooper
Zero/few shot learning components for scikit-learn pipelines with large-language models and transformers.
Why stormtrooper?
Other packages promise to provide at least similar functionality (scikit-llm), why should you choose stormtrooper instead?
- Fine-grained control over you pipeline.
- Variety: stormtrooper allows you to use virtually all canonical approaches for zero and few-shot classification including NLI, Seq2Seq and Generative open-access models from Transformers, SetFit and even OpenAI's large language models.
- Prompt engineering: You can adjust prompt templates to your hearts content.
- Performance
- Easy inference on GPU if you have access to it.
- Interfacing HuggingFace's TextGenerationInference API, the most efficient way to host models locally.
- Async interaction with external APIs, this can speed up inference with OpenAI's models quite drastically.
- Extensive Documentation
- Throrough API reference and loads of examples to get you started.
- Battle-hardened
- We at the Center For Humanities Computing are making extensive use of this package. This means you can rest assured that the package works under real-world pressure. As such you can expect regular updates and maintance.
- Simple
- We opted for as bare-bones of an implementation and little coupling as possible. The library works at the lowest level of abstraction possible, and we hope our code will be rather easy for others to understand and contribute to.
New in version 0.5.0
stormtrooper now uses chat templates from HuggingFace transformers for generative models. This means that you no longer have to pass model-specific prompt templates to these and can define system and user prompts separately.
from stormtrooper import GenerativeZeroShotClassifier
system_prompt = "You're a helpful assistant."
user_prompt = """
Classify a text into one of the following categories: {classes}
Text to clasify:
"{X}"
"""
model = GenerativeZeroShotClassifier().fit(None, ["political", "not political"])
model.predict("Joe Biden is no longer the candidate of the Democrats.")
Examples
Here are a couple of motivating examples to get you hooked. Find more in our docs.
pip install stormtrooper
class_labels = ["atheism/christianity", "astronomy/space"]
example_texts = [
"God came down to earth to save us.",
"A new nebula was recently discovered in the proximity of the Oort cloud."
]
Zero-shot learning
For zero-shot learning you can use zero-shot models:
from stormtrooper import ZeroShotClassifier
classifier = ZeroShotClassifier().fit(None, class_labels)
Generative models (GPT, Llama):
from stormtrooper import GenerativeZeroShotClassifier
classifier = GenerativeZeroShotClassifier("meta-llama/Meta-Llama-3.1-8B-Instruct").fit(None, class_labels)
Text2Text models (T5): If you are running low on resources I would personally recommend T5.
from stormtrooper import Text2TextZeroShotClassifier
# You can define a custom prompt, but a default one is available
prompt = "..."
classifier =Text2TextZeroShotClassifier(prompt=prompt).fit(None, class_labels)
predictions = classifier.predict(example_texts)
assert list(predictions) == ["atheism/christianity", "astronomy/space"]
OpenAI models: You can now use OpenAI's chat LLMs in stormtrooper workflows.
from stormtrooper import OpenAIZeroShotClassifier
classifier = OpenAIZeroShotClassifier("gpt-4").fit(None, class_labels)
predictions = classifier.predict(example_texts)
assert list(predictions) == ["atheism/christianity", "astronomy/space"]
Few-Shot Learning
For few-shot tasks you can only use Generative, Text2Text, OpenAI (aka. promptable) or SetFit models.
from stormtrooper import GenerativeFewShotClassifier, Text2TextFewShotClassifier, SetFitFewShotClassifier
classifier = SetFitFewShotClassifier().fit(example_texts, class_labels)
predictions = model.predict(["Calvinists believe in predestination."])
assert list(predictions) == ["atheism/christianity"]
Fuzzy Matching
Generative and text2text models by default will fuzzy match results to the closest class label, you can disable this behavior
by specifying fuzzy_match=False
.
If you want fuzzy matching speedup, you should install python-Levenshtein
.
Inference on GPU
From version 0.2.2 you can run models on GPU. You can specify the device when initializing a model:
classifier = Text2TextZeroShotClassifier(device="cuda:0")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file stormtrooper-0.5.0.tar.gz
.
File metadata
- Download URL: stormtrooper-0.5.0.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.9.13 Linux/5.15.0-117-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f21274d72e9844b7a16fc3083d9a60df1b0d840a421cc772c152558d86066515 |
|
MD5 | 273618a0fba4022e0b38fdf5b25c858c |
|
BLAKE2b-256 | 1c3f28546e7f9b6358d15e7eb01191aeed9069b79e4b152a47987287995021f3 |
File details
Details for the file stormtrooper-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: stormtrooper-0.5.0-py3-none-any.whl
- Upload date:
- Size: 22.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.9.13 Linux/5.15.0-117-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 680f66c1566fc005c1e5e8c74185c058bc7af703ebdfe35327cca32f03419cec |
|
MD5 | ccf43c28da5008f46f55bedab9c9269d |
|
BLAKE2b-256 | 0b88938e95c55a3d6df14f1071800669186dee3db10dda17dcb20e5ae2342d08 |