Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,56 +1,33 @@
|
|
1 |
-
import
|
2 |
-
from langchain.memory import ConversationBufferMemory
|
3 |
-
from langchain.agents import AgentExecutor, Tool
|
4 |
-
from langchain.chat_models import ChatGoogleGenerativeAI
|
5 |
-
import genai
|
6 |
-
from gemini import GeminiAgent
|
7 |
-
from typing import Optional
|
8 |
from duckduckgo_search import DDGS
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
return ChatGoogleGenerativeAI(model_name=model_name)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
self.agent = GeminiAgent(
|
30 |
-
tools=self.tools,
|
31 |
-
llm=self.llm,
|
32 |
-
memory=self.memory,
|
33 |
-
)
|
34 |
-
|
35 |
-
def _web_search(self, query: str, domain: Optional[str] = None) -> str:
|
36 |
try:
|
37 |
results = DDGS().text(query, max_results=3)
|
38 |
return "\n".join([r["body"] for r in results])
|
39 |
except Exception as e:
|
40 |
return f"Error en la búsqueda: {str(e)}"
|
41 |
-
|
42 |
-
def run(self, query: str) -> str:
|
43 |
-
"""Procesa las consultas del usuario con reintentos y manejo de errores."""
|
44 |
-
max_retries = 3
|
45 |
-
base_sleep = 1
|
46 |
-
for attempt in range(max_retries):
|
47 |
-
try:
|
48 |
-
response = self.agent.run(query)
|
49 |
-
return response
|
50 |
-
except Exception as e:
|
51 |
-
sleep_time = base_sleep * (attempt + 1)
|
52 |
-
if attempt < max_retries - 1:
|
53 |
-
print(f"Intento {attempt + 1} fallido. Reintentando en {sleep_time} segundos...")
|
54 |
-
time.sleep(sleep_time)
|
55 |
-
continue
|
56 |
-
return f"Error procesando la consulta: {str(e)}"
|
|
|
1 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from duckduckgo_search import DDGS
|
3 |
|
4 |
+
# --- Clase de Agente ---
|
5 |
+
class BasicAgent:
|
6 |
+
def __init__(self):
|
7 |
+
print("BasicAgent initialized.")
|
|
|
8 |
|
9 |
+
def __call__(self, question: str) -> str:
|
10 |
+
"""
|
11 |
+
Este agente ahora realizará una búsqueda en DuckDuckGo
|
12 |
+
si detecta que la consulta parece una pregunta sobre búsqueda web.
|
13 |
+
"""
|
14 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
15 |
+
|
16 |
+
# Si la pregunta contiene "buscar", realizamos una búsqueda en DuckDuckGo
|
17 |
+
if "buscar" in question.lower():
|
18 |
+
fixed_answer = self.web_search(question)
|
19 |
+
else:
|
20 |
+
fixed_answer = "This is a default answer."
|
21 |
+
|
22 |
+
print(f"Agent returning answer: {fixed_answer}")
|
23 |
+
return fixed_answer
|
24 |
|
25 |
+
def web_search(self, query: str) -> str:
|
26 |
+
"""
|
27 |
+
Realiza una búsqueda en DuckDuckGo y devuelve los primeros resultados.
|
28 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
try:
|
30 |
results = DDGS().text(query, max_results=3)
|
31 |
return "\n".join([r["body"] for r in results])
|
32 |
except Exception as e:
|
33 |
return f"Error en la búsqueda: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|