Cómo crear agentes de IA que planifiquen y ejecuten con participación humana y con la aprobación explícita del usuario utilizando LangGraph y Streamlit
app_code = r”” importar os, json, uuid importar streamlit como st al escribir importar TypedDict, Lista, Dict, Cualquiera, Opcional de pydantic importar BaseModel, Campo de openai importar OpenAI de langgraph.graph importar StateGraph, INICIO, FINAL de langgraph.types importar Comando, interrumpir desde langgraph.checkpoint.memory importar InMemorySaver def tool_search_flights(origen: str, destino: str, fecha_salida: str, fecha_retorno: str, presupuesto_usd: int) -> Dict[str, Any]: opciones = [
{“airline”: “SkyJet”, “route”: f”{origin}->{destination}”, “depart”: depart_date, “return”: return_date, “price_usd”: int(budget_usd*0.55)},
{“airline”: “AeroBlue”, “route”: f”{origin}->{destination}”, “depart”: depart_date, “return”: return_date, “price_usd”: int(budget_usd*0.70)},
{“airline”: “Nimbus Air”, “route”: f”{origin}->{destination}”, “depart”: depart_date, “return”: return_date, “price_usd”: int(budget_usd*0.62)},
]
opciones = ordenado (opciones, clave = lambda x: x[“price_usd”]) devolver {“tool”: “search_flights”, “top_options”: opciones[:2]} def tool_search_hotels(ciudad: str, noches: int, presupuesto_usd: int, preferencias: Lista[str]) -> Dictar[str, Any]: base = max(60, int(presupuesto_usd / max(noches, 1))) selecciones = [
{“name”: “Central Boutique”, “city”: city, “nightly_usd”: int(base*0.95), “notes”: [“walkable”, “great reviews”]}, {“name”: “Riverside Stay”, “city”: ciudad, “nightly_usd”: int(base*0.80), “notas”: [“quiet”, “good value”]}, {“nombre”: “Modern Loft Hotel”, “ciudad”: ciudad, “nightly_usd”: int(base*1.10), “notas”: [“new”, “gym”]}, ]si “lujo” en [p.lower() for p in preferences]: selecciones = ordenados (selecciones, clave = lambda x: -x[“nightly_usd”]) más: selecciones = ordenado (selecciones, clave = lambda x: x[“nightly_usd”]) devolver {“herramienta”: “search_hotels”, “top_options”: selecciones[:2]} def tool_build_day_by_day(ciudad: str, días: int, ambiente: str) -> Dict[str, Any]: bloques = []
for d in range(1, días+1): blocks.append({ “día”: d, “mañana”: f”{ciudad}: café + un lugar de visita obligada”, “tarde”: f”{ciudad}: actividad {vibe} + almuerzo local”, “tarde”: f”{ciudad}: lugar para el atardecer + cena + caminata nocturna opcional” }) return {“tool”: “draft_itinerary”, “days”: blocks} ”’