last working update
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ from langchain_community.llms import Together
|
|
| 6 |
from langchain import hub
|
| 7 |
from operator import itemgetter
|
| 8 |
from langchain.schema.runnable import RunnableParallel
|
|
|
|
|
|
|
| 9 |
from langchain.chains import LLMChain
|
| 10 |
from langchain.chains import RetrievalQA
|
| 11 |
from langchain.schema.output_parser import StrOutputParser
|
|
@@ -19,23 +21,33 @@ import time
|
|
| 19 |
|
| 20 |
# Load the embedding function
|
| 21 |
model_name = "BAAI/bge-base-en"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
encode_kwargs=encode_kwargs
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
# Load the LLM
|
| 33 |
llm = Together(
|
| 34 |
model="mistralai/Mixtral-8x22B-Instruct-v0.1",
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
| 39 |
|
| 40 |
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
| 41 |
|
|
@@ -45,24 +57,20 @@ def _combine_documents(
|
|
| 45 |
doc_strings = [format_document(doc, document_prompt) for doc in docs]
|
| 46 |
return document_separator.join(doc_strings)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
chistory = []
|
| 51 |
|
|
|
|
| 52 |
# Append the new message to the chat history
|
| 53 |
chistory.append({"role": role, "content": content})
|
| 54 |
|
| 55 |
-
|
| 56 |
# Define the Streamlit app
|
| 57 |
def app():
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
with st.sidebar:
|
| 62 |
-
|
| 63 |
st.title("dochatter")
|
| 64 |
# Create a dropdown selection box
|
| 65 |
option = st.selectbox(
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
# Depending on the selected option, choose the appropriate retriever
|
| 68 |
if option == 'RespiratoryFishman':
|
|
@@ -72,131 +80,61 @@ def app():
|
|
| 72 |
elif option == 'RespiratoryMurray':
|
| 73 |
persist_directory = "./respmurray/"
|
| 74 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="respmurraynotes")
|
| 75 |
-
|
| 76 |
-
|
| 77 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 78 |
elif option == 'MedMRCP2':
|
| 79 |
persist_directory = "./medmrcp2store/"
|
| 80 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="medmrcp2notes")
|
| 81 |
-
|
| 82 |
-
|
| 83 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 84 |
elif option == 'General Medicine':
|
| 85 |
persist_directory = "./oxfordmedbookdir/"
|
| 86 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="oxfordmed")
|
| 87 |
-
|
| 88 |
-
|
| 89 |
retriever = vectordb.as_retriever(search_kwargs={"k": 7})
|
| 90 |
-
|
| 91 |
-
|
| 92 |
else:
|
| 93 |
persist_directory = "./mrcpchromadb/"
|
| 94 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="mrcppassmednotes")
|
| 95 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
# Session State
|
| 112 |
-
|
| 113 |
if "messages" not in st.session_state.keys():
|
| 114 |
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question which contains the themes of the conversation. Do not write the question. Do not write the answer.
|
| 128 |
-
|
| 129 |
Chat History:
|
| 130 |
{chat_history}
|
| 131 |
Follow Up Input: {question}
|
|
|
|
|
|
|
| 132 |
|
| 133 |
template = """You are helping a doctor. Answer with what you know from the context provided. Please be as detailed and thorough. Answer the question based on the following context:
|
| 134 |
{context}
|
| 135 |
-
|
| 136 |
Question: {question}
|
| 137 |
"""
|
| 138 |
ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
|
| 139 |
|
| 140 |
-
|
| 141 |
_inputs = RunnableParallel(
|
| 142 |
standalone_question=RunnablePassthrough.assign(
|
| 143 |
chat_history=lambda x: chistory
|
| 144 |
) | CONDENSE_QUESTION_PROMPT | llmc | StrOutputParser(),
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
)
|
| 149 |
_context = {
|
| 150 |
"context": itemgetter("standalone_question") | retriever | _combine_documents,
|
|
|
|
| 151 |
}
|
| 152 |
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | llm
|
| 153 |
|
| 154 |
-
st.header("
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
for message in st.session_state.messages:
|
| 167 |
with st.chat_message(message["role"]):
|
| 168 |
st.write(message["content"])
|
| 169 |
store_chat_history(message["role"], message["content"])
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
prompts2 = st.chat_input("Say something")
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
if prompts2:
|
| 194 |
st.session_state.messages.append({"role": "user", "content": prompts2})
|
| 195 |
with st.chat_message("user"):
|
| 196 |
st.write(prompts2)
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
if st.session_state.messages[-1]["role"] != "assistant":
|
| 201 |
with st.chat_message("assistant"):
|
| 202 |
with st.spinner("Thinking..."):
|
|
@@ -216,32 +154,5 @@ def app():
|
|
| 216 |
st.error(f"An error occurred: {e}")
|
| 217 |
time.sleep(2) # Wait 2 seconds before retrying
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
if __name__ == '__main__':
|
| 247 |
-
app()
|
|
|
|
| 6 |
from langchain import hub
|
| 7 |
from operator import itemgetter
|
| 8 |
from langchain.schema.runnable import RunnableParallel
|
| 9 |
+
from langchain.schema import format_document
|
| 10 |
+
from typing import List, Tuple
|
| 11 |
from langchain.chains import LLMChain
|
| 12 |
from langchain.chains import RetrievalQA
|
| 13 |
from langchain.schema.output_parser import StrOutputParser
|
|
|
|
| 21 |
|
| 22 |
# Load the embedding function
|
| 23 |
model_name = "BAAI/bge-base-en"
|
| 24 |
+
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
|
| 25 |
+
|
| 26 |
+
embedding_function = HuggingFaceBgeEmbeddings(
|
| 27 |
+
model_name=model_name,
|
| 28 |
encode_kwargs=encode_kwargs
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Load the LLM
|
| 32 |
llm = Together(
|
| 33 |
model="mistralai/Mixtral-8x22B-Instruct-v0.1",
|
| 34 |
+
temperature=0.2,
|
| 35 |
+
max_tokens=19096,
|
| 36 |
+
top_k=10,
|
| 37 |
+
together_api_key=os.environ['pilotikval']
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
# Load the summarizeLLM
|
| 41 |
+
llmc = Together(
|
| 42 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 43 |
+
temperature=0.2,
|
| 44 |
+
max_tokens=1024,
|
| 45 |
+
top_k=1,
|
| 46 |
+
together_api_key=os.environ['pilotikval']
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
msgs = StreamlitChatMessageHistory(key="langchain_messages")
|
| 50 |
+
memory = ConversationBufferMemory(chat_memory=msgs)
|
| 51 |
|
| 52 |
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
| 53 |
|
|
|
|
| 57 |
doc_strings = [format_document(doc, document_prompt) for doc in docs]
|
| 58 |
return document_separator.join(doc_strings)
|
| 59 |
|
|
|
|
|
|
|
| 60 |
chistory = []
|
| 61 |
|
| 62 |
+
def store_chat_history(role: str, content: str):
|
| 63 |
# Append the new message to the chat history
|
| 64 |
chistory.append({"role": role, "content": content})
|
| 65 |
|
|
|
|
| 66 |
# Define the Streamlit app
|
| 67 |
def app():
|
|
|
|
|
|
|
|
|
|
| 68 |
with st.sidebar:
|
|
|
|
| 69 |
st.title("dochatter")
|
| 70 |
# Create a dropdown selection box
|
| 71 |
option = st.selectbox(
|
| 72 |
+
'Which retriever would you like to use?',
|
| 73 |
+
('General Medicine', 'RespiratoryFishman', 'RespiratoryMurray', 'MedMRCP2', 'OldMedicine')
|
| 74 |
)
|
| 75 |
# Depending on the selected option, choose the appropriate retriever
|
| 76 |
if option == 'RespiratoryFishman':
|
|
|
|
| 80 |
elif option == 'RespiratoryMurray':
|
| 81 |
persist_directory = "./respmurray/"
|
| 82 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="respmurraynotes")
|
|
|
|
|
|
|
| 83 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 84 |
elif option == 'MedMRCP2':
|
| 85 |
persist_directory = "./medmrcp2store/"
|
| 86 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="medmrcp2notes")
|
|
|
|
|
|
|
| 87 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 88 |
elif option == 'General Medicine':
|
| 89 |
persist_directory = "./oxfordmedbookdir/"
|
| 90 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="oxfordmed")
|
|
|
|
|
|
|
| 91 |
retriever = vectordb.as_retriever(search_kwargs={"k": 7})
|
|
|
|
|
|
|
| 92 |
else:
|
| 93 |
persist_directory = "./mrcpchromadb/"
|
| 94 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="mrcppassmednotes")
|
| 95 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
# Session State
|
|
|
|
| 98 |
if "messages" not in st.session_state.keys():
|
| 99 |
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question which contains the themes of the conversation. Do not write the question. Do not write the answer.
|
|
|
|
| 102 |
Chat History:
|
| 103 |
{chat_history}
|
| 104 |
Follow Up Input: {question}
|
| 105 |
+
Standalone question:"""
|
| 106 |
+
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
| 107 |
|
| 108 |
template = """You are helping a doctor. Answer with what you know from the context provided. Please be as detailed and thorough. Answer the question based on the following context:
|
| 109 |
{context}
|
|
|
|
| 110 |
Question: {question}
|
| 111 |
"""
|
| 112 |
ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
|
| 113 |
|
|
|
|
| 114 |
_inputs = RunnableParallel(
|
| 115 |
standalone_question=RunnablePassthrough.assign(
|
| 116 |
chat_history=lambda x: chistory
|
| 117 |
) | CONDENSE_QUESTION_PROMPT | llmc | StrOutputParser(),
|
|
|
|
|
|
|
|
|
|
| 118 |
)
|
| 119 |
_context = {
|
| 120 |
"context": itemgetter("standalone_question") | retriever | _combine_documents,
|
| 121 |
+
"question": lambda x: x["standalone_question"],
|
| 122 |
}
|
| 123 |
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | llm
|
| 124 |
|
| 125 |
+
st.header("Ask Away!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
for message in st.session_state.messages:
|
| 127 |
with st.chat_message(message["role"]):
|
| 128 |
st.write(message["content"])
|
| 129 |
store_chat_history(message["role"], message["content"])
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
prompts2 = st.chat_input("Say something")
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
if prompts2:
|
| 134 |
st.session_state.messages.append({"role": "user", "content": prompts2})
|
| 135 |
with st.chat_message("user"):
|
| 136 |
st.write(prompts2)
|
| 137 |
|
|
|
|
|
|
|
| 138 |
if st.session_state.messages[-1]["role"] != "assistant":
|
| 139 |
with st.chat_message("assistant"):
|
| 140 |
with st.spinner("Thinking..."):
|
|
|
|
| 154 |
st.error(f"An error occurred: {e}")
|
| 155 |
time.sleep(2) # Wait 2 seconds before retrying
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
if __name__ == '__main__':
|
| 158 |
+
app()
|