11# agents/orchestrator.py
22import logging
33import time
4+ import random # Import random for selecting responses
45from .base import BaseAgent
56from .query_analyzer import QueryAnalyzerAgent
67from .retriever import RetrieverAgent
1011
1112logger = logging .getLogger (__name__ )
1213
14+ # --- Greeting/General Chat Handling ---
15+ COMMON_GREETINGS = {
16+ "hi" , "hello" , "hey" , "greetings" , "good morning" , "good afternoon" , "good evening" , "yo" , "sup"
17+ }
18+ GENERAL_CHAT = {
19+ "thanks" , "thank you" , "ok" , "okay" , "cool" , "awesome" , "great" , "bye" , "goodbye" , "see you" , "how are you" , "how's it going" , "what's up"
20+ }
21+ FRIENDLY_RESPONSES = [
22+ "Hi there! 😊 How can I help you explore Sri Lankan history today?" ,
23+ "Hello! Ready to dive into some history? Ask me anything about the textbook." ,
24+ "Hey! What aspect of Sri Lankan history are you interested in learning about?" ,
25+ "Greetings! I'm here to help with your questions about the Grade 11 history text." ,
26+ "Thanks for stopping by! What historical topic is on your mind?" ,
27+ "You're welcome! Anything else I can help you find in the history text?" ,
28+ "Okay! Let me know your next question about Sri Lankan history." ,
29+ "Glad I could help! Feel free to ask more questions." ,
30+ "Goodbye! Come back anytime to learn more history." ,
31+ "See you later! Happy studying!" ,
32+ "I'm doing well, thank you! Ready to assist with your history questions. What would you like to know?" ,
33+ ]
34+ ACKNOWLEDGEMENT_RESPONSES = [
35+ "You're welcome!" ,
36+ "No problem!" ,
37+ "Glad I could help!" ,
38+ "Anytime!" ,
39+ ]
40+ FAREWELL_RESPONSES = [
41+ "Goodbye!" ,
42+ "See you later!" ,
43+ "Take care!" ,
44+ "Happy studying!" ,
45+ ]
46+ HOW_ARE_YOU_RESPONSES = [
47+ "I'm doing well, thank you for asking! I'm ready to help you with Sri Lankan history. What's your question?" ,
48+ "I'm an AI, so I don't have feelings, but I'm fully operational and ready to assist you with history!" ,
49+ "Functioning optimally! How can I help you with the history textbook today?" ,
50+ ]
51+ # --- End Greeting/General Chat Handling ---
52+
53+
1354class OrchestratorAgent (BaseAgent ):
1455 """Agent responsible for orchestrating the QA workflow."""
1556 def __init__ (self ):
@@ -22,7 +63,7 @@ def __init__(self):
2263 logger .info ("✅ Orchestrator ready." )
2364
2465 def run (self , query : str , chat_history : list = None ) -> dict :
25- """Runs the full QA pipeline with query analysis, retrieval, context expansion, and generation .
66+ """Runs the full QA pipeline or handles greetings/general chat .
2667
2768 Args:
2869 query: The user's input query.
@@ -35,6 +76,30 @@ def run(self, query: str, chat_history: list = None) -> dict:
3576 orchestration_start_time = time .time ()
3677 logger .info (f"\n 🔄 Orchestrating response for query: '{ query } '" )
3778
79+ # --- Check for Greetings/General Chat ---
80+ normalized_query = query .lower ().strip ().rstrip ('?.!' )
81+ if normalized_query in COMMON_GREETINGS :
82+ response = random .choice ([r for r in FRIENDLY_RESPONSES if "Hi" in r or "Hello" in r or "Hey" in r or "Greetings" in r ])
83+ logger .info (f"💬 Detected greeting. Responding: '{ response } '" )
84+ return {"answer" : response , "references" : [], "query_analysis" : {"type" : "greeting" }, "retrieved_chunks" : []}
85+ elif normalized_query in {"thanks" , "thank you" }:
86+ response = random .choice (ACKNOWLEDGEMENT_RESPONSES )
87+ logger .info (f"💬 Detected thanks. Responding: '{ response } '" )
88+ return {"answer" : response , "references" : [], "query_analysis" : {"type" : "acknowledgement" }, "retrieved_chunks" : []}
89+ elif normalized_query in {"bye" , "goodbye" , "see you" }:
90+ response = random .choice (FAREWELL_RESPONSES )
91+ logger .info (f"💬 Detected farewell. Responding: '{ response } '" )
92+ return {"answer" : response , "references" : [], "query_analysis" : {"type" : "farewell" }, "retrieved_chunks" : []}
93+ elif normalized_query in {"how are you" , "how's it going" , "what's up" }:
94+ response = random .choice (HOW_ARE_YOU_RESPONSES )
95+ logger .info (f"💬 Detected 'how are you'. Responding: '{ response } '" )
96+ return {"answer" : response , "references" : [], "query_analysis" : {"type" : "status_inquiry" }, "retrieved_chunks" : []}
97+ elif normalized_query in GENERAL_CHAT : # Catch other general phrases
98+ response = random .choice ([r for r in FRIENDLY_RESPONSES if "Okay" in r or "Glad" in r ]) # Generic positive response
99+ logger .info (f"💬 Detected general chat. Responding: '{ response } '" )
100+ return {"answer" : response , "references" : [], "query_analysis" : {"type" : "general_chat" }, "retrieved_chunks" : []}
101+ # --- End Check ---
102+
38103 # 1. Analyze Query
39104 analysis_start_time = time .time ()
40105 logger .info ("Step 1: Analyzing query..." )
@@ -92,7 +157,7 @@ def run(self, query: str, chat_history: list = None) -> dict:
92157 orchestration_duration = time .time () - orchestration_start_time
93158 logger .info (f"✅ Orchestration complete. Total time: { orchestration_duration :.4f} s" )
94159 if orchestration_duration > 3.0 :
95- logger .warning (f"⏱️ Total orchestration time ({ orchestration_duration :.4f} s) exceeded target threshold of 3 seconds." )
160+ logger .warning (f"⏱️ Total orchestration time ({ orchestration_duration :.4f} s) exceeded target threshold of 3 seconds." )
96161
97162 return {
98163 "answer" : final_answer ,
0 commit comments