pragneshbarik commited on
Commit
cebfd3c
1 Parent(s): 8b18fd0

implemented RAG

Browse files
.gitignore CHANGED
@@ -1 +1,4 @@
1
- .env
 
 
 
 
1
+ .env
2
+ *.ipynb
3
+ *.csv
4
+ *.json
__pycache__/mistral7b.cpython-310.pyc ADDED
Binary file (1.22 kB). View file
 
app.py CHANGED
@@ -1,13 +1,56 @@
1
  import streamlit as st
2
  from mistral7b import mistral
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  st.set_page_config(
5
  page_title="Ikigai Chat",
 
6
  )
7
 
8
- import time
9
-
10
-
11
  if "messages" not in st.session_state:
12
  st.session_state.messages = []
13
 
@@ -17,7 +60,6 @@ if "tokens_used" not in st.session_state:
17
  if "inference_time" not in st.session_state:
18
  st.session_state.inference_time = [0.00]
19
 
20
-
21
  if "temp" not in st.session_state:
22
  st.session_state.temp = 0.8
23
 
@@ -25,24 +67,34 @@ if "history" not in st.session_state:
25
  st.session_state.history = [["""
26
  You are not mistral AI, but rather a chat bot trained at Ikigai Labs, when ever asked you need to answer as ikigai Labs' assistant.
27
  Ikigai helps modern analysts and operations teams automate data-intensive business, finance, analytics, and supply-chain operations.
28
- The company's Inventory Ops automates inventory tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.""",
 
 
29
  """
30
  Yes, you are correct. Ikigai Labs is a company that specializes in helping
31
  modern analysts and operations teams automate data-intensive business, finance, analytics,
32
  and supply chain operations. One of their products is Inventory Ops, which automates inventory
33
  tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.
34
  This helps businesses optimize their inventory levels and reduce costs.
35
- Is there anything else you would like to know about Ikigai Labs or their products?
36
-
37
-
38
- """]]
39
 
40
  if "top_k" not in st.session_state:
41
- st.session_state.top_k = 5
42
 
43
  if "repetion_penalty" not in st.session_state :
44
  st.session_state.repetion_penalty = 1
 
 
 
45
  with st.sidebar:
 
 
 
 
 
46
  st.markdown("# Model Analytics")
47
 
48
  st.write("Tokens used :", st.session_state['tokens_used'])
@@ -68,23 +120,20 @@ with st.sidebar:
68
  label="Repetion Penalty", min_value=0., max_value=1., step=0.1, value=1.
69
  )
70
 
71
- st.markdown("---")
 
 
72
 
73
- st.markdown("# Retrieval Settings")
74
- st.slider(label="Documents to retrieve",
75
- min_value=1, max_value=10, value=3)
76
-
77
- st.info("**2023 ©️ Pragnesh Barik**")
78
-
79
-
80
  st.image("ikigai.svg")
81
  st.title("Ikigai Chat")
 
82
 
83
  with st.expander("What is Ikigai Chat ?"):
84
  st.info("""Ikigai Chat is a vector database powered chat agent, it works on the principle of
85
  of Retrieval Augmented Generation (RAG), Its primary function revolves around maintaining an extensive repository of Ikigai Docs and providing users with answers that align with their queries.
86
  This approach ensures a more refined and tailored response to user inquiries.""")
87
-
 
88
 
89
  for message in st.session_state.messages:
90
  with st.chat_message(message["role"]):
@@ -95,8 +144,14 @@ if prompt := st.chat_input("Chat with Ikigai Docs..."):
95
  st.chat_message("user").markdown(prompt)
96
  st.session_state.messages.append({"role": "user", "content": prompt})
97
 
98
- # st.write("ing")
99
  tick = time.time()
 
 
 
 
 
 
100
  with st.spinner("Generating response...") :
101
  response = mistral(prompt, st.session_state.history,
102
  temperature=st.session_state.temp, max_new_tokens=st.session_state.max_tokens)
@@ -110,8 +165,16 @@ if prompt := st.chat_input("Chat with Ikigai Docs..."):
110
  st.session_state["tokens_used"] = len_response + \
111
  st.session_state["tokens_used"]
112
 
 
113
  with st.chat_message("assistant"):
114
- st.markdown(response)
 
 
 
115
  st.session_state.history.append([prompt, response])
116
- st.session_state.messages.append(
117
- {"role": "assistant", "content": response})
 
 
 
 
 
1
  import streamlit as st
2
  from mistral7b import mistral
3
+ import time
4
+ import pandas as pd
5
+ import pinecone
6
+ import os
7
+ from dotenv import load_dotenv
8
+ from sentence_transformers import SentenceTransformer
9
+ load_dotenv()
10
+
11
+ PINECONE_TOKEN = os.getenv('PINECONE_TOKEN')
12
+
13
+ pinecone.init(
14
+ api_key=PINECONE_TOKEN,
15
+ environment='gcp-starter'
16
+ )
17
+
18
+ pinecone_index = pinecone.Index('ikigai-chat')
19
+ text_vectorizer = SentenceTransformer('all-distilroberta-v1')
20
+
21
+ def gen_augmented_prompt(prompt, top_k) :
22
+ query_vector = text_vectorizer.encode(prompt).tolist()
23
+ res = pinecone_index.query(vector=query_vector, top_k=top_k, include_metadata=True)
24
+ matches = res['matches']
25
+
26
+ context = ""
27
+ links = []
28
+ for match in matches :
29
+ context+=match["metadata"]["chunk"] + "\n\n"
30
+ links.append(match["metadata"]["link"])
31
+
32
+
33
+
34
+ generated_prompt = f"""
35
+ FOR THIS GIVEN CONTEXT {context},
36
+
37
+
38
+ ANSWER THE FOLLOWING PROMPT {prompt}
39
+ """
40
+ return generated_prompt, links
41
+
42
+ data = {
43
+ "Attribute": ["LLM", "Text Vectorizer", "Vector Database","CPU", "System RAM"],
44
+ "Information": ["Mistral-7B-Instruct-v0.1 (more models soon)","all-distilroberta-v1", "Hosted Pinecone" ,"2 vCPU", "16 GB"]
45
+ }
46
+ df = pd.DataFrame(data)
47
+
48
 
