Bridgic Amphibious
双模式代理框架:代理在 LLM 驱动模式(on_agent)和确定性模式(on_workflow)下运行,并在两者之间自动回退。
依赖
一个 bridgic-amphibious 项目需要以下包:
| 包 | 描述 |
|---|---|
bridgic-core | 核心框架(Worker, Automa, GraphAutoma, ASL) |
bridgic-amphibious | 双模式代理框架 |
bridgic-llms-openai | LLM 提供商(仅 AGENT / AMPHIFLOW 模式需要) |
python-dotenv | .env 文件加载 |
使用此包之前,您需要通过提供的安装脚本安装依赖:
bash "skills/bridgic-amphibious/scripts/install-deps.sh" "$PWD"
该脚本会检查 uv 是否可用,如果需要会初始化一个 uv 项目,通过 uv add 安装任何缺失的包,并运行 uv sync 来最终完成环境配置。脚本成功退出后,项目即完全初始化并准备好使用——无需手动执行 uv add / uv sync 后续操作。
LLM 设置
Amphibious 代理接受一个 BaseLlm 实例,该实例来自 bridgic LLM 提供商包,并包含 astructure_output 协议。LLM 在 AGENT 和 AMPHIFLOW 模式下是必需的;纯 WORKFLOW 模式可以无需 LLM 运行。
from bridgic.llms.openai import OpenAILlm, OpenAIConfiguration
llm = OpenAILlm(
api_key="your-api-key",
api_base="https://api.openai.com/v1", # 或自定义端点
configuration=OpenAIConfiguration(model="gpt-4o", temperature=0.0),
)
其他具有相同协议的提供商:bridgic.llms.vllm.VllmServerLlm(自托管 vLLM)。
快速开始
from bridgic.amphibious import (
AmphibiousAutoma, CognitiveContext, CognitiveWorker, think_unit,
)
from bridgic.core.agentic.tool_specs import FunctionToolSpec
async def get_weather(city: str) -> str:
"""获取某个城市的天气。"""
return f"晴天,22°C,位于 {city}"
class WeatherAgent(AmphibiousAutoma[CognitiveContext]):
planner = think_unit(
CognitiveWorker.inline("查找天气并提供摘要。"),
max_attempts=5,
)
async def on_agent(self, ctx: CognitiveContext):
await self.planner
agent = WeatherAgent(llm=llm, verbose=True)
result = await agent.arun(
goal="查看东京和伦敦的天气。",
tools=[FunctionToolSpec.from_raw(get_weather)],
)
项目脚手架
使用 CLI 来引导一个新项目:
bridgic-amphibious create -n my_project
bridgic-amphibious create -n my_project --task "导航到 example.com 并提取数据"
bridgic-amphibious create -n my_project --base-dir /path/to/projects
创建文件:task.md、config.py、tools.py、workers.py、agents.py、skills/、result/、log/。
核心概念
代理 = 思考单元 + 上下文编排。 代理通过声明 CognitiveWorker 思考单元并在 on_agent() 或 on_workflow() 中进行编排来定义。
四层架构:
Exposure— 数据可见性抽象(LayeredExposure / EntireExposure)CognitiveContext— 状态容器(目标、工具、技能、历史)CognitiveWorker— 纯思考单元(观察-思考-行动)AmphibiousAutoma— 编排引擎(模式路由、生命周期)
OTC 循环: 观察 -> 思考 -> 行动,每个阶段都有钩子点。
四种运行模式: AGENT(LLM 驱动)、WORKFLOW(确定性)、AMPHIFLOW(工作流 + 代理回退)、AUTO(根据重写的方法自动检测,默认)。
AUTO 解析: 仅重写 on_agent → AGENT;仅重写 on_workflow → WORKFLOW;两者都重写 → AMPHIFLOW。
关键模式
代理模式 — LLM 决策
class MyAgent(AmphibiousAutoma[CognitiveContext]):
worker = think_unit(CognitiveWorker.inline("决定下一步。"), max_attempts=10)
async def on_agent(self, ctx):
await self.worker
工作流模式 — 开发者决策
from bridgic.amphibious import ActionCall
class MyWorkflow(AmphibiousAutoma[CognitiveContext]):
async def on_workflow(self, ctx):
result = yield ActionCall("tool_name", arg1="value")
# result 是 List[ToolResult]
# 纯工作流模式不需要 LLM。
await MyWorkflow().arun(goal="...", tools=[...])
Amphiflow 模式 — 工作流带代理回退
from bridgic.amphibious import RunMode, AgentCall
class MyHybrid(AmphibiousAutoma[CognitiveContext]):
fixer = think_unit(CognitiveWorker.inline("修复问题。"), max_attempts=5)
async def on_agent(self, ctx): await self.fixer
async def on_workflow(self, ctx):
yield ActionCall("fill_field", name="user", value="john")
yield ActionCall("click_button", name="submit")
await MyHybrid(llm=llm).arun(
goal="...", tools=[...],
mode=RunMode.AMPHIFLOW, max_consecutive_fallbacks=2,
)
人工介入
from bridgic.amphibious import ActionCall, HumanCall
class MyAgent(AmphibiousAutoma[CognitiveContext]):
worker = think_unit(CognitiveWorker.inline("执行步骤。"), max_attempts=10)
async def on_agent(self, ctx):
await self.worker
feedback = await self.request_human("继续吗?") # 入口 1:代码级别
async def on_workflow(self, ctx):
yield ActionCall("do_something", arg="value")
feedback = yield HumanCall(prompt="确认?") # 入口 2:工作流 yield
# 入口 3:LLM 工具 — `request_human` 自动注入到每个代理的
# 工具列表中,因此 LLM 可以直接调用,无需你在 tools=[...] 中列出。
# (显式传递 `request_human_tool` 是无害的——它会去重。)
await MyAgent(llm=llm).arun(goal="...", tools=[my_tool])
# 如果你想显式传参,导入并传递 `request_human_tool` 同样可以。
from bridgic.amphibious.builtin_tools import request_human_tool
await agent.arun(goal="...", tools=[request_human_tool, ...]) # 也可以
自定义 Pydantic 输出
from pydantic import BaseModel
class Plan(BaseModel):
phases: list[str]
class Planner(AmphibiousAutoma[CognitiveContext]):
plan = think_unit(
CognitiveWorker.inline("创建一个计划。", output_schema=Plan),
max_attempts=1,
)
async def on_agent(self, ctx):
result = await self.plan # 返回 Plan 实例
阶段注解(快照)
async def on_agent(self, ctx):
async with self.snapshot(goal="研究阶段"):
await self.researcher
async with self.snapshot(goal="写作阶段"):
await self.writer
参考文件
- 架构细节(执行模式、暴露系统、内存层级、认知策略):请参阅 references/architecture.md
- 完整 API 参考(所有类、方法、参数、类型):请参阅 references/api-reference.md
- 完整代码模式与示例(所有钩子类型、技能、追踪、过滤等):请参阅 references/patterns.md