Spaces:
Runtime error
Runtime error
cccc
Browse files- my_tools.py +12 -48
my_tools.py
CHANGED
|
@@ -21,7 +21,18 @@ from langchain_community.retrievers import TavilySearchAPIRetriever
|
|
| 21 |
from llama_index.core.schema import Document
|
| 22 |
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# -------------------------------------------------------------------
|
| 26 |
# 1) MONKEY-PATCH PARA ChatMessage (por requerimiento de LlamaIndex)
|
| 27 |
# -------------------------------------------------------------------
|
|
@@ -398,53 +409,6 @@ tool_descriptions = "\n".join([
|
|
| 398 |
# -------------------------------------------------------------------
|
| 399 |
# 6) PROMPT DE SISTEMA MEJORADO with few-shot examples
|
| 400 |
# -------------------------------------------------------------------
|
| 401 |
-
system_prompt_deprecated = f"""
|
| 402 |
-
Eres Alfred, un agente ReAct eficiente y preciso. Tu objetivo es responder correctamente usando las herramientas disponibles.
|
| 403 |
-
A continuación tienes ejemplos de cómo usar cada herramienta:
|
| 404 |
-
|
| 405 |
-
### EJEMPLO 1: Clasificación botánica
|
| 406 |
-
Usuario: "I have a grocery list: milk, eggs, broccoli, celery, lettuce. Please give me only the vegetables, alphabetized, comma-separated."
|
| 407 |
-
Agente (pensando): "La pregunta menciona 'grocery list' y 'vegetables' -> usar classify_botanical_foods"
|
| 408 |
-
Agente (acción):
|
| 409 |
-
{{"tool": "classify_botanical_foods", "input": "milk, eggs, broccoli, celery, lettuce"}}
|
| 410 |
-
Observación: "Verduras: broccoli, celery, lettuce\nFrutas: \nOtros: eggs, milk"
|
| 411 |
-
Agente (respuesta final): "Verduras: broccoli, celery, lettuce"
|
| 412 |
-
|
| 413 |
-
### EJEMPLO 2: Excel ventas de comida
|
| 414 |
-
Usuario: "Attached is an Excel file with sales data. What were the total sales from food only?"
|
| 415 |
-
Agente (pensando): "Menciona 'Excel' y 'food' -> usar read_excel_data"
|
| 416 |
-
Agente (acción):
|
| 417 |
-
{{"tool": "read_excel_data", "input": "data/attached.xlsx"}}
|
| 418 |
-
Observación (CSV): "item,type,sales\nBurger,food,1000\nSoda,drink,200\nPizza,food,1500"
|
| 419 |
-
Agente (respuesta final): "Total food sales: $2500.00"
|
| 420 |
-
|
| 421 |
-
### EJEMPLO 3: Roster de jugadores
|
| 422 |
-
Usuario: "Who are the pitchers with the numbers before and after Taishō Tamai as of July 2023? Use last names only, Roman characters."
|
| 423 |
-
Agente (pensando): "La pregunta menciona 'Taishō Tamai' y 'pitchers' -> usar scrape_wiki_table o web_search"
|
| 424 |
-
Agente (acción):
|
| 425 |
-
{{"tool": "scrape_wiki_table", "input": ["Hokkaido Nippon-Ham Fighters roster", "Players", 0]}}
|
| 426 |
-
Observación (CSV): "Number,Name,Position\n... ,Tamai Taishō,Pitcher\n45,Uehara,Pitcher\n47,Kakui,Pitcher, ..."
|
| 427 |
-
Agente (procesar): "Tamai tiene número 46. El pitcher 45 es Uehara, el 47 es Kakui."
|
| 428 |
-
Agente (respuesta final): "Uehara, Kakui"
|
| 429 |
-
|
| 430 |
-
### FLUJO GENERAL:
|
| 431 |
-
1. LEE la pregunta y detecta palabras clave:
|
| 432 |
-
- "grocery list", "vegetables" -> classify_botanical_foods
|
| 433 |
-
- "attached Excel", "sales" -> read_excel_data
|
| 434 |
-
- "roster", "pitchers", "number" -> scrape_wiki_table o web_search
|
| 435 |
-
- "Malko Competition" -> scrape_wiki_table
|
| 436 |
-
- "tabla Markdown" -> analyze_markdown_table
|
| 437 |
-
- "invertir texto" -> reverse_text
|
| 438 |
-
- "ejecutar código" -> execute_code
|
| 439 |
-
2. PRODUCE el "TOOL CALL" en formato JSON con el nombre exacto de la herramienta.
|
| 440 |
-
3. EJECUTA la herramienta y recibe la salida.
|
| 441 |
-
4. PROCESA la salida (filter, sumar, ordenar) en Python si es necesario.
|
| 442 |
-
5. RESPONDE con el formato exacto que SAIA espera (solo la parte solicitada, sin texto extra).
|
| 443 |
-
|
| 444 |
-
Herramientas disponibles (USAR EXÁCTAMENTE estos nombres):
|
| 445 |
-
{tool_descriptions}
|
| 446 |
-
"""
|
| 447 |
-
|
| 448 |
system_prompt = f"""
|
| 449 |
You are Alfred, a ReAct agent. Your goal is to answer correctly using the available tools.
|
| 450 |
|
|
|
|
| 21 |
from llama_index.core.schema import Document
|
| 22 |
|
| 23 |
|
| 24 |
+
def check_required_keys():
|
| 25 |
+
missing = []
|
| 26 |
+
if not os.getenv("TAVILY_API_KEY"):
|
| 27 |
+
missing.append("TAVILY_API_KEY")
|
| 28 |
+
# Podés agregar más claves si querés chequear otras
|
| 29 |
+
if missing:
|
| 30 |
+
print(f"⚠️ WARNING: Missing API keys: {', '.join(missing)}. Agent will not function properly!")
|
| 31 |
+
else:
|
| 32 |
+
print("✅ All required API keys are present.")
|
| 33 |
+
|
| 34 |
+
# Lo llamás apenas arranca:
|
| 35 |
+
check_required_keys()
|
| 36 |
# -------------------------------------------------------------------
|
| 37 |
# 1) MONKEY-PATCH PARA ChatMessage (por requerimiento de LlamaIndex)
|
| 38 |
# -------------------------------------------------------------------
|
|
|
|
| 409 |
# -------------------------------------------------------------------
|
| 410 |
# 6) PROMPT DE SISTEMA MEJORADO with few-shot examples
|
| 411 |
# -------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
system_prompt = f"""
|
| 413 |
You are Alfred, a ReAct agent. Your goal is to answer correctly using the available tools.
|
| 414 |
|