Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,40 +25,51 @@ class Agent1:
|
|
| 25 |
def __init__(self, model):
|
| 26 |
self.model = model
|
| 27 |
|
|
|
|
| 28 |
def rephrase_and_split(self, user_input: str) -> List[str]:
|
| 29 |
rephrase_prompt = PromptTemplate(
|
| 30 |
input_variables=["query"],
|
| 31 |
template="""
|
| 32 |
Rephrase the given query into one or more concise, search-engine-friendly formats.
|
| 33 |
If the query contains multiple distinct questions, split them.
|
| 34 |
-
Provide ONLY the rephrased queries
|
| 35 |
-
|
| 36 |
Query: {query}
|
| 37 |
-
|
| 38 |
Rephrased queries:"""
|
| 39 |
)
|
| 40 |
-
|
| 41 |
chain = LLMChain(llm=self.model, prompt=rephrase_prompt)
|
| 42 |
response = chain.run(query=user_input).strip()
|
| 43 |
-
|
| 44 |
# Split the response at "Rephrased queries:" and take the second part
|
| 45 |
split_response = response.split("Rephrased queries:", 1)
|
| 46 |
if len(split_response) > 1:
|
| 47 |
response = split_response[1].strip()
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
rephrased_queries = [q.strip() for q in response.split('\n') if q.strip()
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def process(self, user_input: str) -> Dict[str, List[Dict[str, str]]]:
|
| 56 |
queries = self.rephrase_and_split(user_input)
|
|
|
|
| 57 |
results = {}
|
| 58 |
for query in queries:
|
| 59 |
results[query] = google_search(query)
|
| 60 |
return results
|
| 61 |
-
|
| 62 |
def load_document(file: NamedTemporaryFile) -> List[Document]:
|
| 63 |
"""Loads and splits the document into pages."""
|
| 64 |
loader = PyPDFLoader(file.name)
|
|
|
|
| 25 |
def __init__(self, model):
|
| 26 |
self.model = model
|
| 27 |
|
| 28 |
+
|
| 29 |
def rephrase_and_split(self, user_input: str) -> List[str]:
|
| 30 |
rephrase_prompt = PromptTemplate(
|
| 31 |
input_variables=["query"],
|
| 32 |
template="""
|
| 33 |
Rephrase the given query into one or more concise, search-engine-friendly formats.
|
| 34 |
If the query contains multiple distinct questions, split them.
|
| 35 |
+
Provide ONLY the rephrased queries, one per line. Do not include any explanations or additional text.
|
| 36 |
+
|
| 37 |
Query: {query}
|
| 38 |
+
|
| 39 |
Rephrased queries:"""
|
| 40 |
)
|
| 41 |
+
|
| 42 |
chain = LLMChain(llm=self.model, prompt=rephrase_prompt)
|
| 43 |
response = chain.run(query=user_input).strip()
|
| 44 |
+
|
| 45 |
# Split the response at "Rephrased queries:" and take the second part
|
| 46 |
split_response = response.split("Rephrased queries:", 1)
|
| 47 |
if len(split_response) > 1:
|
| 48 |
response = split_response[1].strip()
|
| 49 |
+
|
| 50 |
+
# Split the response into individual queries
|
| 51 |
+
rephrased_queries = [q.strip() for q in response.split('\n') if q.strip()]
|
| 52 |
+
|
| 53 |
+
# Filter out any queries that seem to be instructions or explanations
|
| 54 |
+
rephrased_queries = [q for q in rephrased_queries if not q.lower().startswith(("rephrase", "query", "provide"))]
|
| 55 |
+
|
| 56 |
+
# If no valid rephrased queries, return the original input split into two if it contains 'and'
|
| 57 |
+
if not rephrased_queries:
|
| 58 |
+
if ' and ' in user_input.lower():
|
| 59 |
+
return user_input.split(' and ')
|
| 60 |
+
else:
|
| 61 |
+
return [user_input]
|
| 62 |
+
|
| 63 |
+
return rephrased_queries
|
| 64 |
|
| 65 |
def process(self, user_input: str) -> Dict[str, List[Dict[str, str]]]:
|
| 66 |
queries = self.rephrase_and_split(user_input)
|
| 67 |
+
print("Rephrased queries:", queries) # Add this line
|
| 68 |
results = {}
|
| 69 |
for query in queries:
|
| 70 |
results[query] = google_search(query)
|
| 71 |
return results
|
| 72 |
+
|
| 73 |
def load_document(file: NamedTemporaryFile) -> List[Document]:
|
| 74 |
"""Loads and splits the document into pages."""
|
| 75 |
loader = PyPDFLoader(file.name)
|