changes v2
Browse files
app.py
CHANGED
@@ -2,26 +2,20 @@ import streamlit as st
|
|
2 |
import os
|
3 |
from langchain.vectorstores import Chroma
|
4 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
5 |
-
from
|
6 |
from langchain import hub
|
7 |
from operator import itemgetter
|
8 |
-
from langchain.schema
|
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
|
20 |
-
|
21 |
|
22 |
# Load the embedding function
|
23 |
model_name = "BAAI/bge-base-en"
|
24 |
-
encode_kwargs = {'normalize_embeddings': True}
|
25 |
|
26 |
embedding_function = HuggingFaceBgeEmbeddings(
|
27 |
model_name=model_name,
|
@@ -30,16 +24,13 @@ embedding_function = HuggingFaceBgeEmbeddings(
|
|
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_new_tokens
|
43 |
top_k=12,
|
44 |
together_api_key=os.environ['pilotikval']
|
45 |
)
|
@@ -48,7 +39,7 @@ llm = Together(
|
|
48 |
llmc = Together(
|
49 |
model="mistralai/Mixtral-8x22B-Instruct-v0.1",
|
50 |
temperature=0.2,
|
51 |
-
max_new_tokens
|
52 |
top_k=3,
|
53 |
together_api_key=os.environ['pilotikval']
|
54 |
)
|
@@ -56,99 +47,55 @@ llmc = Together(
|
|
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 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
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}
|
@@ -161,7 +108,6 @@ def app():
|
|
161 |
"""
|
162 |
ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
|
163 |
|
164 |
-
|
165 |
_inputs = RunnableParallel(
|
166 |
standalone_question=RunnablePassthrough.assign(
|
167 |
chat_history=lambda x: chistory
|
@@ -176,51 +122,18 @@ def app():
|
|
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"):
|
@@ -235,38 +148,5 @@ def app():
|
|
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()
|
|
|
2 |
import os
|
3 |
from langchain.vectorstores import Chroma
|
4 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
5 |
+
from langchain_together import Together # Updated import
|
6 |
from langchain import hub
|
7 |
from operator import itemgetter
|
8 |
+
from langchain.schema import RunnableParallel, format_document # Updated import paths
|
|
|
9 |
from typing import List, Tuple
|
10 |
+
from langchain.chains import LLMChain, RetrievalQA, ConversationalRetrievalChain
|
|
|
11 |
from langchain.schema.output_parser import StrOutputParser
|
12 |
+
from langchain.memory import StreamlitChatMessageHistory, ConversationBufferMemory, ConversationSummaryMemory
|
|
|
|
|
|
|
13 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, PromptTemplate
|
14 |
+
from langchain.schema import RunnableLambda, RunnablePassthrough
|
|
|
15 |
|
16 |
# Load the embedding function
|
17 |
model_name = "BAAI/bge-base-en"
|
18 |
+
encode_kwargs = {'normalize_embeddings': True}
|
19 |
|
20 |
embedding_function = HuggingFaceBgeEmbeddings(
|
21 |
model_name=model_name,
|
|
|
24 |
|
25 |
# Load the ChromaDB vector store
|
26 |
# persist_directory="./mrcpchromadb/"
|
27 |
+
# vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="mrcppassmednotes")
|
|
|
|
|
|
|
28 |
|
29 |
# Load the LLM
|
30 |
llm = Together(
|
31 |
model="mistralai/Mixtral-8x22B-Instruct-v0.1",
|
32 |
temperature=0.2,
|
33 |
+
max_new_tokens=22000,
|
34 |
top_k=12,
|
35 |
together_api_key=os.environ['pilotikval']
|
36 |
)
|
|
|
39 |
llmc = Together(
|
40 |
model="mistralai/Mixtral-8x22B-Instruct-v0.1",
|
41 |
temperature=0.2,
|
42 |
+
max_new_tokens=1000,
|
43 |
top_k=3,
|
44 |
together_api_key=os.environ['pilotikval']
|
45 |
)
|
|
|
47 |
msgs = StreamlitChatMessageHistory(key="langchain_messages")
|
48 |
memory = ConversationBufferMemory(chat_memory=msgs)
|
49 |
|
|
|
|
|
50 |
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
51 |
|
52 |
+
def _combine_documents(docs, document_prompt=DEFAULT_DOCUMENT_PROMPT, document_separator="\n\n"):
|
53 |
+
doc_strings = [format_document(doc, document_prompt) for doc in docs]
|
54 |
+
return document_separator.join(doc_strings)
|
|
|
|
|
|
|
|
|
55 |
|
56 |
chistory = []
|
57 |
|
58 |
def store_chat_history(role: str, content: str):
|
|
|
59 |
chistory.append({"role": role, "content": content})
|
60 |
|
|
|
61 |
# Define the Streamlit app
|
62 |
def app():
|
|
|
|
|
|
|
63 |
with st.sidebar:
|
|
|
64 |
st.title("dochatter")
|
|
|
65 |
option = st.selectbox(
|
66 |
'Which retriever would you like to use?',
|
67 |
('General Medicine', 'RespiratoryFishman', 'RespiratoryMurray', 'MedMRCP2', 'OldMedicine')
|
68 |
)
|
69 |
+
|
70 |
if option == 'RespiratoryFishman':
|
71 |
persist_directory="./respfishmandbcud/"
|
72 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="fishmannotescud")
|
73 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
|
|
74 |
|
75 |
+
elif option == 'RespiratoryMurray':
|
76 |
persist_directory="./respmurray/"
|
77 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="respmurraynotes")
|
78 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
|
|
79 |
|
80 |
+
elif option == 'MedMRCP2':
|
81 |
persist_directory="./medmrcp2store/"
|
82 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="medmrcp2notes")
|
83 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
|
|
84 |
|
85 |
+
elif option == 'General Medicine':
|
86 |
persist_directory="./oxfordmedbookdir/"
|
87 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="oxfordmed")
|
88 |
retriever = vectordb.as_retriever(search_kwargs={"k": 7})
|
89 |
+
|
|
|
90 |
else:
|
91 |
persist_directory="./mrcpchromadb/"
|
92 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_function, collection_name="mrcppassmednotes")
|
93 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
|
|
|
|
94 |
|
95 |
+
if "messages" not in st.session_state:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
_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.
|
|
|
|
|
99 |
Chat History:
|
100 |
{chat_history}
|
101 |
Follow Up Input: {question}
|
|
|
108 |
"""
|
109 |
ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
|
110 |
|
|
|
111 |
_inputs = RunnableParallel(
|
112 |
standalone_question=RunnablePassthrough.assign(
|
113 |
chat_history=lambda x: chistory
|
|
|
122 |
}
|
123 |
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | llm
|
124 |
|
125 |
+
st.header("Hello Doctor, How can I help?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"):
|
|
|
148 |
message = {"role": "assistant", "content": response}
|
149 |
st.session_state.messages.append(message)
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
if __name__ == '__main__':
|
152 |
+
app()
|