How to do zero-shot text classification with OpenAI
Quick answer
Use the OpenAI API's
chat.completions.create method with a prompt that instructs the model to classify text into given categories without prior training. Provide the text and candidate labels in the message content for zero-shot classification using models like gpt-4o.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK and set your API key as an environment variable.
- Install SDK:
pip install openai - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install openai output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
Use the OpenAI client to send a prompt that asks the model to classify the input text into specified labels. This example uses gpt-4o for zero-shot classification.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
text_to_classify = "I love the new design of your product!"
candidate_labels = ["positive", "negative", "neutral"]
prompt = (
f"Classify the following text into one of these categories: {', '.join(candidate_labels)}.\n"
f"Text: \"{text_to_classify}\"\n"
"Return only the label."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
label = response.choices[0].message.content.strip()
print(f"Predicted label: {label}") output
Predicted label: positive
Common variations
- Use different models like
gpt-4o-minifor faster, cheaper classification. - For async usage, use
asynciowith the OpenAI client. - Use prompt templates to handle multiple texts or batch classification.
- Incorporate streaming for real-time classification feedback (less common for classification).
import asyncio
from openai import OpenAI
async def classify_text_async(text, labels):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
f"Classify the following text into one of these categories: {', '.join(labels)}.\n"
f"Text: \"{text}\"\n"
"Return only the label."
)
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
async def main():
label = await classify_text_async("The movie was boring and too long.", ["positive", "negative", "neutral"])
print(f"Predicted label: {label}")
asyncio.run(main()) output
Predicted label: negative
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If the model returns unexpected output, refine the prompt to be more explicit about returning only the label.
- For rate limit errors, consider retrying with exponential backoff or upgrading your plan.
Key Takeaways
- Use
chat.completions.createwith a clear prompt for zero-shot classification. - Pass candidate labels explicitly in the prompt for best results.
- Use
gpt-4ofor accuracy orgpt-4o-minifor cost-effective classification. - Always set your API key via environment variables to avoid authentication issues.
- Refine prompts to ensure the model returns only the classification label.