Implement clickable links when chatbot cites a public-facing article

#3
Files changed (1) hide show
  1. app.py +18 -0
app.py CHANGED
@@ -113,6 +113,22 @@ def ensure_kb(client):
113
  return True, ""
114
  return False, (KB.error or "Knowledge base could not be initialized.")
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  # ----------------------------------------------------------------------
117
  # Supabase persistence — every participant is identified by their email.
118
  # Their 16 answers and their whole conversation are written to Supabase as
@@ -813,6 +829,8 @@ def respond(user_msg, chat_display, convo, n_followups, api_key, email,
813
  "\n\n_This summary reflects what you shared. Please bring it to your care team. "
814
  "It is not medical advice._"
815
  )
 
 
816
 
817
  chat_display = chat_display + [{"role": "assistant", "content": display_reply}]
818
 
 
113
  return True, ""
114
  return False, (KB.error or "Knowledge base could not be initialized.")
115
 
116
+ def link_document_titles(text):
117
+ """
118
+ Turn document titles with participant-facing URLs into clickable
119
+ Markdown links. Sources without URLs stay as plain text.
120
+ """
121
+ if not text or not KB.ready:
122
+ return text
123
+
124
+ for title, url in KB.source_link_map().items():
125
+ text = text.replace(
126
+ title,
127
+ f"[{title}]({url})"
128
+ )
129
+
130
+ return text
131
+
132
  # ----------------------------------------------------------------------
133
  # Supabase persistence — every participant is identified by their email.
134
  # Their 16 answers and their whole conversation are written to Supabase as
 
829
  "\n\n_This summary reflects what you shared. Please bring it to your care team. "
830
  "It is not medical advice._"
831
  )
832
+
833
+ display_reply = link_document_titles(display_reply)
834
 
835
  chat_display = chat_display + [{"role": "assistant", "content": display_reply}]
836