akshansh36 commited on
Commit
d09d051
·
verified ·
1 Parent(s): 72a8774

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -29,7 +29,7 @@ PINECONE_API=os.getenv("PINECONE_API_KEY")
29
  pc = pinecone.Pinecone(
30
  api_key=PINECONE_API
31
  )
32
- index_name = "iocl"
33
  index = pc.Index(index_name)
34
  # MongoDB connection setup
35
  MONGO_URI = os.getenv("MONGO_URI")
@@ -142,21 +142,21 @@ def regenerate_response():
142
 
143
 
144
 
145
- # When generating a response, pass only the latest 15 messages to the LLM
146
  def generate_summary(chunks, query, chat_history):
147
  try:
148
- # Limit the history sent to the LLM to the latest 15 question-answer pairs
149
- limited_history = chat_history[-5:] if len(chat_history) > 5 else chat_history
150
 
151
  # Create conversation history for the LLM, only using the last 15 entries
152
  history_text = "\n".join([f"User: {q['improved_question']}\nLLM: {q['answer']}" for q in limited_history])
153
 
154
  # Define the system and user prompts including the limited history
155
  prompt = ChatPromptTemplate.from_messages([
156
- ("system", """You are a chatbot specializing in answering queries related to Indian Oil Corporation Limited (IOCL). You will be provided with chunks of data from the IOCL website to answer user queries. Each chunk will include associated URLs, which you must reference in your responses to show the source of the information.
157
  Key Guidelines:
158
-
159
- 1.If the user query is not clear, or you think multiple answers are possbile, you can ask for clarification with proper reasoning. Do not mention chunk name in any of your replies.
 
160
  2.Detailed and Clear: Provide thorough, clear, and concise responses without omitting relevant information from the data chunks.
161
  3.Natural Summarization: When answering, you must not directly quote chunk names,formats. Instead, summarize or interpret the data naturally and conversationally.
162
  4.Use Conversation History: Refer back to the conversation history to maintain consistency and build on prior responses, if applicable.
@@ -164,22 +164,24 @@ def generate_summary(chunks, query, chat_history):
164
  6.Graceful Handling of General Queries: If a user sends greetings, introduction, salutations, or unrelated questions, respond appropriately and conversationally.
165
  7.Include Source URLs: Always include the URLs from the relevant chunks of data that you're using to answer the query.
166
  8.Thoroughly looks for answer to the query in the provided chunks before replying, if you feel the query is irrelevant or answer is not present then you can ask user to clarify or tell that it cannot be answered.
167
-
168
 
169
- """),
170
 
171
  ("human", f'''
172
  "Query":\n {query}\n
 
173
  "Extracted Data": \n{chunks}\n
 
174
  "Previous Conversation History": \n{history_text}\n
175
-
176
  '''
177
  )
178
  ])
179
 
180
  # Chain the prompt with LLM for response generation
181
  chain = prompt | llm
182
- result = chain.invoke({"Query": query,"Extracted Data": chunks,"Previous Conversation History":history_text})
183
 
184
  # Return the generated response
185
  logging.info(f"LLM answer is :{result}")
@@ -189,13 +191,12 @@ def generate_summary(chunks, query, chat_history):
189
  st.error(f"Error answering your question: {e}")
190
  return None
191
 
192
-
193
- def get_context_from_messages(query,chat_history):
194
  try:
195
 
196
  logging.info(f"Getting context from original query: {query}")
197
 
198
- # Limit the history sent to the LLM to the latest 15 question-answer pairs
199
  limited_history = chat_history[-3:] if len(chat_history) > 3 else chat_history
200
 
201
  # Create conversation history for the LLM, only using the last 15 entries
@@ -203,12 +204,11 @@ def get_context_from_messages(query,chat_history):
203
 
204
  # Define the system and user prompts including the limited history
