Spaces:
Sleeping
Sleeping
Commit ·
7e9f52f
1
Parent(s): 91410f8
feat: LLM-generated welcome summary for knowledge base contents
Browse filesReplace raw KB overview on welcome page with a concise, friendly
LLM-generated summary that highlights available topics, data volume,
and suggests example questions. Falls back to truncated raw text if
LLM call fails. Web UI caches summary in session state.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- app_cli.py +6 -14
- app_web.py +7 -9
- src/kb_meta.py +60 -0
app_cli.py
CHANGED
|
@@ -9,7 +9,7 @@ from rich.text import Text
|
|
| 9 |
|
| 10 |
from src.config_loader import load_config, get_api_key
|
| 11 |
from src.ingest import ingest_documents
|
| 12 |
-
from src.kb_meta import
|
| 13 |
from src.query_engine import understand_query
|
| 14 |
from src.retriever import retrieve
|
| 15 |
from src.verifier import verify_and_respond
|
|
@@ -214,19 +214,11 @@ def main() -> None:
|
|
| 214 |
welcome.append("OFF", style="bold red")
|
| 215 |
welcome.append(". Use /websearch on to enable.\n\n")
|
| 216 |
|
| 217 |
-
# KB overview summary
|
| 218 |
-
|
| 219 |
-
if
|
| 220 |
-
welcome.append("Knowledge Base
|
| 221 |
-
|
| 222 |
-
overview_body = kb_overview
|
| 223 |
-
for marker in ("=== KNOWLEDGE BASE OVERVIEW ===", "=== END OVERVIEW ==="):
|
| 224 |
-
overview_body = overview_body.replace(marker, "")
|
| 225 |
-
overview_body = overview_body.strip()
|
| 226 |
-
# Truncate for the welcome panel (keep first ~600 chars)
|
| 227 |
-
if len(overview_body) > 600:
|
| 228 |
-
overview_body = overview_body[:600].rsplit("\n", 1)[0] + "\n..."
|
| 229 |
-
welcome.append(overview_body + "\n\n", style="dim")
|
| 230 |
|
| 231 |
welcome.append("Type your question, or /help for commands.\n", style="italic")
|
| 232 |
welcome.append("Type /quit to exit.", style="italic dim")
|
|
|
|
| 9 |
|
| 10 |
from src.config_loader import load_config, get_api_key
|
| 11 |
from src.ingest import ingest_documents
|
| 12 |
+
from src.kb_meta import summarize_kb_for_welcome
|
| 13 |
from src.query_engine import understand_query
|
| 14 |
from src.retriever import retrieve
|
| 15 |
from src.verifier import verify_and_respond
|
|
|
|
| 214 |
welcome.append("OFF", style="bold red")
|
| 215 |
welcome.append(". Use /websearch on to enable.\n\n")
|
| 216 |
|
| 217 |
+
# KB overview summary (LLM-generated welcome summary)
|
| 218 |
+
kb_summary = summarize_kb_for_welcome(cfg)
|
| 219 |
+
if kb_summary:
|
| 220 |
+
welcome.append("Knowledge Base:\n", style="bold")
|
| 221 |
+
welcome.append(kb_summary + "\n\n", style="dim")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
welcome.append("Type your question, or /help for commands.\n", style="italic")
|
| 224 |
welcome.append("Type /quit to exit.", style="italic dim")
|
app_web.py
CHANGED
|
@@ -4,7 +4,7 @@ import streamlit as st
|
|
| 4 |
|
| 5 |
from src.config_loader import load_config, get_api_key
|
| 6 |
from src.ingest import get_chroma_collection, ingest_documents
|
| 7 |
-
from src.kb_meta import
|
| 8 |
from src.query_engine import understand_query
|
| 9 |
from src.retriever import retrieve
|
| 10 |
from src.verifier import verify_and_respond
|
|
@@ -289,16 +289,14 @@ def main():
|
|
| 289 |
|
| 290 |
st.title(bot_name)
|
| 291 |
|
| 292 |
-
# Show KB
|
| 293 |
if not st.session_state.messages:
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
overview_body = overview_body.replace(marker, "")
|
| 299 |
-
overview_body = overview_body.strip()
|
| 300 |
with st.expander("Knowledge Base Contents", expanded=True):
|
| 301 |
-
st.markdown(
|
| 302 |
|
| 303 |
render_sidebar()
|
| 304 |
render_chat()
|
|
|
|
| 4 |
|
| 5 |
from src.config_loader import load_config, get_api_key
|
| 6 |
from src.ingest import get_chroma_collection, ingest_documents
|
| 7 |
+
from src.kb_meta import summarize_kb_for_welcome
|
| 8 |
from src.query_engine import understand_query
|
| 9 |
from src.retriever import retrieve
|
| 10 |
from src.verifier import verify_and_respond
|
|
|
|
| 289 |
|
| 290 |
st.title(bot_name)
|
| 291 |
|
| 292 |
+
# Show KB summary on first visit (LLM-generated welcome summary)
|
| 293 |
if not st.session_state.messages:
|
| 294 |
+
if "kb_welcome_summary" not in st.session_state:
|
| 295 |
+
st.session_state.kb_welcome_summary = summarize_kb_for_welcome(cfg)
|
| 296 |
+
kb_summary = st.session_state.kb_welcome_summary
|
| 297 |
+
if kb_summary:
|
|
|
|
|
|
|
| 298 |
with st.expander("Knowledge Base Contents", expanded=True):
|
| 299 |
+
st.markdown(kb_summary)
|
| 300 |
|
| 301 |
render_sidebar()
|
| 302 |
render_chat()
|
src/kb_meta.py
CHANGED
|
@@ -401,3 +401,63 @@ def build_and_store_overview(collection, cfg: dict) -> str:
|
|
| 401 |
save_kb_meta(overview, cfg)
|
| 402 |
upsert_meta_chunk(collection, overview)
|
| 403 |
return overview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
save_kb_meta(overview, cfg)
|
| 402 |
upsert_meta_chunk(collection, overview)
|
| 403 |
return overview
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
_WELCOME_SUMMARY_PROMPT = """\
|
| 407 |
+
Given the following knowledge base overview, write a brief, friendly
|
| 408 |
+
welcome summary (3-6 sentences) for the chatbot's landing page.
|
| 409 |
+
|
| 410 |
+
Tell the user:
|
| 411 |
+
- What topics and datasets are available to ask about
|
| 412 |
+
- Roughly how much data there is (number of files, datasets, or data points)
|
| 413 |
+
- 2-3 example questions they could ask based on the actual content
|
| 414 |
+
|
| 415 |
+
Keep it conversational and concise. Do NOT use bullet points or headings.
|
| 416 |
+
Do NOT include technical details like column names, file extensions, or chunk counts.
|
| 417 |
+
|
| 418 |
+
--- KNOWLEDGE BASE OVERVIEW ---
|
| 419 |
+
{kb_overview}
|
| 420 |
+
--- END ---
|
| 421 |
+
|
| 422 |
+
Write the welcome summary now:"""
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
def summarize_kb_for_welcome(cfg: dict) -> str:
|
| 426 |
+
"""Generate a short, friendly welcome summary from the KB meta overview.
|
| 427 |
+
|
| 428 |
+
Uses the LLM to produce a conversational summary suitable for the
|
| 429 |
+
welcome page. Falls back to the raw overview text (stripped of markers)
|
| 430 |
+
if the LLM call fails.
|
| 431 |
+
|
| 432 |
+
Returns empty string if no KB meta exists.
|
| 433 |
+
"""
|
| 434 |
+
kb_overview = load_kb_meta(cfg)
|
| 435 |
+
if not kb_overview:
|
| 436 |
+
return ""
|
| 437 |
+
|
| 438 |
+
# Strip markers for clean input
|
| 439 |
+
clean = kb_overview
|
| 440 |
+
for marker in ("=== KNOWLEDGE BASE OVERVIEW ===", "=== END OVERVIEW ==="):
|
| 441 |
+
clean = clean.replace(marker, "")
|
| 442 |
+
clean = clean.strip()
|
| 443 |
+
if not clean:
|
| 444 |
+
return ""
|
| 445 |
+
|
| 446 |
+
try:
|
| 447 |
+
from src.llm import generate
|
| 448 |
+
prompt = _WELCOME_SUMMARY_PROMPT.format(kb_overview=clean)
|
| 449 |
+
summary = generate(
|
| 450 |
+
"You are a friendly research assistant. Be concise.",
|
| 451 |
+
prompt,
|
| 452 |
+
cfg,
|
| 453 |
+
max_tokens=512,
|
| 454 |
+
)
|
| 455 |
+
if summary and summary.strip():
|
| 456 |
+
return summary.strip()
|
| 457 |
+
except Exception:
|
| 458 |
+
pass
|
| 459 |
+
|
| 460 |
+
# Fallback: return truncated raw overview
|
| 461 |
+
if len(clean) > 600:
|
| 462 |
+
clean = clean[:600].rsplit("\n", 1)[0] + "\n..."
|
| 463 |
+
return clean
|