harshitv804 commited on
Commit
14d123e
·
verified ·
1 Parent(s): 10db5dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_groq import ChatGroq
2
+ from langchain import PromptTemplate
3
+ import pickle
4
+ import streamlit as st
5
+ import os
6
+ import time
7
+ from langchain.memory import ConversationBufferWindowMemory
8
+ from langchain.chains import ConversationalRetrievalChain
9
+ from langchain_community.retrievers import TFIDFRetriever
10
+ from langchain.retrievers.document_compressors.flashrank_rerank import FlashrankRerank
11
+ from langchain_community.document_transformers import LongContextReorder
12
+ from langchain.retrievers import ContextualCompressionRetriever
13
+ from langchain.retrievers.document_compressors import DocumentCompressorPipeline
14
+
15
+ col1, col2, col3 = st.columns([1,4,1])
16
+ with col2:
17
+ st.image("https://github.com/harshitv804/RAG-without-Vector-DB/assets/100853494/cebecf35-bdd6-459c-8a1e-bad7c9a8037b")
18
+
19
+ st.markdown(
20
+ """
21
+ <style>
22
+ div.stButton > button:first-child {
23
+ background-color: #ffffff;
24
+ color: #000000
25
+ }
26
+ div.stButton > button:active {
27
+ background-color: #ff6262;
28
+ }
29
+
30
+ div[data-testid="stStatusWidget"] div button {
31
+ display: none;
32
+ }
33
+
34
+ .reportview-container {
35
+ margin-top: -2em;
36
+ }
37
+ #MainMenu {visibility: hidden;}
38
+ .stDeployButton {display:none;}
39
+ footer {visibility: hidden;}
40
+ #stDecoration {display:none;}
41
+ button[title="View fullscreen"]{
42
+ visibility: hidden;}
43
+ </style>
44
+ """,
45
+ unsafe_allow_html=True,
46
+ )
47
+
48
+ def reset_conversation():
49
+ st.session_state.messages = []
50
+ st.session_state.memory.clear()
51
+
52
+ if "messages" not in st.session_state:
53
+ st.session_state.messages = []
54
+
55
+ if "memory" not in st.session_state:
56
+ st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history",return_messages=True)
57
+
58
+
59
+ with open('cosmos_data.dump', 'rb') as f:
60
+ doc = pickle.load(f)
61
+
62
+ flash_rerank = FlashrankRerank(model="ms-marco-MiniLM-L-12-v2",top_n=8)
63
+ reordering = LongContextReorder()
64
+
65
+ tfid_retriever = TFIDFRetriever.from_documents(doc,k=50)
66
+
67
+ pipeline = DocumentCompressorPipeline(transformers=[reordering,flash_rerank])
68
+ compression_retriever = ContextualCompressionRetriever(
69
+ base_compressor=pipeline, base_retriever=tfid_retriever
70
+ )
71
+
72
+ prompt_template = """<s>[INST]This is a chat template and you are the guide to the universe, your primary objective is to provide accurate and concise information related to universe based on the user's questions. Do not generate your own questions and answers. You will adhere strictly to the instructions provided, offering relevant context from the knowledge base while avoiding unnecessary details. Your responses will be brief, to the point, and in compliance with the established format. If a question falls outside the given context, you will refrain from utilizing the chat history and instead rely on your own knowledge base to generate an appropriate response. You will prioritize the user's query and refrain from posing additional questions. The aim is to deliver professional, precise, and contextually relevant information pertaining to the universe.
73
+ CONTEXT: {context}
74
+ CHAT HISTORY: {chat_history}
75
+ QUESTION: {question}
76
+ ANSWER:
77
+ </s>[INST]
78
+ """
79
+
80
+ prompt = PromptTemplate(template=prompt_template,
81
+ input_variables=['context', 'question', 'chat_history'])
82
+
83
+ llm = ChatGroq(temperature=0.7,max_tokens=1024, groq_api_key=GROQ_API, model_name="mixtral-8x7b-32768")
84
+
85
+ qa = ConversationalRetrievalChain.from_llm(
86
+ llm=llm,
87
+ memory=st.session_state.memory,
88
+ retriever=compression_retriever,
89
+ combine_docs_chain_kwargs={'prompt': prompt}
90
+ )
91
+
92
+ for message in st.session_state.messages:
93
+ with st.chat_message(message.get("role")):
94
+ st.write(message.get("content"))
95
+
96
+ input_prompt = st.chat_input("Say something")
97
+
98
+ if input_prompt:
99
+ with st.chat_message("user"):
100
+ st.write(input_prompt)
101
+
102
+ st.session_state.messages.append({"role":"user","content":input_prompt})
103
+
104
+ with st.chat_message("assistant"):
105
+ with st.status("Thinking 💡...",expanded=True):
106
+ result = qa.invoke(input=input_prompt)
107
+
108
+ message_placeholder = st.empty()
109
+
110
+ full_response = "⚠️ **_Note: Information provided may be inaccurate._** \n\n\n"
111
+ for chunk in result["answer"]:
112
+ full_response+=chunk
113
+ time.sleep(0.02)
114
+
115
+ message_placeholder.markdown(full_response+" ▌")
116
+ st.button('Reset All Chat 🗑️', on_click=reset_conversation)
117
+
118
+ st.session_state.messages.append({"role":"assistant","content":result["answer"]})