Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,42 +1,57 @@
|
|
1 |
-
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
self.
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
|
20 |
-
def
|
21 |
-
|
22 |
-
try:
|
23 |
-
wiki_response = self.wikipedia_tool.search(question) # Usa 'search' en lugar de 'run'
|
24 |
-
if wiki_response:
|
25 |
-
return wiki_response
|
26 |
-
except Exception as e:
|
27 |
-
print(f"Error buscando en Wikipedia: {e}")
|
28 |
-
|
29 |
-
# Si Wikipedia no tiene resultados, intentar con DuckDuckGo
|
30 |
try:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
except Exception as e:
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
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 smolagent_tools import WikipediaSearchTool, WebSearchTool, SmolagentToolWrapper
|
8 |
|
9 |
+
# Función para configuración de llm
|
10 |
+
def setup_llm(api_key: str, model_name: str = "gemini-2.0-flash"):
|
11 |
+
"""Configuración del modelo de lenguaje con Gemini."""
|
12 |
+
genai.configure(api_key=api_key)
|
13 |
+
return ChatGoogleGenerativeAI(model_name=model_name)
|
14 |
|
15 |
+
# Herramientas adicionales
|
16 |
+
class EnhancedAgent:
|
17 |
+
def __init__(self, api_key: str, model_name: str = "gemini-2.0-flash"):
|
18 |
+
self.api_key = api_key
|
19 |
+
self.model_name = model_name
|
20 |
+
self.llm = setup_llm(api_key, model_name)
|
21 |
+
self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
22 |
+
|
23 |
+
self.tools = [
|
24 |
+
SmolagentToolWrapper(WikipediaSearchTool()),
|
25 |
+
Tool(name="web_search", func=self._web_search, description="Realizar búsqueda web avanzada")
|
26 |
+
]
|
27 |
+
|
28 |
+
self.agent = GeminiAgent(
|
29 |
+
tools=self.tools,
|
30 |
+
llm=self.llm,
|
31 |
+
memory=self.memory,
|
32 |
)
|
33 |
|
34 |
+
def _web_search(self, query: str, domain: Optional[str] = None) -> str:
|
35 |
+
"""Realiza una búsqueda en la web usando DuckDuckGo con limitación de tasa y reintentos."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
+
search_tool = WebSearchTool()
|
38 |
+
results = search_tool.search(query, domain)
|
39 |
+
return results
|
40 |
except Exception as e:
|
41 |
+
return f"Error al realizar la búsqueda: {str(e)}"
|
42 |
|
43 |
+
def run(self, query: str) -> str:
|
44 |
+
"""Procesa las consultas del usuario con reintentos y manejo de errores."""
|
45 |
+
max_retries = 3
|
46 |
+
base_sleep = 1
|
47 |
+
for attempt in range(max_retries):
|
48 |
+
try:
|
49 |
+
response = self.agent.run(query)
|
50 |
+
return response
|
51 |
+
except Exception as e:
|
52 |
+
sleep_time = base_sleep * (attempt + 1)
|
53 |
+
if attempt < max_retries - 1:
|
54 |
+
print(f"Intento {attempt + 1} fallido. Reintentando en {sleep_time} segundos...")
|
55 |
+
time.sleep(sleep_time)
|
56 |
+
continue
|
57 |
+
return f"Error procesando la consulta: {str(e)}"
|