205
  prompt = ChatPromptTemplate.from_messages([
206
- ("system", """"I will provide you with a user query and up to the last 3 messages from the chat history, including both questions and answers.Your task is to reformulate the user query nicely which makes complete sense and is completely self contained.
207
  The provided queries are related to Indian Oil Corporation limited (IOCL).
208
  1. If the query is a follow-up, use the provided chat history to reconstruct a well-defined, contextually complete query that can stand alone."
209
  2. if the query is self contained, if applicable try to improve it to make is coherent.
210
  3. if the user query is salutations, greetings or not relevant in that case give the query back as it is.
211
- 4. Even if the user query is just a one word query, you must formulate a well defined query.
212
  I have provided an output format below, stricly follow it. Do not give anything else other than just the output.
213
  expected_output_format: "query: String or None"
214
  """),
@@ -221,7 +221,7 @@ def get_context_from_messages(query,chat_history):
221
 
222
  # Chain the prompt with LLM for response generation
223
  chain = prompt | llm
224
- result = chain.invoke({"Query": query,"Previous Conversation History":history_text})
225
  logging.info(f"llm answer for query extraction is :{result}")
226
 
227
  # Return the generated response
@@ -231,6 +231,7 @@ def get_context_from_messages(query,chat_history):
231
  logging.error(f"exception occured in getting query from original query :{e}")
232
  return None
233
 
 
234
  def get_query_from_llm_answer(llm_output):
235
  match = re.search(r'query:\s*(.*)', llm_output)
236
  if match:
@@ -292,7 +293,7 @@ if user_question:
292
  query=user_question
293
 
294
  query_embedding=embeddings.embed_query(query)
295
- search_results = index.query(vector=query_embedding, top_k=3, include_metadata=True)
296
  matches=search_results['matches']
297
 
298
  content=""
@@ -306,7 +307,7 @@ if user_question:
306
  reply = generate_summary(content, query, st.session_state['chat_history'])
307
 
308
  if reply:
309
- # Append the new question-answer pair t o chat history
310
  st.session_state['chat_history'].append({"question": user_question, "answer": reply,"improved_question":query})
311
 
312
  # Update the current chat session in MongoDB
 
29
  pc = pinecone.Pinecone(
30
  api_key=PINECONE_API
31
  )
32
+ index_name = "iocl2"
33
  index = pc.Index(index_name)
34
  # MongoDB connection setup
35
  MONGO_URI = os.getenv("MONGO_URI")
 
142
 
143
 
144
 
 
145
  def generate_summary(chunks, query, chat_history):
146
  try:
147
+ # Limit the history sent to the LLM to the latest 3 question-answer pairs
148
+ limited_history = chat_history[-3:] if len(chat_history) > 3 else chat_history
149
 
150
  # Create conversation history for the LLM, only using the last 15 entries
151
  history_text = "\n".join([f"User: {q['improved_question']}\nLLM: {q['answer']}" for q in limited_history])
152
 
153
  # Define the system and user prompts including the limited history
154
  prompt = ChatPromptTemplate.from_messages([
155
+ ("system", """You are a chatbot specializing in answering queries related to Indian Oil Corporation Limited (IOCL). You will be provided with chunks of data from the IOCL website to answer user queries. Each chunk will include associated URLs, You must give the url of the chunks which you are using to answer the query.
156
  Key Guidelines:
157
+
158
+ 1.If the user query is not clear, or you think multiple answers are possbile, you should ask for clarification with proper reasoning.
159
+ 2.Do not mention chunk name in any of your replies.
160
  2.Detailed and Clear: Provide thorough, clear, and concise responses without omitting relevant information from the data chunks.
161
  3.Natural Summarization: When answering, you must not directly quote chunk names,formats. Instead, summarize or interpret the data naturally and conversationally.
162
  4.Use Conversation History: Refer back to the conversation history to maintain consistency and build on prior responses, if applicable.
 
164
  6.Graceful Handling of General Queries: If a user sends greetings, introduction, salutations, or unrelated questions, respond appropriately and conversationally.
165
  7.Include Source URLs: Always include the URLs from the relevant chunks of data that you're using to answer the query.
166
  8.Thoroughly looks for answer to the query in the provided chunks before replying, if you feel the query is irrelevant or answer is not present then you can ask user to clarify or tell that it cannot be answered.
167
+ 9.Sometimes chunks might contain very less data still use it if its relevant.
168
 
169
+ """),
170
 
171
  ("human", f'''
172
  "Query":\n {query}\n
173
+ Below are the pinecone chunks that should be used to answer the user query:
174
  "Extracted Data": \n{chunks}\n
175
+ Below is the previous conversation history:
176
  "Previous Conversation History": \n{history_text}\n
177
+
178
  '''
179
  )
180
  ])
181
 
182
  # Chain the prompt with LLM for response generation
183
  chain = prompt | llm
184
+ result = chain.invoke({"Query": query, "Extracted Data": chunks, "Previous Conversation History": history_text})
185
 
186
  # Return the generated response
187
  logging.info(f"LLM answer is :{result}")
 
191
  st.error(f"Error answering your question: {e}")
192
  return None
193
 
194
+ def get_context_from_messages(query, chat_history):
 
195
  try:
196
 
197
  logging.info(f"Getting context from original query: {query}")
198
 
199
+ # Limit the history sent to the LLM to the latest 3 question-answer pairs
200
  limited_history = chat_history[-3:] if len(chat_history) > 3 else chat_history
201
 
202
  # Create conversation history for the LLM, only using the last 15 entries
 
204
 
205
  # Define the system and user prompts including the limited history
206
  prompt = ChatPromptTemplate.from_messages([
207
+ ("system", """"I will provide you with a user query and up to the last 3 messages from the chat history which includes both questions and answers.Your task is to understand the user query nicely and restructure it if required such that it makes complete sense and is completely self contained.
208
  The provided queries are related to Indian Oil Corporation limited (IOCL).
209
  1. If the query is a follow-up, use the provided chat history to reconstruct a well-defined, contextually complete query that can stand alone."
210
  2. if the query is self contained, if applicable try to improve it to make is coherent.
211
  3. if the user query is salutations, greetings or not relevant in that case give the query back as it is.
 
212
  I have provided an output format below, stricly follow it. Do not give anything else other than just the output.
213
  expected_output_format: "query: String or None"
214
  """),
 
221
 
222
  # Chain the prompt with LLM for response generation
223
  chain = prompt | llm
224
+ result = chain.invoke({"Query": query, "Previous Conversation History": history_text})
225
  logging.info(f"llm answer for query extraction is :{result}")
226
 
227
  # Return the generated response
 
231
  logging.error(f"exception occured in getting query from original query :{e}")
232
  return None
233
 
234
+
235
  def get_query_from_llm_answer(llm_output):
236
  match = re.search(r'query:\s*(.*)', llm_output)
237
  if match:
 
293
  query=user_question
294
 
295
  query_embedding=embeddings.embed_query(query)
296
+ search_results = index.query(vector=query_embedding, top_k=15, include_metadata=True)
297
  matches=search_results['matches']
298
 
299
  content=""
 
307
  reply = generate_summary(content, query, st.session_state['chat_history'])
308
 
309
  if reply:
310
+ # Append the new question-answer pair to chat history
311
  st.session_state['chat_history'].append({"question": user_question, "answer": reply,"improved_question":query})
312
 
313
  # Update the current chat session in MongoDB