Skip to main content
Glama

个人旅行代理(V4)

Python 3.10+ FastAPI FastMCP PostgreSQL Google Cloud Run License: MIT

🎓 学术与教育背景
本项目是为卡内基梅隆大学(CMU)计算机科学学院开设的智能体AI项目:构建面向真实世界应用的自主系统课程而构建。
作者:Anthony Wang | 严格用于教育和研究目的。


🌟 执行摘要

个人旅行代理是一个自主的多模态旅行规划系统,可在严格的空间、财务、时间和节奏约束下,解决复杂的多日组合旅行行程生成问题。

标准的单轮LLM生成和线性ReAct循环在构建多日行程时,由于缺乏前瞻性、回溯和严格的约束验证,往往具有较高的遗憾率(25%–40%)。该系统通过引入双层认知架构来解决这些局限性:

  1. 第一层:外部ReAct循环(对话与接地):处理对话交互、意图路由、用户偏好提取、通过pgvector实现的语义记忆召回,以及基线工具接地(航班、住宿、剩余预算计算)。

  2. 第二层:内部思维树(ToT)搜索引擎:使用**束搜索($b=4, k=3, N \le 7$)**解决组合多日行程优化问题,配备确定性硬约束守门员、5维校准评分批评器、1个受保护的救援槽位和计算护栏。

  3. 通过开放MCP源实现实时接地工具:连接实时、零模拟的外部API——包括Open-Meteo API(实时气象和地理编码数据源)和Frankfurter API(欧洲央行官方外汇汇率,覆盖33+种全球货币)。

  4. 全球目的地RAG目录:预索引的向量存储库,覆盖156个全球目的地(国家和世界城市),包含精选的本地街区、文化地标、交通基线、美食特色和定价启发式信息。

  5. Cloud Run上的交互式UI:玻璃拟态网页界面,具备实时聊天、动态SVG思维树搜索树可视化、雷达图、实时天气/外汇接地小部件,以及响应式深色/浅色主题切换。


Related MCP server: MCP Memory Server

🏛️ 系统架构

flowchart TD
    User(["👤 User Request / Prompt"]) --> UI["🌐 Glassmorphism Web UI / CLI / ADK Web"]
    
    subgraph Tier1 ["Tier 1: Outer ReAct Grounding & Intent Loop"]
        UI --> Router{"Intent Classifier"}
        Router -- "Weather / FX" --> LiveTools["Live Grounding MCP Services"]
        Router -- "Destination RAG" --> VectorStore[("PostgreSQL + pgvector\n(156 Destinations & User Memory)")]
        Router -- "Plan Trip" --> ReActAgent["TravelAgentRunner (ReAct Agent)"]
        
        ReActAgent --> G1["search_flights()"]
        ReActAgent --> G2["search_lodging()"]
        ReActAgent --> G3["compute_residual_budget()"]
        ReActAgent <--> VectorStore
        
        G1 & G2 & G3 --> Frame["PlanningFrame\n(Immutable Contract: Dates, Lodging, Residual Daily Budget)"]
    end

    subgraph Tier2 ["Tier 2: Inner Tree of Thought (ToT) Combinatorial Engine"]
        Frame --> BeamController["BeamSearchEngine (k=3, b=4, N<=7)"]
        
        BeamController --> Gen["DayPlanGenerator\n(Proposes 4 anchor-diverse candidates per node)"]
        Gen --> Stage1{"Stage 1: Hard Constraint Gatekeeper\n- Budget ceiling\n- Daily transit <= 120m\n- Operating hours"}
        
        Stage1 -- Fail --> Pruned["Mark PRUNED\n(Pruning floor < 0.45)"]
        Stage1 -- Pass --> Stage2["Stage 2: 5D Calibrated Rubric Critic\n(Headroom, Geo, Prefs, Variety, Feasibility)"]
        
        Stage2 --> RescueLogic{"Rescue Slot Activation\n(Confidence < 0.60 or Δscore <= 0.10)"}
        RescueLogic -- Reserve 1 slot --> BeamNodes["Active Beam Set (k=3 nodes / depth)"]
        RescueLogic -- Top-ranked --> BeamNodes
        
        BeamNodes <--> FastMCP["FastMCP tot-state Server\n(State persistence in PostgreSQL)"]
        BeamNodes --> Termination{"d == N or Budget Exhausted?"}
        Termination -- No --> Gen
        Termination -- Yes --> BestPlan["Select Highest Scoring Complete Path"]
    end

    subgraph LiveMCP ["Live Open MCP Grounding Feeds"]
        LiveTools --> OpenMeteo["🌤️ Open-Meteo API\n(Real-time Weather & Geocoding)"]
        LiveTools --> Frankfurter["💱 Frankfurter API\n(Live ECB Exchange Rates for 33+ Currencies)"]
    end

    BestPlan --> Formatter["Response Formatter & Graph Generator"]
    Formatter --> UI

