Spaces:
Runtime error
Runtime error
tttt
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598import osimport mathimport timeimport asyncioimport subprocessimport requestsimport pandas as pdfrom io import BytesIO, StringIOfrom bs4 import BeautifulSoupimport wikipediafrom pydantic import Fieldimport google.generativeai as genai# LlamaIndex importsfrom llama_index.core.llms import ChatMessage, LLMMetadata, LLM, CompletionResponsefrom llama_index.core.tools import FunctionToolfrom llama_index.core.agent import ReActAgentfrom llama_index.core.callbacks.llama_debug import LlamaDebugHandlerfrom langchain_community.retrievers import TavilySearchAPIRetrieverfrom llama_index.core.schema import Documentdef check_required_keys():missing = []if not os.getenv("TAVILY_API_KEY"):missing.append("TAVILY_API_KEY")# Podés agregar más claves si querés chequear otrasif missing:print(f"⚠️ WARNING: Missing API keys: {', '.join(missing)}. Agent will not function properly!")else:print("✅ All required API keys are present.")# Lo llamás apenas arranca:check_required_keys()# -------------------------------------------------------------------# 1) MONKEY-PATCH PARA ChatMessage (por requerimiento de LlamaIndex)# -------------------------------------------------------------------ChatMessage.message = property(lambda self: self)# -------------------------------------------------------------------# 2) Clase GeminiLLM personalizada# -------------------------------------------------------------------class GeminiLLM(LLM):model_name: str = Field(default="models/gemini-1.5-flash-latest")temperature: float = Field(default=0.0)_model: object = None_gen_cfg: object = Noneclass Config:extra = "allow"def __init__(self, **kwargs):super().__init__(**kwargs)actual_model_name = self.model_nameif not isinstance(actual_model_name, str):field_def = self.__fields__.get("model_name")if field_def and hasattr(field_def, 'default'):actual_model_name = field_def.defaultif not isinstance(actual_model_name, str):actual_model_name = "models/gemini-1.5-flash-latest"actual_temperature = self.temperatureif not isinstance(actual_temperature, (float, int)):temp_field_def = self.__fields__.get("temperature")if temp_field_def and hasattr(temp_field_def, 'default'):actual_temperature = temp_field_def.defaultif not isinstance(actual_temperature, (float, int)):actual_temperature = 0.0key = os.getenv("GEMINI_API_KEY")if not key:raise ValueError("GEMINI_API_KEY no configurada en variables de entorno")genai.configure(api_key=key)self._gen_cfg = genai.types.GenerationConfig(temperature=actual_temperature)self._model = genai.GenerativeModel(model_name=actual_model_name,generation_config=self._gen_cfg)if self.callback_manager is None:from llama_index.core.callbacks.base import CallbackManagerself.callback_manager = CallbackManager([])if not self.callback_manager.handlers:self.callback_manager.add_handler(LlamaDebugHandler())@propertydef metadata(self):actual_model_name_meta = self.model_nameif not isinstance(actual_model_name_meta, str):field_meta = self.__fields__.get("model_name")if field_meta and hasattr(field_meta, 'default'):actual_model_name_meta = field_meta.defaultif not isinstance(actual_model_name_meta, str):actual_model_name_meta = "models/gemini-1.5-flash-latest"return LLMMetadata(context_window=1048576,num_output=8192,is_chat_model=True,is_function_calling_model=True,model_name=actual_model_name_meta,)def chat(self, messages: list[ChatMessage], **kwargs):hist = []for m in messages[:-1]:role = "user" if m.role == "user" else "model"hist.append({"role": role, "parts": [{"text": str(m.content)}]})last = str(messages[-1].content)session = self._model.start_chat(history=hist)try:resp = session.send_message(last)return ChatMessage(role="assistant", content=resp.text)except Exception as e:return ChatMessage(role="assistant", content=f"Error Gemini chat: {e}")async def achat(self, messages: list[ChatMessage], **kwargs):return await asyncio.to_thread(self.chat, messages, **kwargs)def stream_complete(self, prompt: str, formatted=False, **kwargs):stream = self._model.generate_content(str(prompt), stream=True)def gen():acc = ""for chunk in stream:delta = getattr(chunk, "text", "")if not delta and hasattr(chunk, 'parts') and chunk.parts:delta = chunk.parts[0].textif delta:acc += deltayield CompletionResponse(text=acc, delta=delta)return gen()async def astream_complete(self, prompt: str, formatted=False, **kwargs):sync_gen = await asyncio.to_thread(self.stream_complete, prompt, formatted=formatted, **kwargs)async def async_gen_wrapper():for item in sync_gen:yield itemreturn async_gen_wrapper()def stream_chat(self, messages: list[ChatMessage], **kwargs):hist = []for m in messages[:-1]:role = "user" if m.role == "user" else "model"hist.append({"role": role, "parts": [{"text": str(m.content)}]})last = str(messages[-1].content)session = self._model.start_chat(history=hist)stream = session.send_message(last, stream=True)def gen():acc = ""for chunk in stream:delta = getattr(chunk, "text", "")if not delta and hasattr(chunk, 'parts') and chunk.parts:delta = chunk.parts[0].textif delta:acc += deltayield ChatMessage(role="assistant", content=acc, additional_kwargs={"delta": delta})return gen()async def astream_chat(self, messages: list[ChatMessage], **kwargs):sync_gen = await asyncio.to_thread(self.stream_chat, messages, **kwargs)async def async_gen_wrapper():for item in sync_gen:yield itemreturn async_gen_wrapper()def complete(self, prompt: str, formatted=False, **kwargs):try:resp = self._model.generate_content(str(prompt))return CompletionResponse(text=resp.text)except Exception as e:return CompletionResponse(text=f"Error Gemini complete: {e}")async def acomplete(self, prompt: str, formatted=False, **kwargs):return await asyncio.to_thread(self.complete, prompt, formatted=formatted, **kwargs)# -------------------------------------------------------------------# 3) HERRAMIENTAS PERSONALIZADAS# -------------------------------------------------------------------HEADERS = {'User-Agent': 'Mozilla/5.0'}def buscar_web(query, num_results=8):retriever = TavilySearchAPIRetriever(api_key=os.getenv("TAVILY_API_KEY"), k=num_results)results = retriever.invoke(query)# Formatear resultados para pasarlos al LLMformatted_results = []for i, doc in enumerate(results):formatted_results.append(f"Result {i+1}:\nTitle: {doc.metadata.get('title','')}\nSource: {doc.metadata.get('source','')}\nContent: {doc.page_content}\n")return "\n\n".join(formatted_results)def reverse_text(text: str) -> str:"""Invierte el orden de los caracteres en 'text'."""return text[::-1]def analyze_table(table_md: str, question: str) -> str:"""Recibe una tabla en Markdown (con pipes y separadores) y, si la pregunta menciona 'conmut',verifica la conmutatividad de la matriz; en otro caso, devuelve el CSV equivalente."""try:#lines = [l for l in table_md.splitlines() if l.strip() and '---' not in l]# quitamos separadores y líneas vacías; aseguramos salto tras el encabezadolines = [l for l in table_md.strip().splitlines()if l.strip() and not l.lstrip().startswith('|---')]rows = [[c.strip() for c in l.strip().strip('|').split('|')] for l in lines]if len(rows) < 2:return "Tabla Markdown mal formateada o vacía."df = pd.DataFrame(rows[1:], columns=rows[0])if 'conmut' in question.lower():cols = df.columns.tolist()[1:]counter = set()for x in cols:for y in cols:try:if df.loc[df[rows[0][0]] == x, y].iat[0] != df.loc[df[rows[0][0]] == y, x].iat[0]:counter.update([x, y])except:continuereturn ', '.join(sorted(counter)) or 'Conmutativa'return df.to_csv(index=False)except Exception as e:return f"Error analyze_table: {e}"def execute_code(code: str) -> str:"""Primero intenta evaluar con eval() en un entorno protegido; si falla, invoca un subproceso 'python -c'."""try:allowed_globals = {'__builtins__': None, 'math': math}try:val = eval(code, allowed_globals, {})return str(val)except:res = subprocess.run(["python", "-S", "-c", code],capture_output=True, text=True, timeout=10)if res.returncode != 0:return f"Error código: {res.stderr.strip()}"return res.stdout.strip() or "(sin salida)"except subprocess.TimeoutExpired:return "Error ejecutar código: timeout"except Exception as e:return f"Error crítico: {e}"def read_excel_data(file_path: str, sheet_name=0) -> str:"""Si file_path empieza con 'http', descarga el contenido y lee con pandas.Si es una ruta local, lee directamente. Devuelve el CSV."""try:if file_path.startswith(('http://', 'https://')):resp = requests.get(file_path, headers=HEADERS, timeout=30)resp.raise_for_status()df = pd.read_excel(BytesIO(resp.content), sheet_name=sheet_name)else:if not os.path.exists(file_path):return f"Error read_excel_data: archivo '{file_path}' no encontrado"df = pd.read_excel(file_path, sheet_name=sheet_name)df = df.fillna('')return df.to_csv(index=False)except Exception as e:return f"Error read_excel_data: {e}"def classify_botanical_foods(items_list_str: str) -> str:"""Splits an input list of foods (English names) into botanical Vegetables,Fruits, and Others, and returns the three groups as comma-separated lists.Nothing that is a botanical fruit appears in the Vegetables list."""# --- botanical criteria -------------------------------------------------botanical_fruits = {"tomato", "bell pepper", "pepper", "green beans", "beans", "zucchini","cucumber", "eggplant", "corn", "peas", "pea", "pumpkin", "squash","avocado"}botanical_vegetables = {"broccoli", "celery", "lettuce", "kale", "spinach", "sweet potatoes","sweet potato", "potato", "onion", "garlic", "carrot", "okra","cabbage", "cauliflower", "beet", "turnip", "parsnip", "leek"}# -----------------------------------------------------------------------raw_items = [token.strip().lower() for token in items_list_str.split(",")]vegetables = []fruits = []others = []for item in raw_items:if item in botanical_vegetables and item not in botanical_fruits:vegetables.append(item)elif item in botanical_fruits:fruits.append(item)else:others.append(item)# De-duplicate and alphabetisevegetables = sorted(set(vegetables))fruits = sorted(set(fruits))others = sorted(set(others))return (f"Vegetables: {', '.join(vegetables)}\n"f"Fruits: {', '.join(fruits)}\n"f"Others: {', '.join(others)}")def scrape_wikipedia_table_deprecated(page_title: str, section: str, table_index: int = 0) -> str:"""Busca una sección en una página de Wikipedia y extrae la tabla indicada (por índice).Devuelve el CSV."""try:wikipedia.set_lang("en")page = wikipedia.page(page_title, auto_suggest=False)soup = BeautifulSoup(page.html(), 'html.parser')header = next((h for h in soup.find_all(['h2', 'h3']) if section.lower() in h.get_text(strip=True).lower()),None)if not header:return f"Sección '{section}' no encontrada en '{page_title}'"tables = []for sib in header.find_next_siblings():if sib.name in ['h2', 'h3']:breakif sib.name == 'table' and 'wikitable' in sib.get('class', []):tables.append(sib)if table_index >= len(tables):return f"Tabla índice {table_index} fuera de rango (solo {len(tables)} tablas)."df = pd.read_html(str(tables[table_index]))[0]return df.to_csv(index=False)except Exception as e:return f"Error scrape_wiki_table: {e}"def scrape_wikipedia_table(page_title: str,section: str | None = None,table_index: int = 0) -> str:"""Devuelve la tabla pedida en Markdown.Si `section` es None ⇒ busca en toda la página."""base_url = "https://en.wikipedia.org/wiki/"url = base_url + page_title.replace(" ", "_")html = requests.get(url, timeout=15).textsoup = BeautifulSoup(html, "html.parser")# encontrar tablasif section:header = soup.find(id=section)if not header:raise ValueError(f"Section '{section}' not found.")tables = header.find_all_next("table", class_="wikitable")else:tables = soup.find_all("table", class_="wikitable")if not tables or table_index >= len(tables):raise ValueError(f"Table index {table_index} out of range (found {len(tables)})")df = pd.read_html(str(tables[table_index]), flavor="bs4")[0]return df.to_markdown(index=False)# -------------------------------------------------------------------# 4) ENVUELTORES DE HERRAMIENTAS (FunctionTool)# -------------------------------------------------------------------search_tool = FunctionTool.from_defaults(fn=buscar_web,name="web_search",description="Searches the web using TavilySearch API.")reverse_tool = FunctionTool.from_defaults(fn=reverse_text,name="reverse_text",description="Invierte el texto recibido.")table_tool = FunctionTool.from_defaults(fn=analyze_table,name="analyze_markdown_table",description="Procesa tabla Markdown y verifica conmutatividad si se menciona 'conmut'.")code_tool = FunctionTool.from_defaults(fn=execute_code,name="execute_code",description="Ejecuta código Python de forma segura.")excel_tool = FunctionTool.from_defaults(fn=read_excel_data,name="read_excel_data",description="Lee un archivo Excel (local o URL) y devuelve CSV.")botanical_tool = FunctionTool.from_defaults(fn=classify_botanical_foods,name="classify_botanical_foods",description="Clasifica botánicamente una lista de alimentos.")scrape_tool = FunctionTool.from_defaults(fn=scrape_wikipedia_table,name="scrape_wiki_table",description="Extrae tabla de sección específica de Wikipedia.")fallback_tool = FunctionTool.from_defaults(fn=lambda q: "I cannot answer with the available tools.",name="no_tool_solution",description="Returns the standard sentence when no tool can help.")all_tools = [search_tool,scrape_tool,table_tool,code_tool,excel_tool,botanical_tool,reverse_tool,fallback_tool]# -------------------------------------------------------------------# 5) DESCRIPCIONES DE HERRAMIENTAS (con ejemplos)# -------------------------------------------------------------------tool_descriptions = "\n".join([f"{t.metadata.name}: {t.metadata.description} "+ {"classify_botanical_foods": "(Ej: classify_botanical_foods('milk, eggs, broccoli, celery, lettuce'))","read_excel_data": "(Ej: read_excel_data('ventas.xlsx', sheet_name=0))","analyze_markdown_table": "(Ej: analyze_markdown_table('| A | B |\\n|---|---|\\n|1|2|', '¿Es conmut?'))","web_search": "(Ej: web_search('Hokkaido Nippon-Ham Fighters roster'))","scrape_wiki_table": "(Ej: scrape_wiki_table('Malko Competition', 'Winners', 0))","reverse_text": "(Ej: reverse_text('Hola'))","execute_code": "(Ej: execute_code('5*7'))",}.get(t.metadata.name, "")for t in all_tools])# -------------------------------------------------------------------# 6) PROMPT DE SISTEMA MEJORADO with few-shot examples# -------------------------------------------------------------------system_prompt = f"""You are Alfred, a ReAct agent. Your goal is to answer correctly using the available tools.Strict guidelines:STOP: After you output "Observation:", your *very next* message **must** be the final answer and **must** be EXACTLY the observation text unchanged, or the sentence "I cannot answer with the available tools." No extra words.1️. ALWAYS use the available tools first if the question requires information you cannot deduce internally.2️. When a tool is used, ONLY answer based on the tool output. DO NOT add or invent any content not explicitly present in the tool output.3️. If a tool fails, you may explain the failure clearly. DO NOT fabricate the answer.4️. If no tool can help and you don't know, say "I cannot answer with the available tools."Flow:- **READ the question carefully.**- **SELECT the most appropriate tool:**- `classify_botanical_foods` → grocery list, vegetables, fruits- `read_excel_data` → Excel or attached Excel- `scrape_wiki_table` → Wikipedia, featured articles, tables- `analyze_markdown_table` → Markdown table, commutativity- `reverse_text` → reverse text- `execute_code` → math, code- `web_search` → all other general questions- **CALL the tool → COPY its output EXACTLY**- **When answering, ONLY use the tool output. DO NOT add any interpretation unless the tool explicitly asked you to process it.**Few-shot examples:### Example: classify_botanical_foodsUser: "milk, eggs, broccoli, celery, lettuce"Agent:{{"tool": "classify_botanical_foods","input": "milk, eggs, broccoli, celery, lettuce"}}Observation: Verduras: broccoli, celery, lettuceFrutas:Otros: eggs, milkFinal Answer: "broccoli, celery, lettuce"### Example: analyze_markdown_tableUser: "Check commutativity"Agent:{{"tool": "analyze_markdown_table","input": "|A|B|C|\\n|---|---|---|\\n|A|A|B|C|..."}}Observation: a, bFinal Answer: "a, b"---ONLY respond following this flow. DO NOT answer using your internal knowledge if a tool is required and available.If unsure, default to using the most appropriate tool first.Available tools:{tool_descriptions}"""# -------------------------------------------------------------------# 7) INICIALIZAR EL AGENTE ReActAgent# -------------------------------------------------------------------llm = GeminiLLM()alfred_agent = ReActAgent.from_tools(tools=all_tools,llm=llm,system_prompt=system_prompt,verbose=True,max_iterations=25,callback_manager=llm.callback_manager,handle_parsing_errors=True)# --- auxiliar: extraer observación limpia o fallback ----------def _extract_observation(raw: str) -> str:"""Si el agente produjo un paso con 'Observation:', devuelve exactamenteese texto (sin espacios iniciales/finales). De lo contrario devuelve raw."""if "Observation:" in raw:# ejemplo: "Observation: Verduras: ...\nFinal Answer: ..."obs = raw.split("Observation:", 1)[1].strip()# cortamos si accidentalmente quedó un "Final Answer:" concatenadoif "Final Answer:" in obs:obs = obs.split("Final Answer:", 1)[0].strip()# si el fallback-tool fue llamado, obs ya contiene la frase estándarreturn obsreturn raw.strip()# --------------------------------------------------------------def basic_agent_response(question: str) -> str:"""- Maneja el caso especial de Excel adjunto.- Ejecuta el ReActAgent y limpia la salida para cumplir las reglas SAIA."""try:lower_q = question.lower()# 1) Caso Excel adjunto ------------------------------------------------if "attached excel" in lower_q or "archivo excel" in lower_q:excel_result = read_excel_data("data/attached.xlsx")return (excel_resultif "Error" not in excel_resultelse "The Excel file is not available.")# 2) Ejecutar agente ---------------------------------------------------print(f"[DEBUG] ➜ Pregunta: {question}")raw_resp = alfred_agent.query(question) # puede ser ChatMessage o str# 3) Normalizar respuesta ---------------------------------------------# a) si es ChatMessageif hasattr(raw_resp, "response") and raw_resp.response is not None:cleaned = _extract_observation(str(raw_resp.response))else:cleaned = _extract_observation(str(raw_resp))# 4) Garantizar fallback único -----------------------------------------if not cleaned:cleaned = "I cannot answer with the available tools."return cleaned# 5) Manejo de errores -----------------------------------------------------except Exception as e:print(f"[ERROR] {e}")return "I cannot answer with the available tools."'''def basic_agent_response(question: str) -> str:"""Detecta "Excel adjunto" o usa ReActAgent.query para el resto."""try:if "attached excel" in question.lower() or "archivo excel" in question.lower():excel_result = read_excel_data("data/attached.xlsx")if "Error" in excel_result:return "The Excel file is not available."return excel_resultresp = alfred_agent.query(question)if hasattr(resp, 'response') and resp.response is not None:return str(resp.response)elif resp is not None:return str(resp)return "No se generó una respuesta válida."except Exception as e:return f"Error crítico del agente: {e}"'''123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363import osimport mathimport asyncioimport subprocessimport requestsfrom io import BytesIOfrom bs4 import BeautifulSoupfrom pydantic import Field# ---------- OPTIONAL & LAZY IMPORTS ----------# (avoid hard‑failure if libs are absent; import inside tools when needed)# ---------- LLM WRAPPER ----------from llama_index.core.llms import ChatMessage, LLMMetadata, LLM, CompletionResponsefrom llama_index.core.agent import ReActAgentfrom llama_index.core.callbacks.llama_debug import LlamaDebugHandlerfrom llama_index.core.tools import FunctionToolfrom llama_index.core.schema import Documentfrom langchain_community.retrievers import TavilySearchAPIRetrieverimport google.generativeai as genai# ---------- BASIC SETUP ----------HEADERS = {"User-Agent": "Mozilla/5.0"}def check_required_keys() -> None:missing = []if not os.getenv("TAVILY_API_KEY"):missing.append("TAVILY_API_KEY")if not os.getenv("GEMINI_API_KEY"):missing.append("GEMINI_API_KEY")if missing:print(f"⚠️ WARNING: Missing API keys: {', '.join(missing)}. Agent will not function properly!")else:print("✅ All required API keys are present.")check_required_keys()# Monkey‑patch requerido por LlamaIndexChatMessage.message = property(lambda self: self)# ---------- GEMINI LLM ----------class GeminiLLM(LLM):model_name: str = Field(default="models/gemini-1.5-flash-latest")temperature: float = Field(default=0.0)_model = Noneclass Config:extra = "allow"def __init__(self, **kwargs):super().__init__(**kwargs)api_key = os.getenv("GEMINI_API_KEY")if not api_key:raise ValueError("GEMINI_API_KEY not set in environment")genai.configure(api_key=api_key)self._model = genai.GenerativeModel(model_name=self.model_name, generation_config=genai.types.GenerationConfig(temperature=self.temperature))if self.callback_manager is None:from llama_index.core.callbacks.base import CallbackManagerself.callback_manager = CallbackManager([])if not self.callback_manager.handlers:self.callback_manager.add_handler(LlamaDebugHandler())# ----- metadata -----@propertydef metadata(self):return LLMMetadata(context_window=1_048_576,num_output=8192,is_chat_model=True,is_function_calling_model=True,model_name=self.model_name,)# ----- sync chat -----def chat(self, messages: list[ChatMessage], **kwargs) -> ChatMessage:history = [{"role": ("user" if m.role == "user" else "model"), "parts": [{"text": str(m.content)}]}for m in messages[:-1]]last_user_msg = str(messages[-1].content)session = self._model.start_chat(history=history)try:response = session.send_message(last_user_msg)return ChatMessage(role="assistant", content=response.text)except Exception as exc:return ChatMessage(role="assistant", content=f"Error Gemini chat: {exc}")# ----- async chat -----async def achat(self, messages: list[ChatMessage], **kwargs):return await asyncio.to_thread(self.chat, messages, **kwargs)# ----- completion helpers (rarely used) -----def complete(self, prompt: str, formatted: bool = False, **kwargs):try:resp = self._model.generate_content(str(prompt))return CompletionResponse(text=resp.text)except Exception as exc:return CompletionResponse(text=f"Error Gemini complete: {exc}")async def acomplete(self, prompt: str, formatted: bool = False, **kwargs):return await asyncio.to_thread(self.complete, prompt, formatted=formatted, **kwargs)# ---------- TOOLING ----------def web_search(query: str, num_results: int = 5) -> str:"""Tavily search -> concatenated, citation‑ready snippet list (includes URL)."""try:retriever = TavilySearchAPIRetriever(api_key=os.getenv("TAVILY_API_KEY"), k=num_results)results = retriever.invoke(query)formatted = []for i, doc in enumerate(results, start=1):formatted.append(f"Result {i}:\nTitle: {doc.metadata.get('title','')}\nURL: {doc.metadata.get('source','')}\nContent: {doc.page_content}\n")return "\n\n".join(formatted)except Exception as exc:return f"Error web_search: {exc}"def reverse_text(text: str) -> str:return text[::-1]# small util for optional pandasdef _pd_safe_import():try:import pandas as pd # noqa: F401return pdexcept ModuleNotFoundError:raise RuntimeError("pandas not available in this environment")def analyze_markdown_table(table_md: str, question: str) -> str:"""Check commutativity or return CSV. Requires pandas lazily."""try:pd = _pd_safe_import()lines = [l for l in table_md.strip().splitlines() if l.strip() and not l.lstrip().startswith("|---")]rows = [[c.strip() for c in l.strip().strip("|").split("|")] for l in lines]if len(rows) < 2:return "Error analyze_table: empty or malformed markdown table"df = pd.DataFrame(rows[1:], columns=rows[0])if "conmut" in question.lower():cols = df.columns.tolist()[1:]offenders = {colfor x in colsfor y in colsif df.loc[df[rows[0][0]] == x, y].iat[0] != df.loc[df[rows[0][0]] == y, x].iat[0]}return ", ".join(sorted(offenders)) or "Conmutativa"return df.to_csv(index=False)except Exception as exc:return f"Error analyze_markdown_table: {exc}"def execute_code(code: str) -> str:"""Runs arbitrary **short** python code in a sandboxed subprocess."""try:res = subprocess.run(["python", "-S", "-c", code], capture_output=True, text=True, timeout=10)if res.returncode == 0:output = res.stdout.strip()return f"Output: {output if output else '(No output)'}"return f"Error: {res.stderr.strip()}"except subprocess.TimeoutExpired:return "Error: execution timeout"except Exception as exc:return f"Error execute_code: {exc}"def read_excel_data(file_path: str, sheet_name: int | str = 0) -> str:"""Downloads or opens an excel file and returns CSV (requires pandas)."""try:pd = _pd_safe_import()if file_path.startswith(("http://", "https://")):resp = requests.get(file_path, headers=HEADERS, timeout=20)resp.raise_for_status()df = pd.read_excel(BytesIO(resp.content), sheet_name=sheet_name)else:if not os.path.exists(file_path):return f"Error read_excel_data: file '{file_path}' not found"df = pd.read_excel(file_path, sheet_name=sheet_name)return df.fillna("").to_csv(index=False)except Exception as exc:return f"Error read_excel_data: {exc}"# --- botanical classifier (unchanged) ---def classify_botanical_foods(items_list_str: str) -> str:botanical_fruits = {"tomato","bell pepper","pepper","green beans","beans","zucchini","cucumber","eggplant","corn","peas","pea","pumpkin","squash","avocado",}botanical_vegetables = {"broccoli","celery","lettuce","kale","spinach","sweet potatoes","sweet potato","potato","onion","garlic","carrot","okra","cabbage","cauliflower","beet","turnip","parsnip","leek",}vegs, fruits, others = set(), set(), set()for token in (t.strip().lower() for t in items_list_str.split(",")):if token in botanical_vegetables and token not in botanical_fruits:vegs.add(token)elif token in botanical_fruits:fruits.add(token)else:others.add(token)return (f"Vegetables: {', '.join(sorted(vegs))}\n"f"Fruits: {', '.join(sorted(fruits))}\n"f"Others: {', '.join(sorted(others))}")# --- flexible Wikipedia table scraper ---def scrape_wiki_table(page_title: str, section: str | None = None, table_index: int = 0) -> str:"""Returns the requested Wikipedia table in markdown."""try:url = f"https://en.wikipedia.org/wiki/{page_title.replace(' ', '_')}"html = requests.get(url, timeout=15).textsoup = BeautifulSoup(html, "html.parser")def _find_tables(s: BeautifulSoup):return s.find_all("table", class_="wikitable")if section:header_tag = soup.find(lambda tag: tag.name in {"h2", "h3"} and section.lower() in tag.get_text(" ", strip=True).lower())if not header_tag:return f"Error scrape_wiki_table: section '{section}' not found"tables = header_tag.find_all_next("table", class_="wikitable")else:tables = _find_tables(soup)if not tables or table_index >= len(tables):return f"Error scrape_wiki_table: table index {table_index} out of range (found {len(tables)})"pd = _pd_safe_import()df = pd.read_html(str(tables[table_index]), flavor="bs4")[0]return df.to_markdown(index=False)except Exception as exc:return f"Error scrape_wiki_table: {exc}"# --- generic URL text scraper ---def scrape_url_text(url: str) -> str:"""Downloads a webpage and returns cleaned visible text (trimmed to 8k chars)."""try:html = requests.get(url, headers=HEADERS, timeout=20).textsoup = BeautifulSoup(html, "html.parser")for tag in soup(["script", "style", "noscript"]):tag.decompose()raw_text = "\n".join(t.strip() for t in soup.get_text("\n").splitlines() if t.strip())return raw_text[:8000]except Exception as exc:return f"Error scrape_url_text: {exc}"# ---------- TOOL WRAPPERS ----------tool_defs = [(web_search, "web_search", "Searches the web via Tavily."),(scrape_wiki_table, "scrape_wiki_table", "Extracts a wikitable from Wikipedia."),(scrape_url_text, "scrape_url_text", "Fetch any URL and return visible text."),(analyze_markdown_table, "analyze_markdown_table", "Analyze a markdown table (commutativity etc)."),(execute_code, "execute_code", "Run short python snippets securely."),(read_excel_data, "read_excel_data", "Load Excel (URL or local) → CSV."),(classify_botanical_foods, "classify_botanical_foods", "Botanically classify food list."),(reverse_text, "reverse_text", "Reverse a text string."),(lambda q: "I cannot answer with the available tools.", "no_tool_solution", "Fallback answer when stuck."),]TOOLS = [FunctionTool.from_defaults(fn=fn, name=name, description=desc) for fn, name, desc in tool_defs]# ---------- SYSTEM PROMPT ----------tool_desc_str = "\n".join(f"{t.metadata.name}: {t.metadata.description}" for t in TOOLS)SYSTEM_PROMPT = f"""You are Alfred, a ReAct agent. Use the provided tools to answer.Rules:1. Try a relevant tool first when external info is needed.2. After a tool call you receive `Observation:`. Your *very next* assistant message **must** be exactly that observation (untouched) *or* the fixed string "I cannot answer with the available tools." – no extra text.3. If a tool fails, think why and try an alternative (different params / another tool) once before giving up.4. Do not invent facts.Available tools:{tool_desc_str}"""# ---------- REACT AGENT ----------llm = GeminiLLM()agent = ReActAgent.from_tools(tools=TOOLS,llm=llm,system_prompt=SYSTEM_PROMPT,verbose=True,max_iterations=25,callback_manager=llm.callback_manager,handle_parsing_errors=True,)# Helper to strip to the last Observation or fallbackdef _extract_observation(raw: str) -> str:if "Observation:" in raw:obs = raw.split("Observation:", 1)[1].strip()if "Final Answer:" in obs:obs = obs.split("Final Answer:", 1)[0].strip()return obsreturn raw.strip()# Public entry pointdef basic_agent_response(question: str) -> str:try:print(f"[DEBUG] ➜ Question: {question}")raw_resp = agent.query(question)cleaned = _extract_observation(str(raw_resp.response if hasattr(raw_resp, "response") else raw_resp))return cleaned or "I cannot answer with the available tools."except Exception as exc:print(f"[ERROR] {exc}")return "I cannot answer with the available tools."
my_tools.py+242-477