akshansh36 commited on
Commit
e8a6d88
·
verified ·
1 Parent(s): 3014b70

improved prompt and reduced chunks being fetched and chat history being passed

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -146,38 +146,37 @@ def regenerate_response():
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[-10:] if len(chat_history) > 10 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 who specialises in answering user queries related to Indan Oil Corporation Limitied(IOCL).
157
- this is the extracted chunks of data from the Indian Oil Corporation Limited (IOCL) website. You will be provided with a query, and you must use these chunks of data to answer it comprehensively.
158
- Each chunk of data has website source page urls associated to it, you must include these relevant urls in your answer telling the source.
159
- Additionally, the conversation history is also provided which may contain relevant context or prior queries and responses. Use this history to ensure your answer is accurate and coherent, building on previous information if necessary.
160
-
161
  Key Guidelines:
162
- 1.Accuracy is paramount: If the extracted data or conversation history does not contain the information required to answer the query, clearly state, "The answer is not available in the context." Do not attempt to provide a speculative or incorrect response.
163
- 2.Be detailed: Provide clear, concise, and thorough answers without omitting any relevant information from the extracted data.
164
- 3.Avoid quoting field names: When responding, avoid directly quoting or referencing field names or formats from the extracted data. Instead, present the information naturally, as if summarizing or interpreting the data. Try to give the answer in points.
165
- 4.Use the conversation history: When applicable, refer to earlier parts of the conversation to ensure consistency and accuracy in your response.
166
- 6.Answer the queries in conversational style.
167
- 7.It is must to include urls present in the chunk which you are using to formulate your answer.
168
- 8. Its possible that sometimes a user query will be greetings, your introduction, salutations, exclamations or completely unrelated to iocl handle them gracefully.
 
169
  """),
 
170
  ("human", f'''
171
- Previous Conversation History: \n{history_text}\n
172
- "Extracted Data": \n{chunks}\n
173
  "Query":\n {query}\n
 
 
 
174
  '''
175
  )
176
  ])
177
 
178
  # Chain the prompt with LLM for response generation
179
  chain = prompt | llm
180
- result = chain.invoke({"Previous Concversation History":history_text,"Extracted Data": chunks, "Query": query})
181
 
182
  # Return the generated response
183
  logging.info(f"LLM answer is :{result}")
@@ -210,7 +209,7 @@ def get_context_from_messages(query,chat_history):
210
  """),
211
  ("human", f'''
212
  "Query":\n {query}\n
213
- Previous Conversation History: \n{history_text}\n
214
  '''
215
  )
216
  ])
@@ -288,7 +287,7 @@ if user_question:
288
  query=user_question
289
 
290
  query_embedding=embeddings.embed_query(query)
291
- search_results = index.query(vector=query_embedding, top_k=5, include_metadata=True)
292
  matches=search_results['matches']
293
 
294
  content=""
@@ -298,7 +297,7 @@ if user_question:
298
  content += f"chunk{i}: {chunk}\n" + f"url{i}: {url}\n"
299
 
300
 
301
-
302
  reply = generate_summary(content, query, st.session_state['chat_history'])
303
 
304
  if reply:
 
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
+ 1.Accurate Responses: If the answer to a query isn't found in the provided chunks or conversation history, respond with: "The answer is not available in the context." Avoid speculation or providing incorrect answers.
159
+ 2.Detailed and Clear: Provide thorough, clear, and concise responses without omitting relevant information from the data chunks.
160
+ 3.Natural Summarization: When answering, avoid directly quoting field names or formats. Instead, summarize or interpret the data naturally and conversationally.
161
+ 4.Use Conversation History: Refer back to the conversation history to maintain consistency and build on prior responses, if applicable.
162
+ 5.Ignore Unanswered Queries: If the conversation history contains previous responses like "The answer is not available in the context," disregard them when formulating your current response.
163
+ 6.Graceful Handling of General Queries: If a user sends greetings, introduction, salutations, or unrelated questions, respond appropriately and conversationally.
164
+ 7.Include Source URLs: Always include the URLs from the relevant chunks of data that you're using to answer the query.
165
+
166
  """),
167
+
168
  ("human", f'''
 
 
169
  "Query":\n {query}\n
170
+ "Extracted Data": \n{chunks}\n
171
+ "Previous Conversation History": \n{history_text}\n
172
+
173
  '''
174
  )
175
  ])
176
 
177
  # Chain the prompt with LLM for response generation
178
  chain = prompt | llm
179
+ result = chain.invoke({"Query": query,"Extracted Data": chunks,"Previous Conversation History":history_text})
180
 
181
  # Return the generated response
182
  logging.info(f"LLM answer is :{result}")
 
209
  """),
210
  ("human", f'''
211
  "Query":\n {query}\n
212
+ "Previous Conversation History": \n{history_text}\n
213
  '''
214
  )
215
  ])
 
287
  query=user_question
288
 
289
  query_embedding=embeddings.embed_query(query)
290
+ search_results = index.query(vector=query_embedding, top_k=3, include_metadata=True)
291
  matches=search_results['matches']
292
 
293
  content=""
 
297
  content += f"chunk{i}: {chunk}\n" + f"url{i}: {url}\n"
298
 
299
 
300
+ print(f"content being passed is {content}")
301
  reply = generate_summary(content, query, st.session_state['chat_history'])
302
 
303
  if reply: