Skip to content

LangChain Agent API


create_agent() 完整参数

参数类型默认值说明
modelstr 或 BaseChatModel无(必填)语言模型
toolsSequence 或 NoneNone工具列表。支持 @tool 函数、Pydantic 模型、dict
system_promptstr 或 SystemMessage 或 NoneNone系统提示词
middlewareSequence[AgentMiddleware]()中间件列表
response_formatResponseFormat 或 type 或 dict 或 NoneNone结构化输出配置
state_schematype[AgentState] 或 NoneNone自定义状态结构
context_schematype 或 NoneNone运行时上下文结构
checkpointerCheckpointer 或 NoneNone对话持久化
storeBaseStore 或 NoneNone跨会话存储
interrupt_beforelist[str] 或 NoneNone在这些节点前暂停
interrupt_afterlist[str] 或 NoneNone在这些节点后暂停
debugboolFalse是否输出详细日志
namestr 或 NoneNoneAgent 名称
cacheBaseCache 或 NoneNone缓存配置

CompiledStateGraph 方法

方法说明
invoke(input, config=None)同步运行,返回最终状态
ainvoke(input, config=None)异步运行,返回最终状态
stream(input, config=None, stream_mode="updates")同步流式运行
astream(input, config=None, stream_mode="updates")异步流式运行
get_state(config)获取当前状态快照
update_state(config, values)手动更新状态

AgentState 结构

字段类型是否必填说明
messageslist[AnyMessage]消息历史(add_messages reducer)
jump_to"tools" 或 "model" 或 "end" 或 None流程跳转(ephemeral)
structured_responseAny结构化输出结果(OmitFromInput)

常用用法示例

实例

python

from langchain.agents import create_agent

# 基本用法

agent = create_agent(model="deepseek:deepseek-v4-flash", tools=[tool1, tool2])

result = agent.invoke({"messages": [HumanMessage(content="你好")]})

# 完整配置

agent = create_agent(

    model="deepseek:deepseek-v4-flash",

    tools=[tool1, tool2],

    system_prompt="你是助手。",

    middleware=[my_middleware],

    response_format=MySchema,

    checkpointer=checkpointer,

    store=store,

    name="my_agent",

)

# 流式运行

for chunk in agent.stream(inputs, stream_mode="updates"):

    print(chunk)

# 获取状态

state = agent.get_state({"configurable": {"thread_id": "1"}})

AI 思考中...

LangChain Chat Model API

LangChain Messages API

基于 VitePress 构建,部署于 GitHub Pages