Rifqi Hafizuddin commited on
Commit ·
35b204d
1
Parent(s): 430c361
[KM-564] edit agent logic
Browse files
src/agents/chat_handler.py
CHANGED
|
@@ -28,10 +28,10 @@ from typing import TYPE_CHECKING, Any
|
|
| 28 |
from langchain_core.messages import BaseMessage
|
| 29 |
|
| 30 |
from src.middlewares.logging import get_logger
|
| 31 |
-
|
| 32 |
from src.retrieval.base import RetrievalResult
|
|
|
|
| 33 |
from .chatbot import ChatbotAgent, DocumentChunk
|
| 34 |
-
from .
|
| 35 |
|
| 36 |
if TYPE_CHECKING:
|
| 37 |
from ..catalog.reader import CatalogReader
|
|
@@ -54,7 +54,7 @@ class ChatHandler:
|
|
| 54 |
|
| 55 |
def __init__(
|
| 56 |
self,
|
| 57 |
-
intent_router:
|
| 58 |
answer_agent: ChatbotAgent | None = None,
|
| 59 |
catalog_reader: CatalogReader | None = None,
|
| 60 |
query_service: QueryService | None = None,
|
|
@@ -70,9 +70,9 @@ class ChatHandler:
|
|
| 70 |
# Lazy default-dep builders
|
| 71 |
# ------------------------------------------------------------------
|
| 72 |
|
| 73 |
-
def _get_intent_router(self) ->
|
| 74 |
if self._intent_router is None:
|
| 75 |
-
self._intent_router =
|
| 76 |
return self._intent_router
|
| 77 |
|
| 78 |
def _get_answer_agent(self) -> ChatbotAgent:
|
|
|
|
| 28 |
from langchain_core.messages import BaseMessage
|
| 29 |
|
| 30 |
from src.middlewares.logging import get_logger
|
|
|
|
| 31 |
from src.retrieval.base import RetrievalResult
|
| 32 |
+
|
| 33 |
from .chatbot import ChatbotAgent, DocumentChunk
|
| 34 |
+
from .orchestration import OrchestratorAgent
|
| 35 |
|
| 36 |
if TYPE_CHECKING:
|
| 37 |
from ..catalog.reader import CatalogReader
|
|
|
|
| 54 |
|
| 55 |
def __init__(
|
| 56 |
self,
|
| 57 |
+
intent_router: OrchestratorAgent | None = None,
|
| 58 |
answer_agent: ChatbotAgent | None = None,
|
| 59 |
catalog_reader: CatalogReader | None = None,
|
| 60 |
query_service: QueryService | None = None,
|
|
|
|
| 70 |
# Lazy default-dep builders
|
| 71 |
# ------------------------------------------------------------------
|
| 72 |
|
| 73 |
+
def _get_intent_router(self) -> OrchestratorAgent:
|
| 74 |
if self._intent_router is None:
|
| 75 |
+
self._intent_router = OrchestratorAgent()
|
| 76 |
return self._intent_router
|
| 77 |
|
| 78 |
def _get_answer_agent(self) -> ChatbotAgent:
|
src/agents/{intent_router.py → orchestration.py}
RENAMED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
Output: needs_search (bool) + source_hint ∈ { chat, unstructured, structured }
|
| 4 |
+ rewritten_query (standalone form of the user's question, history-resolved).
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
@@ -21,7 +23,7 @@ from pydantic import BaseModel, Field
|
|
| 21 |
|
| 22 |
from src.middlewares.logging import get_logger
|
| 23 |
|
| 24 |
-
logger = get_logger("
|
| 25 |
|
| 26 |
SourceHint = Literal["chat", "unstructured", "structured"]
|
| 27 |
|
|
@@ -75,7 +77,7 @@ def _build_default_chain() -> Runnable:
|
|
| 75 |
return prompt | llm.with_structured_output(IntentRouterDecision)
|
| 76 |
|
| 77 |
|
| 78 |
-
class
|
| 79 |
"""Classifies a user message into chat / unstructured / structured.
|
| 80 |
|
| 81 |
Inject `structured_chain` for tests; default builds the production
|
|
|
|
| 1 |
+
"""OrchestratorAgent — classifies a user message and emits source_hint.
|
| 2 |
|
| 3 |
Output: needs_search (bool) + source_hint ∈ { chat, unstructured, structured }
|
| 4 |
+ rewritten_query (standalone form of the user's question, history-resolved).
|
| 5 |
|
| 6 |
+
Phase 2 replaces the previous intent-classification body. The class name
|
| 7 |
+
is preserved so existing import sites (`from src.agents.orchestration
|
| 8 |
+
import OrchestratorAgent`) keep working. The default LLM chain is
|
| 9 |
+
constructed lazily so the module is import-safe even without `.env`
|
| 10 |
+
populated.
|
| 11 |
"""
|
| 12 |
|
| 13 |
from __future__ import annotations
|
|
|
|
| 23 |
|
| 24 |
from src.middlewares.logging import get_logger
|
| 25 |
|
| 26 |
+
logger = get_logger("orchestrator")
|
| 27 |
|
| 28 |
SourceHint = Literal["chat", "unstructured", "structured"]
|
| 29 |
|
|
|
|
| 77 |
return prompt | llm.with_structured_output(IntentRouterDecision)
|
| 78 |
|
| 79 |
|
| 80 |
+
class OrchestratorAgent:
|
| 81 |
"""Classifies a user message into chat / unstructured / structured.
|
| 82 |
|
| 83 |
Inject `structured_chain` for tests; default builds the production
|