49
  st.set_page_config(
50
  page_title="Ikigai Chat",
51
+ page_icon="🤖",
52
  )
53
 
 
 
 
54
  if "messages" not in st.session_state:
55
  st.session_state.messages = []
56
 
 
60
  if "inference_time" not in st.session_state:
61
  st.session_state.inference_time = [0.00]
62
 
 
63
  if "temp" not in st.session_state:
64
  st.session_state.temp = 0.8
65
 
 
67
  st.session_state.history = [["""
68
  You are not mistral AI, but rather a chat bot trained at Ikigai Labs, when ever asked you need to answer as ikigai Labs' assistant.
69
  Ikigai helps modern analysts and operations teams automate data-intensive business, finance, analytics, and supply-chain operations.
70
+ The company's Inventory Ops automates inventory tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.
71
+ """,
72
+
73
  """
74
  Yes, you are correct. Ikigai Labs is a company that specializes in helping
75
  modern analysts and operations teams automate data-intensive business, finance, analytics,
76
  and supply chain operations. One of their products is Inventory Ops, which automates inventory
77
  tracking and monitoring by creating a single, real-time view of inventory across all locations and channels.
78
  This helps businesses optimize their inventory levels and reduce costs.
79
+ Is there anything else you would like to know about Ikigai Labs or their products?"""],
80
+ ["""You are ikigai chat from now on, so answer accordingly""",
81
+ """Sure, I will do my best to answer your questions as Ikigai Chat.
82
+ Let me know if you have any specific questions about Ikigai Labs or our products."""]]
83
 
84
  if "top_k" not in st.session_state:
85
+ st.session_state.top_k = 3
86
 
87
  if "repetion_penalty" not in st.session_state :
88
  st.session_state.repetion_penalty = 1
89
+
90
+ if "rag_enabled" not in st.session_state :
91
+ st.session_state.rag_enabled = True
92
  with st.sidebar:
93
+ st.markdown("# Retrieval Settings")
94
+ st.session_state.rag_enabled = st.toggle("Activate RAG")
95
+ st.session_state.top_k = st.slider(label="Documents to retrieve",
96
+ min_value=1, max_value=10, value=3, disabled=not st.session_state.rag_enabled)
97
+ st.markdown("---")
98
  st.markdown("# Model Analytics")
99
 
100
  st.write("Tokens used :", st.session_state['tokens_used'])
 
120
  label="Repetion Penalty", min_value=0., max_value=1., step=0.1, value=1.
121
  )
122
 
123
+ st.markdown("""
124
+ > **2023 ©️ Pragnesh Barik**
125
+ """)
126
 
 
 
 
 
 
 
 
127
  st.image("ikigai.svg")
128
  st.title("Ikigai Chat")
129
+ st.caption("Maintained and developed by Pragnesh Barik.")
130
 
131
  with st.expander("What is Ikigai Chat ?"):
132
  st.info("""Ikigai Chat is a vector database powered chat agent, it works on the principle of
133
  of Retrieval Augmented Generation (RAG), Its primary function revolves around maintaining an extensive repository of Ikigai Docs and providing users with answers that align with their queries.
134
  This approach ensures a more refined and tailored response to user inquiries.""")
135
+
136
+ st.table(df)
137
 
138
  for message in st.session_state.messages:
139
  with st.chat_message(message["role"]):
 
144
  st.chat_message("user").markdown(prompt)
145
  st.session_state.messages.append({"role": "user", "content": prompt})
146
 
147
+
148
  tick = time.time()
149
+
150
+ links = []
151
+ if st.session_state.rag_enabled :
152
+ with st.spinner("Fetching relevent documents from Ikigai Docs...."):
153
+ prompt, links = gen_augmented_prompt(prompt=prompt, top_k=st.session_state.top_k)
154
+
155
  with st.spinner("Generating response...") :
156
  response = mistral(prompt, st.session_state.history,
157
  temperature=st.session_state.temp, max_new_tokens=st.session_state.max_tokens)
 
165
  st.session_state["tokens_used"] = len_response + \
166
  st.session_state["tokens_used"]
167
 
168
+ formatted_links = ", ".join(links)
169
  with st.chat_message("assistant"):
170
+ if st.session_state.rag_enabled :
171
+ st.markdown(response + f"""\n\nFetched from :\n {formatted_links}""")
172
+ else :
173
+ st.markdown(response)
174
  st.session_state.history.append([prompt, response])
175
+
176
+ if st.session_state.rag_enabled :
177
+ st.session_state.messages.append(
178
+ {"role": "assistant", "content": response + f"""\n\nFetched from :\n {formatted_links}"""})
179
+ else :
180
+ st.session_state.messages.append({"role": "assistant", "content": response})
chat_log.txt DELETED
File without changes
id_log.txt DELETED
File without changes
requirements.txt CHANGED
@@ -73,3 +73,5 @@ validators==0.22.0
73
  watchdog==3.0.0
74
  wcwidth==0.2.6
75
  zipp==3.17.0
 
 
 
73
  watchdog==3.0.0
74
  wcwidth==0.2.6
75
  zipp==3.17.0
76
+ sentence-transformers==2.2.2
77
+ pinecone-client==2.2.4