Sbnos commited on
Commit
4bfe10b
·
verified ·
1 Parent(s): b3bd92b

going back

Browse files
Files changed (1) hide show
  1. app.py +185 -58
app.py CHANGED
@@ -1,35 +1,52 @@
1
  import streamlit as st
2
  import os
3
- from langchain_community.vectorstores import Chroma
4
- from langchain_community.embeddings import HuggingFaceBgeEmbeddings
5
- from langchain_community.llms import Together
6
- from langchain.chains import create_retrieval_chain, create_history_aware_retriever, LLMChain
7
- from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, PromptTemplate
 
 
 
 
 
 
 
8
  from langchain.memory import ConversationBufferMemory
9
- from langchain_community.chat_message_histories import StreamlitChatMessageHistory
10
- import time
 
 
 
11
 
12
  # Load the embedding function
13
  model_name = "BAAI/bge-base-en"
14
- encode_kwargs = {'normalize_embeddings': True}
15
 
16
  embedding_function = HuggingFaceBgeEmbeddings(
17
  model_name=model_name,
18
  encode_kwargs=encode_kwargs
19
  )
20
 
 
 
 
 
 
 
 
21
  # Load the LLM
22
  llm = Together(
23
  model="mistralai/Mixtral-8x22B-Instruct-v0.1",
24
  temperature=0.2,
25
  max_tokens=19096,
26
- top_k=10,
27
  together_api_key=os.environ['pilotikval']
28
  )
29
 
30
  # Load the summarizeLLM
31
  llmc = Together(
32
- model="mistralai/Mixtral-8x7B-Instruct-v0.1",
33
  temperature=0.2,
34
  max_tokens=1024,
35
  top_k=1,
@@ -37,109 +54,219 @@ llmc = Together(
37
  )
38
 
39
  msgs = StreamlitChatMessageHistory(key="langchain_messages")
40
- memory = ConversationBufferMemory(chat_memory=msgs, memory_key="chat_history", return_messages=True)
 
 
41
 
42
  DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
43
 
44
  def _combine_documents(
45
  docs, document_prompt=DEFAULT_DOCUMENT_PROMPT, document_separator="\n\n"
46
  ):
47
- doc_strings = [format_document(doc, document_prompt) for doc in docs]
48
- return document_separator.join(doc_strings)
 
 
49
 
50
  chistory = []
51
 
52
  def store_chat_history(role: str, content: str):
 
53
  chistory.append({"role": role, "content": content})
54
 
 
 
55
  def app():
 
 
 
56
  with st.sidebar:
 
57
  st.title("dochatter")
 
58
  option = st.selectbox(
59
  'Which retriever would you like to use?',
60
  ('General Medicine', 'RespiratoryFishman', 'RespiratoryMurray', 'MedMRCP2', 'OldMedicine')
61
  )
 
62
  if option == 'RespiratoryFishman':
63
- persist_directory = "./respfishmandbcud/"
64
- vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="fishmannotescud")
65
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
66
- elif option == 'RespiratoryMurray':
67
- persist_directory = "./respmurray/"
68
- vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="respmurraynotes")
 
 
69
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
70
- elif option == 'MedMRCP2':
71
- persist_directory = "./medmrcp2store/"
72
- vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="medmrcp2notes")
 
 
73
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
74
- elif option == 'General Medicine':
75
- persist_directory = "./oxfordmedbookdir/"
76
- vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="oxfordmed")
 
 
77
  retriever = vectordb.as_retriever(search_kwargs={"k": 7})
 
 
78
  else:
79
- persist_directory = "./mrcpchromadb/"
80
- vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="mrcppassmednotes")
81
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
 
 
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  if "messages" not in st.session_state.keys():
84
  st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
 
 
 
 
 
 
 
 
85
 
86
- condense_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.
 
 
 
87
  Chat History:
88
  {chat_history}
89
- Follow-Up Input: {question}
90
  Standalone question:"""
91
- CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(condense_template)
92
 
93
- answer_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:
94
  {context}
95
- Question: {question}"""
96
- ANSWER_PROMPT = ChatPromptTemplate.from_template(answer_template)
 
