tbels89 commited on
Commit
7a8ebb9
Β·
1 Parent(s): e2956fd

updated retriever prompt and UI

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -281,10 +281,13 @@ def build_prompt(user_query, retrieved_chunks, messages):
281
  # Add retrieved chunks as system context
282
  if retrieved_chunks:
283
  chunks_text = "\n".join([f"Page {c['page_num']}:\n{c['content']}" for c in retrieved_chunks])
284
- prompt_messages.append({
285
- "role": "system",
286
- "content": "Retrieved content from textbook. Base response on this as your guiding knowledge. Do not make assumptions - keep your responses based on this context as source of truth:\n" + chunks_text
287
- })
 
 
 
288
 
289
  # Add the user's query
290
  prompt_messages.append({"role": "user", "content": user_query})
@@ -429,7 +432,7 @@ for m in messages:
429
  st.markdown(m['content'])
430
 
431
  # Input area for user query
432
- if prompt := st.chat_input("Ask Prometheus..."):
433
  # If user inputs 'quit', clear conversation history
434
  if prompt.strip().lower() == 'quit':
435
  try:
@@ -445,6 +448,11 @@ if prompt := st.chat_input("Ask Prometheus..."):
445
  finally:
446
  conn.close()
447
  else:
 
 
 
 
 
448
  # Add user message to DB
449
  add_message("user", prompt.strip())
450
 
@@ -456,8 +464,8 @@ if prompt := st.chat_input("Ask Prometheus..."):
456
  results = []
457
  logger.warning("Retriever is not available.")
458
 
459
- # Load updated messages (including user query)
460
- messages = load_messages()
461
 
462
  # Build prompt
463
  llm_prompt = build_prompt(prompt.strip(), results, messages)
@@ -474,7 +482,8 @@ if prompt := st.chat_input("Ask Prometheus..."):
474
 
475
  # Display retrieved sources
476
  if results:
477
- with st.expander("πŸ“– Sources", expanded=True):
 
478
  for i, res in enumerate(results):
479
  st.markdown(f"- **Result {i+1}:** Page {res['page_num']}")
480
  st.code(res['content'])
@@ -482,7 +491,7 @@ if prompt := st.chat_input("Ask Prometheus..."):
482
  st.warning("No relevant sources found.")
483
 
484
  # Optional log viewer
485
- with st.expander("πŸ“œ View Logs (Optional)", expanded=True):
486
  try:
487
  with open(LOG_FILE_PATH, 'r') as log_file:
488
  logs = log_file.read()
 
281
  # Add retrieved chunks as system context
282
  if retrieved_chunks:
283
  chunks_text = "\n".join([f"Page {c['page_num']}:\n{c['content']}" for c in retrieved_chunks])
284
+ retrieved_content_instruction = (
285
+ "You must answer the user query strictly based on the following retrieved content. "
286
+ "Do not use any external knowledge or make assumptions beyond this information. "
287
+ "If the content is insufficient, explain that no answer can be provided because the question is beyond the scope of the course."
288
+ )
289
+ prompt_messages.append({"role": "system", "content": retrieved_content_instruction + "\n" + chunks_text})
290
+
291
 
292
  # Add the user's query
293
  prompt_messages.append({"role": "user", "content": user_query})
 
432
  st.markdown(m['content'])
433
 
434
  # Input area for user query
435
+ if prompt := st.chat_input("Ask Prometheus... Type 'quit' to clear history."):
436
  # If user inputs 'quit', clear conversation history
437
  if prompt.strip().lower() == 'quit':
438
  try:
 
448
  finally:
449
  conn.close()
450
  else:
451
+ # Immediately display user message in the UI
452
+ messages.append({"role": "user", "content": prompt.strip()})
453
+ with st.chat_message("user"):
454
+ st.markdown(prompt.strip())
455
+
456
  # Add user message to DB
457
  add_message("user", prompt.strip())
458
 
 
464
  results = []
465
  logger.warning("Retriever is not available.")
466
 
467
+ # Load updated messages (including user query) - checking if nessessary
468
+ #messages = load_messages()
469
 
470
  # Build prompt
471
  llm_prompt = build_prompt(prompt.strip(), results, messages)
 
482
 
483
  # Display retrieved sources
484
  if results:
485
+ with st.container():
486
+ st.subheader("πŸ“– Sources")
487
  for i, res in enumerate(results):
488
  st.markdown(f"- **Result {i+1}:** Page {res['page_num']}")
489
  st.code(res['content'])
 
491
  st.warning("No relevant sources found.")
492
 
493
  # Optional log viewer
494
+ with st.expander("πŸ“œ View Logs (Optional)", expanded=False):
495
  try:
496
  with open(LOG_FILE_PATH, 'r') as log_file:
497
  logs = log_file.read()