How to beginner to intermediate · 3 min read

Improve classification accuracy tips

Quick answer
To improve classification accuracy with AI models, ensure high-quality, balanced training data and use clear, specific prompts with few-shot examples. Choose a powerful model like gpt-4o and tune parameters such as temperature=0 for deterministic outputs.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (50 kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Use the OpenAI gpt-4o model with a classification prompt including few-shot examples and set temperature=0 for consistent results.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

messages = [
    {"role": "system", "content": "You are a helpful assistant for text classification."},
    {"role": "user", "content": "Classify the sentiment of this text: 'I love this product!'\nAnswer with one word: Positive, Negative, or Neutral."},
    {"role": "assistant", "content": "Positive"},
    {"role": "user", "content": "Classify the sentiment of this text: 'This is the worst experience ever.'\nAnswer with one word: Positive, Negative, or Neutral."},
    {"role": "assistant", "content": "Negative"},
    {"role": "user", "content": "Classify the sentiment of this text: 'The movie was okay, nothing special.'\nAnswer with one word: Positive, Negative, or Neutral."}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    temperature=0,
    max_tokens=10
)

print("Classification:", response.choices[0].message.content.strip())
output
Classification: Neutral

Common variations

Try asynchronous calls for scalability, experiment with different models like gpt-4o-mini for cost efficiency, or use streaming for real-time classification feedback.

python
import os
import asyncio
from openai import OpenAI

async def classify_text():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [
        {"role": "system", "content": "You are a helpful assistant for text classification."},
        {"role": "user", "content": "Classify the sentiment: 'I hate waiting in lines.'"}
    ]
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,
        max_tokens=10
    )
    print("Async classification:", response.choices[0].message.content.strip())

asyncio.run(classify_text())
output
Async classification: Negative

Troubleshooting

  • If classifications are inconsistent, lower temperature to 0 for deterministic outputs.
  • Ensure your few-shot examples are clear and representative of your classification categories.
  • Check your API key and environment variable setup if you get authentication errors.

Key Takeaways

  • Use high-quality, balanced data and clear few-shot examples to guide classification.
  • Set temperature=0 for consistent, deterministic classification results.
  • Choose a model that balances accuracy and cost, such as gpt-4o or gpt-4o-mini.
  • Use asynchronous calls to scale classification tasks efficiently.
  • Validate environment variables and API keys to avoid authentication issues.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