Scikit-LLM is a Python package that helps integrate large language models (LLMs) into the scikit-learn framework. It helps in accomplishing text analysis tasks. If you are familiar with scikit-learn, it will be easier for you to work with Scikit-LLM.
It is important to note that Scikit-LLM does not replace scikit-learn. scikit-learn is a general-purpose machine learning library but Scikit-LLM is specifically designed for text analysis tasks.
Getting Started With Scikit-LLM
To get started with Scikit-LLM, you'll need to install the library and configure your API key. To install the library, open your IDE and create a new virtual environment. This will help prevent any potential library version conflicts. Then, run the following command in the terminal.
pip install scikit-llm
This command will install Scikit-LLM and its required dependencies.
To configure your API key, you need to acquire one from your LLM provider. To obtain the OpenAI API key, follow these steps:
Proceed to the OpenAI API page. Then click on your profile located in the upper-right corner of the window. Select View API keys. This will take you to the API keys page.
On the API keys page, click on the Create new secret key button.
Name your API key and click on the Create secret key button to generate the key. After generation, you need to copy the key and store it in a safe place as OpenAI will not display the key again. If you lose it, you will need to generate a new one.
The full source code is available in a GitHub repository.
Now that you have your API key, open your IDE and import SKLLMConfig class from the Scikit-LLM library. This class allows you to set configuration options related to the usage of large language models.
from skllm.config import SKLLMConfig
This class expects you to set your OpenAI API key and organization details.
# Set your OpenAI API key
SKLLMConfig.set_openai_key("Your API key")
# Set your OpenAI organization
SKLLMConfig.set_openai_org("Your organization ID")
The organization ID and the name are not the same. Organization ID is a unique identifier of your organization. To obtain your organization ID, proceed to the OpenAI Organization settings page and copy it. You have now established a connection between Scikit-LLM and the large language model.
Scikit-LLM requires you to have a pay-as-you-go plan. This is because the free trial OpenAI account has a rate limit of three requests per minute which is not sufficient for Scikit-LLM.
Trying to use the free trial account will lead to an error similar to the one below while performing text analysis.
To learn more about rate limits. Proceed to the OpenAI rate limits page.
The LLM provider is not limited to only OpenAI. You can use other LLM providers as well.
Importing the Required Libraries and Loading the Dataset
Import pandas which you will use to load the dataset. Also, from Scikit-LLM and scikit-learn, import the required classes.
import pandas as pd
from skllm import ZeroShotGPTClassifier, MultiLabelZeroShotGPTClassifier
from skllm.preprocessing import GPTSummarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import MultiLabelBinarizer
Next, load the dataset you want to perform text analysis on. This code uses the IMDB movies dataset. You can however tweak it to use your own dataset.
# Load your dataset
data = pd.read_csv("imdb_movies_dataset.csv")
# Extract the first 100 rows
data = data.head(100)
Using only the first 100 rows of the dataset is not mandatory. You can use your entire dataset.
Next, extract the features and label columns. Then split your dataset into train and test sets.
# Extract relevant columns
X = data['Description']
# Assuming 'Genre' contains the labels for classification
y = data['Genre']
# Split the dataset into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
The Genre column contains the labels you want to predict.
Zero-Shot Text Classification With Scikit-LLM
Zero-shot text classification is a feature offered by large language models. It classifies text into predefined categories without the need for explicit training on labeled data. This capability is very useful when dealing with tasks where you need to classify text into categories you didn't anticipate during model training.
To perform zero-shot text classification using Scikit-LLM, use the ZeroShotGPTClassifier class.
# Perform Zero-Shot Text Classification
zero_shot_clf = ZeroShotGPTClassifier(openai_model="gpt-3.5-turbo")
zero_shot_clf.fit(X_train, y_train)
zero_shot_predictions = zero_shot_clf.predict(X_test)
# Print Zero-Shot Text Classification Report
print("Zero-Shot Text Classification Report:")
print(classification_report(y_test, zero_shot_predictions))
The output is as follows:
The classification report provides metrics for each label that the model is trying to predict.
Multi-Label Zero-Shot Text Classification With Scikit-LLM
In some scenarios, a single text may belong to multiple categories simultaneously. Traditional classification models struggle with this. Scikit-LLM on the other hand makes this classification possible. Multi-label zero-shot text classification is crucial in assigning multiple descriptive labels to a single text sample.
Use MultiLabelZeroShotGPTClassifier to predict which labels are appropriate for each text sample.
# Perform Multi-Label Zero-Shot Text Classification
# Make sure to provide a list of candidate labels
candidate_labels = ["Action", "Comedy", "Drama", "Horror", "Sci-Fi"]
multi_label_zero_shot_clf = MultiLabelZeroShotGPTClassifier(max_labels=2)
multi_label_zero_shot_clf.fit(X_train, candidate_labels)
multi_label_zero_shot_predictions = multi_label_zero_shot_clf.predict(X_test)
# Convert the labels to binary array format using MultiLabelBinarizer
mlb = MultiLabelBinarizer()
y_test_binary = mlb.fit_transform(y_test)
multi_label_zero_shot_predictions_binary = mlb.transform(multi_label_zero_shot_predictions)
# Print Multi-Label Zero-Shot Text Classification Report
print("Multi-Label Zero-Shot Text Classification Report:")
print(classification_report(y_test_binary, multi_label_zero_shot_predictions_binary))
In the code above, you define the candidate labels that your text might belong to.
The output is as shown below:
This report helps you understand how well your model is performing for each label in multi-label classification.
Text Vectorization With Scikit-LLM
In text vectorization textual data is converted into a numerical format that machine learning models can understand. Scikit-LLM offers the GPTVectorizer for this. It allows you to transform text into fixed-dimensional vectors using GPT models.
You can achieve this using the Term Frequency-Inverse Document Frequency.
# Perform Text Vectorization using TF-IDF
tfidf_vectorizer = TfidfVectorizer(max_features=1000)
X_train_tfidf = tfidf_vectorizer.fit_transform(X_train)
X_test_tfidf = tfidf_vectorizer.transform(X_test)
# Print the TF-IDF vectorized features for the first few samples
print("TF-IDF Vectorized Features (First 5 samples):")
print(X_train_tfidf[:5]) # Change to X_test_tfidf if you want to print the test set
Here is the output:
The output represents the TF-IDF vectorized features for the first 5 samples in the dataset.
Text Summarization With Scikit-LLM
Text summarization helps in condensing a piece of text while preserving its most critical information. Scikit-LLM offers the GPTSummarizer, which uses the GPT models to generate concise summaries of text.
# Perform Text Summarization
summarizer = GPTSummarizer(openai_model="gpt-3.5-turbo", max_words=15)
summaries = summarizer.fit_transform(X_test)
print(summaries)
The output is as follows:
The above is a summary of the test data.
Build Applications on Top of LLMs
Scikit-LLM opens up a world of possibilities for text analysis with large language models. Understanding the technology behind large language models is crucial. It will help you understand their strengths and weaknesses that can assist you in building efficient applications on top of this cutting-edge technology.