langgraph开发入门初稿——简稿

简化版搭建langgraph

1.定义工具,prompt模板,模型,agent运行类
tools=[tool1,tool2,…]
prompt=…
model=…
agent_runnable = create_openai_functions_agent(llm, tools, prompt)

2.定义agent图的状态
class AgentState:
input: This is the input string representing the main ask from the user, passed in as input.
chat_history This is any previous conversation messages, also passed in as input.
intermediate_steps: This is list of actions and corresponding observations that the agent takes over time. This is updated each iteration of the agent.
agent_outcome: This is the response from the agent, either an AgentAction or AgentFinish. The AgentExecutor should finish when this is an AgentFinish, otherwise it should call the requested tools.

3.定义代理决定是否继续执行和执行工具的函数
def run_agent(data):
决定使用的工具,最后返回使用的tools
def execute_tools(data):
返回工具执行的结果,这一步也是中间步骤的执行结果
def should_continue(data):
条件边缘的执行判定
遇见AgentFinish就不执行工具了

4.开始定义整张图
首先是定义workflow,并起着监视,记录的一些功能
workflow = StateGraph(AgentState)
接下来先添加点,给每个函数加一个名字,设置入口点,再添加线,边缘线
.set_entry_point
.add_node
.add_edge
.add_conditional_edges(两个点之间根据continue的返回结果判定是否继续)

5.最后进行整合编译,然后输入
app = workflow.compile()
inputs = {“input”: “what is the weather in sf”, “chat_history”: []}
for s in app.stream(inputs):
print(list(s.values())[0])
print(“----”)

总结

整体还是很清晰的,整体的思路就是
1.先定义agent的一些基础概念:模型,prompt模板还有用到的工具,最后创建function call格式的对话工具
2.然后定义graph图中间可表示运行状态的东西:有输入,对话历史,中间步骤(工具使用情况),最后还有工具的输出
3.接着定义agent:定义一个要是用什么工具的判断器,定义一个执行工具的函数,最后定义在条件边缘时的判断方法
4.接下来先创建图,就开始定义点,定义线等等,但是因为点和线是没有先后顺序的,所以定义一个入口点,添加条件边也是为了让模型自己选择继不继续采取某一条边,这里以后可扩展性还是很强的。最后启动图。
5.最后就可以正常使用了

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值