停止过度训练:使用 LangGraph 和 WHOOP 构建 Bio-Hacker 代理

发布: (2026年2月7日 GMT+8 09:00)
5 分钟阅读
原文: Dev.to

Source: Dev.to

我们都有过这样的经历。日程表上排着 “沉重的腿部训练日”,但醒来时感觉像被卡车撞了一样。你的心率变异性(HRV)跌到谷底,恢复分数只有可怜的 14 %。传统做法要么硬着头皮坚持(冒受伤风险),要么手动调动日程。

但我们是开发者。我们不手动操作。

在本教程中,我们将构建一个 Bio‑Hacker Agent —— 一个自主系统,能够:

  • 通过 WHOOP API 监控你的生物指标。
  • 当恢复指标低于阈值时,重写你的 Google Calendar推荐个性化的低强度营养计划

完成后,你将了解如何使用最前沿的技术栈,将可穿戴物联网数据与可执行的 AI 决策相结合:

  • Python 3.10+
  • LangGraph(状态机编排)
  • Pydantic‑AI(基于模式的 LLM 输出)
  • Google Calendar API
  • WHOOP Recovery API

数据流概览

graph TD
    A[Start: Daily Sync] --> B[Fetch WHOOP Recovery Data]
    B --> C{Is HRV/Recovery Low?}
    C -- "No (Green Zone)" --> D[Keep Existing Schedule]
    C -- "Yes (Red/Yellow Zone)" --> E[Agent: Initiate Pivot]
    E --> F[Google Calendar API: Reschedule Workout]
    E --> G[Pydantic AI: Generate Anti‑Inflammatory Recipe]
    F --> H[Notify User via Slack/Push]
    G --> H
    D --> I[End]
    H --> I

前置条件

项目为什么需要它
WHOOP 开发者账户访问 Recovery API
Google Cloud 控制台(已启用 Calendar API)以编程方式更新日历事件
Python 3.10+现代语言特性
langgraph, pydantic-ai, google-api-python-client代理的核心库

1️⃣ 用 Pydantic 定义代理的 “大脑”

from pydantic import BaseModel, Field
from typing import List, Optional

class RecoveryAction(BaseModel):
    """Schema for the agent’s decision output."""
    is_pivot_needed: bool = Field(
        description="True if HRV is below 50 % of the 7‑day average"
    )
    new_workout_type: Optional[str] = Field(
        description="Recommended activity (e.g., Yoga, Zone 2 Walk)"
    )
    reasoning: str = Field(
        description="Explanation for the change based on metrics"
    )
    suggested_calories: int = Field(
        description="Adjusted calorie target for low‑intensity day"
    )

2️⃣ 使用 LangGraph 构建决策图

from langgraph.graph import StateGraph, END
from typing import TypedDict, List

# ----------------------------------------------------------------------
#  State definition (typed dict for static checking)
# ----------------------------------------------------------------------
class AgentState(TypedDict):
    hrv_score: int
    recovery_percent: int
    calendar_events: List[str]
    action_plan: RecoveryAction

# ----------------------------------------------------------------------
#  Node: fetch biometrics from WHOOP (simulated here)
# ----------------------------------------------------------------------
def check_biometrics(state: AgentState):
    # Real implementation would call WHOOP API
    # Simulating a “Red Recovery” day:
    return {"hrv_score": 35, "recovery_percent": 22}

# ----------------------------------------------------------------------
#  Node: analyze metrics and decide whether to pivot
# ----------------------------------------------------------------------
def analyze_and_pivot(state: AgentState):
    # LLM (e.g., GPT‑4o) would normally generate this decision
    if state["recovery_percent"] < 30:
        # Example pivot decision
        return {
            "is_pivot_needed": True,
            "new_workout_type": "Yoga",
            "reasoning": "HRV below threshold; recommend low‑intensity activity.",
            "suggested_calories": 1800,
        }
    else:
        return {
            "is_pivot_needed": False,
            "new_workout_type": None,
            "reasoning": "Metrics within normal range.",
            "suggested_calories": 2500,
        }

# ----------------------------------------------------------------------
#  Assemble the graph
# ----------------------------------------------------------------------
graph = StateGraph(AgentState)

graph.add_node("fetch", check_biometrics)
graph.add_node("decide", analyze_and_pivot)

graph.set_entry_point("fetch")
graph.add_edge("fetch", "decide")
graph.add_edge("decide", END)

app = graph.compile()

生产提示: 处理 OAuth 令牌刷新、错误重试,并在存储受保护健康信息(PHI)时遵守 HIPAA 等用户隐私规定。

4️⃣ 生成以恢复为重点的餐食计划

def generate_recipe(action: RecoveryAction):
    prompt = (
        f"Create a meal plan for {action.suggested_calories} calories "
        f"focused on anti‑inflammatory ingredients for someone with low HRV."
    )
    # Call your LLM provider here (OpenAI, Anthropic, etc.)
    # response = client.chat.completions.create(...)

    print("Recipe generated for recovery day!")

5️⃣ 下一步与扩展

  • Slack / 推送通知 – 在你醒来的瞬间提醒。
  • MyFitnessPal 集成 – 自动记录宏目标。
  • 睡眠数据分析 – 融入 WHOOP 睡眠阶段以做出更丰富的决策。

进一步阅读

对于更深入了解生产级健康技术代理,请参阅 WellAlly 博客——他们涵盖顶级长寿初创公司使用的架构蓝图。

祝 hacking 愉快,愿你的恢复始终最佳!

在下方留下评论或分享你自己的生物黑客脚本!别忘了访问 wellally.tech/blog 获取更多关于 AI 驱动健康未来的洞见。

0 浏览
Back to Blog

相关文章

阅读更多 »

UX/UI 排版

Typography 是指什么?- 使用哪种字体 - 在什么位置多大 - 多粗 - 行间距 - …