Text classification with kluster.ai API and bespokelabs-curator¶
This notebook showcases how to use the kluster.ai Batch API to classify a data set based on a predefined set of categories. In our example, we use an extract from the IMDB top 1000 movies dataset and categorize them into "Action," "Adventure," "Comedy," "Crime," "Documentary," "Drama," "Fantasy," "Horror," "Romance," or "Sci-Fi." We use a movie dataset, but you can adapt this example by using your data and categories relevant to your use case. With this approach, you can effortlessly process datasets of any scale, from small collections to extensive datasets, and obtain categorized results powered by a state-of-the-art language model.
Prerequisites¶
Before getting started, ensure you have the following:
- A kluster.ai account - sign up on the kluster.ai platform if you don't have one
- A kluster.ai API key - after signing in, go to the API Keys section and create a new key. For detailed instructions, check out the Get an API key guide
Setup¶
In this notebook, we'll use Python's getpass
module to safely input the key. Provide your unique kluster.ai API key (ensure there are no spaces).
%pip install -q bespokelabs-curator
WARNING: google-cloud-aiplatform 1.71.1 does not provide the extra 'all' Note: you may need to restart the kernel to use updated packages.
from getpass import getpass
api_key = getpass("Enter your kluster.ai API key: ")
Enter your kluster.ai API key: ········
from bespokelabs import curator
llm = curator.LLM(
model_name="klusterai/Meta-Llama-3.1-8B-Instruct-Turbo",
batch=True,
backend="klusterai",
backend_params={"api_key": api_key, "completion_window": "1h"})
Get the data¶
This notebook includes a preloaded sample dataset derived from the Top 1000 IMDb Movies dataset. It contains movie descriptions ready for classification. No additional setup is needed—simply proceed to the next steps to begin working with this data.
movies = ["Breakfast at Tiffany's: A young New York socialite becomes interested in a young man who has moved into her apartment building, but her past threatens to get in the way.",
"Giant: Sprawling epic covering the life of a Texas cattle rancher and his family and associates.",
"From Here to Eternity: In Hawaii in 1941, a private is cruelly punished for not boxing on his unit's team, while his captain's wife and second-in-command are falling in love.",
"Lifeboat: Several survivors of a torpedoed merchant ship in World War II find themselves in the same lifeboat with one of the crew members of the U-boat that sank their ship.",
"The 39 Steps: A man in London tries to help a counter-espionage Agent. But when the Agent is killed, and the man stands accused, he must go on the run to save himself and stop a spy ring which is trying to steal top secret information."]
prompts = [f"Classify the main genre of the given movie description based on the following genres(Respond with only the genre): “Action”, “Adventure”, “Comedy”, “Crime”, “Documentary”, “Drama”, “Fantasy”, “Horror”, “Romance”, “Sci-Fi”.\n{movie}" for movie in movies]
Perform Batch inference with Curator¶
To execute the inference job, we'll follow three straightforward steps:
- Create the inference file - we'll generate a file with the desired requests to be processed by the model
- Upload the inference file - once the file is ready, we'll upload it to the kluster.ai platform using the API, where it will be queued for processing
- Start the job - after the file is uploaded, we'll initiate the job to process the uploaded data
Everything is set up for you – just run the cells below to watch it all come together.
responses = llm(prompts)
responses['response']
['Romance', 'Drama', 'Drama', 'Action/Drama', 'Action/Adventure']
Conclusion¶
You've successfully completed the classification request using the kluster.ai Batch API! This process showcases how you can efficiently handle and classify large amounts of data with ease. The Batch API empowers you to scale your workflows seamlessly, making it an invaluable tool for processing extensive datasets.