重磅發(fā)布!LangChain 1.0 Alpha 來了,Agent 終于統(tǒng)一了! 原創(chuàng)
LangChain 自推出以來迅速成為開發(fā)者構(gòu)建 LLM 驅(qū)動(dòng)應(yīng)用的重要框架之一,支持多種鏈(chains)、代理(agents)、工具調(diào)用、向量檢索、模型接口等功能。其生態(tài)不斷壯大,但也因模式多樣、抽象紛繁導(dǎo)致開發(fā)者面對(duì)不同 “agent 模式” 時(shí)感到困惑。為提升一致性、可維護(hù)性與企業(yè)級(jí)穩(wěn)定性,LangChain 在 2025 年 9 月推出了 v1.0.0 Alpha,與 LangGraph 一起邁入 1.x 版本演進(jìn)。
為什么要推出 v1.0?
- 企業(yè)信心:1.0 版本象征著更加穩(wěn)定、信賴,降低企業(yè)采用風(fēng)險(xiǎn)。
- 抽象收斂:過去存在多種 agent 模式(ReAct、Plan-and-Solve、React-Retry 等),v1.0 把主流用例統(tǒng)一遷移到一個(gè)成型的 agent 抽象上,復(fù)雜或非常規(guī)場景則推薦使用更底層、靈活的 LangGraph。
- 標(biāo)準(zhǔn)輸出格式:不同 LLM 提供商返回結(jié)構(gòu)迥異,新增?
?content_blocks?
? 屬性,用以統(tǒng)一 reasoning、工具調(diào)用、引用、媒體類型等新 API 特性
核心新特性概覽
統(tǒng)一代理抽象:create_agent
過去要實(shí)現(xiàn)“調(diào)用工具 → 整理結(jié)果 → 輸出結(jié)構(gòu)化 JSON”,要手寫 chain,現(xiàn)在一行搞定:
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage
from pydantic import BaseModel
class WeatherInfo(BaseModel):
city: str
temperature_c: float
condition: str
def fake_weather(city: str) -> str:
returnf"25,Sunny"if city == "Singapore"else"18,Rainy"
agent = create_agent(
model="openai:gpt-4o-mini",
tools=[fake_weather],
response_format=WeatherInfo
)
res = agent.invoke({"messages": [
HumanMessage("What's the weather like in Singapore?")
]})
print(res["structured_response"])
# WeatherInfo(city='Singapore', temperature_c=25.0, condition='Sunny')
設(shè)計(jì)解析
- 所有舊版 agent(ReAct, Plan-and-Execute 等)均重構(gòu)為?
?create_agent?
? 的預(yù)設(shè)配置。 - 底層統(tǒng)一使用?
?LangGraph?
? 作為執(zhí)行引擎,暴露標(biāo)準(zhǔn)接口:??invoke(input: dict) -> dict?
?。 - 輸入輸出結(jié)構(gòu)標(biāo)準(zhǔn)化:
a.輸入:??{"messages": List[BaseMessage]}?
?
b.輸出:??{"messages": List[BaseMessage], "structured_response": Optional[BaseModel]}?
?
工程價(jià)值
- 降低學(xué)習(xí)成本:開發(fā)者只需掌握一個(gè)接口。
- 提升可測(cè)試性:輸入輸出結(jié)構(gòu)固定,便于 mock 和斷言。
- 支持熱插拔:更換模型或工具集,不影響調(diào)用層。
標(biāo)準(zhǔn)化內(nèi)容模型:content_blocks
所有 ??AIMessage?
?? 新增 ??.content_blocks: List[ContentBlock]?
? 屬性。
類型定義(簡化)
class TextBlock(ContentBlock):
text: str
class ToolCallBlock(ContentBlock):
name: str
args: dict
class CitationBlock(ContentBlock):
sources: List[Source]
class ImageBlock(ContentBlock):
mime_type: str
data: bytes
設(shè)計(jì)解析
- 所有 LLM 輸出(文本、工具調(diào)用、引用、圖像等)均封裝為類型化塊。
- 各提供商差異被抽象層屏蔽:
a.OpenAI??function_call?
? →??ToolCallBlock?
?
b.Anthropic??<thinking>?
? →??TextBlock?
? +??reasoning=True?
?
c.Claude XML 工具 →??ToolCallBlock?
?
- 支持懶加載:如?
?ImageBlock.data?
? 僅在訪問時(shí)解碼。
工程價(jià)值
- 模型切換成本趨近于零。
- 輸出可被程序精確消費(fèi),無需正則/字符串匹配。
- 為多模態(tài)、流式輸出、結(jié)構(gòu)化引用提供擴(kuò)展基礎(chǔ)。
Runtime:LangGraph 成為默認(rèn)引擎
??create_agent?
?? 返回的 agent 本質(zhì)是一個(gè)編譯后的 ??LangGraph?
? 實(shí)例。
核心能力
- 狀態(tài)持久化:對(duì)話歷史、工具調(diào)用結(jié)果、中間變量存儲(chǔ)于?
?GraphState?
?。 - 流程控制:
a.條件邊(conditional edges):根據(jù)狀態(tài)決定下一節(jié)點(diǎn)。
b.循環(huán)控制:自動(dòng)重試、最大步數(shù)限制。
c.異常恢復(fù):工具失敗自動(dòng)進(jìn)入 error 節(jié)點(diǎn)。
- 可觀測(cè)性:內(nèi)置 tracing,支持 LangSmith 集成。
示例
自定義重試邏輯:
from langgraph.graph import StateGraph
def tool_node(state):
try:
result = tool.invoke(state["input"])
return {"result": result, "error": None}
except Exception as e:
return {"result": None, "error": str(e)}
def should_retry(state):
if state["error"] and state["retry_count"] < 3:
return"tool_node"
else:
return"end_node"
graph = StateGraph(AgentState)
graph.add_node("tool_node", tool_node)
graph.add_conditional_edges("tool_node", should_retry)
工程價(jià)值
- 企業(yè)級(jí)韌性:自動(dòng)重試、熔斷、降級(jí)。
- 復(fù)雜流程支持:審批流、多階段任務(wù)、人工干預(yù)點(diǎn)。
- 調(diào)試友好:狀態(tài)可序列化、可回放、可單元測(cè)試。
升級(jí)路徑與兼容策略
包結(jié)構(gòu)重構(gòu)
舊模塊 | 新位置 | 說明 |
? | ? | 僅保留核心抽象 |
? | ? | 索引、社區(qū)工具等遷出 |
? | ? | 舊版 agent 執(zhí)行器 |
漸進(jìn)式遷移建議
- 新項(xiàng)目:直接使用?
?create_agent?
? +??content_blocks?
?。 - 存量項(xiàng)目:
- 安裝?
?langchain-legacy?
? 保持兼容。 - 逐步替換?
?initialize_agent(..., AgentType.REACT...)?
? →??create_react_agent(...)?
?(兼容層)。 - 最終遷移至?
?create_agent(...)?
?。
- 注意 Breaking Changes:
- Python ≥ 3.10
- ?
?BaseMessage.text()?
? →.text
(屬性) - 默認(rèn)返回?
?AIMessage?
?,非 ??str?
?
本文轉(zhuǎn)載自????AI 博物院???? 作者:longyunfeigu
