Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -312,40 +312,54 @@ class BasicAgent:
|
|
| 312 |
def __init__(self):
|
| 313 |
self.llm = GeminiLLM()
|
| 314 |
self.chain = LLMChain(prompt=PromptTemplate.from_template("{question}"), llm=self.llm)
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
def __call__(self, question: str) -> str:
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
print(f"Modelo saturado, reintentando en {backoff} segundos...")
|
| 336 |
-
time.sleep(backoff)
|
| 337 |
-
backoff *= 2
|
| 338 |
-
continue
|
| 339 |
-
else:
|
| 340 |
-
return "El modelo está saturado. Por favor, inténtalo más tarde."
|
| 341 |
-
# Otros errores que pueden ser manejados
|
| 342 |
-
elif "quota" in error_str:
|
| 343 |
-
return "Se ha superado la cuota de la API. Intenta nuevamente más tarde."
|
| 344 |
-
elif "timeout" in error_str:
|
| 345 |
-
return "La solicitud tardó demasiado. Intenta nuevamente."
|
| 346 |
-
else:
|
| 347 |
-
return "Error procesando la pregunta."
|
| 348 |
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
|
| 351 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 312 |
def __init__(self):
|
| 313 |
self.llm = GeminiLLM()
|
| 314 |
self.chain = LLMChain(prompt=PromptTemplate.from_template("{question}"), llm=self.llm)
|
| 315 |
+
|
| 316 |
+
def call_tool(self, tool_name: str, *args, **kwargs):
|
| 317 |
+
# Simple mapping from tool name to function
|
| 318 |
+
tools = {
|
| 319 |
+
"wiki_search": wiki_search,
|
| 320 |
+
"load_file": load_file,
|
| 321 |
+
"web_search": web_search,
|
| 322 |
+
"arxiv_search": arxiv_search,
|
| 323 |
+
"multiply": multiply,
|
| 324 |
+
"add": add,
|
| 325 |
+
"subtract": subtract,
|
| 326 |
+
"divide": divide,
|
| 327 |
+
"modulus": modulus,
|
| 328 |
+
"power": power,
|
| 329 |
+
"square_root": square_root,
|
| 330 |
+
"extract_numbers": extract_numbers,
|
| 331 |
+
"extract_keywords": extract_keywords,
|
| 332 |
+
"extract_names": extract_names,
|
| 333 |
+
"find_non_commutative_pairs": find_non_commutative_pairs,
|
| 334 |
+
}
|
| 335 |
+
func = tools.get(tool_name)
|
| 336 |
+
if func:
|
| 337 |
+
return func(*args, **kwargs)
|
| 338 |
+
else:
|
| 339 |
+
return f"Tool '{tool_name}' not found."
|
| 340 |
+
|
| 341 |
def __call__(self, question: str) -> str:
|
| 342 |
+
question = question.lower()
|
| 343 |
+
|
| 344 |
+
# Very simple logic to decide which tool to call based on question keywords
|
| 345 |
+
if "wikipedia" in question or "who is" in question or "what is" in question:
|
| 346 |
+
# Extract query for Wikipedia (here just remove the word "wikipedia")
|
| 347 |
+
query = question.replace("wikipedia", "").strip()
|
| 348 |
+
docs = self.call_tool("wiki_search", query=query, load_max_docs=2)
|
| 349 |
+
# Return titles of found documents
|
| 350 |
+
return "\n".join([doc.metadata.get("title", "No title") for doc in docs])
|
| 351 |
+
|
| 352 |
+
elif "add" in question or "plus" in question:
|
| 353 |
+
# Extract numbers and add (simple demo)
|
| 354 |
+
nums = self.call_tool("extract_numbers", question)
|
| 355 |
+
if len(nums) >= 2:
|
| 356 |
+
return str(self.call_tool("add", nums[0], nums[1]))
|
| 357 |
+
else:
|
| 358 |
+
return "I couldn’t find enough numbers to add."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
+
else:
|
| 361 |
+
# If no tool matched, fallback to the LLM
|
| 362 |
+
return self.chain.run({"question": question})
|
| 363 |
|
| 364 |
|
| 365 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|