typescriptintermediate

AI Text Classification with Prompts

Classify text into categories using structured prompting with confidence scores and explanations.

typescript
import OpenAI from 'openai';
import { z } from 'zod';

const openai = new OpenAI();

const ClassificationSchema = z.object({
  category: z.string(),
  confidence: z.number().min(0).max(1),
  reasoning: z.string(),
});

type Classification = z.infer<typeof ClassificationSchema>;

export async function classifyText(
  text: string,
  categories: string[],
): Promise<Classification> {
  const res = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      {
        role: 'system',
        content: `Classify the text into one of these categories: ${categories.join(', ')}.
Respond with JSON: {"category": "...", "confidence": 0.0-1.0, "reasoning": "..."}`,
      },
      { role: 'user', content: text },
    ],
    response_format: { type: 'json_object' },
  });

  const parsed = JSON.parse(res.choices[0].message.content ?? '{}');
  return ClassificationSchema.parse(parsed);
}

const result = await classifyText(
  'The server keeps returning 503 errors during peak hours',
  ['bug', 'feature-request', 'question', 'performance'],
);
console.log(result.category, result.confidence);

Use Cases

  • Automated support ticket routing
  • Content moderation and categorization
  • Sentiment analysis for customer feedback

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.