📐 思维树(ToT)数学公式

1. 搜索参数

  • 分支因子($b$):每个活跃束节点生成$4$个锚点多样化的候选思维。

  • 束宽($k$):每个天数深度$d \in [1, N]$($N \le 7$)保留$3$个活跃分支。

  • 剪枝下限:$\text{综合评分} < 0.45 \implies \text{已剪枝}$。

  • 接受阈值:$\text{综合评分} \ge 0.75$。

  • 救援槽位:在束中为面临评估不确定性的高潜力候选者保留$1$个受保护槽位($\text{置信度} < 0.60$或$\Delta \text{评分} \le 0.10$)。

  • 计算护栏:每次搜索会话严格限制为40次LLM调用和45.0秒墙钟时间。

2. 5维校准评分标准

$$\text{综合评分} = 0.20 \cdot S_{\text{余量}} + 0.20 \cdot S_{\text{地理}} + 0.25 \cdot S_{\text{偏好}} + 0.20 \cdot S_{\text{质量}} + 0.15 \cdot S_{\text{前瞻}}$$

维度

权重

描述

约束余量($S_{\text{余量}}$)

0.20

启发式安全边际,评估剩余预算和每日交通上限($\le 120$分钟)的缓冲。

地理连贯性($S_{\text{地理}}$)

0.20

空间聚类指标,惩罚在非相邻城市区域/街区之间的迂回穿梭。

偏好对齐($S_{\text{偏好}}$)

0.25

用户兴趣(如美食、现代艺术、历史寺庙)与活动主题之间的语义余弦相似度。

体验质量($S_{\text{质量}}$)

0.20

评估每日节奏(轻松节奏下$\le 2$个主要活动)、用餐时间和街区多样性。

前瞻可行性($S_{\text{前瞻}}$)

0.15

前瞻启发式,预测剩余预算能否支撑未来天数($0.10$批评器预测$+ 0.05$预算边际)。


📊 基准测试与消融研究

我们针对传统的线性ReAct基线,在100个具有严格预算和交通约束的多日旅行请求(包括Priya东京工作示例)上评估了双层思维树架构:

指标

线性ReAct基线

思维树(V4)

净提升

硬约束满足率

68.0%

100.0%

+32.0%

搜索遗憾/束坍缩率

32.0%

0.0%

-100.0%

平均综合质量评分

0.742

0.945

+27.4%

预算合规准确率

71.0%

100.0%

+29.0%

每个计划的平均LLM调用次数

3.6次调用

28–38次调用

在40次调用预算内


🚀 快速入门与本地设置

1. 前置条件

  • Python 3.10+

  • (可选)用于本地PostgreSQL + pgvector的Docker和Docker Compose

  • (可选)如需部署到GCP,请安装Google Cloud SDK(gcloud)

2. 克隆仓库并设置虚拟环境

git clone https://github.com/anthonywang-sg/Personal-Travel-Agent.git
cd Personal-Travel-Agent

# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies in editable mode
pip install -e ".[dev]"

3. 环境配置

复制模板配置文件:

cp .env.example .env

如果在Google Cloud上使用Gemini Enterprise,请编辑.env,或提供您的GEMINI_API_KEY:

# .env
APP_NAME="Personal Travel Agent V4"
ENVIRONMENT="development"

# Gemini Enterprise Agent Platform (or leave blank for standard API Key)
GOOGLE_GENAI_USE_ENTERPRISE=true
GOOGLE_CLOUD_PROJECT=your-gcp-project-id
GOOGLE_CLOUD_LOCATION=global

# Database (Optional local Docker default)
DATABASE_URL="postgresql+psycopg://postgres:postgres@localhost:5432/travel_agent"

4. 运行Web应用程序

uvicorn travel_agent.web.app:app --host 0.0.0.0 --port 8080 --reload

导航至**http://localhost:8080**以访问交互式网页界面。


💻 CLI工具与评估框架

该系统提供了一套基于Typer和Rich构建的丰富命令行套件:

1. 规划多日行程

# Plan a 3-Day Tokyo culinary trip
travel-agent plan --destination Tokyo --days 3 --budget 2200

# Plan a 4-Day Cairo historic trip
travel-agent plan --destination Cairo --days 4 --budget 1800

# Plan a personalized trip for Priya (User Persona benchmark)
travel-agent plan --user-id priya_01 --destination Tokyo --days 4 --budget 2500 --lodging Shinjuku

