Classifier

The classifier tool is a powerful utility in the MindedJS SDK that allows you to categorize text content using AI. It's available both as a tool that can be used in flows and as standalone utility functions that can be imported and used anywhere in your code.

Overview

The classifier tool provides:

  • Single-label classification: Categorize content into one class

  • Multi-label classification: Categorize content into multiple classes

  • Conversation classification: Specialized classification for chat conversations

  • Configurable output formats: JSON or plain text

  • Error handling with fallback defaults

  • Confidence scoring

Using as a Tool

In Flows

You can use the classifier tool in your flows by referencing minded-classifier:

- id: classifyUserIntent
  type: tool
  toolName: minded-classifier
  prompt: |
    Classify the user's message into one of these categories:
    - Technical Issue: Problems with the system
    - Billing: Payment or pricing questions
    - Feature Request: Asking for new features

    Content: "{{userMessage}}"

Tool Parameters

Parameter
Type
Description
Required

content

string

The text content to classify

Yes

classes

array

Classes to classify into (strings, tuples, or objects)

No*

systemPrompt

string

Custom system prompt for classification

No

includeReason

boolean

Whether to include reasoning (default: true)

No

outputFormat

'json' | 'text'

Output format (default: 'json')

No

defaultClass

string

Fallback class if classification fails

No

defaultReason

string

Fallback reason if classification fails

No

*Classes can be provided in the tool parameters or stored in agent memory

Using as a Utility

Import the Utilities

import { toolsLibrary } from '@minded-ai/mindedjs';

const { classify, createClassifier, createConversationClassifier, multiClassify } = toolsLibrary.tools['minded-classifier'];

Direct Classification

const result = await classify(
  "I can't access my account",
  {
    classes: [
      { name: 'Technical', description: 'Technical issues' },
      { name: 'Account', description: 'Account access problems' },
    ],
    includeReason: true,
  },
  agent.llm,
);

console.log(result);
// { class: 'Account', reason: 'User cannot access their account', confidence: 0.95 }

Create Reusable Classifiers

// Simple classifier with string classes
const sentimentClassifier = createClassifier(['positive', 'negative', 'neutral']);

// Classifier with descriptions
const intentClassifier = createClassifier([
  ['greeting', 'User is greeting or saying hello'],
  ['question', 'User is asking a question'],
  ['complaint', 'User is complaining or expressing dissatisfaction'],
  ['goodbye', 'User is saying goodbye or ending conversation'],
]);

// Use the classifier
const sentiment = await sentimentClassifier('This is amazing!', agent.llm);
const intent = await intentClassifier('Hello there!', agent.llm);

Conversation Classification

For classifying entire conversations:

const conversationClassifier = createConversationClassifier([
  { name: 'Resolved', description: 'Issue was resolved successfully' },
  { name: 'Escalated', description: 'Needs escalation to human agent' },
  { name: 'Pending', description: 'Awaiting more information' },
]);

const result = await conversationClassifier(messages, agent.llm);

Multi-Label Classification

When content can belong to multiple categories:

const topics = await multiClassify(
  'The app crashes on login and the UI looks outdated',
  {
    classes: [
      { name: 'Bug', description: 'Software bugs or crashes' },
      { name: 'UI/UX', description: 'User interface issues' },
      { name: 'Authentication', description: 'Login or auth problems' },
    ],
    maxClasses: 3,
  },
  agent.llm,
);

console.log(topics);
// [
//   { class: 'Bug', reason: 'App crashes', confidence: 0.9 },
//   { class: 'Authentication', reason: 'Login issues', confidence: 0.85 },
//   { class: 'UI/UX', reason: 'Outdated UI', confidence: 0.7 }
// ]

Configuration Options

ClassifierConfig Interface

interface ClassifierConfig {
  classes: ClassDefinition[];
  systemPrompt?: string;
  outputFormat?: 'json' | 'text';
  includeReason?: boolean;
  defaultClass?: string;
  defaultReason?: string;
}

Class Definition Formats

Classes can be defined in multiple ways:

// Simple strings
['positive', 'negative', 'neutral'][
  // Tuples with descriptions
  (['technical', 'Technical problems'], ['billing', 'Payment issues'])
][
  // Objects
  ({ name: 'urgent', description: 'Requires immediate attention' }, { name: 'normal', description: 'Standard priority' })
];

Example: Customer Support Bot

import { Agent, toolsLibrary } from '@minded-ai/mindedjs';
import { z } from 'zod';

const classifierTool = toolsLibrary.tools['minded-classifier'].default;

const agent = new Agent({
  memorySchema: z.object({
    ticketPriority: z.string().optional(),
    ticketCategory: z.string().optional(),
  }),
  config: {
    flows: ['./flows'],
    llm: { provider: 'openai', model: 'gpt-4' },
  },
  tools: [classifierTool],
});

// Create specialized classifiers
const priorityClassifier = createClassifier([
  ['urgent', 'Requires immediate attention'],
  ['high', 'Important but not critical'],
  ['normal', 'Standard priority'],
  ['low', 'Can be handled later'],
]);

const categoryClassifier = createClassifier([
  ['technical', 'Technical issues or bugs'],
  ['billing', 'Payment and subscription issues'],
  ['feature', 'Feature requests'],
  ['other', 'General inquiries'],
]);

// Use in message handler
agent.on('message', async (message) => {
  const [priority, category] = await Promise.all([
    priorityClassifier(message.content, agent.llm),
    categoryClassifier(message.content, agent.llm),
  ]);

  // Update agent memory
  await agent.updateMemory({
    ticketPriority: priority.class,
    ticketCategory: category.class,
  });
});

`

Last updated