Skip to main content

createQuestionClassifierNode

workflow.createQuestionClassifierNode() Creates a classification node with input string. It spelled the question, category list, and description into a LLM prompt, and asked the model to return only the category name; if the return value is not in the allowed categories, the node outputs an empty string.

Signature

workflow.createQuestionClassifierNode<
const Categories extends readonly QuestionClassifierCategory[],
>(
options: QuestionClassifierNodeOptions<Categories>,
): BaseNode<string, QuestionClassifierOutput<Categories>>

Parameter

ParameterTypeDescription
optionsQuestionClassifierNodeOptions<Categories>Define provider, classifications, user descriptions, and LLM settings.

Options Field

FieldTypeDescription
namestringNode name, default "question-classifier".
providerLLMProviderSourceprovider used for classification. Required.
categoriesQuestionClassifierCategory[]Classification allowed to be returned. The name cannot be duplicated. Required.
userDescriptionstringClassification task and user scenario description. Required.
systemstringsystem prompt. The default value is "You are a strict question classifier.".
settingsLLMCallSettings subsetLLM call parameters; overrides the default temperature: 0, maxOutputTokens: 50.
historyLimitnumberThe number of nodes retained in the execution history. The default value is 50.
metadataWorkflowNodeMetadataInputThe node displays meta information.

Input and output

NameTypeDescription
InputstringThe problem to be classified. An empty string triggers an input validation error.
OutputCategoryName | ""If the classification is matched, the classification name is returned. If the model returns a non-allowed classification, an empty string is returned.

QuestionClassifierCategory

FieldDescription
nameCategory name, which is also the exact text that the model must return. Required.
descriptionThe classification description is written to prompt. Required.

LLM Request Content

FragmentSourceDescription
Classification RulesFixed template during node executionRequires that only the category name be returned, no JSON, Markdown, or explanation be returned.
userDescriptionoptionsBusiness classification description.
categoriesoptionsList allowable classifications and descriptions by serial number.
questionNode InputUser issues.

Example

const classifier = workflow.createQuestionClassifierNode({
name: "route-question",
provider: workflow.createLLMProviderRef("default"),
userDescription: "Classify whether the user is asking a technical question or making casual conversation.",
categories: [
{ name: "technical", description: "Code, APIs, deployment, or error troubleshooting." },
{ name: "chat", description: "General conversation or a non-technical question." },
] as const,
});

const category = await workflow.runNode(classifier, input.message, context);

Error

SituationError TypeDescription
categories is emptyinput_validationAt least one classification is required.
Duplicate Category Nameinput_validationname cannot be repeated.
userDescription or question is emptyinput_validationRequired text cannot be empty.
provider call failedprovider_callThrown by the provider layer.