cehck
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import streamlit as st
|
|
| 3 |
from together import Together
|
| 4 |
from langchain_community.vectorstores import Chroma
|
| 5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
-
import streamlit.components.v1 as components
|
| 7 |
|
| 8 |
# --- Configuration ---
|
| 9 |
# TogetherAI API key (env var name pilotikval)
|
|
@@ -13,21 +12,14 @@ if not TOGETHER_API_KEY:
|
|
| 13 |
st.stop()
|
| 14 |
|
| 15 |
# Initialize TogetherAI client
|
| 16 |
-
|
| 17 |
-
client = Together()
|
| 18 |
|
| 19 |
# Embeddings setup
|
| 20 |
EMBED_MODEL_NAME = "BAAI/bge-base-en"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
model_name=EMBED_MODEL_NAME,
|
| 26 |
-
model_kwargs={"device": "cpu"},
|
| 27 |
-
encode_kwargs={"normalize_embeddings": True},
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
embeddings = load_embeddings()
|
| 31 |
|
| 32 |
# Sidebar: select collection
|
| 33 |
st.sidebar.title("DocChatter RAG")
|
|
@@ -55,16 +47,12 @@ persist_directory = dirs[collection]
|
|
| 55 |
collection_name = cols[collection]
|
| 56 |
|
| 57 |
# Load Chroma vector store
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
| 65 |
-
return vectorstore.as_retriever(search_kwargs={"k": 20})
|
| 66 |
-
|
| 67 |
-
retriever = load_vectorstore(embeddings, persist_directory, collection_name)
|
| 68 |
|
| 69 |
# System prompt template
|
| 70 |
|
|
@@ -80,7 +68,7 @@ def build_system(context: str) -> dict:
|
|
| 80 |
"""
|
| 81 |
prompt = f"""
|
| 82 |
You are a world-class medical assistant and conversational partner.
|
| 83 |
-
Listen carefully to the user
|
| 84 |
If any part of the question is unclear, ask a clarifying question before proceeding.
|
| 85 |
Organize your answer with clear headings or bullet points, and refer back to specific context snippets as needed.
|
| 86 |
Always be empathetic, concise, and precise in your medical explanations.
|
|
@@ -91,43 +79,6 @@ Retain memory of previous user messages to support follow-up interactions.
|
|
| 91 |
"""
|
| 92 |
return {"role": "system", "content": prompt}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
def copy_button(text_to_copy: str, button_id: str):
|
| 96 |
-
"""
|
| 97 |
-
Creates a copy button using HTML and JavaScript.
|
| 98 |
-
"""
|
| 99 |
-
# Escape text for JavaScript
|
| 100 |
-
escaped_text = text_to_copy.replace('\\', '\\\\').replace('`', '\\`').replace('$', '\\$')
|
| 101 |
-
|
| 102 |
-
html_code = f"""
|
| 103 |
-
<div style="display: inline-block;">
|
| 104 |
-
<button id="copy-btn-{button_id}" onclick="copyToClipboard{button_id}()"
|
| 105 |
-
style="background: none; border: none; cursor: pointer; font-size: 20px;
|
| 106 |
-
padding: 5px; margin-left: 10px; vertical-align: middle;"
|
| 107 |
-
title="Copy to clipboard">
|
| 108 |
-
📋
|
| 109 |
-
</button>
|
| 110 |
-
<span id="copied-msg-{button_id}" style="color: green; font-size: 12px; margin-left: 5px; display: none;">
|
| 111 |
-
✓ Copied!
|
| 112 |
-
</span>
|
| 113 |
-
</div>
|
| 114 |
-
<script>
|
| 115 |
-
function copyToClipboard{button_id}() {{
|
| 116 |
-
const text = `{escaped_text}`;
|
| 117 |
-
navigator.clipboard.writeText(text).then(function() {{
|
| 118 |
-
document.getElementById('copied-msg-{button_id}').style.display = 'inline';
|
| 119 |
-
setTimeout(function() {{
|
| 120 |
-
document.getElementById('copied-msg-{button_id}').style.display = 'none';
|
| 121 |
-
}}, 2000);
|
| 122 |
-
}}, function(err) {{
|
| 123 |
-
console.error('Failed to copy: ', err);
|
| 124 |
-
}});
|
| 125 |
-
}}
|
| 126 |
-
</script>
|
| 127 |
-
"""
|
| 128 |
-
components.html(html_code, height=40)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
st.title("🩺 DocChatter RAG (Streaming & Memory)")
|
| 132 |
|
| 133 |
# Initialize chat history
|
|
@@ -142,16 +93,8 @@ chat_tab, clear_tab = st.tabs(["Chat", "Clear History"])
|
|
| 142 |
|
| 143 |
with chat_tab:
|
| 144 |
# Display existing chat
|
| 145 |
-
for
|
| 146 |
-
|
| 147 |
-
st.chat_message("user").write(msg['content'])
|
| 148 |
-
else: # assistant
|
| 149 |
-
with st.chat_message("assistant"):
|
| 150 |
-
col1, col2 = st.columns([0.95, 0.05])
|
| 151 |
-
with col1:
|
| 152 |
-
st.write(msg['content'])
|
| 153 |
-
with col2:
|
| 154 |
-
copy_button(msg['content'], f"msg{idx}")
|
| 155 |
|
| 156 |
# Handle new user input
|
| 157 |
if user_prompt:
|
|
@@ -160,7 +103,10 @@ with chat_tab:
|
|
| 160 |
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 161 |
|
| 162 |
# Retrieve top-k documents
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
context = "\n---\n".join([d.page_content for d in docs])
|
| 165 |
|
| 166 |
# Build TogetherAI message sequence
|
|
@@ -191,11 +137,8 @@ with chat_tab:
|
|
| 191 |
|
| 192 |
# Save assistant response
|
| 193 |
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
| 194 |
-
|
| 195 |
-
# Rerun to show copy button for the new message
|
| 196 |
-
st.rerun()
|
| 197 |
|
| 198 |
with clear_tab:
|
| 199 |
if st.button("🗑️ Clear chat history"):
|
| 200 |
st.session_state.chat_history = []
|
| 201 |
-
st.
|
|
|
|
| 3 |
from together import Together
|
| 4 |
from langchain_community.vectorstores import Chroma
|
| 5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
# TogetherAI API key (env var name pilotikval)
|
|
|
|
| 12 |
st.stop()
|
| 13 |
|
| 14 |
# Initialize TogetherAI client
|
| 15 |
+
client = Together(api_key=TOGETHER_API_KEY)
|
|
|
|
| 16 |
|
| 17 |
# Embeddings setup
|
| 18 |
EMBED_MODEL_NAME = "BAAI/bge-base-en"
|
| 19 |
+
embeddings = HuggingFaceEmbeddings(
|
| 20 |
+
model_name=EMBED_MODEL_NAME,
|
| 21 |
+
encode_kwargs={"normalize_embeddings": True},
|
| 22 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Sidebar: select collection
|
| 25 |
st.sidebar.title("DocChatter RAG")
|
|
|
|
| 47 |
collection_name = cols[collection]
|
| 48 |
|
| 49 |
# Load Chroma vector store
|
| 50 |
+
vectorstore = Chroma(
|
| 51 |
+
collection_name=collection_name,
|
| 52 |
+
persist_directory=persist_directory,
|
| 53 |
+
embedding_function=embeddings
|
| 54 |
+
)
|
| 55 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 20}) # k=20
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# System prompt template
|
| 58 |
|
|
|
|
| 68 |
"""
|
| 69 |
prompt = f"""
|
| 70 |
You are a world-class medical assistant and conversational partner.
|
| 71 |
+
Listen carefully to the user’s questions, reference the context below, and provide a thorough, evidence-based response.
|
| 72 |
If any part of the question is unclear, ask a clarifying question before proceeding.
|
| 73 |
Organize your answer with clear headings or bullet points, and refer back to specific context snippets as needed.
|
| 74 |
Always be empathetic, concise, and precise in your medical explanations.
|
|
|
|
| 79 |
"""
|
| 80 |
return {"role": "system", "content": prompt}
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
st.title("🩺 DocChatter RAG (Streaming & Memory)")
|
| 83 |
|
| 84 |
# Initialize chat history
|
|
|
|
| 93 |
|
| 94 |
with chat_tab:
|
| 95 |
# Display existing chat
|
| 96 |
+
for msg in st.session_state.chat_history:
|
| 97 |
+
st.chat_message(msg['role']).write(msg['content'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Handle new user input
|
| 100 |
if user_prompt:
|
|
|
|
| 103 |
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 104 |
|
| 105 |
# Retrieve top-k documents
|
| 106 |
+
try:
|
| 107 |
+
docs = retriever.invoke({"query": user_prompt})
|
| 108 |
+
except Exception:
|
| 109 |
+
docs = retriever.get_relevant_documents(user_prompt)
|
| 110 |
context = "\n---\n".join([d.page_content for d in docs])
|
| 111 |
|
| 112 |
# Build TogetherAI message sequence
|
|
|
|
| 137 |
|
| 138 |
# Save assistant response
|
| 139 |
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
with clear_tab:
|
| 142 |
if st.button("🗑️ Clear chat history"):
|
| 143 |
st.session_state.chat_history = []
|
| 144 |
+
st.experimental_rerun()
|