categorize.py

Overview

The categorize.py file defines a component within the InfiniFlow system responsible for classifying user input queries into predefined categories using a large language model (LLM). This component leverages prompt engineering and example-based learning to assign a single, most appropriate category to a given input query, facilitating downstream workflows based on the classification result.

The file primarily contains two classes:

This component is designed to be flexible and extendable, allowing users to define categories, provide examples, and descriptions to guide the classification.


Classes and Methods

Class: CategorizeParam

Inherits from: LLMParam

Defines and manages the parameters required by the Categorize component.

Attributes

Methods

Usage Example

param = CategorizeParam()
param.category_description = {
    "Billing": {
        "description": "Questions related to billing and payments.",
        "examples": ["How do I update my payment method?", "Why was I charged twice?"],
        "to": ["billing_handler"]
    },
    "Technical": {
        "description": "Technical support and troubleshooting.",
        "examples": ["My app crashes on startup.", "How to reset my password?"],
        "to": ["tech_support_handler"]
    }
}
param.message_history_window_size = 3
param.check()
param.update_prompt()
print(param.sys_prompt)

Class: Categorize

Inherits from: LLM, ABC (Abstract Base Class)

Defines the categorization component that uses an LLM chat model to classify inputs.

Class Attributes

Methods


Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class CategorizeParam {
        +category_description: dict
        +query: str
        +message_history_window_size: int
        +sys_prompt: str
        +__init__()
        +check()
        +get_input_form() dict
        +update_prompt()
    }

    class Categorize {
        +component_name: str = "Categorize"
        +_invoke(**kwargs)
        +thoughts() str
    }

    CategorizeParam <|-- Categorize

    class LLMParam
    class LLM

    CategorizeParam --|> LLMParam
    Categorize --|> LLM
    Categorize --|> ABC

Summary

The categorize.py file implements a flexible LLM-powered categorization component within InfiniFlow, allowing dynamic category definitions enriched with descriptions and examples. It tightly integrates with the system's LLM framework and workflow canvas to classify user queries and route subsequent processing steps.
The design emphasizes prompt engineering, model interaction, and robust output parsing to ensure accurate and actionable category assignments.