Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
rag.py
CHANGED
|
@@ -87,17 +87,33 @@ def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
|
| 87 |
print(f"Error querying Groq API: {e}")
|
| 88 |
return ""
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def get_best_answer(user_input):
|
| 91 |
-
"""Core function to find or generate the best response for a user query"""
|
| 92 |
# 1. Check for empty input
|
| 93 |
if not user_input.strip():
|
| 94 |
-
return {"response": "Please enter a valid question.", "should_scroll":
|
| 95 |
|
| 96 |
user_input_lower = user_input.lower().strip()
|
| 97 |
|
| 98 |
# 2. Check for minimum word count (3 words)
|
| 99 |
if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
|
| 100 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# 3. Handle greetings (regardless of word count)
|
| 103 |
if any(greet in user_input_lower for greet in GREETINGS):
|
|
@@ -163,36 +179,3 @@ def get_best_answer(user_input):
|
|
| 163 |
"response": response,
|
| 164 |
"should_scroll": True
|
| 165 |
}
|
| 166 |
-
|
| 167 |
-
def handle_submit(input_field_value):
|
| 168 |
-
"""Main function to handle user submissions"""
|
| 169 |
-
user_input = input_field_value.strip()
|
| 170 |
-
|
| 171 |
-
if not user_input:
|
| 172 |
-
return {"response": "Please enter a question", "should_scroll": False}
|
| 173 |
-
|
| 174 |
-
response = get_best_answer(user_input)
|
| 175 |
-
|
| 176 |
-
# Ensure consistent response format
|
| 177 |
-
if isinstance(response, str):
|
| 178 |
-
return {"response": response, "should_scroll": True}
|
| 179 |
-
elif isinstance(response, dict):
|
| 180 |
-
return response
|
| 181 |
-
else:
|
| 182 |
-
return {"response": "An error occurred while processing your request.", "should_scroll": False}
|
| 183 |
-
|
| 184 |
-
# Example usage
|
| 185 |
-
if __name__ == "__main__":
|
| 186 |
-
# Test the system
|
| 187 |
-
test_questions = [
|
| 188 |
-
"Hello",
|
| 189 |
-
"What's the fee structure?",
|
| 190 |
-
"What are the admission requirements for BSIT?",
|
| 191 |
-
"Short"
|
| 192 |
-
]
|
| 193 |
-
|
| 194 |
-
for question in test_questions:
|
| 195 |
-
print(f"\nQuestion: {question}")
|
| 196 |
-
response = handle_submit(question)
|
| 197 |
-
print(f"Response: {response['response']}")
|
| 198 |
-
print(f"Should scroll: {response['should_scroll']}")
|
|
|
|
| 87 |
print(f"Error querying Groq API: {e}")
|
| 88 |
return ""
|
| 89 |
|
| 90 |
+
def handle_submit():
|
| 91 |
+
user_input = input_field.value.strip()
|
| 92 |
+
|
| 93 |
+
if not user_input:
|
| 94 |
+
show_message("Please enter a question")
|
| 95 |
+
return
|
| 96 |
+
|
| 97 |
+
response = get_best_answer(user_input)
|
| 98 |
+
|
| 99 |
+
if response.get('should_scroll', False):
|
| 100 |
+
scroll_to_answer()
|
| 101 |
+
|
| 102 |
+
display_response(response.get('response', ''))
|
| 103 |
+
|
| 104 |
def get_best_answer(user_input):
|
|
|
|
| 105 |
# 1. Check for empty input
|
| 106 |
if not user_input.strip():
|
| 107 |
+
return {"response": "Please enter a valid question.", "should_scroll": True}
|
| 108 |
|
| 109 |
user_input_lower = user_input.lower().strip()
|
| 110 |
|
| 111 |
# 2. Check for minimum word count (3 words)
|
| 112 |
if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
|
| 113 |
+
return {
|
| 114 |
+
"response": "Please ask your question properly with at least 3 words.",
|
| 115 |
+
"should_scroll": True
|
| 116 |
+
}
|
| 117 |
|
| 118 |
# 3. Handle greetings (regardless of word count)
|
| 119 |
if any(greet in user_input_lower for greet in GREETINGS):
|
|
|
|
| 179 |
"response": response,
|
| 180 |
"should_scroll": True
|
| 181 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|