Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -303,8 +303,10 @@ def generate_prompt(query, context_chunks):
|
|
| 303 |
"""Generates a prompt for the LLM."""
|
| 304 |
context_str = "\n\n".join(context_chunks)
|
| 305 |
liaison_directory_url = "https://libguides.gc.cuny.edu/directory/subject"
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
| 308 |
If your answer identifies a specific librarian or subject liaison, please also include this link to the main subject liaison directory: {liaison_directory_url}
|
| 309 |
|
| 310 |
Context:
|
|
@@ -318,7 +320,7 @@ Answer:"""
|
|
| 318 |
return prompt
|
| 319 |
|
| 320 |
# --- Streamlit App UI ---
|
| 321 |
-
st.title("📚
|
| 322 |
|
| 323 |
# User input (only proceed if collection loaded)
|
| 324 |
if collection:
|
|
@@ -379,35 +381,38 @@ if collection and st.button("Ask"):
|
|
| 379 |
st.warning("Please enter a question.")
|
| 380 |
else:
|
| 381 |
st.markdown("---")
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
route_decision = "RAG"
|
| 395 |
-
|
| 396 |
-
# --- Handle specific routes ---
|
| 397 |
-
if route_decision == "HOURS":
|
| 398 |
-
st.info("You can find the current library hours here: [https://gc-cuny.libcal.com/hours](https://gc-cuny.libcal.com/hours)")
|
| 399 |
-
st.stop()
|
| 400 |
-
# ... (other routes) ...
|
| 401 |
-
elif route_decision == "EVENTS_CALENDAR":
|
| 402 |
-
events_url = "https://gc-cuny.libcal.com/calendar?cid=15537&t=d&d=0000-00-00&cal=15537&inc=0"
|
| 403 |
-
st.info(f"You can find information about upcoming library events and workshops on the calendar here: [{events_url}]({events_url})")
|
| 404 |
-
st.stop()
|
| 405 |
-
# --- End LLM Routing Step ---
|
| 406 |
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
query_variations = generate_query_variations(query, query_hf_inference, HF_GENERATION_MODEL)
|
| 412 |
all_queries = [query] + query_variations
|
| 413 |
logging.info(f"--- DIAGNOSTIC: All queries for search: {all_queries}")
|
|
@@ -531,16 +536,16 @@ if collection and st.button("Ask"):
|
|
| 531 |
# Add instructions or footer
|
| 532 |
st.sidebar.header("About This Demo")
|
| 533 |
st.sidebar.info(
|
| 534 |
-
"This is an experimental RAG demo
|
| 535 |
-
"1.
|
| 536 |
-
"2.
|
| 537 |
-
"3.
|
| 538 |
-
"4. Requires a `HUGGING_FACE_HUB_TOKEN` set as
|
| 539 |
)
|
| 540 |
st.sidebar.header("Configuration Used")
|
| 541 |
-
st.sidebar.markdown(f"**
|
| 542 |
-
st.sidebar.markdown(f"**Query Embedding
|
| 543 |
-
st.sidebar.markdown(f"**Generation LLM
|
| 544 |
st.sidebar.markdown(f"**Vector Store:** ChromaDB (In-Memory)")
|
| 545 |
st.sidebar.markdown(f"**Retrieval Mode:** Vector Search Only")
|
| 546 |
st.sidebar.markdown(f"**Final Unique Chunks:** `{TOP_K}` (from initial `{INITIAL_N_RESULTS}` vector search)")
|
|
|
|
| 303 |
"""Generates a prompt for the LLM."""
|
| 304 |
context_str = "\n\n".join(context_chunks)
|
| 305 |
liaison_directory_url = "https://libguides.gc.cuny.edu/directory/subject"
|
| 306 |
+
# Updated system prompt for clarity
|
| 307 |
+
prompt = f"""You are an AI assistant for the CUNY Graduate Center Library (also known as the Mina Rees Library).
|
| 308 |
+
Based *only* on the following context extracted from the GC Library's LibGuides, answer the user's question about GC Library resources, services, or policies.
|
| 309 |
+
Do not use any prior knowledge. If the context doesn't contain the answer, state that the information wasn't found in the provided LibGuides context.
|
| 310 |
If your answer identifies a specific librarian or subject liaison, please also include this link to the main subject liaison directory: {liaison_directory_url}
|
| 311 |
|
| 312 |
Context:
|
|
|
|
| 320 |
return prompt
|
| 321 |
|
| 322 |
# --- Streamlit App UI ---
|
| 323 |
+
st.title("📚 Ask the CUNY Graduate Center Library (RAG Demo)") # Updated title
|
| 324 |
|
| 325 |
# User input (only proceed if collection loaded)
|
| 326 |
if collection:
|
|
|
|
| 381 |
st.warning("Please enter a question.")
|
| 382 |
else:
|
| 383 |
st.markdown("---")
|
| 384 |
+
# --- LLM Routing Step (Moved Before Spinner) ---
|
| 385 |
+
logging.info(f"Routing query: {query}")
|
| 386 |
+
routing_prompt = ROUTING_PROMPT_TEMPLATE.format(user_query=query)
|
| 387 |
+
try:
|
| 388 |
+
route_decision = query_hf_inference(routing_prompt).strip().upper()
|
| 389 |
+
logging.info(f"LLM (HF API) route decision: {route_decision}")
|
| 390 |
+
if route_decision.startswith("ERROR:"):
|
| 391 |
+
st.error(f"Routing failed: {route_decision}")
|
| 392 |
+
st.stop()
|
| 393 |
+
except Exception as e:
|
| 394 |
+
logging.error(f"LLM (HF API) routing failed: {e}. Defaulting to RAG.")
|
| 395 |
+
route_decision = "RAG" # Default to RAG on routing failure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
+
# --- Handle specific routes immediately ---
|
| 398 |
+
if route_decision == "HOURS":
|
| 399 |
+
st.info("You can find the current library hours here: [https://gc-cuny.libcal.com/hours](https://gc-cuny.libcal.com/hours)")
|
| 400 |
+
st.stop()
|
| 401 |
+
elif route_decision == "EVENTS_CALENDAR":
|
| 402 |
+
events_url = "https://gc-cuny.libcal.com/calendar?cid=15537&t=d&d=0000-00-00&cal=15537&inc=0"
|
| 403 |
+
st.info(f"You can find information about upcoming library events and workshops on the calendar here: [{events_url}]({events_url})")
|
| 404 |
+
st.stop()
|
| 405 |
+
# Add other direct routes here if needed (e.g., CATALOG_SEARCH, ILL_REQUEST)
|
| 406 |
+
# elif route_decision == "CATALOG_SEARCH":
|
| 407 |
+
# st.info("Search the library catalog here: [LINK_TO_CATALOG]")
|
| 408 |
+
# st.stop()
|
| 409 |
+
|
| 410 |
+
# --- Proceed with RAG/Research Query if not handled above ---
|
| 411 |
+
if route_decision in ["RAG", "RESEARCH_QUERY"]: # Only proceed if it's a general or research query
|
| 412 |
+
spinner_text = "Thinking... (RAG)" if route_decision != "RESEARCH_QUERY" else "Thinking... (Research Query)"
|
| 413 |
+
with st.spinner(spinner_text):
|
| 414 |
+
# 1. Generate Query Variations (using HF API)
|
| 415 |
+
logging.info(f"Proceeding with retrieval for query (Route: {route_decision}): {query}")
|
| 416 |
query_variations = generate_query_variations(query, query_hf_inference, HF_GENERATION_MODEL)
|
| 417 |
all_queries = [query] + query_variations
|
| 418 |
logging.info(f"--- DIAGNOSTIC: All queries for search: {all_queries}")
|
|
|
|
| 536 |
# Add instructions or footer
|
| 537 |
st.sidebar.header("About This Demo")
|
| 538 |
st.sidebar.info(
|
| 539 |
+
"This is an experimental RAG demo for the CUNY Graduate Center Library (Mina Rees Library).\n\n"
|
| 540 |
+
"1. Loads pre-computed embeddings from a Hugging Face Dataset.\n"
|
| 541 |
+
"2. Embeds user queries locally.\n"
|
| 542 |
+
"3. Uses the Hugging Face Inference API for LLM generation.\n"
|
| 543 |
+
"4. Requires a `HUGGING_FACE_HUB_TOKEN` (set as Space secret `HF_TOKEN` or in `.env`)."
|
| 544 |
)
|
| 545 |
st.sidebar.header("Configuration Used")
|
| 546 |
+
st.sidebar.markdown(f"**Data Source:** HF Dataset (`{HF_DATASET_ID}`)")
|
| 547 |
+
st.sidebar.markdown(f"**Query Embedding:** Local (`{LOCAL_EMBEDDING_MODEL}`)")
|
| 548 |
+
st.sidebar.markdown(f"**Generation LLM:** HF API (`{HF_GENERATION_MODEL}`)")
|
| 549 |
st.sidebar.markdown(f"**Vector Store:** ChromaDB (In-Memory)")
|
| 550 |
st.sidebar.markdown(f"**Retrieval Mode:** Vector Search Only")
|
| 551 |
st.sidebar.markdown(f"**Final Unique Chunks:** `{TOP_K}` (from initial `{INITIAL_N_RESULTS}` vector search)")
|