Spaces:
Sleeping
Sleeping
Fix wikipedia tool, max_steps y bug de tipo int
Browse files- app.py +35 -2
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -15,7 +15,38 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 15 |
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
-
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
SYSTEM_PROMPT = """Eres un asistente experto que responde preguntas de forma precisa y concisa.
|
| 21 |
Razona paso a paso si hace falta, pero SIEMPRE termina tu respuesta con una unica linea con este formato exacto:
|
|
@@ -33,9 +64,10 @@ class BasicAgent:
|
|
| 33 |
print("BasicAgent initialized.")
|
| 34 |
model = InferenceClientModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 35 |
self.agent = CodeAgent(
|
| 36 |
-
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 37 |
model=model,
|
| 38 |
add_base_tools=True,
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
def __call__(self, question: str) -> str:
|
|
@@ -43,6 +75,7 @@ class BasicAgent:
|
|
| 43 |
try:
|
| 44 |
full_prompt = f"{SYSTEM_PROMPT}\n\nPregunta: {question}"
|
| 45 |
raw_answer = self.agent.run(full_prompt)
|
|
|
|
| 46 |
if "ANSWER:" in raw_answer:
|
| 47 |
final_answer = raw_answer.split("ANSWER:")[-1].strip()
|
| 48 |
else:
|
|
|
|
| 15 |
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
+
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool, Tool
|
| 19 |
+
import wikipedia
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class WikipediaSearchTool(Tool):
|
| 23 |
+
name = "wikipedia_search"
|
| 24 |
+
description = (
|
| 25 |
+
"Busca en Wikipedia y devuelve un resumen del articulo mas relevante "
|
| 26 |
+
"para la query dada. Utilo para preguntas sobre hechos, biografias, "
|
| 27 |
+
"discografias, fechas, etc."
|
| 28 |
+
)
|
| 29 |
+
inputs = {
|
| 30 |
+
"query": {
|
| 31 |
+
"type": "string",
|
| 32 |
+
"description": "El termino o pregunta a buscar en Wikipedia",
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
output_type = "string"
|
| 36 |
+
|
| 37 |
+
def forward(self, query: str) -> str:
|
| 38 |
+
try:
|
| 39 |
+
wikipedia.set_lang("en")
|
| 40 |
+
return wikipedia.summary(query, sentences=8, auto_suggest=True)
|
| 41 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 42 |
+
try:
|
| 43 |
+
return wikipedia.summary(e.options[0], sentences=8)
|
| 44 |
+
except Exception as e2:
|
| 45 |
+
return f"Error de desambiguacion: {e2}"
|
| 46 |
+
except wikipedia.exceptions.PageError:
|
| 47 |
+
return f"No se encontro pagina de Wikipedia para: {query}"
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error buscando en Wikipedia: {e}"
|
| 50 |
|
| 51 |
SYSTEM_PROMPT = """Eres un asistente experto que responde preguntas de forma precisa y concisa.
|
| 52 |
Razona paso a paso si hace falta, pero SIEMPRE termina tu respuesta con una unica linea con este formato exacto:
|
|
|
|
| 64 |
print("BasicAgent initialized.")
|
| 65 |
model = InferenceClientModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 66 |
self.agent = CodeAgent(
|
| 67 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
|
| 68 |
model=model,
|
| 69 |
add_base_tools=True,
|
| 70 |
+
max_steps=6,
|
| 71 |
)
|
| 72 |
|
| 73 |
def __call__(self, question: str) -> str:
|
|
|
|
| 75 |
try:
|
| 76 |
full_prompt = f"{SYSTEM_PROMPT}\n\nPregunta: {question}"
|
| 77 |
raw_answer = self.agent.run(full_prompt)
|
| 78 |
+
raw_answer = str(raw_answer) # el agente puede devolver int, float, etc.
|
| 79 |
if "ANSWER:" in raw_answer:
|
| 80 |
final_answer = raw_answer.split("ANSWER:")[-1].strip()
|
| 81 |
else:
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ requests
|
|
| 3 |
pandas
|
| 4 |
smolagents
|
| 5 |
ddgs
|
| 6 |
-
markdownify
|
|
|
|
|
|
| 3 |
pandas
|
| 4 |
smolagents
|
| 5 |
ddgs
|
| 6 |
+
markdownify
|
| 7 |
+
wikipedia
|