97
 
98
- question_generator_chain = LLMChain(llm=llmc, prompt=CONDENSE_QUESTION_PROMPT)
99
- combine_docs_chain = LLMChain(llm=llm, prompt=ANSWER_PROMPT)
100
 
101
- history_aware_retriever = create_history_aware_retriever(
102
- llm=llmc,
103
- retriever=retriever,
104
- prompt=CONDENSE_QUESTION_PROMPT
 
 
 
105
  )
 
 
 
 
 
 
 
 
 
 
106
 
107
- conversational_qa_chain = create_retrieval_chain(
108
- history_aware_retriever,
109
- combine_docs_chain
110
- )
111
 
 
 
 
 
112
  st.header("Ask Away!")
 
113
  for message in st.session_state.messages:
114
  with st.chat_message(message["role"]):
115
  st.write(message["content"])
116
  store_chat_history(message["role"], message["content"])
117
 
 
 
 
 
 
118
  prompts2 = st.chat_input("Say something")
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  if prompts2:
121
  st.session_state.messages.append({"role": "user", "content": prompts2})
122
  with st.chat_message("user"):
123
  st.write(prompts2)
 
 
124
 
125
  if st.session_state.messages[-1]["role"] != "assistant":
126
  with st.chat_message("assistant"):
127
  with st.spinner("Thinking..."):
128
- for _ in range(3):
129
- try:
130
- response = conversational_qa_chain.invoke(
131
- {
132
- "input": prompts2,
133
- "chat_history": chistory,
134
- }
135
- )
136
- st.write(response["answer"])
137
- message = {"role": "assistant", "content": response["answer"]}
138
- st.session_state.messages.append(message)
139
- break
140
- except Exception as e:
141
- st.error(f"An error occurred: {e}")
142
- time.sleep(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  if __name__ == '__main__':
145
- app()
 
1
  import streamlit as st
2
  import os
3
+ from langchain.vectorstores import Chroma
4
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
5
+ from langchain.llms import Together
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
14
+ from langchain.memory import StreamlitChatMessageHistory
15
  from langchain.memory import ConversationBufferMemory
16
+ from langchain.chains import ConversationalRetrievalChain
17
+ from langchain.memory import ConversationSummaryMemory
18
+ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, PromptTemplate
19
+ from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
20
+
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 ChromaDB vector store
32
+ # persist_directory="./mrcpchromadb/"
33
+ # vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function,collection_name="mrcppassmednotes")
34
+
35
+
36
+
37
+
38
  # Load the LLM
39
  llm = Together(
40
  model="mistralai/Mixtral-8x22B-Instruct-v0.1",
41
  temperature=0.2,
42
  max_tokens=19096,
43
+ top_k=6,
44
  together_api_key=os.environ['pilotikval']
45
  )
46
 
47
  # Load the summarizeLLM
48
  llmc = Together(
49
+ model="mistralai/Mixtral-8x22B-Instruct-v0.1",
50
  temperature=0.2,
51
  max_tokens=1024,
52
  top_k=1,
 
54
  )
55
 
56
  msgs = StreamlitChatMessageHistory(key="langchain_messages")
57
+ memory = ConversationBufferMemory(chat_memory=msgs)
58
+
59
+
60
 
61
  DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
62
 
63
  def _combine_documents(
64
  docs, document_prompt=DEFAULT_DOCUMENT_PROMPT, document_separator="\n\n"
65
  ):
66
+ doc_strings = [format_document(doc, document_prompt) for doc in docs]
67
+ return document_separator.join(doc_strings)
68
+
69
+
70
 
71
  chistory = []
72
 
73
  def store_chat_history(role: str, content: str):
74
+ # Append the new message to the chat history
75
  chistory.append({"role": role, "content": content})
76
 
77
+
78
+ # Define the Streamlit app
79
  def app():
80
+
81
+
82
+
83
  with st.sidebar:
84
+
85
  st.title("dochatter")
86
+ # Create a dropdown selection box
87
  option = st.selectbox(
88
  'Which retriever would you like to use?',
89
  ('General Medicine', 'RespiratoryFishman', 'RespiratoryMurray', 'MedMRCP2', 'OldMedicine')
90
  )
91
+ # Depending on the selected option, choose the appropriate retriever
92
  if option == 'RespiratoryFishman':
93
+ persist_directory="./respfishmandbcud/"
94
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function,collection_name="fishmannotescud")
95
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
96
+ retriever = retriever # replace with your actual retriever
97
+
98
+ if option == 'RespiratoryMurray':
99
+ persist_directory="./respmurray/"
100
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function,collection_name="respmurraynotes")
101
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
102
+ retriever = retriever
103
+
104
+ if option == 'MedMRCP2':
105
+ persist_directory="./medmrcp2store/"
106
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function,collection_name="medmrcp2notes")
107
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
108
+ retriever = retriever
109
+
110
+ if option == 'General Medicine':
111
+ persist_directory="./oxfordmedbookdir/"
112
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function,collection_name="oxfordmed")
113
  retriever = vectordb.as_retriever(search_kwargs={"k": 7})
