from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.prompts import PromptTemplate
# from langchain_together import Together
from langchain_community.llms import HuggingFaceEndpoint
import os
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationalRetrievalChain
import streamlit as st
import time
st.set_page_config(page_title="MedChat", page_icon="favicon.png")
col1, col2, col3 = st.columns([1,4,1])
with col2:
st.image("https://github.com/harshitv804/MedChat/assets/100853494/86b9efcc-32cd-42ae-a2ce-90e5c6c0401e")
st.markdown(
"""
""",
unsafe_allow_html=True,
)
def reset_conversation():
st.session_state.messages = []
st.session_state.memory.clear()
if "messages" not in st.session_state:
st.session_state.messages = []
if "memory" not in st.session_state:
st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history",return_messages=True)
embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True, "revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
db = FAISS.load_local("medchat_db", embeddings, allow_dangerous_deserialization=True)
db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 4})
prompt_template = """[INST]Follow these instructions carefully: You are a medical practitioner chatbot providing accurate medical information, adopting a doctor's perspective in your responses. Utilize the provided context, chat history, and question, choosing only the necessary information based on the user's query. Don't answer to any questions if in contexts, Avoid generating your own questions and answers. Do not reference chat history if irrelevant to the current question; only use it for similar-related queries. Prioritize the given context when searching for relevant information, emphasizing clarity and conciseness in your responses. If multiple medicines share the same name but have different strengths, ensure to mention them. Exclude any mention of medicine costs. Stick to context directly related to the user's question, and use your knowledge base to answer inquiries outside the given context. Abstract and concise responses are key; do not repeat the chat template in your answers. If you lack information, simply state that you don't know.
CONTEXT: {context}
CHAT HISTORY: {chat_history}
QUESTION: {question}
ANSWER:
[INST]
"""
prompt = PromptTemplate(template=prompt_template,
input_variables=['context', 'question', 'chat_history'])
HUGGINGFACEHUB_API_TOKEN=os.environ['HUGGINGFACEHUB_API_TOKEN']
repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
llm = HuggingFaceEndpoint(
repo_id=repo_id, max_length=512, temperature=0.7, token=HUGGINGFACEHUB_API_TOKEN
)
# llm = Together(
# model="mistralai/Mistral-7B-Instruct-v0.2",
# temperature=0.7,
# max_tokens=512,
# together_api_key=f"{TOGETHER_AI_API}"
# )
qa = ConversationalRetrievalChain.from_llm(
llm=llm,
memory=st.session_state.memory,
retriever=db_retriever,
combine_docs_chain_kwargs={'prompt': prompt}
)
for message in st.session_state.messages:
with st.chat_message(message.get("role")):
st.write(message.get("content"))
input_prompt = st.chat_input("Say something")
if input_prompt:
with st.chat_message("user"):
st.write(input_prompt)
st.session_state.messages.append({"role":"user","content":input_prompt})
with st.chat_message("assistant"):
with st.status("Thinking 💡...",expanded=True):
result = qa.invoke(input=input_prompt)
message_placeholder = st.empty()
full_response = "⚠️ **_Note: Information provided may be inaccurate. Consult a qualified doctor for accurate advice._** \n\n\n"
for chunk in result["answer"]:
full_response+=chunk
time.sleep(0.02)
message_placeholder.markdown(full_response+" ▌")
st.button('Reset All Chat 🗑️', on_click=reset_conversation)
st.session_state.messages.append({"role":"assistant","content":result["answer"]})