|
| 1 | +""" |
| 2 | +Classifier Agent — the entry-point router for Xmem. |
| 3 | +
|
| 4 | +Classifies user input into one or more intent categories (code, profile, |
| 5 | +event) so downstream agents only receive the sub-queries relevant to them. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from typing import Any, Dict |
| 11 | + |
| 12 | +from langchain_core.language_models import BaseChatModel |
| 13 | + |
| 14 | +from src.agents.base import BaseAgent |
| 15 | +from src.prompts.classifier import build_system_prompt, pack_classification_query |
| 16 | +from src.schemas.classification import ClassificationResult |
| 17 | +from src.utils.text import parse_raw_response_to_classifications |
| 18 | + |
| 19 | + |
| 20 | +class ClassifierAgent(BaseAgent): |
| 21 | + def __init__(self, model: BaseChatModel) -> None: |
| 22 | + super().__init__( |
| 23 | + model=model, |
| 24 | + name="classifier", |
| 25 | + system_prompt=build_system_prompt(), |
| 26 | + ) |
| 27 | + |
| 28 | + async def arun(self, state: Dict[str, Any]) -> ClassificationResult: |
| 29 | + user_input = state.get("user_query") |
| 30 | + if not user_input: |
| 31 | + self.logger.debug("Empty query — returning empty classifications.") |
| 32 | + return ClassificationResult(classifications=[]) |
| 33 | + |
| 34 | + user_message = pack_classification_query(user_input) |
| 35 | + messages = self._build_messages(user_message) |
| 36 | + raw_content = await self._call_model(messages) |
| 37 | + classifications = parse_raw_response_to_classifications(raw_content) |
| 38 | + |
| 39 | + if classifications: |
| 40 | + self.logger.info("=" * 50) |
| 41 | + self.logger.info("Extracted Classifications:") |
| 42 | + for idx, cls in enumerate(classifications, 1): |
| 43 | + self.logger.info( |
| 44 | + " %d. source=%s query=%s", idx, cls["source"], cls["query"] |
| 45 | + ) |
| 46 | + self.logger.info("Total classifications: %d", len(classifications)) |
| 47 | + self.logger.info("=" * 50) |
| 48 | + else: |
| 49 | + self.logger.info("No actionable classifications found (trivial input).") |
| 50 | + |
| 51 | + return ClassificationResult(classifications=classifications) |
0 commit comments