Shreyas094 commited on
Commit
a0f74b4
1 Parent(s): d254d07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -32
app.py CHANGED
@@ -278,57 +278,58 @@ def rephrase_query(chat_history, query, temperature=0.2):
278
  system_prompt = f"""
279
  You are a highly intelligent and context-aware conversational assistant. Your tasks are as follows:
280
 
281
- 1. **Entity Identification and Quotation**:
282
- - Analyze the user's query to identify the main entities (e.g., organizations, brands, products, locations).
283
- - For each identified entity, enclose ONLY the entity itself in double quotes within the query.
284
- - If no identifiable entities are found, proceed without adding quotes.
285
-
286
- 2. **Query Preservation**:
287
- - Maintain the entire original query, including any parts after commas or other punctuation.
288
- - Do not remove or truncate any part of the original query.
289
-
290
- 3. **Appending Current Year**:
291
- - Append "after: {current_year}" to the end of the rephrased query.
292
- - Ensure there is a space before "after:" for proper formatting.
293
- - Do not use quotes or the "+" operator when adding the year.
294
-
295
- 4. **Output**:
296
- - Return ONLY the rephrased query, ensuring it is concise, clear, and contextually accurate.
297
- - Do not include any additional commentary or explanation.
 
 
 
 
 
 
298
 
299
  ### Example Scenarios
300
-
301
- **Scenario 1: Query with One Entity**
302
-
303
  - **User Query**: "What is the latest news on Golomt Bank?"
304
  - **Rephrased Query**: "What is the latest news on \"Golomt Bank\" after: {current_year}"
305
 
306
- **Scenario 2: Query with Multiple Entities and Comma**
 
 
 
307
 
 
308
  - **User Query**: "What is the latest news about Prospect Capital, did the rating change?"
309
  - **Rephrased Query**: "What is the latest news about \"Prospect Capital\", did the rating change after: {current_year}"
310
 
311
- **Scenario 3: Query Without Recognizable Entities**
312
-
313
  - **User Query**: "How does photosynthesis work?"
314
  - **Rephrased Query**: "How does photosynthesis work? after: {current_year}"
315
-
316
  """
317
-
318
  user_prompt = f"""
319
  Conversation context:
320
  {chat_history}
321
-
322
  New query: {query}
323
-
324
  Rephrased query:
325
  """
326
-
327
  messages = [
328
  {"role": "system", "content": system_prompt},
329
  {"role": "user", "content": user_prompt}
330
  ]
331
-
332
  try:
333
  logger.info(f"Sending rephrasing request to LLM with temperature {temperature}")
334
  response = client.chat_completion(
@@ -338,12 +339,10 @@ Rephrased query:
338
  )
339
  logger.info("Received rephrased query from LLM")
340
  rephrased_question = response.choices[0].message.content.strip()
341
-
342
  # Remove surrounding quotes if present
343
  if (rephrased_question.startswith('"') and rephrased_question.endswith('"')) or \
344
  (rephrased_question.startswith("'") and rephrased_question.endswith("'")):
345
  rephrased_question = rephrased_question[1:-1].strip()
346
-
347
  logger.info(f"Rephrased Query (cleaned): {rephrased_question}")
348
  return rephrased_question
349
  except Exception as e:
@@ -533,7 +532,7 @@ Your response should be detailed, informative, accurate, and directly relevant t
533
  response = groq_client.chat.completions.create(
534
  messages=messages,
535
  model="llama-3.1-8b-instant",
536
- max_tokens=4192,
537
  temperature=temperature,
538
  top_p=0.9,
539
  frequency_penalty=1.2,
 
278
  system_prompt = f"""
279
  You are a highly intelligent and context-aware conversational assistant. Your tasks are as follows:
280
 
281
+ 1. Determine if the new query is a continuation of the previous conversation or an entirely new topic.
282
+
283
+ 2. For both continuations and new topics:
284
+ a. **Entity Identification and Quotation**:
285
+ - Analyze the user's query to identify the main entities (e.g., organizations, brands, products, locations).
286
+ - For each identified entity, enclose ONLY the entity itself in double quotes within the query.
287
+ - If no identifiable entities are found, proceed without adding quotes.
288
+ b. **Query Preservation**:
289
+ - Maintain the entire original query, including any parts after commas or other punctuation.
290
+ - Do not remove or truncate any part of the original query.
291
+
292
+ 3. If it's a continuation:
293
+ - Incorporate relevant information from the context to make the query more specific and contextual.
294
+ - Ensure that entities from the previous context are properly quoted if they appear in the rephrased query.
295
+
296
+ 4. For both continuations and new topics:
297
+ - Append "after: {current_year}" to the end of the rephrased query.
298
+ - Ensure there is a space before "after:" for proper formatting.
299
+ - Do not use quotes or the "+" operator when adding the year.
300
+
301
+ 5. **Output**:
302
+ - Return ONLY the rephrased query, ensuring it is concise, clear, and contextually accurate.
303
+ - Do not include any additional commentary or explanation.
304
 
305
  ### Example Scenarios
306
+ **Scenario 1: New Topic**
 
 
307
  - **User Query**: "What is the latest news on Golomt Bank?"
308
  - **Rephrased Query**: "What is the latest news on \"Golomt Bank\" after: {current_year}"
309
 
310
+ **Scenario 2: Continuation**
311
+ - **Previous Query**: "What is the latest news on Golomt Bank?"
312
+ - **User Query**: "How did the Bank perform in Q2 2024?"
313
+ - **Rephrased Query**: "How did \"Golomt Bank\" perform in Q2 2024 after: {current_year}"
314
 
315
+ **Scenario 3: Query with Multiple Entities and Comma**
316
  - **User Query**: "What is the latest news about Prospect Capital, did the rating change?"
317
  - **Rephrased Query**: "What is the latest news about \"Prospect Capital\", did the rating change after: {current_year}"
318
 
319
+ **Scenario 4: Query Without Recognizable Entities**
 
320
  - **User Query**: "How does photosynthesis work?"
321
  - **Rephrased Query**: "How does photosynthesis work? after: {current_year}"
 
322
  """
 
323
  user_prompt = f"""
324
  Conversation context:
325
  {chat_history}
 
326
  New query: {query}
 
327
  Rephrased query:
328
  """
 
329
  messages = [
330
  {"role": "system", "content": system_prompt},
331
  {"role": "user", "content": user_prompt}
332
  ]
 
333
  try:
334
  logger.info(f"Sending rephrasing request to LLM with temperature {temperature}")
335
  response = client.chat_completion(
 
339
  )
340
  logger.info("Received rephrased query from LLM")
341
  rephrased_question = response.choices[0].message.content.strip()
 
342
  # Remove surrounding quotes if present
343
  if (rephrased_question.startswith('"') and rephrased_question.endswith('"')) or \
344
  (rephrased_question.startswith("'") and rephrased_question.endswith("'")):
345
  rephrased_question = rephrased_question[1:-1].strip()
 
346
  logger.info(f"Rephrased Query (cleaned): {rephrased_question}")
347
  return rephrased_question
348
  except Exception as e:
 
532
  response = groq_client.chat.completions.create(
533
  messages=messages,
534
  model="llama-3.1-8b-instant",
535
+ max_tokens=5500,
536
  temperature=temperature,
537
  top_p=0.9,
538
  frequency_penalty=1.2,