typescriptintermediate
AI Text Classification with Prompts
Classify text into categories using structured prompting with confidence scores and explanations.
typescriptPress ⌘/Ctrl + Shift + C to copy
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.
pythonintermediate
Text Classification with Hugging Face
Fine-tune or use pre-trained Hugging Face models for text classification.
Best for: Sentiment analysis
#ai#huggingface
pythonbeginner
Zero-Shot Text Classification
Classify text into custom categories using zero-shot NLI models without training data.
Best for: content categorization
#huggingface#zero-shot
typescriptbeginner
Few-Shot Prompt Template
Build structured few-shot prompts with examples, system instructions, and output format constraints.
Best for: Consistent AI outputs
#prompts#few-shot
typescriptadvanced
AI Prompt Chaining Pattern
Chain multiple LLM calls sequentially where each step's output feeds into the next for complex tasks.
Best for: Complex multi-step AI workflows
#prompt-engineering#chaining