| from src.chatbot_v2.layers.perception import PerceptionLayer
|
| from src.chatbot_v2.layers.cognition import CognitionLayer, CognitiveOutput
|
| from src.chatbot_v2.layers.action import ActionLayer
|
| from src.chatbot_v2.layers.memory import MemoryLayer
|
| from typing import List, Dict
|
|
|
| class ChatbotOrchestrator:
|
| """
|
| DESIGN PATTERN: ReAct (Reason + Act) - Orchestrator
|
|
|
| Structure:
|
| 1. Loop:
|
| - Update Perception
|
| - Consult Cognition (Reasoning)
|
| - If Action needed -> Call Action Layer -> Loop
|
| - If Final Answer -> Return
|
| """
|
|
|
| def __init__(self):
|
|
|
| self.perception = PerceptionLayer()
|
| self.brain = CognitionLayer()
|
| self.hands = ActionLayer()
|
| self.memory = MemoryLayer()
|
|
|
| async def run(self, user_input: str, external_history: List[Dict[str, str]] = None):
|
| """
|
| Runs the agent loop.
|
| Args:
|
| user_input: The new message from the user.
|
| external_history: Existing chat history (e.g. from Streamlit).
|
| """
|
|
|
| if external_history:
|
| self.memory.set_history(list(external_history))
|
|
|
|
|
| env_state = self.perception.perceive(user_input)
|
| self.memory.add_entry("user", env_state.user_input)
|
|
|
|
|
| for step in range(5):
|
| print(f"\n--- Step {step+1} (ReAct Loop) ---")
|
|
|
|
|
| history = self.memory.get_history()
|
| decision: CognitiveOutput = await self.brain.decide(history)
|
|
|
| print(f"[Think]: {decision.thought}")
|
|
|
|
|
| if decision.final_answer:
|
| print(f"[Final Answer]: {decision.final_answer}")
|
| self.memory.add_entry("assistant", decision.final_answer)
|
| return decision.final_answer
|
|
|
| if decision.action:
|
|
|
| print(f"[Action Needed]: Call {decision.action} with {decision.action_input}")
|
| tool_result = await self.hands.execute(decision.action, decision.action_input or {})
|
|
|
|
|
| print(f"[Observation]: {tool_result}")
|
| self.memory.add_entry("system", f"Tool {decision.action} returned: {tool_result}")
|
|
|
| return "I apologize, but I got stuck in a loop trying to answer your request."
|
|
|