Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -164,35 +164,35 @@ async def chat(request: ChatRequest):
|
|
| 164 |
"""Send a question to the chatbot with user session isolation"""
|
| 165 |
if base_chatbot is None:
|
| 166 |
raise HTTPException(status_code=503, detail="Chatbot not initialized")
|
| 167 |
-
|
| 168 |
if not request.question.strip():
|
| 169 |
raise HTTPException(status_code=400, detail="Question cannot be empty")
|
| 170 |
-
|
| 171 |
if not request.user_id:
|
| 172 |
raise HTTPException(status_code=400, detail="user_id is required")
|
| 173 |
-
|
| 174 |
try:
|
| 175 |
logger.info(f"User {request.user_id}: {request.question[:50]}...")
|
| 176 |
-
|
| 177 |
# Get user session
|
| 178 |
session = get_or_create_session(request.user_id)
|
| 179 |
-
|
| 180 |
# Resolve pronouns using user's context
|
| 181 |
resolved_question = base_chatbot._resolve_pronouns_for_session(
|
| 182 |
-
request.question,
|
| 183 |
session.conversation_context
|
| 184 |
)
|
| 185 |
-
|
| 186 |
# Retrieve relevant chunks
|
| 187 |
retrieved_data = base_chatbot._retrieve(resolved_question, k=20)
|
| 188 |
-
|
| 189 |
# Search user's chat history (not global)
|
| 190 |
relevant_past_chats = base_chatbot._search_session_history(
|
| 191 |
resolved_question,
|
| 192 |
session.chat_history,
|
| 193 |
k=5
|
| 194 |
)
|
| 195 |
-
|
| 196 |
# Build prompt with user's context
|
| 197 |
prompt = base_chatbot._build_prompt_for_session(
|
| 198 |
resolved_question,
|
|
@@ -201,26 +201,23 @@ async def chat(request: ChatRequest):
|
|
| 201 |
session.chat_history,
|
| 202 |
session.conversation_context
|
| 203 |
)
|
| 204 |
-
|
| 205 |
-
#
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
response = base_chatbot.llm_client.chat.completions.create(
|
| 209 |
model="meta-llama/Llama-3.1-8B-Instruct",
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
)
|
| 214 |
-
|
| 215 |
-
answer = response.choices[0].message.content
|
| 216 |
-
|
| 217 |
# Update user's conversation context
|
| 218 |
base_chatbot._update_conversation_context_for_session(
|
| 219 |
request.question,
|
| 220 |
answer,
|
| 221 |
session.conversation_context
|
| 222 |
)
|
| 223 |
-
|
| 224 |
# Store in user's history
|
| 225 |
chat_entry = {
|
| 226 |
'timestamp': datetime.now().isoformat(),
|
|
@@ -229,7 +226,7 @@ async def chat(request: ChatRequest):
|
|
| 229 |
'used_past_context': len(relevant_past_chats) > 0
|
| 230 |
}
|
| 231 |
session.chat_history.append(chat_entry)
|
| 232 |
-
|
| 233 |
response_data = ChatResponse(
|
| 234 |
question=request.question,
|
| 235 |
answer=answer,
|
|
@@ -240,15 +237,14 @@ async def chat(request: ChatRequest):
|
|
| 240 |
'current_context': session.conversation_context.get('current_employee')
|
| 241 |
}
|
| 242 |
)
|
| 243 |
-
|
| 244 |
logger.info(f"User {request.user_id}: Question processed successfully")
|
| 245 |
return response_data
|
| 246 |
-
|
| 247 |
except Exception as e:
|
| 248 |
logger.error(f"Error for user {request.user_id}: {e}")
|
| 249 |
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 250 |
|
| 251 |
-
|
| 252 |
@app.post("/api/reset")
|
| 253 |
async def reset_chat(user_id: str):
|
| 254 |
"""Reset chat history for specific user"""
|
|
|
|
| 164 |
"""Send a question to the chatbot with user session isolation"""
|
| 165 |
if base_chatbot is None:
|
| 166 |
raise HTTPException(status_code=503, detail="Chatbot not initialized")
|
| 167 |
+
|
| 168 |
if not request.question.strip():
|
| 169 |
raise HTTPException(status_code=400, detail="Question cannot be empty")
|
| 170 |
+
|
| 171 |
if not request.user_id:
|
| 172 |
raise HTTPException(status_code=400, detail="user_id is required")
|
| 173 |
+
|
| 174 |
try:
|
| 175 |
logger.info(f"User {request.user_id}: {request.question[:50]}...")
|
| 176 |
+
|
| 177 |
# Get user session
|
| 178 |
session = get_or_create_session(request.user_id)
|
| 179 |
+
|
| 180 |
# Resolve pronouns using user's context
|
| 181 |
resolved_question = base_chatbot._resolve_pronouns_for_session(
|
| 182 |
+
request.question,
|
| 183 |
session.conversation_context
|
| 184 |
)
|
| 185 |
+
|
| 186 |
# Retrieve relevant chunks
|
| 187 |
retrieved_data = base_chatbot._retrieve(resolved_question, k=20)
|
| 188 |
+
|
| 189 |
# Search user's chat history (not global)
|
| 190 |
relevant_past_chats = base_chatbot._search_session_history(
|
| 191 |
resolved_question,
|
| 192 |
session.chat_history,
|
| 193 |
k=5
|
| 194 |
)
|
| 195 |
+
|
| 196 |
# Build prompt with user's context
|
| 197 |
prompt = base_chatbot._build_prompt_for_session(
|
| 198 |
resolved_question,
|
|
|
|
| 201 |
session.chat_history,
|
| 202 |
session.conversation_context
|
| 203 |
)
|
| 204 |
+
|
| 205 |
+
# ✅ CORRECT: Use text_generation for InferenceClient
|
| 206 |
+
answer = base_chatbot.llm_client.text_generation(
|
| 207 |
+
prompt,
|
|
|
|
| 208 |
model="meta-llama/Llama-3.1-8B-Instruct",
|
| 209 |
+
max_new_tokens=512,
|
| 210 |
+
temperature=0.3,
|
| 211 |
+
return_full_text=False
|
| 212 |
)
|
| 213 |
+
|
|
|
|
|
|
|
| 214 |
# Update user's conversation context
|
| 215 |
base_chatbot._update_conversation_context_for_session(
|
| 216 |
request.question,
|
| 217 |
answer,
|
| 218 |
session.conversation_context
|
| 219 |
)
|
| 220 |
+
|
| 221 |
# Store in user's history
|
| 222 |
chat_entry = {
|
| 223 |
'timestamp': datetime.now().isoformat(),
|
|
|
|
| 226 |
'used_past_context': len(relevant_past_chats) > 0
|
| 227 |
}
|
| 228 |
session.chat_history.append(chat_entry)
|
| 229 |
+
|
| 230 |
response_data = ChatResponse(
|
| 231 |
question=request.question,
|
| 232 |
answer=answer,
|
|
|
|
| 237 |
'current_context': session.conversation_context.get('current_employee')
|
| 238 |
}
|
| 239 |
)
|
| 240 |
+
|
| 241 |
logger.info(f"User {request.user_id}: Question processed successfully")
|
| 242 |
return response_data
|
| 243 |
+
|
| 244 |
except Exception as e:
|
| 245 |
logger.error(f"Error for user {request.user_id}: {e}")
|
| 246 |
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 247 |
|
|
|
|
| 248 |
@app.post("/api/reset")
|
| 249 |
async def reset_chat(user_id: str):
|
| 250 |
"""Reset chat history for specific user"""
|