Pamudu13 commited on
Commit
11f7ceb
·
verified ·
1 Parent(s): 45d1b1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -83,16 +83,18 @@ def generate_response(
83
 
84
  context = search_relevant_text(message) # Get relevant content from PDF
85
 
86
- # Start with the system message in the first user message
87
- messages = []
88
- first_msg = f"{system_message}\n\nContext: {context}\nQuestion: {message}"
89
- messages.append({"role": "user", "content": first_msg})
 
 
90
 
91
  # Add conversation history ensuring alternating pattern (user, assistant, user, assistant...)
92
  for user_msg, bot_msg in history:
93
- if user_msg.strip(): # Check if user message is not empty
94
  messages.append({"role": "user", "content": user_msg})
95
- if bot_msg.strip(): # Check if assistant message is not empty
96
  messages.append({"role": "assistant", "content": bot_msg})
97
 
98
  try:
@@ -106,6 +108,7 @@ def generate_response(
106
  print(f"Error generating response: {str(e)}")
107
  return "I apologize, but I encountered an error while generating the response. Please try again."
108
 
 
109
  @app.route('/')
110
  def index():
111
  """Serve the HTML page for the user interface"""
 
83
 
84
  context = search_relevant_text(message) # Get relevant content from PDF
85
 
86
+ if not context.strip(): # If no relevant content is found, refuse to answer
87
+ return "I can only answer based on the uploaded PDF. Your question is outside the document's content."
88
+
89
+ messages = [
90
+ {"role": "user", "content": f"{system_message}\n\nContext: {context}\nQuestion: {message}"}
91
+ ]
92
 
93
  # Add conversation history ensuring alternating pattern (user, assistant, user, assistant...)
94
  for user_msg, bot_msg in history:
95
+ if user_msg.strip():
96
  messages.append({"role": "user", "content": user_msg})
97
+ if bot_msg.strip():
98
  messages.append({"role": "assistant", "content": bot_msg})
99
 
100
  try:
 
108
  print(f"Error generating response: {str(e)}")
109
  return "I apologize, but I encountered an error while generating the response. Please try again."
110
 
111
+
112
  @app.route('/')
113
  def index():
114
  """Serve the HTML page for the user interface"""