当前位置: 首页 > wzjs >正文

一个专门做试题的网站微信营销软件群发

一个专门做试题的网站,微信营销软件群发,用thinkphp做的网站,做零售去哪个外贸网站目录 1. 引言2. 添加Human Assistance工具3. 编译状态图4. 提示聊天机器人5. 恢复执行参考 1. 引言 智能体可能不可靠,甚至需要人工输入才能完成任务。同样,对于某些操作,你可能需要在运行前获得人工批准,以保证一切按预期运行。 …

目录

  • 1. 引言
  • 2. 添加Human Assistance工具
  • 3. 编译状态图
  • 4. 提示聊天机器人
  • 5. 恢复执行
  • 参考

1. 引言

  智能体可能不可靠,甚至需要人工输入才能完成任务。同样,对于某些操作,你可能需要在运行前获得人工批准,以保证一切按预期运行。
  LangGraph的持久层支持人机交互工作流,允许根据用户反馈暂停和恢复执行。此功能的主要接口是interrupt函数。在节点内部调用Interrupt将暂停执行。可以通过传入command来interrupt执行,并接收新的人工输入。interrupt在人机工程学上类似于Python的内置input(),但也有一些注意事项。

2. 添加Human Assistance工具

  初始化聊天模型:

from langchain.chat_models import init_chat_modelllm = init_chat_model("deepseek:deepseek-chat"
)

  使用附加工具将human assistance附加到状态图中:

from typing import Annotatedfrom langchain_tavily import TavilySearch
from langchain_core.tools import tool
from typing_extensions import TypedDictfrom langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_conditionfrom langgraph.types import Command, interruptclass State(TypedDict):messages: Annotated[list, add_messages]graph_builder = StateGraph(State)@tool
def human_assistance(query: str) -> str:"""Request assistance from a human."""human_response = interrupt({"query": query})return human_response["data"]tool = TavilySearch(max_results=2)
tools = [tool, human_assistance]
llm_with_tools = llm.bind_tools(tools)def chatbot(state: State):message = llm_with_tools.invoke(state["messages"])# Because we will be interrupting during tool execution,# we disable parallel tool calling to avoid repeating any# tool invocations when we resume.assert len(message.tool_calls) <= 1return {"messages": [message]}graph_builder.add_node("chatbot", chatbot)tool_node = ToolNode(tools=tools)
graph_builder.add_node("tools", tool_node)graph_builder.add_conditional_edges("chatbot",tools_condition,
)
graph_builder.add_edge("tools", "chatbot")
graph_builder.add_edge(START, "chatbot")

3. 编译状态图

  使用检查点编译状态图:

memory = MemorySaver()graph = graph_builder.compile(checkpointer=memory)

4. 提示聊天机器人

  向聊天机器人提出一个问题,该问题将使用human assistance工具:

user_input = "I need some expert guidance for building an AI agent. Could you request assistance for me?"
config = {"configurable": {"thread_id": "1"}}events = graph.stream({"messages": [{"role": "user", "content": user_input}]},config,stream_mode="values",
)
for event in events:if "messages" in event:event["messages"][-1].pretty_print()

  运行结果为:
在这里插入图片描述
  聊天机器人生成了一个工具调用,但随后执行被中断。如果你检查状态图,会发现它在工具节点处停止了:

snapshot = graph.get_state(config)
snapshot.next

  运行结果为:

('tools',)

5. 恢复执行

  要恢复执行需要传递一个包含工具所需数据的Command对象。此数据的格式可根据需要自定义。在本例中,使用一个带有键”data"字典:

human_response = ("We, the experts are here to help! We'd recommend you check out LangGraph to build your agent."" It's much more reliable and extensible than simple autonomous agents."
)human_command = Command(resume={"data": human_response})events = graph.stream(human_command, config, stream_mode="values")
for event in events:if "messages" in event:event["messages"][-1].pretty_print()

  运行结果为:

================================== Ai Message ==================================
Tool Calls:human_assistance (call_0_cee258cf-15db-49d4-8495-46761c7ddc65)Call ID: call_0_cee258cf-15db-49d4-8495-46761c7ddc65Args:query: I need expert guidance for building an AI agent.
================================= Tool Message =================================
Name: human_assistanceWe, the experts are here to help! We'd recommend you check out LangGraph to build your agent. It's much more reliable and extensible than simple autonomous agents.
================================== Ai Message ==================================Great! It seems the experts recommend using **LangGraph** for building your AI agent, as it is more reliable and extensible compared to simple autonomous agents. If you'd like, I can provide more details about LangGraph or assist you with specific steps to get started. Let me know how you'd like to proceed!

参考

https://langchain-ai.github.io/langgraph/tutorials/get-started/4-human-in-the-loop/

http://www.dtcms.com/wzjs/245187.html

相关文章:

  • 怎么自建设部网站查询公司资质企业推广策划方案
  • 吉林省水土保持生态建设网站网站seo推广seo教程
  • 企业备案信息哪里可以查优化大师免费安装下载
  • 网站解析后几天可以访问网站推广优化怎么做最好
  • 新网站多久会被百度收录海口网站排名提升
  • 银行的网站做的真垃圾全网自媒体平台
  • 自己做的网站放在服务器哪里黑科技引流工具
  • 消费者联盟网站怎么做品牌设计公司
  • 免费做网站的好不好seo关键词快速排名介绍
  • 诸城做网站域名查询服务器
  • 重庆网站建设快忻科技中国搜索引擎市场份额
  • go做网站seo服务加盟
  • 专业网站建设维护是什么免费网站建设哪个好
  • 外贸网站有什么新媒体营销六种方式
  • 淘宝网站怎么做网站站长工具网
  • 个人门户网站微信广告平台推广
  • 网站建设优化公司哪家好微信seo排名优化软件
  • WordPress文章投票seo诊断站长
  • 开发网站app公司调研报告万能模板
  • 湖州网站建设哪家好长沙seo袁飞
  • 简单的网站首页模板seo优化或网站编辑
  • wordpress资讯主题模板手机端seo
  • 珠海华中建设工程有限公司网站查询网
  • asp建网站站长网站推广
  • 备案网站多长时间磁力搜索器在线
  • 免费做一建或二建题目的网站做做网站
  • 网站搭建后显示建设中海外seo培训
  • 企业网站导航设计window优化大师官网
  • 上海市建设协会考试网站杭州seo公司排名
  • 做外贸的网站有哪几个网站制作的流程是什么