0
0
GenaiHow-ToBeginner · 4 min read

How to Use Prompts for Classification in AI Models

To use prompts for classification, you design a clear question or instruction that guides the AI to assign a category or label to input data. The prompt should include the input and a request for a specific class or label, enabling the model to predict the correct category based on the given context.
📐

Syntax

A classification prompt typically includes three parts:

  • Input data: The text or data you want to classify.
  • Instruction: A clear request asking the model to classify the input.
  • Output format: Specify how you want the answer, such as a label or category name.

Example syntax:

"Classify the following text into categories: [input text]. Answer with one category."
text
"Classify the following text into categories: {input_text}. Answer with one category."
💻

Example

This example shows how to use a prompt to classify movie reviews as either 'Positive' or 'Negative'. The prompt includes the review text and asks the model to respond with the sentiment.

python
from transformers import pipeline

# Load a text-generation model (e.g., GPT-2 or GPT-3 via API)
classifier = pipeline('text-generation', model='gpt2')

review = "I loved the movie, it was fantastic and thrilling!"
prompt = f"Classify the sentiment of this review as Positive or Negative: '{review}' Answer:" 

result = classifier(prompt, max_length=50, num_return_sequences=1)
print(result[0]['generated_text'])
Output
Classify the sentiment of this review as Positive or Negative: 'I loved the movie, it was fantastic and thrilling!' Answer: Positive
⚠️

Common Pitfalls

Common mistakes when using prompts for classification include:

  • Being too vague or unclear in the instruction, causing the model to give unrelated answers.
  • Not specifying the expected output format, leading to inconsistent or verbose responses.
  • Using ambiguous input data that confuses the model.

Always keep prompts simple, clear, and focused on the classification task.

text
wrong_prompt = "What do you think about this? 'The food was bad.'"
right_prompt = "Classify the sentiment of this sentence as Positive or Negative: 'The food was bad.' Answer:"
📊

Quick Reference

Prompt PartDescriptionExample
Input DataThe text or item to classify'The movie was great!'
InstructionClear request for classificationClassify the sentiment as Positive or Negative
Output FormatHow the answer should be givenAnswer with one word: Positive or Negative

Key Takeaways

Design prompts with clear instructions and specify the expected output format.
Include the input data explicitly in the prompt for context.
Avoid vague or ambiguous language to get accurate classification results.
Test prompts with different inputs to ensure consistent model behavior.