harshitv804 commited on
Commit
d2f5407
1 Parent(s): b817615

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.vectorstores import FAISS
2
+ from langchain_community.embeddings import HuggingFaceEmbeddings
3
+ from langchain.callbacks.manager import CallbackManager
4
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
5
+ from langchain.chains import LLMChain
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain_community.llms import LlamaCpp
8
+ from langchain.memory import ConversationBufferMemory
9
+ from langchain.chains import ConversationalRetrievalChain
10
+ import streamlit as st
11
+ from langchain.callbacks import StreamlitCallbackHandler
12
+ import time
13
+ st.set_page_config(layout="wide")
14
+
15
+ embeddings = HuggingFaceEmbeddings(model_name="BAAI/llm-embedder")
16
+ db = FAISS.load_local("faiss_index", embeddings)
17
+ db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 3})
18
+ callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
19
+
20
+ llm = LlamaCpp(
21
+ model_path=r"C:\Users\TESS\Downloads\mistral-7b-instruct-v0.1.Q4_K_M.gguf",
22
+ temperature=0.75,
23
+ max_tokens=2000,
24
+ n_ctx = 4000,
25
+ top_p=1,
26
+ n_gpu_layers=10)
27
+
28
+ custom_prompt_template = """You are a medical practitioner who provides right medical information. Use the given following pieces of information to answer the user's question correctly. Add additional information from your knowledge base if necessary. If you don't know the answer, just say that you don't know, don't try to make up an answer.
29
+
30
+ Context: {context}
31
+
32
+ Current conversation: {chat_history}
33
+
34
+ Human: {question}
35
+ Only return the helpful answer below and nothing else.
36
+
37
+ AI Assistant:
38
+ """
39
+ prompt = PromptTemplate(template=custom_prompt_template,
40
+ input_variables=['context', 'question','chat_history'])
41
+
42
+ qa = ConversationalRetrievalChain.from_llm(
43
+ llm=llm,
44
+ memory=ConversationBufferMemory(
45
+ memory_key="chat_history",
46
+ ai_prefix="AI Assistant",
47
+ return_messages=True
48
+ ),
49
+ retriever=db_retriever,
50
+ combine_docs_chain_kwargs={'prompt': prompt}
51
+ )
52
+
53
+ if "messages" not in st.session_state:
54
+ st.session_state.messages = []
55
+
56
+ # st.write(st.session_state.messages)
57
+
58
+ for message in st.session_state.messages:
59
+ with st.chat_message(message.get("role")):
60
+ st.write(message.get("content"))
61
+
62
+ prompt = st.chat_input("Say something")
63
+
64
+ if prompt:
65
+ with st.chat_message("user"):
66
+ st.write(prompt)
67
+
68
+ st.session_state.messages.append({"role":"user","content":prompt})
69
+
70
+ with st.chat_message("assistant"):
71
+ with st.status("Thinking...",expanded=True):
72
+ st_callback = StreamlitCallbackHandler(st.container())
73
+ result = qa.invoke(input=prompt,callbacks=[st_callback])
74
+ st.session_state.messages.append({"role":"assistant","content":result["answer"]})
75
+ message_placeholder = st.empty()
76
+ full_response = ""
77
+
78
+ for chunk in result["answer"].split():
79
+ full_response+=chunk+" "
80
+ time.sleep(0.10)
81
+
82
+ message_placeholder.markdown(full_response)