Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -157,12 +157,13 @@ Rules:
|
|
| 157 |
from alert import execute_function
|
| 158 |
return execute_function(function_name, kwargs)
|
| 159 |
|
| 160 |
-
def process_query(self, user_message: str, chat_id: Optional[str] = None, image_url: Optional[str] = None) -> Dict[str, Any]:
|
| 161 |
"""
|
| 162 |
-
Process user query with improved conversation management
|
| 163 |
|
| 164 |
Args:
|
| 165 |
user_message: The user's message
|
|
|
|
| 166 |
chat_id: Optional chat ID. If None, generates a new one
|
| 167 |
image_url: Optional image URL
|
| 168 |
|
|
@@ -170,20 +171,31 @@ Rules:
|
|
| 170 |
Dictionary containing response, chat_id, and message IDs
|
| 171 |
"""
|
| 172 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
# Handle chat ID
|
| 174 |
if not chat_id:
|
| 175 |
-
chat_id = self.manager.generate_chat_id()
|
| 176 |
is_new_chat = True
|
| 177 |
-
logger.info(f"Generated new chat ID: {chat_id}")
|
| 178 |
else:
|
| 179 |
-
is_new_chat = not self.manager.chat_exists(chat_id)
|
| 180 |
if is_new_chat:
|
| 181 |
-
logger.info(f"Creating new chat with provided ID: {chat_id}")
|
| 182 |
else:
|
| 183 |
-
logger.info(f"Continuing existing chat: {chat_id}")
|
| 184 |
|
| 185 |
-
# Get conversation history
|
| 186 |
-
conversation_history = self.manager.get_history(chat_id)
|
| 187 |
|
| 188 |
# Prepare messages for AI
|
| 189 |
messages = [{"role": "system", "content": self.system_prompt}]
|
|
@@ -264,6 +276,65 @@ Rules:
|
|
| 264 |
else:
|
| 265 |
response_content = message.content
|
| 266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
# Add messages to conversation history with unique IDs
|
| 268 |
user_msg = self.manager.add_message(chat_id, "user", user_message, image_url)
|
| 269 |
assistant_msg = self.manager.add_message(chat_id, "assistant", response_content)
|
|
|
|
| 157 |
from alert import execute_function
|
| 158 |
return execute_function(function_name, kwargs)
|
| 159 |
|
| 160 |
+
def process_query(self, user_message: str, user_id: str, chat_id: Optional[str] = None, image_url: Optional[str] = None) -> Dict[str, Any]:
|
| 161 |
"""
|
| 162 |
+
Process user query with user authentication and improved conversation management
|
| 163 |
|
| 164 |
Args:
|
| 165 |
user_message: The user's message
|
| 166 |
+
user_id: The user ID for authentication and isolation
|
| 167 |
chat_id: Optional chat ID. If None, generates a new one
|
| 168 |
image_url: Optional image URL
|
| 169 |
|
|
|
|
| 171 |
Dictionary containing response, chat_id, and message IDs
|
| 172 |
"""
|
| 173 |
try:
|
| 174 |
+
# Validate user_id
|
| 175 |
+
if not user_id:
|
| 176 |
+
return {
|
| 177 |
+
"error": "User ID is required for authentication",
|
| 178 |
+
"chat_id": None,
|
| 179 |
+
"is_new_chat": True,
|
| 180 |
+
"user_message_id": None,
|
| 181 |
+
"assistant_message_id": None,
|
| 182 |
+
"total_messages": 0
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
# Handle chat ID
|
| 186 |
if not chat_id:
|
| 187 |
+
chat_id = self.manager.generate_chat_id(user_id)
|
| 188 |
is_new_chat = True
|
| 189 |
+
logger.info(f"Generated new chat ID: {chat_id} for user: {user_id}")
|
| 190 |
else:
|
| 191 |
+
is_new_chat = not self.manager.chat_exists(chat_id, user_id)
|
| 192 |
if is_new_chat:
|
| 193 |
+
logger.info(f"Creating new chat with provided ID: {chat_id} for user: {user_id}")
|
| 194 |
else:
|
| 195 |
+
logger.info(f"Continuing existing chat: {chat_id} for user: {user_id}")
|
| 196 |
|
| 197 |
+
# Get conversation history for this user
|
| 198 |
+
conversation_history = self.manager.get_history(chat_id, user_id)
|
| 199 |
|
| 200 |
# Prepare messages for AI
|
| 201 |
messages = [{"role": "system", "content": self.system_prompt}]
|
|
|
|
| 276 |
else:
|
| 277 |
response_content = message.content
|
| 278 |
|
| 279 |
+
# Add messages to conversation history with unique IDs
|
| 280 |
+
user_msg = self.manager.add_message(chat_id, user_id, "user", user_message, image_url)
|
| 281 |
+
assistant_msg = self.manager.add_message(chat_id, user_id, "assistant", response_content)
|
| 282 |
+
|
| 283 |
+
return {
|
| 284 |
+
"response": response_content,
|
| 285 |
+
"chat_id": chat_id,
|
| 286 |
+
"is_new_chat": is_new_chat,
|
| 287 |
+
"user_message_id": user_msg.get("message_id"),
|
| 288 |
+
"assistant_message_id": assistant_msg.get("message_id"),
|
| 289 |
+
"total_messages": len(self.manager.get_history(chat_id, user_id))
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
except Exception as e:
|
| 293 |
+
logger.error(f"Error processing query for chat {chat_id}, user {user_id}: {e}")
|
| 294 |
+
return {
|
| 295 |
+
"error": f"I apologize, but I encountered an error: {str(e)}. Please try again or rephrase your question.",
|
| 296 |
+
"chat_id": chat_id or self.manager.generate_chat_id(user_id) if user_id else None,
|
| 297 |
+
"is_new_chat": True,
|
| 298 |
+
"user_message_id": None,
|
| 299 |
+
"assistant_message_id": None,
|
| 300 |
+
"total_messages": 0
|
| 301 |
+
}.tool_calls
|
| 302 |
+
]
|
| 303 |
+
})
|
| 304 |
+
|
| 305 |
+
# Execute all tool calls
|
| 306 |
+
for tool_call in message.tool_calls:
|
| 307 |
+
function_name = tool_call.function.name
|
| 308 |
+
function_args = json.loads(tool_call.function.arguments)
|
| 309 |
+
|
| 310 |
+
logger.info(f"Calling function: {function_name} with args: {function_args}")
|
| 311 |
+
|
| 312 |
+
# Call the function
|
| 313 |
+
function_result = self.call_function(function_name, function_args)
|
| 314 |
+
|
| 315 |
+
# Add function result to messages
|
| 316 |
+
messages.append({
|
| 317 |
+
"role": "tool",
|
| 318 |
+
"tool_call_id": tool_call.id,
|
| 319 |
+
"content": json.dumps(function_result)
|
| 320 |
+
})
|
| 321 |
+
|
| 322 |
+
# Add final system prompt for generating response
|
| 323 |
+
messages.append({
|
| 324 |
+
"role": "system",
|
| 325 |
+
"content": self.final_system
|
| 326 |
+
})
|
| 327 |
+
|
| 328 |
+
# Get final response
|
| 329 |
+
final_response = self.client.chat.completions.create(
|
| 330 |
+
model=self.config.model_name,
|
| 331 |
+
messages=messages,
|
| 332 |
+
temperature=self.config.temperature
|
| 333 |
+
)
|
| 334 |
+
response_content = final_response.choices[0].message.content
|
| 335 |
+
else:
|
| 336 |
+
response_content = message.content
|
| 337 |
+
|
| 338 |
# Add messages to conversation history with unique IDs
|
| 339 |
user_msg = self.manager.add_message(chat_id, "user", user_message, image_url)
|
| 340 |
assistant_msg = self.manager.add_message(chat_id, "assistant", response_content)
|