Shreyas094 commited on
Commit
24a7323
·
verified ·
1 Parent(s): 64581a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -40
app.py CHANGED
@@ -39,55 +39,43 @@ def duckduckgo_search(query):
39
  logging.error(f"Error during DuckDuckGo search: {str(e)}")
40
  return []
41
 
42
- async def rephrase_query(query, history, model):
43
- logging.info("Rephrasing query based on history and model.")
44
- system_message = """You are an AI assistant tasked with analyzing and rephrasing user queries.
45
- Your goal is to determine whether the query is unique or related to the previous conversation, and then rephrase it appropriately for web search.
46
- The rephrased query should be succinct and optimized for search engines.
47
-
48
- If the query is unique (i.e., unrelated to the conversation), rephrase it to make it more specific and searchable.
49
- If the query is related to the previous conversation, incorporate relevant context from the conversation to enhance search relevance.
50
-
51
- Provide your analysis in the following format:
52
- <analysis>Your reasoning about whether the query is unique or related</analysis>
53
- <rephrased_query>The rephrased query</rephrased_query>"""
54
-
55
- user_message = f"""Current query: {query}
56
-
57
- Previous conversation history:
58
- {history}
59
-
60
- Analyze the query and provide a rephrased version suitable for web search."""
61
 
62
  client = InferenceClient(model, token=huggingface_token)
63
-
64
  try:
65
- logging.info(f"Sending rephrase request to model {model}.")
66
  response = await asyncio.to_thread(
67
  client.text_generation,
68
- prompt=f"{system_message}\n\n{user_message}",
69
- max_new_tokens=150,
70
  temperature=0.2,
71
  )
72
- logging.info("Rephrase response received.")
73
-
74
- # Extract the rephrased query from the response
75
- analysis_start = response.find("<analysis>")
76
- analysis_end = response.find("</analysis>")
77
- rephrased_start = response.find("<rephrased_query>")
78
- rephrased_end = response.find("</rephrased_query>")
79
-
80
- if analysis_start != -1 and analysis_end != -1 and rephrased_start != -1 and rephrased_end != -1:
81
- analysis = response[analysis_start + 10:analysis_end].strip()
82
- rephrased_query = response[rephrased_start + 17:rephrased_end].strip()
83
- logging.info(f"Rephrased query: {rephrased_query}")
84
- return analysis, rephrased_query
85
- else:
86
- logging.error("Failed to parse the rephrase response.")
87
- return None, query
88
  except Exception as e:
89
  logging.error(f"Error in rephrase_query: {str(e)}")
90
- return None, query
91
 
92
  def create_web_search_vectors(search_results):
93
  logging.info(f"Creating web search vectors from {len(search_results)} search results.")
 
39
  logging.error(f"Error during DuckDuckGo search: {str(e)}")
40
  return []
41
 
42
+ async def rephrase_query(query, context, model):
43
+ # Log the original query for debugging
44
+ logging.info(f"Original query: {query}")
45
+
46
+ prompt = f"""You are a highly intelligent conversational chatbot. Your task is to analyze the given context and new query, then decide whether to rephrase the query with or without incorporating the context. Follow these steps:
47
+ 1. Determine if the new query is a continuation of the previous conversation or an entirely new topic.
48
+ 2. If it's a continuation, rephrase the query by incorporating relevant information from the context to make it more specific and contextual.
49
+ 3. If it's a new topic, rephrase the query to make it more appropriate for a web search, focusing on clarity and accuracy without using the previous context.
50
+ 4. Provide ONLY the rephrased query without any additional explanation or reasoning.
51
+
52
+ Context: {context}
53
+
54
+ New query: {query}
55
+
56
+ Rephrased query:"""
 
 
 
 
57
 
58
  client = InferenceClient(model, token=huggingface_token)
59
+
60
  try:
 
61
  response = await asyncio.to_thread(
62
  client.text_generation,
63
+ prompt=prompt,
64
+ max_new_tokens=100,
65
  temperature=0.2,
66
  )
67
+
68
+ # The response should be the rephrased query as per your prompt
69
+ rephrased_query = response.strip()
70
+
71
+ # Log the rephrased query
72
+ logging.info(f"Rephrased query: {rephrased_query}")
73
+
74
+ return rephrased_query
75
+
 
 
 
 
 
 
 
76
  except Exception as e:
77
  logging.error(f"Error in rephrase_query: {str(e)}")
78
+ return query # Fallback to the original query if there's an error
79
 
80
  def create_web_search_vectors(search_results):
81
  logging.info(f"Creating web search vectors from {len(search_results)} search results.")