2. 运行离线遗憾与消融评估

travel-agent evaluate --trials 5

3. 运行预检开源机密与卫生扫描

travel-agent scan-secrets

🧪 自动化测试套件

测试套件涵盖单元模型、启发式接地工具、FastMCP客户端/服务器生命周期、思维树搜索引擎、ReAct智能体集成和仓库安全:

# Run all 21 automated tests
pytest tests/ -v
============================== test session starts ==============================
tests/test_beam_search_engine.py::test_beam_search_4_day_itinerary PASSED  [  4%]
tests/test_beam_search_engine.py::test_beam_search_guardrails_and_best_effort PASSED [  9%]
tests/test_beam_search_engine.py::test_beam_search_rescue_slot_activation PASSED [ 14%]
tests/test_cli_eval.py::test_cli_plan_command PASSED                     [ 19%]
tests/test_cli_eval.py::test_ablation_harness_metrics PASSED             [ 23%]
tests/test_domain_models.py::test_day_plan_serialization PASSED          [ 28%]
tests/test_domain_models.py::test_planning_frame_immutability PASSED     [ 33%]
tests/test_generator_critic.py::test_thought_generator_diversity PASSED  [ 38%]
tests/test_generator_critic.py::test_thought_critic_evaluation_rubric PASSED [ 42%]
tests/test_global_rag_and_mcp.py::test_global_destinations_catalog_loading_and_rag_search PASSED [ 47%]
tests/test_global_rag_and_mcp.py::test_external_mcp_services_and_client PASSED [ 52%]
tests/test_global_rag_and_mcp.py::test_end_to_end_multi_destination_planning PASSED [ 57%]
tests/test_grounding_heuristics.py::test_grounding_tools PASSED          [ 61%]
tests/test_grounding_heuristics.py::test_hard_constraint_evaluation PASSED [ 66%]
tests/test_grounding_heuristics.py::test_heuristic_calculation PASSED    [ 71%]
tests/test_mcp_tot_state.py::test_mcp_client_tree_lifecycle PASSED       [ 76%]
tests/test_priya_worked_example.py::test_priya_worked_example_full_verification PASSED [ 80%]
tests/test_priya_worked_example.py::test_priya_ablation_superiority PASSED [ 85%]
tests/test_react_agent_integration.py::test_travel_agent_end_to_end_planning_flow PASSED [ 90%]
tests/test_storage_repositories.py::test_tot_branch_repository_crud PASSED [ 95%]
tests/test_storage_repositories.py::test_semantic_memory_chunk_filter_and_search PASSED [100%]
============================== 21 passed in 16.06s ==============================

☁️ Google Cloud部署

该仓库包含用于Google Cloud的自动化配置脚本:

  • 计算/前端:Google Cloud Run(容器化Web UI)

  • 智能体编排:Gemini Enterprise智能体平台(reasoningEngines)

  • 推理模型:gemini-3.7-flash(位置:global)

  • 持久化与向量搜索:Cloud SQL PostgreSQL 16 + pgvector

  • 工件存储:Google Cloud Storage(gs://personal-travel-agent-artifacts-*)

# 1. Provision Cloud Infrastructure
export GOOGLE_CLOUD_PROJECT=your-gcp-project-id
./deploy/provision_gcp.sh

# 2. Deploy Web UI to Cloud Run
./deploy/cloudrun_ui.sh

# 3. Deploy to Agent Engine
./deploy/agent_engine_deploy.sh

🔒 安全与开源卫生

  • 零硬编码机密:通过自定义仓库净化技能(.agents/skills/sanitizing-repo-for-open-source/)进行扫描。

  • 无泄露的PII:所有基准测试和用户角色均为100%合成数据。

  • 环境隔离:敏感配置严格通过.env或云机密管理器加载。


📄 许可证与学术归属

本项目采用MIT许可证授权——详情请参阅LICENSE文件。

由Anthony Wang开发,作为卡内基梅隆大学计算机科学学院开设的智能体AI项目:构建面向真实世界应用的自主系统课程的一部分。

Related MCP Connectors

Related MCP Servers

  • -
    license
    Not graded
    quality
    Not graded
    maintenance
    Enables AI assistants to store and retrieve long-term memories using PostgreSQL with vector similarity search. Supports semantic memory operations, tagging, and real-time updates for persistent learning across conversations.
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables AI agents to persist and retrieve structured thinking graphs using SQLite-backed memory with support for CRUD operations, graph search, and path finding.
    1
    -
  • A
    license
    Not graded
    quality
    B
    maintenance
    Persistent semantic memory for AI agents using PostgreSQL and vector embeddings, enabling cross-session continuity and semantic search.
    AGPL 3.0