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 代理接受來自 bridgic LLM 提供者套件、具備 astructure_output 協定的 BaseLlm 實例。AGENT 和 AMPHIFLOW 模式需要 LLM;純 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