
使用Scala Play框架構建REST API
平臺 | 一鍵注冊 | 免費額度 |
---|---|---|
LangSmith(調試) | https://smith.langchain.com | 5K runs/月 |
LangGraph Cloud(部署) | https://cloud.langgraph.com | 50 GPU-min/日 |
New Project
→ 選擇模板 Multi-Agent Travel Planner Visual Editor
?? 畫布說明(表格速查)
節點 | 角色 | 使用模型 | 輸出 |
---|---|---|---|
?? 行程規劃器 Planner | 中央路由 | gpt-4o-mini | 任務拆分 JSON |
?? FlightAgent | 機票比價 | gpt-4-turbo | 航班列表 |
?? HotelAgent | 酒店搜索 | claude-3.5-sonnet | 酒店列表 |
?? LocalGuide | 地接推薦 | gemini-1.5-pro | 景點+餐廳 |
點擊 Deploy
→ 獲得 HTTPS Endpoint:
https://api.langgraph.app/run/travel-planner-{your-id}
用 curl 測試:
curl -X POST https://api.langgraph.app/run/travel-planner-{your-id} \
-H "Content-Type: application/json" \
-d '{"query":"帶爸媽去東京5天,預算2萬,行程不要太累"}'
返回結果(截斷):
{
"status": "success",
"plan": {
"flights": {...},
"hotels": {...},
"itinerary": {...}
},
"total_cost": "¥19,800"
}
pip install "langgraph[cli]" # 官方 CLI
langgraph new multi-agent-tutorial
cd multi-agent-tutorial && langgraph up
# 瀏覽器自動打開 http://localhost:8000/ui
需求:在旅行規劃結束后,每 12 小時監控機票價格變化,降價即推送。
步驟 1:編寫節點函數
from langgraph.graph import StateGraph
from typing import TypedDict
class TravelState(TypedDict):
plan: dict
alerts: list
def price_monitor(state: TravelState):
# 調用 Skyscanner API 獲取最新價格
new_price = skyscanner.fetch(state["plan"]["flights"]["id"])
if new_price < state["plan"]["flights"]["price"]:
state["alerts"].append({"type": "price_drop", "new_price": new_price})
return state
步驟 2:插入循環邊
builder = StateGraph(TravelState)
builder.add_node("planner", planner)
builder.add_node("monitor", price_monitor)
# 先規劃,再進入監控循環
builder.add_edge("planner", "monitor")
builder.add_edge("monitor", "monitor") # 自循環
步驟 3:在 UI 中可視化調試
本地 UI 支持拖拽添加節點,右鍵節點 → Edit Node
可直接修改 Python 代碼并熱重載。
角色 | 描述 | 工具 |
---|---|---|
??? 采訪 Agent | 爬取 30+ 源 | Jina Reader |
?? 撰稿 Agent | 生成 600 字快訊 | gpt-4o |
? 事實核查 Agent | 交叉驗證 3 源 | Google Fact Check Tools |
????? 編輯 Agent | 潤色 + 起標題 | claude-3.5 |
代碼片段(精簡):
from langgraph.prebuilt import create_react_agent
interviewer = create_react_agent(
llm=ChatOpenAI(model="gpt-4o"),
tools=[JinaReader(), GoogleFactCheck()]
)
節點 | 功能 | 模型 | 輸出 |
---|---|---|---|
?? 數據收集 Agent | 抓取財報 + 新聞 | 自研 FinBERT | JSON |
?? 估值 Agent | DCF + 相對估值 | gpt-4-turbo | FairPrice |
?? 風險 Agent | 輿情情感 + 異常檢測 | claude-3.5 | RiskScore |
?? 組合優化 Agent | 馬克維茨再平衡 | 自建 OR-Tools | 調倉指令 |
全部節點通過 共享內存 傳遞 RiskState
,延遲 2 秒。
指標 | 目標 | 手段 |
---|---|---|
冷啟動 | 3 s | 預置 GPU 池 |
節點延遲 | P95 800 ms | 流式輸出 + 并行 |
吞吐 | 100 req/s | 水平分片 |
使用 LangSmith Cost Dashboard
工具 | 用途 | 地址 |
---|---|---|
?? LangSmith Trace | 節點級鏈路追蹤 | https://smith.langchain.com |
?? Weave 日志 | 自定義指標 | https://wandb.ai/site/weave |
?? Prometheus | 系統級監控 | https://prometheus.io |
?? PagerDuty | SLA 告警 | https://pagerduty.com |
如果你在 2025 年想讓 AI 真正“協作”而非“單打獨斗”,LangGraph 是目前最成熟的選擇。