Spaces:
Running
Running
Commit
·
e76dfcc
1
Parent(s):
1b80ae8
Update to chat interface with conversation history
Browse files- src/ui/components.py +46 -13
src/ui/components.py
CHANGED
|
@@ -4,15 +4,21 @@ from src.config import Settings
|
|
| 4 |
from src.retrieval.search import retrieve, format_citations
|
| 5 |
from src.llm.answer import compose_answer
|
| 6 |
|
| 7 |
-
def _ask(query: str, settings, embedder, vstore) -> tuple[
|
|
|
|
| 8 |
if not query or not query.strip():
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
hits: List[Tuple[str, float]] = retrieve(query, vstore=vstore, embedder=embedder, k=settings.top_k)
|
| 11 |
if not hits:
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
answer = compose_answer(query, hits, settings)
|
| 14 |
citations = format_citations(hits, max_items=settings.top_k)
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
def build_app(settings: Settings, embedder, vstore):
|
| 18 |
with gr.Blocks(title=settings.title) as demo:
|
|
@@ -23,17 +29,44 @@ def build_app(settings: Settings, embedder, vstore):
|
|
| 23 |
else "— retrieval + summarizer enabled.")
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
citations = gr.JSON(label="Source & similarity")
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
return demo
|
|
|
|
| 4 |
from src.retrieval.search import retrieve, format_citations
|
| 5 |
from src.llm.answer import compose_answer
|
| 6 |
|
| 7 |
+
def _ask(query: str, history: List, settings, embedder, vstore) -> tuple[List, List[dict]]:
|
| 8 |
+
"""Process a chat message and return updated history + citations."""
|
| 9 |
if not query or not query.strip():
|
| 10 |
+
history.append((query, "Please enter a question about the documents."))
|
| 11 |
+
return history, []
|
| 12 |
+
|
| 13 |
hits: List[Tuple[str, float]] = retrieve(query, vstore=vstore, embedder=embedder, k=settings.top_k)
|
| 14 |
if not hits:
|
| 15 |
+
history.append((query, "No relevant passages found. Try adjusting your query."))
|
| 16 |
+
return history, []
|
| 17 |
+
|
| 18 |
answer = compose_answer(query, hits, settings)
|
| 19 |
citations = format_citations(hits, max_items=settings.top_k)
|
| 20 |
+
history.append((query, answer))
|
| 21 |
+
return history, citations
|
| 22 |
|
| 23 |
def build_app(settings: Settings, embedder, vstore):
|
| 24 |
with gr.Blocks(title=settings.title) as demo:
|
|
|
|
| 29 |
else "— retrieval + summarizer enabled.")
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Chat interface
|
| 33 |
+
chatbot = gr.Chatbot(label="Conversation", height=400)
|
| 34 |
+
with gr.Row():
|
| 35 |
+
query = gr.Textbox(
|
| 36 |
+
label="",
|
| 37 |
+
placeholder="Ask a question about the documents...",
|
| 38 |
+
scale=4,
|
| 39 |
+
container=False
|
| 40 |
+
)
|
| 41 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 42 |
+
|
| 43 |
+
# Citations accordion below chat
|
| 44 |
+
with gr.Accordion("Citations (top matches from last query)", open=False):
|
| 45 |
citations = gr.JSON(label="Source & similarity")
|
| 46 |
+
|
| 47 |
+
# Clear button
|
| 48 |
+
clear_btn = gr.Button("Clear Chat", size="sm")
|
| 49 |
|
| 50 |
+
def on_submit(message, history):
|
| 51 |
+
"""Handle message submission."""
|
| 52 |
+
new_history, new_citations = _ask(message, history or [], settings, embedder, vstore)
|
| 53 |
+
return new_history, new_citations, "" # Return empty string to clear input
|
| 54 |
+
|
| 55 |
+
def clear_chat():
|
| 56 |
+
"""Clear the chat history."""
|
| 57 |
+
return [], []
|
| 58 |
|
| 59 |
+
# Wire up the interactions
|
| 60 |
+
submit_btn.click(
|
| 61 |
+
on_submit,
|
| 62 |
+
inputs=[query, chatbot],
|
| 63 |
+
outputs=[chatbot, citations, query]
|
| 64 |
+
)
|
| 65 |
+
query.submit(
|
| 66 |
+
on_submit,
|
| 67 |
+
inputs=[query, chatbot],
|
| 68 |
+
outputs=[chatbot, citations, query]
|
| 69 |
+
)
|
| 70 |
+
clear_btn.click(clear_chat, outputs=[chatbot, citations])
|
| 71 |
|
| 72 |
return demo
|