Shreyas094 commited on
Commit
d55e623
·
verified ·
1 Parent(s): 56c5787

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -32
app.py CHANGED
@@ -61,7 +61,7 @@ SAFE_SEARCH_OPTIONS = [
61
  async def determine_query_type(query: str, chat_history: List[List[str]], temperature: float = 0.1) -> QueryType:
62
  """
63
  Determine whether a query should be answered from knowledge base or require web search.
64
- Now with improved context handling.
65
  """
66
  logger.info(f'Determining query type for: {query}')
67
  try:
@@ -78,37 +78,31 @@ async def determine_query_type(query: str, chat_history: List[List[str]], temper
78
  system_prompt = """You are Sentinel, an intelligent AI agent tasked with determining whether a user query requires a web search or can be answered using your existing knowledge base. Your knowledge cutoff date is April 2024, and the current date is November 2024.
79
 
80
  Rules for Classification:
81
-
82
- 1. RESPOND WITH ONLY "knowledge_base" OR "web_search" - NO OTHER TEXT
83
-
84
- 2. Consider conversation context:
85
- - Look for references to previous turns in the conversation
86
- - Check if the query is a follow-up to earlier discussion
87
- - Consider if previous context requires updated information
88
-
89
- 3. Classify as "web_search" if:
90
- - Query explicitly asks for current/latest/recent information
91
- - References events or data after April 2024
92
- - Requires real-time information (prices, weather, news)
93
- - Uses words like "current", "latest", "now", "today"
94
- - Asks about ongoing events or situations
95
- - Needs verification of recent claims
96
- - Is a follow-up question about current events
97
- - Previous context involves recent/ongoing events
98
-
99
- 4. Classify as "knowledge_base" if:
100
- - Query is about historical events or facts before April 2024
101
- - Involves general knowledge, concepts, or theories
102
- - Is casual conversation or greeting
103
- - Asks for explanations of established topics
104
- - Requires logical reasoning or analysis
105
- - Is about personal opinions or hypotheticals
106
- - Is a follow-up to a knowledge-base discussion
107
- - Previous context is about historical or conceptual topics"""
108
 
109
  messages = [
110
  {"role": "system", "content": system_prompt},
111
- {"role": "user", "content": f"Previous conversation:\n{chat_context}\n\nCurrent query: {query}\n\nClassify this query based on the rules above, considering the conversation context."}
112
  ]
113
 
114
  response = groq_client.chat.completions.create(
@@ -120,13 +114,18 @@ Rules for Classification:
120
  )
121
 
122
  result = response.choices[0].message.content.strip().lower()
123
- logger.info(f'Query type determined as: {result} with context')
124
 
 
 
 
 
 
 
125
  return QueryType.WEB_SEARCH if result == "web_search" else QueryType.KNOWLEDGE_BASE
126
 
127
  except Exception as e:
128
- logger.error(f'Error determining query type: {e}')
129
- return QueryType.WEB_SEARCH
130
 
131
  async def process_knowledge_base_query(query: str, chat_history: List[List[str]], temperature: float = 0.7) -> str:
132
  """Handle queries that can be answered from the knowledge base, with context."""
 
61
  async def determine_query_type(query: str, chat_history: List[List[str]], temperature: float = 0.1) -> QueryType:
62
  """
63
  Determine whether a query should be answered from knowledge base or require web search.
64
+ Now with improved context handling and response validation.
65
  """
66
  logger.info(f'Determining query type for: {query}')
67
  try:
 
78
  system_prompt = """You are Sentinel, an intelligent AI agent tasked with determining whether a user query requires a web search or can be answered using your existing knowledge base. Your knowledge cutoff date is April 2024, and the current date is November 2024.
79
 
80
  Rules for Classification:
81
+ IMPORTANT: You must ONLY respond with either "knowledge_base" or "web_search" - no other text or explanation is allowed.
82
+
83
+ Classify as "web_search" if the query:
84
+ - Explicitly asks for current/latest/recent information
85
+ - References events or data after April 2024
86
+ - Requires real-time information (prices, weather, news)
87
+ - Uses words like "current", "latest", "now", "today"
88
+ - Asks about ongoing events or situations
89
+ - Needs verification of recent claims
90
+ - Is a follow-up question about current events
91
+ - Previous context involves recent/ongoing events
92
+
93
+ Classify as "knowledge_base" if the query:
94
+ - Is about historical events or facts before April 2024
95
+ - Involves general knowledge, concepts, or theories
96
+ - Is casual conversation or greeting
97
+ - Asks for explanations of established topics
98
+ - Requires logical reasoning or analysis
99
+ - Is about personal opinions or hypotheticals
100
+ - Is a follow-up to a knowledge-base discussion
101
+ - Previous context is about historical or conceptual topics"""
 
 
 
 
 
 
102
 
103
  messages = [
104
  {"role": "system", "content": system_prompt},
105
+ {"role": "user", "content": f"Previous conversation:\n{chat_context}\n\nQuery to classify: {query}\n\nRespond ONLY with 'knowledge_base' or 'web_search':"}
106
  ]
107
 
108
  response = groq_client.chat.completions.create(
 
114
  )
115
 
116
  result = response.choices[0].message.content.strip().lower()
 
117
 
118
+ # Validate the response
119
+ if result not in ["knowledge_base", "web_search"]:
120
+ logger.warning(f'Invalid response from LLM: {result}. Defaulting to knowledge_base')
121
+ return QueryType.KNOWLEDGE_BASE
122
+
123
+ logger.info(f'Query type determined as: {result}')
124
  return QueryType.WEB_SEARCH if result == "web_search" else QueryType.KNOWLEDGE_BASE
125
 
126
  except Exception as e:
127
+ logger.error(f'Error determining query type: {e}. Defaulting to knowledge_base')
128
+ return QueryType.KNOWLEDGE_BASE
129
 
130
  async def process_knowledge_base_query(query: str, chat_history: List[List[str]], temperature: float = 0.7) -> str:
131
  """Handle queries that can be answered from the knowledge base, with context."""