企业宣传,产品推广,广告招商,广告投放联系seowdb

从LangChain更新LangGraph 大幅优化智能体性能

智能体开发畛域正在迅速开展,LangChain也随之一直演化退化。只管传统的LangChain智能体(尤其是基于AgentExecutor构建的)曾经提供了稳固的服务,但LangGraph的产生带来了更为弱小和灵敏的处置打算。

本文指点读者如何将智能体迁徙至LangGraph,使迁徙后的智能体能够充沛应用LangGraph的最新技术长处。

1 传统LangChain与LangGraph

传统LangChain智能体是基于AgentExecutor类构建的,为LangChain平台中的智能体开发提供了一种结构化的方法,并为智能体的行为提供了片面的性能选项。

LangGraph代表了LangChain智能体开发的新纪元。它赋予了开发者构建高度定制化和可控智能体的才干。与之前的版本相比,LangGraph提供了更为精细的控制才干。

2 为什么迁徙至LangGraph

迁徙至LangGraph可以解锁多个好处:

3 代码成功

上方是将传统LangChain智能体迁徙到LangGraph所需的代码级别更改。

步骤I:装置库

pip install -U langgraph langchain langchain-openai

步骤II:智能体的基本经常使用

from langchain.agents import AgentExecutor, create_tool_calling_agentfrom langchain.memory import ChatMessageHistoryfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables.history import RunnableWithMessageHistoryfrom langchain_core.tools import toolfrom langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-4o")memory = ChatMessageHistory(session_id="test-session")prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant."),# First put the history("placeholder", "{chat_history}"),# Then the new input("human", "{input}"),# Finally the scratchpad("placeholder", "{agent_scratchpad}"),])@tooldef magic_function(input: int) -> int:"""Applies a magic function to an input."""return input + 2tools = [magic_function]agent = create_tool_calling_agent(model, tools, prompt)agent_executor = AgentExecutor(agent=agent, tools=tools)agent_with_chat_history = RunnableWithMessageHistory(agent_executor,# 这是必须的,由于在大少数事实场景中,须要一个会话ID# 但在这里没有真正经常使用,由于经常使用的是便捷的内存ChatMessageHistorylambda session_id: memory,input_messages_key="input",history_messages_key="chat_history",)config = {"configurable": {"session_id": "test-session"}}print(agent_with_chat_history.invoke({"input": "Hi, I'm polly! What's the output of magic_function of 3?"}, config)["output"])print("---")print(agent_with_chat_history.invoke({"input": "Remember my name?"}, config)["output"])print("---")print(agent_with_chat_history.invoke({"input": "what was that output again?"}, config)["output"])# 输入Hi Polly! The output of the magic function for the input 3 is 5.---Yes, I remember your name, Polly! How can I assist you further?---The output of the magic function for the input 3 is 5.

步骤III:LangGraph的智能体形态治理

from langchain_core.messages import SystemMessagefrom langgraph.checkpoint import MemorySaver# 内存中的审核点保留器from langgraph.prebuilt import create_react_agentsystem_message = "You are a helpful assistant."# 这也可以是一个SystemMessage对象# system_message = SystemMessage(content="You are a helpful assistant. Respond only in Spanish.")memory = MemorySaver()app = create_react_agent(model, tools, messages_modifier=system_message, checkpointer=memory)config = {"configurable": {"thread_id": "test-thread"}}print(app.invoke({"messages": [("user", "Hi, I'm polly! What's the output of magic_function of 3?")]},config,)["messages"][-1].content)print("---")print(app.invoke({"messages": [("user", "Remember my name?")]}, config)["messages"][-1].content)print("---")print(app.invoke({"messages": [("user", "what was that output again?")]}, config)["messages"][-1].content)# 输入Hi Polly! The output of the magic_function for the input 3 is 5.---Yes, your name is Polly!---The output of the magic_function for the input 3 was 5.

4 结语

迁徙至LangGraph的智能体会取得更深档次的才干和灵敏性。依照既定步骤并了解系统信息的概念,将有助于成功平滑过渡,并优化智能体的性能体现。

本文转载自​​,作者:

© 版权声明
评论 抢沙发
加载中~
每日一言
不怕万人阻挡,只怕自己投降
Not afraid of people blocking, I'm afraid their surrender