adriansanz commited on
Commit
5cb0f8d
·
verified ·
1 Parent(s): 3fd0e63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -32
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
- print("BasicAgent con GeminiLLM inicializado.")
316
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  def __call__(self, question: str) -> str:
318
- print(f"Pregunta recibida: {question[:50]}...")
319
-
320
- max_retries = 5
321
- backoff = 2 # segundos
322
-
323
- for attempt in range(max_retries):
324
- try:
325
- respuesta = self.chain.run({"question": question})
326
- print(f"Respuesta: {respuesta}")
327
- return respuesta
328
- except Exception as e:
329
- error_str = str(e).lower()
330
- print(f"Error en intento {attempt + 1}: {e}")
331
-
332
- # Manejar error 503 específico
333
- if "503" in error_str or "model is overloaded" in error_str:
334
- if attempt < max_retries - 1:
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):