114
+ retriever = retriever
115
+
116
  else:
117
+ persist_directory="./mrcpchromadb/"
118
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function,collection_name="mrcppassmednotes")
119
  retriever = vectordb.as_retriever(search_kwargs={"k": 5})
120
+ retriever = retriever # replace with your actual retriever
121
+ retriever = retriever # replace with your actual retriever
122
 
123
+ #template = """You are an AI chatbot having a conversation with a human. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
124
+ #{context}
125
+ #{history}
126
+ #Human: {human_input}
127
+ #AI: """
128
+ #prompt = PromptTemplate(input_variables=["history", "question"], template=template)
129
+ #template = st.text_area("Template", value=template, height=180)
130
+ #prompt2 = ChatPromptTemplate.from_template(template)
131
+
132
+
133
+
134
+
135
+ # Session State
136
+ # Store LLM generated responses
137
  if "messages" not in st.session_state.keys():
138
  st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
 
148
+
149
+ ## Retry lets go
150
+
151
+ _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.
152
  Chat History:
153
  {chat_history}
154
+ Follow Up Input: {question}
155
  Standalone question:"""
156
+ CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
157
 
158
+ 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:
159
  {context}
160
+ Question: {question}
161
+ """
162
+ ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
163
 
 
 
164
 
165
+ _inputs = RunnableParallel(
166
+ standalone_question=RunnablePassthrough.assign(
167
+ chat_history=lambda x: chistory
168
+ )
169
+ | CONDENSE_QUESTION_PROMPT
170
+ | llmc
171
+ | StrOutputParser(),
172
  )
173
+ _context = {
174
+ "context": itemgetter("standalone_question") | retriever | _combine_documents,
175
+ "question": lambda x: x["standalone_question"],
176
+ }
177
+ conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | llm
178
+
179
+
180
+
181
+
182
+
183
 
 
 
 
 
184
 
185
+
186
+
187
+
188
+
189
  st.header("Ask Away!")
190
+ # Display the messages
191
  for message in st.session_state.messages:
192
  with st.chat_message(message["role"]):
193
  st.write(message["content"])
194
  store_chat_history(message["role"], message["content"])
195
 
196
+ # prompt = hub.pull("rlm/rag-prompt")
197
+
198
+
199
+
200
+
201
  prompts2 = st.chat_input("Say something")
202
 
203
+ # Implement using different book sources, if statements
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
  if prompts2:
219
  st.session_state.messages.append({"role": "user", "content": prompts2})
220
  with st.chat_message("user"):
221
  st.write(prompts2)
222
+
223
+
224
 
225
  if st.session_state.messages[-1]["role"] != "assistant":
226
  with st.chat_message("assistant"):
227
  with st.spinner("Thinking..."):
228
+ response = conversational_qa_chain.invoke(
229
+ {
230
+ "question": prompts2,
231
+ "chat_history": chistory,
232
+ }
233
+ )
234
+ st.write(response)
235
+ message = {"role": "assistant", "content": response}
236
+ st.session_state.messages.append(message)
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+ # Create a button to submit the question
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+ # Initialize history
269
+ history = []
270
 
271
  if __name__ == '__main__':
272
+ app()