thomascerniglia commited on
Commit
e76dfcc
·
1 Parent(s): 1b80ae8

Update to chat interface with conversation history

Browse files
Files changed (1) hide show
  1. 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[str, List[dict]]:
 
8
  if not query or not query.strip():
9
- return "Please enter a question about the documents.", []
 
 
10
  hits: List[Tuple[str, float]] = retrieve(query, vstore=vstore, embedder=embedder, k=settings.top_k)
11
  if not hits:
12
- return "No relevant passages found. Try adjusting your query.", []
 
 
13
  answer = compose_answer(query, hits, settings)
14
  citations = format_citations(hits, max_items=settings.top_k)
15
- return answer, citations
 
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
- query = gr.Textbox(label="Your question", placeholder="e.g., Orders about Sector 4 in 1963?")
27
- ask_btn = gr.Button("Search", variant="primary")
28
- answer = gr.Markdown("Ask a question to see excerpts.")
29
- with gr.Accordion("Citations (top matches)", open=False):
 
 
 
 
 
 
 
 
 
30
  citations = gr.JSON(label="Source & similarity")
 
 
 
31
 
32
- def on_ask(q):
33
- a, c = _ask(q, settings, embedder, vstore)
34
- return a, c
 
 
 
 
 
35
 
36
- ask_btn.click(on_ask, inputs=[query], outputs=[answer, citations])
37
- query.submit(on_ask, inputs=[query], outputs=[answer, citations])
 
 
 
 
 
 
 
 
 
 
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