Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import torch
|
|
@@ -53,6 +52,13 @@ class ChatHistory:
|
|
| 53 |
def clear(self):
|
| 54 |
self.messages = []
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Load environment variables and setup
|
| 57 |
load_dotenv()
|
| 58 |
|
|
@@ -102,25 +108,6 @@ retriever = db.as_retriever(
|
|
| 102 |
search_kwargs={"k": 5}
|
| 103 |
)
|
| 104 |
|
| 105 |
-
#llm = ChatCerebras(
|
| 106 |
-
# model="llama-3.3-70b",
|
| 107 |
-
# api_key=C_apikey,
|
| 108 |
-
# streaming=True
|
| 109 |
-
#)
|
| 110 |
-
|
| 111 |
-
# llm = ChatOpenAI(
|
| 112 |
-
# model="meta-llama/Llama-3.3-70B-Instruct",
|
| 113 |
-
# temperature=0,
|
| 114 |
-
# max_tokens=None,
|
| 115 |
-
# timeout=None,
|
| 116 |
-
# max_retries=2,
|
| 117 |
-
# api_key=HF_TOKEN, # if you prefer to pass api key in directly instaed of using env vars
|
| 118 |
-
# base_url="https://api-inference.huggingface.co/v1/",
|
| 119 |
-
# stream=True,
|
| 120 |
-
|
| 121 |
-
# )
|
| 122 |
-
|
| 123 |
-
|
| 124 |
llm = ChatGoogleGenerativeAI(
|
| 125 |
model="gemini-2.0-flash-thinking-exp-01-21",
|
| 126 |
temperature=0,
|
|
@@ -129,7 +116,6 @@ llm = ChatGoogleGenerativeAI(
|
|
| 129 |
max_retries=2,
|
| 130 |
api_key=GEMINI,
|
| 131 |
stream=True,
|
| 132 |
-
|
| 133 |
)
|
| 134 |
|
| 135 |
template = """
|
|
@@ -142,113 +128,70 @@ Core Principles
|
|
| 142 |
|
| 143 |
1. Source of Truth: Rely exclusively on the information available in the retrieved context and chat history. Avoid fabricating details or using external knowledge.
|
| 144 |
|
| 145 |
-
|
| 146 |
2. Clarity and Precision: Provide clear, concise, and professional responses, ensuring they are easy to understand.
|
| 147 |
|
| 148 |
-
|
| 149 |
3. Actionable Guidance: Offer practical solutions, step-by-step workflows, and troubleshooting advice tailored to Mawared HR queries.
|
| 150 |
|
| 151 |
-
|
| 152 |
4. Structured Instructions: Use numbered or bullet-point lists for complex processes to ensure clarity.
|
| 153 |
|
| 154 |
-
|
| 155 |
5. Targeted Clarification: Ask specific, polite questions to gather missing details when a query lacks sufficient information.
|
| 156 |
|
| 157 |
-
|
| 158 |
6. Exclusive Focus: Limit your responses strictly to Mawared HR-related topics, avoiding unrelated discussions.
|
| 159 |
|
| 160 |
-
|
| 161 |
7. Professional Tone: Maintain a friendly, approachable, and professional demeanor in all communications.
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
---
|
| 167 |
|
| 168 |
Response Guidelines
|
| 169 |
|
| 170 |
1. Analyze the Query Thoughtfully
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
Identify the userβs explicit intent and infer additional context where applicable.
|
| 175 |
-
|
| 176 |
-
Note any gaps in the provided information.
|
| 177 |
-
|
| 178 |
|
| 179 |
2. Break Down Context Relevance
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
Match the user's needs to the most applicable information available.
|
| 184 |
-
|
| 185 |
|
| 186 |
3. Develop the Response Step-by-Step
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
What
|
| 191 |
-
|
| 192 |
-
Which parts of the context directly address this?
|
| 193 |
-
|
| 194 |
-
What steps or details should be highlighted for clarity?
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
Provide answers in a structured, easy-to-follow format, using numbered steps or bullet points.
|
| 198 |
-
|
| 199 |
|
| 200 |
4. Ask for Clarifications Strategically
|
| 201 |
-
|
| 202 |
-
If details are insufficient, specify the missing information politely and clearly (e.g., βCould you confirm [specific detail] to proceed with [action/task]?β).
|
| 203 |
-
|
| 204 |
|
| 205 |
5. Ensure Directness and Professionalism
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
Uphold a professional and courteous tone throughout.
|
| 210 |
-
|
| 211 |
|
| 212 |
6. Double-Check for Exclusivity
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
Avoid speculating or introducing external information about Mawared HR.
|
| 217 |
-
|
| 218 |
-
|
| 219 |
|
| 220 |
---
|
| 221 |
|
| 222 |
Handling Information Gaps
|
| 223 |
|
| 224 |
If the context is insufficient to answer the query:
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
Specify what information is required.
|
| 229 |
-
|
| 230 |
-
Avoid fabricating or making assumptions to fill gaps.
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
|
| 235 |
---
|
| 236 |
|
| 237 |
Critical Constraints
|
| 238 |
|
| 239 |
-
Strict Context Reliance: Base all responses solely on the provided context and chat history.
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
Answer Format: Always provide accurate answers in numbered steps without revealing your thought process or using code.
|
| 244 |
-
|
| 245 |
-
|
| 246 |
|
| 247 |
---
|
| 248 |
|
| 249 |
By adhering to these principles and guidelines, ensure every response is accurate, professional, and easy to follow.
|
| 250 |
|
| 251 |
-
|
| 252 |
Previous Conversation: {chat_history}
|
| 253 |
Retrieved Context: {context}
|
| 254 |
Current Question: {question}
|
|
@@ -324,6 +267,9 @@ def ask_question_gradio(question: str, history: List[List[str]]) -> Generator[tu
|
|
| 324 |
# Add final response to chat history
|
| 325 |
chat_history.add_message("assistant", response)
|
| 326 |
|
|
|
|
|
|
|
|
|
|
| 327 |
except Exception as e:
|
| 328 |
logger.error(f"Error during question processing: {e}")
|
| 329 |
if not history:
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import os
|
| 3 |
import torch
|
|
|
|
| 52 |
def clear(self):
|
| 53 |
self.messages = []
|
| 54 |
|
| 55 |
+
# Function to log questions and answers to a file
|
| 56 |
+
def log_to_file(question: str, answer: str):
|
| 57 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
+
log_entry = f"Timestamp: {timestamp}\nQuestion: {question}\nAnswer: {answer}\n\n"
|
| 59 |
+
with open("Logs.txt", "a") as log_file:
|
| 60 |
+
log_file.write(log_entry)
|
| 61 |
+
|
| 62 |
# Load environment variables and setup
|
| 63 |
load_dotenv()
|
| 64 |
|
|
|
|
| 108 |
search_kwargs={"k": 5}
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
llm = ChatGoogleGenerativeAI(
|
| 112 |
model="gemini-2.0-flash-thinking-exp-01-21",
|
| 113 |
temperature=0,
|
|
|
|
| 116 |
max_retries=2,
|
| 117 |
api_key=GEMINI,
|
| 118 |
stream=True,
|
|
|
|
| 119 |
)
|
| 120 |
|
| 121 |
template = """
|
|
|
|
| 128 |
|
| 129 |
1. Source of Truth: Rely exclusively on the information available in the retrieved context and chat history. Avoid fabricating details or using external knowledge.
|
| 130 |
|
|
|
|
| 131 |
2. Clarity and Precision: Provide clear, concise, and professional responses, ensuring they are easy to understand.
|
| 132 |
|
|
|
|
| 133 |
3. Actionable Guidance: Offer practical solutions, step-by-step workflows, and troubleshooting advice tailored to Mawared HR queries.
|
| 134 |
|
|
|
|
| 135 |
4. Structured Instructions: Use numbered or bullet-point lists for complex processes to ensure clarity.
|
| 136 |
|
|
|
|
| 137 |
5. Targeted Clarification: Ask specific, polite questions to gather missing details when a query lacks sufficient information.
|
| 138 |
|
|
|
|
| 139 |
6. Exclusive Focus: Limit your responses strictly to Mawared HR-related topics, avoiding unrelated discussions.
|
| 140 |
|
|
|
|
| 141 |
7. Professional Tone: Maintain a friendly, approachable, and professional demeanor in all communications.
|
| 142 |
|
|
|
|
|
|
|
|
|
|
| 143 |
---
|
| 144 |
|
| 145 |
Response Guidelines
|
| 146 |
|
| 147 |
1. Analyze the Query Thoughtfully
|
| 148 |
+
Carefully review the userβs question and the chat history.
|
| 149 |
+
Identify the userβs explicit intent and infer additional context where applicable.
|
| 150 |
+
Note any gaps in the provided information.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
2. Break Down Context Relevance
|
| 153 |
+
Extract and interpret relevant details from the provided context or chat history.
|
| 154 |
+
Match the user's needs to the most applicable information available.
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
3. Develop the Response Step-by-Step
|
| 157 |
+
Frame a clear, logical structure to your response:
|
| 158 |
+
- What is the user trying to achieve?
|
| 159 |
+
- Which parts of the context directly address this?
|
| 160 |
+
- What steps or details should be highlighted for clarity?
|
| 161 |
+
Provide answers in a structured, easy-to-follow format, using numbered steps or bullet points.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
4. Ask for Clarifications Strategically
|
| 164 |
+
If details are insufficient, specify the missing information politely and clearly (e.g., βCould you confirm [specific detail] to proceed with [action/task]?β).
|
|
|
|
|
|
|
| 165 |
|
| 166 |
5. Ensure Directness and Professionalism
|
| 167 |
+
Keep responses focused, avoiding unnecessary elaboration or irrelevant details.
|
| 168 |
+
Uphold a professional and courteous tone throughout.
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
6. Double-Check for Exclusivity
|
| 171 |
+
Verify that all guidance is strictly derived from the retrieved context or chat history.
|
| 172 |
+
Avoid speculating or introducing external information about Mawared HR.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
---
|
| 175 |
|
| 176 |
Handling Information Gaps
|
| 177 |
|
| 178 |
If the context is insufficient to answer the query:
|
| 179 |
+
- Clearly state that additional details are needed.
|
| 180 |
+
- Specify what information is required.
|
| 181 |
+
- Avoid fabricating or making assumptions to fill gaps.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
---
|
| 184 |
|
| 185 |
Critical Constraints
|
| 186 |
|
| 187 |
+
- Strict Context Reliance: Base all responses solely on the provided context and chat history.
|
| 188 |
+
- Non-Mawared HR Queries: Politely decline to answer questions unrelated to Mawared HR.
|
| 189 |
+
- Answer Format: Always provide accurate answers in numbered steps without revealing your thought process or using code.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
---
|
| 192 |
|
| 193 |
By adhering to these principles and guidelines, ensure every response is accurate, professional, and easy to follow.
|
| 194 |
|
|
|
|
| 195 |
Previous Conversation: {chat_history}
|
| 196 |
Retrieved Context: {context}
|
| 197 |
Current Question: {question}
|
|
|
|
| 267 |
# Add final response to chat history
|
| 268 |
chat_history.add_message("assistant", response)
|
| 269 |
|
| 270 |
+
# Log the question and answer to a file
|
| 271 |
+
log_to_file(question, response)
|
| 272 |
+
|
| 273 |
except Exception as e:
|
| 274 |
logger.error(f"Error during question processing: {e}")
|
| 275 |
if not history:
|