Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
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\
|
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.
|
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."""
|