harshitv804 commited on
Commit
f187321
1 Parent(s): 8022952

Rename app1.py to app.py

Browse files
Files changed (1) hide show
  1. app1.py → app.py +10 -3
app1.py → app.py RENAMED
@@ -3,6 +3,8 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
3
  from langchain.prompts import PromptTemplate
4
  from langchain_together import Together
5
  import os
 
 
6
  from langchain.memory import ConversationBufferWindowMemory
7
  from langchain.chains import ConversationalRetrievalChain
8
  import streamlit as st
@@ -55,7 +57,7 @@ embeddings = HuggingFaceEmbeddings(model_name="BAAI/llm-embedder")
55
  db = FAISS.load_local("medchat_db", embeddings)
56
  db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 3})
57
 
58
- custom_prompt_template = """This is a chat tempalte and you are a medical practitioner lmm who provides correct medical information. You are given the following pieces of information to answer the user's question correctly. Choose only the required context based on the user's question. Utilize the provided knowledge base and search for relevant information. Follow the question format closely. The information should be abstract and concise. Understand all the context given here and generate only the answer. If you don't know the answer, just say that you don't know, don't try to make up an answer.
59
 
60
  CONTEXT: {context}
61
 
@@ -63,7 +65,7 @@ CHAT HISTORY: {chat_history}
63
 
64
  QUESTION: {question}
65
 
66
- ANSWER
67
  """
68
  prompt = PromptTemplate(template=custom_prompt_template,
69
  input_variables=['context', 'question', 'chat_history'])
@@ -76,10 +78,15 @@ llm = Together(
76
  together_api_key=f"{TOGETHER_AI_API}"
77
  )
78
 
 
 
 
 
 
79
  qa = ConversationalRetrievalChain.from_llm(
80
  llm=llm,
81
  memory=st.session_state.memory,
82
- retriever=db_retriever,
83
  combine_docs_chain_kwargs={'prompt': prompt}
84
  )
85
 
 
3
  from langchain.prompts import PromptTemplate
4
  from langchain_together import Together
5
  import os
6
+ from langchain.retrievers.document_compressors import EmbeddingsFilter
7
+ from langchain.retrievers import ContextualCompressionRetriever
8
  from langchain.memory import ConversationBufferWindowMemory
9
  from langchain.chains import ConversationalRetrievalChain
10
  import streamlit as st
 
57
  db = FAISS.load_local("medchat_db", embeddings)
58
  db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 3})
59
 
60
+ custom_prompt_template = """This is a chat tempalte and you are a medical practitioner LLM who provides correct medical information. The way you speak should be in a doctor's perspective. You are given the following pieces of information to answer the user's question correctly. You will be given context, chat history and the question. Choose only the required context based on the user's question. If the follow-up questions are not related to the chat history, then don't use the history. Use chat history when required for similar related questions. While searching for the relevant information always give priority to the context given. If there are multiple medicines same medicine name and different strength then tell the user about it so that the next question by the user will be more specific. Utilize the provided knowledge base and search for relevant information from the context. Follow the user's question and the format closely. The answer should be abstract and concise. Understand all the context given here and generate only the answer, don't repeat the chat template in the answer. If you don't know the answer, just say that you don't know, don't try to make up an answer.
61
 
62
  CONTEXT: {context}
63
 
 
65
 
66
  QUESTION: {question}
67
 
68
+ ANSWER:
69
  """
70
  prompt = PromptTemplate(template=custom_prompt_template,
71
  input_variables=['context', 'question', 'chat_history'])
 
78
  together_api_key=f"{TOGETHER_AI_API}"
79
  )
80
 
81
+ embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.80)
82
+ compression_retriever = ContextualCompressionRetriever(
83
+ base_compressor=embeddings_filter, base_retriever=db_retriever
84
+ )
85
+
86
  qa = ConversationalRetrievalChain.from_llm(
87
  llm=llm,
88
  memory=st.session_state.memory,
89
+ retriever=compression_retriever,
90
  combine_docs_chain_kwargs={'prompt': prompt}
91
  )
92