How to Build Human-in-the-Loop Plan-and-Execute AI Agents with Explicit User Approval Using LangGraph and Streamlit


app_code = r'''
import os, json, uuid
import streamlit as st
from typing import TypedDict, List, Dict, Any, Optional
from pydantic import BaseModel, Field
from openai import OpenAI


from langgraph.graph import StateGraph, START, END
from langgraph.types import Command, interrupt
from langgraph.checkpoint.memory import InMemorySaver




def tool_search_flights(origin: str, destination: str, depart_date: str, return_date: str, budget_usd: int) -> Dict[str, Any]:
   options = [
       {"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)},
   ]
   options = sorted(options, key=lambda x: x["price_usd"])
   return {"tool": "search_flights", "top_options": options[:2]}


def tool_search_hotels(city: str, nights: int, budget_usd: int, preferences: List[str]) -> Dict[str, Any]:
   base = max(60, int(budget_usd / max(nights, 1)))
   picks = [
       {"name": "Central Boutique", "city": city, "nightly_usd": int(base*0.95), "notes": ["walkable", "great reviews"]},
       {"name": "Riverside Stay", "city": city, "nightly_usd": int(base*0.80), "notes": ["quiet", "good value"]},
       {"name": "Modern Loft Hotel", "city": city, "nightly_usd": int(base*1.10), "notes": ["new", "gym"]},
   ]
   if "luxury" in [p.lower() for p in preferences]:
       picks = sorted(picks, key=lambda x: -x["nightly_usd"])
   else:
       picks = sorted(picks, key=lambda x: x["nightly_usd"])
   return {"tool": "search_hotels", "top_options": picks[:2]}


def tool_build_day_by_day(city: str, days: int, vibe: str) -> Dict[str, Any]:
   blocks = []
   for d in range(1, days+1):
       blocks.append({
           "day": d,
           "morning": f"{city}: coffee + a must-see landmark",
           "afternoon": f"{city}: {vibe} activity + local lunch",
           "evening": f"{city}: sunset spot + dinner + optional night walk"
       })
   return {"tool": "draft_itinerary", "days": blocks}
'''



Source link

  • Related Posts

    Vercel Introduces ‘Is Agentic’, a Free Agent-Readiness Scoring Tool That Audits Public Websites Using Ora’s 100+ Checks

    Vercel has released Is Agentic, a public tool that scores how readily AI agents can discover, access, understand, and use a website. Scans are run and scored by Ora, an…

    The Developer’s Guide to NeMo Guardrails for Enterprise AI Safety

    In this tutorial, we build an in-depth NeMo Guardrails pipeline that demonstrates how layered guardrails can control an LLM-based financial assistant across the full request lifecycle. We combine deterministic PII…

    Leave a Reply

    Your email address will not be published. Required fields are marked *