Build a text classifier that categorizes customer feedback into categories (positive, negative, neutral, bug report, feature request).
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
Implementation:
import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function classifyFeedback(feedbackText) {
const completion = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: `You are a feedback classifier. Classify the feedback into exactly one of these categories:
- positive
- negative
- neutral
- bug_report
- feature_request
Respond with ONLY the category name, nothing else.`
},
{
role: 'user',
content: feedbackText
}
],
temperature: 0, // Use 0 for consistent classification
});
return completion.choices[0].message.content.trim();
}
// Batch classification
async function classifyBatchFeedback(feedbackItems) {
const results = await Promise.all(
feedbackItems.map(async (feedback) => ({
text: feedback,
category: await classifyFeedback(feedback)
}))
);
return results;
}
// Usage
const feedbacks = [
"Your app crashes when I try to upload images!",
"Love the new dark mode feature!",
"It would be great to have export to PDF",
"The app is okay, nothing special"
];
const classified = await classifyBatchFeedback(feedbacks);
console.log(classified);