Update app.py
Browse files
app.py
CHANGED
|
@@ -432,104 +432,87 @@ def get_chat_list():
|
|
| 432 |
|
| 433 |
@app.route("/api/chat", methods=["POST"])
|
| 434 |
def chat():
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
|
| 444 |
-
|
|
|
|
| 445 |
data = request.json
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
return jsonify({
|
| 449 |
-
"success": False,
|
| 450 |
-
"response": "No message data received"
|
| 451 |
-
})
|
| 452 |
-
|
| 453 |
user_input = data.get("message", "")
|
| 454 |
session_id = data.get("sessionId", "default")
|
| 455 |
-
|
| 456 |
print(f"Processing message for session {session_id}: {user_input[:100]}...")
|
| 457 |
|
| 458 |
# Get or create session
|
|
|
|
| 459 |
session = ChatSession(session_id)
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
# Extract and store important information
|
| 479 |
-
new_important_info = extract_important_info(raw_response)
|
| 480 |
-
for info in new_important_info:
|
| 481 |
-
session.add_important_info(info)
|
| 482 |
-
print(f"Added important info: {info}")
|
| 483 |
-
|
| 484 |
-
# Format the response with code block structure
|
| 485 |
-
def format_response(response):
|
| 486 |
-
# First, handle code blocks with language specification
|
| 487 |
-
formatted = re.sub(
|
| 488 |
-
r'```(\w+)\n(.*?)\n```',
|
| 489 |
-
lambda m: f'<div class="code-block-wrapper">\n<button class="test-button">Test Code</button>\n<button class="copy-button">Copy Code</button>\n<pre><code class="hljs {m.group(1)}">{m.group(2)}</code></pre>\n<div class="test-results"></div>\n</div>',
|
| 490 |
-
response,
|
| 491 |
-
flags=re.DOTALL
|
| 492 |
-
)
|
| 493 |
-
|
| 494 |
-
# Then handle code blocks without language specification
|
| 495 |
-
formatted = re.sub(
|
| 496 |
-
r'```\n(.*?)\n```',
|
| 497 |
-
lambda m: f'<div class="code-block-wrapper">\n<button class="test-button">Test Code</button>\n<button class="copy-button">Copy Code</button>\n<pre><code class="hljs">{m.group(1)}</code></pre>\n<div class="test-results"></div>\n</div>',
|
| 498 |
-
formatted,
|
| 499 |
-
flags=re.DOTALL
|
| 500 |
-
)
|
| 501 |
-
|
| 502 |
-
# Handle inline code
|
| 503 |
-
formatted = re.sub(
|
| 504 |
-
r'`([^`]+)`',
|
| 505 |
-
r'<code class="inline-code">\1</code>',
|
| 506 |
-
formatted
|
| 507 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
session.add_message("assistant", formatted_response)
|
| 515 |
-
print("Response formatted and stored successfully")
|
| 516 |
-
|
| 517 |
-
return jsonify({
|
| 518 |
-
"success": True,
|
| 519 |
-
"response": formatted_response,
|
| 520 |
-
"important_info": session.important_info
|
| 521 |
-
})
|
| 522 |
|
| 523 |
except Exception as e:
|
| 524 |
import traceback
|
| 525 |
error_trace = traceback.format_exc()
|
| 526 |
print(f"Error in chat endpoint: {str(e)}")
|
| 527 |
-
print(f"
|
| 528 |
|
| 529 |
return jsonify({
|
|
|
|
| 530 |
"success": False,
|
| 531 |
-
"
|
| 532 |
-
"error_details": error_trace
|
| 533 |
})
|
| 534 |
|
| 535 |
|
|
|
|
| 432 |
|
| 433 |
@app.route("/api/chat", methods=["POST"])
|
| 434 |
def chat():
|
| 435 |
+
print("Entering chat endpoint") # Log entry point
|
| 436 |
+
|
| 437 |
+
if not llm_chain:
|
| 438 |
+
print("Error: LLM chain not initialized")
|
| 439 |
+
return jsonify({
|
| 440 |
+
"success": False,
|
| 441 |
+
"response": "System is still initializing. Please try again in a moment."
|
| 442 |
+
})
|
| 443 |
|
| 444 |
+
try:
|
| 445 |
+
# Log incoming request
|
| 446 |
data = request.json
|
| 447 |
+
print(f"Received request data: {data}")
|
| 448 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
user_input = data.get("message", "")
|
| 450 |
session_id = data.get("sessionId", "default")
|
|
|
|
| 451 |
print(f"Processing message for session {session_id}: {user_input[:100]}...")
|
| 452 |
|
| 453 |
# Get or create session
|
| 454 |
+
print("Creating chat session...")
|
| 455 |
session = ChatSession(session_id)
|
| 456 |
|
| 457 |
+
try:
|
| 458 |
+
# Add user message
|
| 459 |
+
print("Adding user message...")
|
| 460 |
+
session.add_message("user", user_input)
|
| 461 |
+
update_chat_metadata(session_id, user_input)
|
| 462 |
+
|
| 463 |
+
# Get memory variables
|
| 464 |
+
print("Getting memory variables...")
|
| 465 |
+
memory_vars = session.get_memory_variables()
|
| 466 |
+
print(f"Memory variables loaded: {str(memory_vars)[:200]}...")
|
| 467 |
+
|
| 468 |
+
# Generate response
|
| 469 |
+
print("Generating LLM response...")
|
| 470 |
+
raw_response = llm_chain.run(
|
| 471 |
+
user_request=user_input,
|
| 472 |
+
chat_history=memory_vars.get("chat_history", ""),
|
| 473 |
+
important_info="\n".join(session.important_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
)
|
| 475 |
+
print(f"Raw response received: {raw_response[:200]}...")
|
| 476 |
+
|
| 477 |
+
# Extract important information
|
| 478 |
+
print("Extracting important info...")
|
| 479 |
+
new_important_info = extract_important_info(raw_response)
|
| 480 |
+
print(f"Extracted important info: {new_important_info}")
|
| 481 |
+
|
| 482 |
+
for info in new_important_info:
|
| 483 |
+
session.add_important_info(info)
|
| 484 |
+
|
| 485 |
+
# Format the response
|
| 486 |
+
print("Formatting response...")
|
| 487 |
+
formatted_response = format_response(raw_response)
|
| 488 |
+
|
| 489 |
+
# Store the response
|
| 490 |
+
print("Storing assistant response...")
|
| 491 |
+
session.add_message("assistant", formatted_response)
|
| 492 |
+
|
| 493 |
+
print("Successfully completed chat processing")
|
| 494 |
+
return jsonify({
|
| 495 |
+
"response": formatted_response,
|
| 496 |
+
"success": True,
|
| 497 |
+
"important_info": session.important_info
|
| 498 |
+
})
|
| 499 |
|
| 500 |
+
except Exception as inner_e:
|
| 501 |
+
print(f"Inner processing error: {str(inner_e)}")
|
| 502 |
+
import traceback
|
| 503 |
+
print(f"Inner error traceback: {traceback.format_exc()}")
|
| 504 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
except Exception as e:
|
| 507 |
import traceback
|
| 508 |
error_trace = traceback.format_exc()
|
| 509 |
print(f"Error in chat endpoint: {str(e)}")
|
| 510 |
+
print(f"Full error traceback: {error_trace}")
|
| 511 |
|
| 512 |
return jsonify({
|
| 513 |
+
"response": f"An error occurred: {str(e)}",
|
| 514 |
"success": False,
|
| 515 |
+
"error_trace": error_trace
|
|
|
|
| 516 |
})
|
| 517 |
|
| 518 |
|