sonali-tamhankar's picture
Changed question to QAPI
edd3984
raw
history blame
3.26 kB
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain import HuggingFaceHub
from langchain.chains import RetrievalQA
import streamlit as st
st.set_page_config(page_title = "Hospital Regulatory Chat", page_icon=":hospital:")
DB_FAISS_PATH = '.'
def get_vectorstore():
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'})
vector_store = FAISS.load_local(DB_FAISS_PATH, embeddings)
return vector_store
vector_store = get_vectorstore()
llm = HuggingFaceHub(repo_id = "meta-llama/Llama-2-7b-chat-hf",model_kwargs={"temperature":0.5}) #, "max_length":512})
qa_chain = RetrievalQA.from_chain_type(llm=llm,
chain_type='stuff',
retriever=vector_store.as_retriever(search_kwargs={'k': 10}),
#retriever=vector_store.as_retriever(search_kwargs={"score_threshold": .01}),
return_source_documents = True
)
source_dictionary = {"data\CMS_SOMA.pdf":"[CMS State Operations Manual Appendix A](https://www.cms.gov/regulations-and-guidance/guidance/manuals/downloads/som107ap_a_hospitals.pdf)",
"data\DOH-RCW.pdf":"[Revised Code of Washington (RCW) Chapter 70.41](https://app.leg.wa.gov/rcw/default.aspx?cite=70.41)",
"data\WAC 246-320.pdf":"[Washington Administrative Code (WAC) 246-320](https://app.leg.wa.gov/WAC/default.aspx?cite=246-320)"}
with st.container():
st.title("Hospital Regulation Chat")
with st.sidebar:
st.subheader("Find regulations for hospitals in the state of Washington.")
st.markdown("""
We look into three sources to find top ten most relevant excerpts:
- [CMS State Operations Manual Appendix A](https://www.cms.gov/regulations-and-guidance/guidance/manuals/downloads/som107ap_a_hospitals.pdf)
- [Revised Code of Washington (RCW) Chapter 70.41](https://app.leg.wa.gov/rcw/default.aspx?cite=70.41)
- [Washington Administrative Code (WAC) 246-320](https://app.leg.wa.gov/WAC/default.aspx?cite=246-320)
""") #, unsafe_allow_html=True)
st.write("This is tool is meant to assist healthcare workers to the extent it can. Please note that the page numbers may be occasionally slightly off, use the matching excerpts to find the reference if this happens.")
st.markdown("**Ask your question and :red[click 'Find excerpts'.]**")
prompt = st.text_input("e.g. What are the rules regarding a Quality Improvement, or QAPI program?")
if (st.button("Find excerpts")):
answer = qa_chain({"query":prompt})
n = len(answer['source_documents'])
for i in range(n):
with st.container():
st.subheader(source_dictionary[answer['source_documents'][i].metadata['source']])
page_no = "**Page: " + str(answer['source_documents'][i].metadata['page']) + "**"
st.markdown(page_no)
st.write("...")
st.write(answer['source_documents'][i].page_content)
st.write("...")
st.write('---------------------------------\n\n')