Update app.py
Browse files
app.py
CHANGED
|
@@ -432,45 +432,61 @@ 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 |
-
|
| 446 |
|
| 447 |
-
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
session.add_message("user", user_input)
|
| 450 |
update_chat_metadata(session_id, user_input)
|
| 451 |
|
| 452 |
# Get memory variables
|
| 453 |
memory_vars = session.get_memory_variables()
|
|
|
|
| 454 |
|
| 455 |
# Generate response
|
|
|
|
| 456 |
raw_response = llm_chain.run(
|
| 457 |
user_request=user_input,
|
| 458 |
chat_history=memory_vars.get("chat_history", ""),
|
| 459 |
important_info="\n".join(session.important_info)
|
| 460 |
)
|
|
|
|
| 461 |
|
| 462 |
# Extract and store important information
|
| 463 |
new_important_info = extract_important_info(raw_response)
|
| 464 |
for info in new_important_info:
|
| 465 |
session.add_important_info(info)
|
|
|
|
| 466 |
|
| 467 |
-
# Format the response
|
| 468 |
def format_response(response):
|
| 469 |
# First, handle code blocks with language specification
|
| 470 |
formatted = re.sub(
|
| 471 |
r'```(\w+)\n(.*?)\n```',
|
| 472 |
-
lambda
|
| 473 |
-
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>',
|
| 474 |
response,
|
| 475 |
flags=re.DOTALL
|
| 476 |
)
|
|
@@ -478,8 +494,7 @@ def chat():
|
|
| 478 |
# Then handle code blocks without language specification
|
| 479 |
formatted = re.sub(
|
| 480 |
r'```\n(.*?)\n```',
|
| 481 |
-
lambda
|
| 482 |
-
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>',
|
| 483 |
formatted,
|
| 484 |
flags=re.DOTALL
|
| 485 |
)
|
|
@@ -493,22 +508,28 @@ def chat():
|
|
| 493 |
|
| 494 |
return formatted
|
| 495 |
|
| 496 |
-
# Format the response
|
|
|
|
| 497 |
formatted_response = format_response(raw_response)
|
| 498 |
-
|
| 499 |
-
# Store the formatted response
|
| 500 |
session.add_message("assistant", formatted_response)
|
|
|
|
| 501 |
|
| 502 |
return jsonify({
|
| 503 |
-
"response": formatted_response,
|
| 504 |
"success": True,
|
|
|
|
| 505 |
"important_info": session.important_info
|
| 506 |
})
|
| 507 |
|
| 508 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
return jsonify({
|
| 510 |
-
"
|
| 511 |
-
"
|
|
|
|
| 512 |
})
|
| 513 |
|
| 514 |
|
|
|
|
| 432 |
|
| 433 |
@app.route("/api/chat", methods=["POST"])
|
| 434 |
def chat():
|
| 435 |
+
try:
|
| 436 |
+
# Check if LLM chain is initialized
|
| 437 |
+
if not llm_chain:
|
| 438 |
+
print("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 |
+
# Get request data
|
| 445 |
+
data = request.json
|
| 446 |
+
if not data:
|
| 447 |
+
print("No data received in request")
|
| 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 |
+
# Add user message and update metadata
|
| 462 |
session.add_message("user", user_input)
|
| 463 |
update_chat_metadata(session_id, user_input)
|
| 464 |
|
| 465 |
# Get memory variables
|
| 466 |
memory_vars = session.get_memory_variables()
|
| 467 |
+
print("Memory variables loaded successfully")
|
| 468 |
|
| 469 |
# Generate response
|
| 470 |
+
print("Generating response...")
|
| 471 |
raw_response = llm_chain.run(
|
| 472 |
user_request=user_input,
|
| 473 |
chat_history=memory_vars.get("chat_history", ""),
|
| 474 |
important_info="\n".join(session.important_info)
|
| 475 |
)
|
| 476 |
+
print(f"Raw response generated: {raw_response[:100]}...")
|
| 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 |
)
|
|
|
|
| 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 |
)
|
|
|
|
| 508 |
|
| 509 |
return formatted
|
| 510 |
|
| 511 |
+
# Format and store the response
|
| 512 |
+
print("Formatting response...")
|
| 513 |
formatted_response = format_response(raw_response)
|
|
|
|
|
|
|
| 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"Traceback: {error_trace}")
|
| 528 |
+
|
| 529 |
return jsonify({
|
| 530 |
+
"success": False,
|
| 531 |
+
"response": f"An error occurred while processing your message: {str(e)}",
|
| 532 |
+
"error_details": error_trace
|
| 533 |
})
|
| 534 |
|
| 535 |
|