微信扫码
添加专属顾问
在人工智能(AI)快速发展的今天,检索增强生成(RAG)系统(LLM Agent应用场景:Agentic RAG)已成为处理简单查询和生成上下文相关响应的常用工具。然而,随着对更复杂AI应用需求的日益增长,开发能够执行复杂多步骤任务、跨交互维持状态并动态适应新信息的系统变得尤为重要。
LangGraph,作为LangChain库的一个强大扩展,正是为解决这一挑战而生。它支持构建具有状态管理和循环计算能力的高级LLM Agent(探索新一代大模型代理(LLM agent)及其架构),让开发者能够创建智能、适应性强且适用于现实世界的AI系统。本文将深入探讨LangGraph如何变革AI开发,并通过一个计算太阳能板节能潜力的AI代理示例,展示其独特功能和应用价值。
LangGraph通过无缝管理图结构、状态和协调,重新定义了AI开发,为创建复杂多参与者应用程序提供了强大支持。自动状态管理确保在交互之间保持上下文,使AI能够智能地响应不断变化的输入。此外,其高效的代理协调机制保证了精确的执行和高效的信息交换,让开发者能够专注于创新工作流程的设计,而不是技术复杂性。其核心由三个关键组件构成:节点(Nodes)、状态(State)和边(Edges)。
节点是图的基本构建块,代表独立的计算步骤或函数。每个节点执行特定任务,如处理输入、做出决策或与外部系统交互。LangGraph支持高度定制化的节点,使得开发者能够根据需要执行广泛的操作。
状态代表了随着计算进行而维护和更新的上下文或记忆。它确保了图中的每个步骤都能访问来自前一步骤的相关信息,从而支持基于累积数据的动态决策制定。这种机制是构建具有长期记忆和复杂行为LLM Agent的关键。
边连接图中的节点,定义了计算从一个步骤到下一个步骤的流动。它们支持条件逻辑,允许根据当前状态和数据改变执行路径,促进数据和控制流在节点之间的传递,从而支持复杂的多步骤工作流程。
自动状态管理:确保跨交互的上下文得以保存,使 AI 能够智能地响应变化的输入。
流畅的代理协调:保证精确执行和高效的信息交换,让开发者专注于创新工作流程而非技术细节。
灵活性和可扩展性:支持定制化的高性能应用开发,同时保持系统的健壮性和可靠性,即使在企业级应用中也表现优异。
利用LangGraph构建LLM Agent
接下来,我们将通过一个具体的例子——计算太阳能板节能的LLM Agent,来展示如何使用LangGraph。这个AI代理可以作为太阳能板销售商网站上的潜在客户开发工具,通过提供个性化的节能估算来吸引潜在客户,并帮助销售团队进行后续跟进。
首先,需要导入构建AI助理所需的Python库和模块:
from langchain_core.tools import toolfrom langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import Runnablefrom langchain_aws import ChatBedrockimport boto3from typing import Annotatedfrom typing_extensions import TypedDictfrom langgraph.graph.message import AnyMessage, add_messagesfrom langchain_core.messages import ToolMessagefrom langchain_core.runnables import RunnableLambdafrom langgraph.prebuilt import ToolNode
步骤 2: 定义节能计算工具
在构建LLM Agent之前,需要定义Agent将使用的工具。这些工具可以是任何能够执行特定任务的函数或类。例如,在太阳能面板节能计算的场景中,可以定义一个compute_savings工具,该工具根据用户的月度电费计算潜在的节能收益。:
def compute_savings(monthly_cost: float) -> float:"""Tool to compute the potential savings when switching to solar energy based on the user's monthly electricity cost.Args:monthly_cost (float): The user's current monthly electricity cost.Returns:dict: A dictionary containing:- 'number_of_panels': The estimated number of solar panels required.- 'installation_cost': The estimated installation cost.- 'net_savings_10_years': The net savings over 10 years after installation costs."""def calculate_solar_savings(monthly_cost):# Assumptions for the calculationcost_per_kWh = 0.28cost_per_watt = 1.50sunlight_hours_per_day = 3.5panel_wattage = 350system_lifetime_years = 10# Monthly electricity consumption in kWhmonthly_consumption_kWh = monthly_cost / cost_per_kWh# Required system size in kWdaily_energy_production = monthly_consumption_kWh / 30system_size_kW = daily_energy_production / sunlight_hours_per_day# Number of panels and installation costnumber_of_panels = system_size_kW * 1000 / panel_wattageinstallation_cost = system_size_kW * 1000 * cost_per_watt# Annual and net savingsannual_savings = monthly_cost * 12total_savings_10_years = annual_savings * system_lifetime_yearsnet_savings = total_savings_10_years - installation_costreturn {"number_of_panels": round(number_of_panels),"installation_cost": round(installation_cost, 2),"net_savings_10_years": round(net_savings, 2)}# Return calculated solar savingsreturn calculate_solar_savings(monthly_cost)
步骤 3: 设置状态管理和错误处理
有效的状态管理是构建健壮AI系统的关键。在LangGraph中,状态通常被定义为一个包含消息列表的字典,这些消息记录了用户输入、Agent输出以及工具调用的结果。使用Python的TypedDict可以帮助定义状态的结构,确保类型安全。。
def handle_tool_error(state) -> dict:"""Function to handle errors that occur during tool execution.Args:state (dict): The current state of the AI agent, which includes messages and tool call details.Returns:dict: A dictionary containing error messages for each tool that encountered an issue."""# Retrieve the error from the current stateerror = state.get("error")# Access the tool calls from the last message in the state's message historytool_calls = state["messages"][-1].tool_calls# Return a list of ToolMessages with error details, linked to each tool call IDreturn {"messages": [ToolMessage(content=f"Error: {repr(error)}\n please fix your mistakes.",# Format the error message for the usertool_call_id=tc["id"],# Associate the error message with the corresponding tool call ID)for tc in tool_calls# Iterate over each tool call to produce individual error messages]}def create_tool_node_with_fallback(tools: list) -> dict:"""Function to create a tool node with fallback error handling.Args:tools (list): A list of tools to be included in the node.Returns:dict: A tool node that uses fallback behavior in case of errors."""# Create a ToolNode with the provided tools and attach a fallback mechanism# If an error occurs, it will invoke the handle_tool_error function to manage the errorreturn ToolNode(tools).with_fallbacks([RunnableLambda(handle_tool_error)],# Use a lambda function to wrap the error handlerexception_key="error"# Specify that this fallback is for handling errors)
创建State类和Assistant类,分别用于管理对话的上下文和执行LLM Agent的逻辑。
class State(TypedDict):messages: Annotated[list[AnyMessage], add_messages]class Assistant:def __init__(self, runnable: Runnable):# Initialize with the runnable that defines the process for interacting with the toolsself.runnable = runnabledef __call__(self, state: State):while True:# Invoke the runnable with the current state (messages and context)result = self.runnable.invoke(state)# If the tool fails to return valid output, re-prompt the user to clarify or retryif not result.tool_calls and (not result.contentor isinstance(result.content, list)and not result.content[0].get("text")):# Add a message to request a valid responsemessages = state["messages"] + [("user", "Respond with a real output.")]state = {**state, "messages": messages}else:# Break the loop when valid output is obtainedbreak# Return the final state after processing the runnablereturn {"messages": result}
LLM(大模型哪个好用?)是Agent的“大脑”,负责理解用户输入并生成响应。可以使用AWS Bedrock或其他LLM服务来配置LLM。配置过程包括设置模型ID、客户端参数等,以确保Agent能够访问LLM并与之交互。
def get_bedrock_client(region):return boto3.client("bedrock-runtime", region_name=region)def create_bedrock_llm(client):return ChatBedrock(model_id='anthropic.claude-3-sonnet-20240229-v1:0', client=client, model_kwargs={'temperature': 0}, region_name='us-east-1')llm = create_bedrock_llm(get_bedrock_client(region='us-east-1'))
工作流程定义了LLM Agent如何与用户交互,何时调用工具,以及如何响应用户输入。这通常通过创建一个包含系统消息和占位符的提示模板来实现。系统消息为Agent提供了指导,而占位符则允许动态插入对话中的消息,这里使用ChatPromptTemplate定义对话模板,并将计算节能的工具绑定到工作流中。
primary_assistant_prompt = ChatPromptTemplate.from_messages([("system",'''You are a helpful customer support assistant for Solar Panels Belgium.You should get the following information from them:- monthly electricity costIf you are not able to discern this info, ask them to clarify! Do not attempt to wildly guess.After you are able to discern all the information, call the relevant tool.''',),("placeholder", "{messages}"),])
LangGraph使用图结构来描述和编排Agent的控制流。在图结构中,节点代表操作(如调用Assistant或工具),边代表这些操作之间的连接。使用LangGraph的API可以创建节点、添加边,并定义条件边以处理不同的执行路径。
builder = StateGraph(State)builder.add_node("assistant", Assistant(part_1_assistant_runnable))builder.add_node("tools", create_tool_node_with_fallback(part_1_tools))builder.add_edge(START, "assistant")# Start with the assistantbuilder.add_conditional_edges("assistant", tools_condition)# Move to tools after inputbuilder.add_edge("tools", "assistant")# Return to assistant after tool executiomemory = MemorySaver()graph = builder.compile(checkpointer=memory)
模拟一个用户与AI助理的对话,以验证整个系统的有效性和功能性。
# import shutilimport uuid# Let's create an example conversation a user might have with the assistanttutorial_questions = ['hey','can you calculate my energy saving',"my montly cost is $100, what will i save"]# Update with the backup file so we can restart from the original place in each section# shutil.copy(backup_file, db)thread_id = str(uuid.uuid4())config = {"configurable": {# The passenger_id is used in our flight tools to# fetch the user's flight information# "passenger_id": "3442 587242",# Checkpoints are accessed by thread_id"thread_id": thread_id,}}_printed = set()for question in tutorial_questions:events = graph.stream({"messages": ("user", question)}, config, stream_mode="values")for event in events:_print_event(event, _printed)
通过上述步骤,我们成功构建了一个基于LangGraph的LLM Agent,用于计算太阳能板的节能效益。这一过程展示了LangGraph在处理复杂、多步骤流程中的强大能力,也说明了如何通过结合先进的AI工具和灵活的图形结构来快速构建高度定制化的智能系统。LangGraph为AI开发者提供了一个强大的平台,帮助他们在各个行业中实现高效、智能的自动化解决方案。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-06-26
拆解Agent Harness的11大核心组件与工程实践(附下载)
2026-06-05
让 Agent 快速上生产:基于 OceanBase 和 LangChain 打造的智能体系统解决方案发布
2026-05-19
90% 的 Agent 失败,不是框架不行,而是卡在 5 个工程问题
2026-05-14
用两行代码将 AgentRun 集成到你的应用
2026-05-06
LangChain 深度智能体(Deep Agents)入门
2026-04-19
万字讲透Agent Harness的十二大模块
2026-04-08
同一个模型,换个Harness排名跳了25位:智能体基础设施完全解剖
2026-03-28
LangChain的DeepAgents子代理实战:复杂任务为什么一定要交给 SubAgent
2026-04-19
2026-04-08
2026-05-06
2026-05-19
2026-05-14
2026-06-05
2026-06-26
2026-03-26
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
欢迎您使用【53AI 官方网站】(以下简称“本网站”或“我们”)。本《会员服务协议》(以下简称“本协议”)是您(以下简称“会员”或“用户”)与【深圳市博思协创网络科技有限公司】之间关于注册、登录及使用本网站会员服务所订立的法律协议。
在您注册或登录前,请务必审慎阅读、充分理解各条款内容,特别是免除或限制责任的条款、知识产权条款、争议解决条款等。此类条款将以加粗形式提示您注意。 当您通过微信公众号授权、手机验证码验证或其他方式成功登录本网站时,即视为您已完全理解并同意接受本协议的全部内容。
一、 定义
本网站:指由【深圳市博思协创网络科技有限公司】运营的,域名为【53ai.com】的网站及相关移动端页面。
会员服务:指本网站向注册会员提供的知识库文章查阅、内容检索及其他相关增值服务。
知识库内容:指本网站发布的包括但不限于文字、图表、数据、研究报告、行业分析等数字化内容资源。
二、 账号注册与登录
登录方式:本网站支持以下登录方式,您可根据实际情况选择:
微信公众号授权登录:您同意将您的微信OpenID信息授权给本网站,用于创建或关联会员账号。
手机验证码登录:您需提供真实有效的手机号码,并通过短信验证码完成身份验证与登录/注册。
账号安全:您的账号仅限您本人使用,禁止赠与、借用、租用、转让或售卖。因您保管不善导致的账号被盗、密码泄露等损失,由您自行承担。
实名认证:根据相关法律法规要求,我们可能要求您在特定功能下完成实名认证。如您拒绝提供,可能无法使用部分或全部服务。
未成年人保护:若您未满18周岁,请在法定监护人的陪同下阅读本协议,并在征得监护人同意后使用本服务。
三、 服务内容与规范
知识库查阅权限:会员登录后,有权按照其会员等级对应的权限范围,在线浏览、检索本网站知识库中的相关文章及内容。
服务变更:我们有权根据业务发展需要,调整、变更或终止部分服务内容,并将以网站公告、公众号消息等方式提前通知。
禁止行为:您在使用服务时不得实施以下行为:
利用技术手段批量爬取、下载、转存知识库内容;
将知识库内容用于商业目的或未经授权地向第三方传播;
干扰本网站正常运行或侵犯其他用户合法权益;
发布违法违规信息或从事违反公序良俗的活动。
四、 知识产权声明
权利归属:本网站知识库中的排版设计、软件代码等内容的知识产权均归【公司全称】或原权利人所有,受《中华人民共和国著作权法》等法律保护。
有限许可:本网站授予会员一项非独占、不可转让、不可转授权的普通许可,仅限于个人学习、研究之目的在线查阅知识库内容。
侵权追责:未经书面许可,任何单位或个人不得以任何形式复制、转载、摘编、镜像、汇编或以其他方式使用上述内容。一经发现,我们保留追究其法律责任的权利。
五、 个人信息保护
我们重视对您个人信息的保护。关于我们如何收集、使用、存储和保护您的个人信息,请单独阅读 《隐私政策》。
您通过微信公众号授权或手机号验证所提供的信息,我们将严格按照《个人信息保护法》的规定处理,仅用于身份识别、服务提供及安全验证等必要用途。
您可以随时通过网站设置或联系客服行使查阅、更正、删除个人信息及撤回授权同意的权利。
六、 免责声明
内容准确性:知识库内容仅供参考,不构成专业建议。我们不对其完整性、准确性、时效性作任何明示或暗示的保证,您应自行判断并承担使用风险。
不可抗力:因自然灾害、政策法规变化、网络故障、第三方平台接口异常(如微信接口维护、运营商短信通道故障)等不可抗力导致的服务中断或延迟,我们不承担违约责任。
第三方链接:本网站可能包含指向第三方网站的链接,该等网站的内容和服务不受我们控制,请您自行甄别风险。
七、 违约责任
如您违反本协议约定,我们有权视情节采取警告、限制功能、暂停服务、注销账号等措施,并保留要求赔偿损失的权利。
如因您的违约行为导致我们遭受行政处罚、第三方索赔或商誉损失,您应承担全部赔偿责任(包括但不限于罚款、赔偿金、律师费、公证费等)。
八、 法律适用与争议解决
本协议的订立、执行和解释均适用中华人民共和国大陆地区法律。
因本协议产生的或与本协议有关的任何争议,双方应友好协商解决;协商不成的,任何一方均可向【公司所在地】有管辖权的人民法院提起诉讼。
九、 其他
本协议构成双方就本服务达成的完整协议,取代此前任何口头或书面约定。
本协议任一条款被认定为无效或不可执行的,不影响其他条款的效力。
我们对本协议享有最终解释权,并在法律允许的范围内保留随时修改的权利。修改后的协议一经公布即生效,继续使用服务即视为同意修订内容。