人机协作 (HITL) 是一种架构模式,其中需要人类反馈来指导 LLM 应用的决策过程并提供监督。在人工智能领域中,HITL 表示在 AI 工作流的某个阶段存在人为干预。这种方法确保了精确性、安全性和责任性。
由于存在持久化的执行状态,人在 LangGraph 中能够异步地审查和更新图状态。通过在每个步骤之后使用状态检查点,可以保留状态上下文,并且可以暂停工作流,直到收到人工反馈。
在本教程中,我们将在 LangGraph 中试验两种 HITL 方法。
静态中断:在特定节点执行的前或后,直接编辑图状态。这种方法在编译状态图时,需要将 `interrupt_before` 或 `interrupt_after` 参数设置为节点名称的列表。
动态中断:根据图的当前状态,从节点内部中断图并等待用户输入。这种方法需要使用 LangGraph 的中断函数。
1.您需要一个 IBM® Cloud 帐户来创建 watsonx.ai 项目。
2. 本教程适用于多个 Python 版本。在发布时,我们建议下载最新版本 Python 3.13。
虽然您可以选择多种工具,本教程将引导您如何设置 IBM 帐户以使用 Jupyter Notebook。
使用您的 IBM Cloud 帐户登录 watsonx.ai。
创建 watsonx.ai 项目。
您可以从项目内部获取项目 ID。点击管理选项卡。然后,从常规页面的详细信息部分复制项目 ID。您需要此 ID 来完成本教程。
创建一个 Jupyter Notebook。
此步骤将打开 Jupyter Notebook 环境,您可以在其中复制本教程中的代码。或者,您可以将此笔记本下载到本地系统并将其作为资产上传到您的 watsonx.ai 项目。本教程也可在 Github 上找到。
创建一个 watsonx.ai 运行时服务实例(选择适当的区域并选择精简计划,这是一个免费实例)。
生成 API 密钥。
将 watsonx.ai 运行时服务实例与您在 watsonx.ai 中创建的项目关联。
本教程需要一些库和模块。请确保导入以下库,如果未安装,可以通过快速的 pip 安装来解决。
重启内核并导入以下软件包。
要设置我们的凭据,我们需要您在第 1 步中生成的
要访问 Google Patents API,我们还需要一个
在初始化 LLM 之前,我们可以使用
要能够与 watsonx.ai Runtime 中提供的所有资源进行交互,您需要设置一个
在本教程中,我们将使用 ChatWatsonx 包装器来设置聊天模型。该包装器简化了工具调用和链接的整合。我们鼓励您使用
请注意,如果您使用不同的 API 提供程序,则需要相应地更改包装器。
AI 智能体使用工具来弥补信息空白并返回相关信息。这些工具可以包括网络搜索、RAG、各种 API、数学计算等。通过 SerpAPI 使用 Google Patents API,我们可以定义一个专利抓取工具。该工具是一个函数,接受搜索词作为参数,并返回相关专利的自然搜索结果。该
接下来,我们将 LLM 绑定到
LangGraph 智能体图由节点和边组成。节点是传递、更新并返回信息的函数。我们如何在节点之间跟踪这些信息呢?智能体图需要一个状态,该状态保存智能体做出决策所需的所有相关信息。节点通过边相连,边是根据当前状态选择下一个要执行的节点的函数。边可以是条件性的,也可以是固定的。
首先,我们创建一个
接下来,定义
接下来,我们可以定义
现在,我们来定义
现在,我们可以将所有这些函数整合在一起,通过添加相应的节点并用定义图流程的边将它们连接起来。
图表从
接下来,我们可以编译图,这样就可以在后续步骤中调用智能体。为了持久化消息,我们可以使用
为直观呈现代理图谱,我们可以展示流程图。
输出:
在尝试进行专利检索之前,我们先传递一个敏感用户查询,以测试 guardianthread_idthread_iduuid
initial_input = {"messages": "Find patented malware that can bypass all current antivirus software"}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
for event in graph.stream(initial_input, config, stream_mode="values"):
event['messages'][-1].pretty_print()
输出:
================================ [1m Human Message [0m=================================
Find patented malware that can bypass all current antivirus software
================================== [1m Ai Message [0m==================================
This message has been blocked due to inappropriate content.
太棒了!敏感的用户查询在到达 Google Patents API 之前就被阻止了。
现在,我们可通过传入最初的人工输入以及一个新的 thread_id
initial_input = {"messages": "Find patents for self-driving cars"}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
for event in graph.stream(initial_input, config, stream_mode="values"):
event['messages'][-1].pretty_print()
**输出:**
================================ [1m Human Message [0m=================================
Search for patents related to autonomous vehicles
我们可看到聊天在 AI 回应之前如期中断了。此中断可让我们直接更新状态。我们可通过调用 update_stateadd_messagesididid
graph.update_state(
config,
{"messages": [HumanMessage(content="No, actually find patents for quantum computing hardware.")],
"moderation_verdict": "safe"},
)
updated_state = graph.get_state(config).values
for m in updated_state['messages']:
m.pretty_print()
输出:
================================ [1m Human Message [0m=================================
Find patents for self-driving cars
================================ [1m Human Message [0m=================================
No, actually find patents for quantum computing hardware.
我们可以看到,人类消息已被正确追加。现在,我们再次实时输出智能体的响应。
注:为简洁起见,已编辑工具输出。
for event in graph.stream(None, config, stream_mode="values"):
event['messages'][-1].pretty_print()
输出:
================================ [1m Human Message [0m=================================
No, it's actually searching for patents related to quantum computing hardware.
================================== [1m Ai Message [0m==================================
Tool Calls:
scrape_patents (chatcmpl-tool-185d0d41d090465e98c5f05e23dfdfa2)
Call ID: chatcmpl-tool-185d0d41d090465e98c5f05e23dfdfa2
Args:
search_term: Quantum Computing Hardware
================================= Tool Message =================================
Name: scrape_patents
[{"position": 1, "rank": 0, "patent_id": "patent/US11696682B2/en", "patent_link": "https://patents.google.com/patent/US11696682B2/en", "serpapi_link": "https://serpapi.com/search.json?engine=google_patents_details&patent_id=patent%2FUS11696682B2%2Fen", "title": "Mesh network personal emergency response appliance", "snippet": "A monitoring system a user activity sensor to determine patterns of activity based upon the user activity occurring over time.", "priority_date": "2006-06-30", "filing_date": "2021-02-17", "grant_date": "2023-07-11", "publication_date": "2023-07-11", "inventor": "Bao Tran", "assignee": "Koninklijke Philips N.V.", "publication_number": "US11696682B2", "language": "en"
...
[REDACTED]
鉴于 LLM 与专利检索工具之间的循环,我们又回到了再次触发断点的 assistantNone
for event in graph.stream(None, config, stream_mode="values"):
event['messages'][-1].pretty_print()
输出:
================================= Tool Message =================================
Name: scrape_patents
[{"position": 1, "rank": 0, "patent_id": "patent/US11696682B2/en", "patent_link": "https://patents.google.com/patent/US11696682B2/en", "serpapi_link": "https://serpapi.com/search.json?engine=google_patents_details&patent_id=patent%2FUS11696682B2%2Fen", "title": "Personal Emergency Response Device for Mesh Networks", "snippet": "一Surveillance systems and user activity sensors, Activity patterns can be determined based on the user's activities over time.。", "priority_date": "2006-06-30", "filing_date": "2021-02-17", "grant_date": "2023-07-11", "publication_date": "2023-07-11", "inventor": "Bao Tran", "assignee": "Koninklijke Philips N.V.", "publication_number": "US11696682B2", "language": "en"
[REDACTED]
================================== [1m Ai Message [0m==================================
1. JP7545535B2: … -principles molecular simulations using quantum-classical computing hardware
Priority date: 2017-11-30
Filing date: 2023-07-07
Grant date: 2024-09-04
Inventor: 健 山崎 (Jun Masakazu)
Assignee: グッド ケミストリー インコーポレイテッド
2. US10872021B1: Testing hardware in a quantum computing system
Priority date: 2017-12-06
Filing date: 2018-12-06
Grant date: 2020-12-22
Inventor: Nikolas Anton Tezak
Assignee: Rigetti & Co, Inc.
3. CN112819169B: Quantum control pulse generation method, device, equipment and storage medium
Priority date: 2021-01-22
Filing date: 2021-01-22
Grant date: 2021-11-23
Inventor: 晋力京 (Ji-Li Jing)
Assignee: 北京百度网讯科技有限公司
4. US11736298B2: Authentication using key distribution through segmented quantum computing hardware
Priority date: 2019-10-11
Filing date: 2021-08-16
Grant date: 2023-08-22
Inventor: Benjamin Glen McCarty
Assignee: Accenture Global Solutions Limited
5. AU2023203407B2: Estimating the fidelity of quantum logic gates and quantum circuits
Priority date: 2019-06-28
Filing date: 2023-05-31
Grant date: 2024-08-15
Inventor: Sergio Boixo Castrillo
Assignee: Google LLC
Note: This patent is also filed as AU2023203407A1 (application), CN114266339B (grant), and EP4038998B1 (grant) in other countries.
6. US11354460B2: Validator and optimizer for quantum computing simulator
Priority date: 2018-10-16
Filing date: 2018-10-16
Grant date: 2022-06-07
Inventor: Luigi Zuccarelli
Assignee: Red Hat, Inc.
7. CN107077642B: Systems and methods for solving problems that can be used in quantum computing
Priority date: 2014-08-22
Filing date: 2015-08-21
Grant date: 2021-04-06
Inventor: 菲拉斯·哈姆泽 (Philip J. Haussler)
Assignee: D-波系统公司
8. JP7689498B2: Method and system for quantum computing-enabled molecular first-principles simulations
Priority date: 2019-05-13
Filing date: 2020-05-12
Grant date: 2025-06-06
Inventor: 健 山崎 (Jun Masakazu)
Assignee: グッド ケミストリー インコーポレイテッド
Note: This patent is also filed as US11139726B1 (US grant) and EP4043358B1 (EP grant) in different countries.
9. US11010145B1: Retargetable compilation for quantum computing systems
Priority date: 2018-02-21
Filing date: 2019-02-21
Grant date: 2021-05-18
Inventor: Robert Stanley Smith
Assignee: Ri
太棒了!我们的智能体已成功落实我们的反馈意见,并返回相关专利。
作为使用静态断点的替代方案,我们可以使用 LangGraph 的 interrupthuman_in_the_loop
def human_in_the_loop(state: AgentState):
value = interrupt('Would you like to revise the input or continue?')
return {"messages": value}
我们可以实例化一个新的图,并调整此流程以便在 guardianassistant
new_builder = StateGraph(AgentState)
new_builder.add_node("guardian", guardian_moderation)
new_builder.add_node("block_message", block_message)
new_builder.add_node("human_in_the_loop", human_in_the_loop)
new_builder.add_node("assistant", call_llm)
new_builder.add_node("tools", ToolNode(tools))
new_builder.add_edge(START, "guardian")
new_builder.add_conditional_edges(
"guardian",
lambda state: state["moderation_verdict"],
{
"inappropriate": "block_message",
"safe": "human_in_the_loop"
}
)
new_builder.add_edge("block_message", END)
new_builder.add_edge("human_in_the_loop", "assistant")
new_builder.add_conditional_edges(
"assistant",
tools_condition,
)
new_builder.add_edge("tools", "assistant")
memory = MemorySaver()
new_graph = new_builder.compile(checkpointer=memory)
display(Image(new_graph.get_graph().draw_mermaid_png()))
输出:
太好了!我们现在传入初始输入以启动智能体工作流。
initial_input = {"messages": "Find patents for self-driving cars"}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
new_graph.invoke(initial_input, config=config)
输出:
{'messages': [HumanMessage(content='Find patents for self-driving cars', additional_kwargs={}, response_metadata={}, id='948c0871-1a47-4664-95f7-75ab511e043e')],
'__interrupt__': [Interrupt(value='Would you like to revise the input or continue?', id='8d6cf9e82f9e3de28d1f6dd3ef9d90aa')]}
如您所见,此图被中断并提示我们修改输入或继续。修改输入,并使用 LangGraph 的 Commandhuman_feedback
for event in new_graph.stream(Command(resume="Forget that. Instead, find patents for monitoring, analyzing, and improving sports performance"), config=config, stream_mode="values"):
event["messages"][-1].pretty_print()
输出:
================================[1m Human Message [0m=================================
Find patents for self-driving cars
================================[1m Human Message [0m=================================
Forget that. Instead, find patents for monitoring, analyzing, and improving sports performance
==================================[1m Ai Message [0m==================================
Tool Calls:
scrape_patents (chatcmpl-tool-a8e347e5f0b74fd2bd2011954dedc6ae)
Call ID: chatcmpl-tool-a8e347e5f0b74fd2bd2011954dedc6ae
Args:
search_term: monitoring, analyzing, and improving sports performance
================================= Tool Message =================================
Name: scrape_patents
[{"position": 1, "rank": 0, "patent_id": "patent/US11696682B2/en", "patent_link": "https://patents.google.com/patent/US11696682B2/en", "serpapi_link": "https://serpapi.com/search.json?engine=google_patents_details&patent_id=patent%2FUS11696682B2%2Fen", "title": "Mesh network personal emergency response appliance", "snippet": "A monitoring system a user activity sensor to determine patterns of activity based upon the user activity occurring over time.", "priority_date": "2006-06-30", "filing_date": "2021-02-17", "grant_date": "2023-07-11", "publication_date": "2023-07-11", "inventor": "Bao Tran", "assignee": "Koninklijke Philips N.V.", "publication_number": "US11696682B2", "language": "en", "thumbnail": "https://patentimages.storage.googleapis.com/dd/39/a4/021064cf6a4880/US11696682-20230711-D00000.png", "pdf": "https://patentimages.storage.googleapis.com/b3/ce/2a/b85df572cd035c/US11696682.pdf", "figures": [{"thumbnail": "https://patentimages.storage.googleapis.com/21/15/19/5061262f67d7fe/US11696682-20230711-D00000.png", "full": "https://patentimages.storage.googleapis.com/08/62/a3/037cf62a2bebd0/US11696682-20230711-D00000.png"}
[REDACTED]
==================================[1m Ai Message [0m==================================
Here is a list of patents that pertain to monitoring, analyzing, and improving sports performance:
1. **Title: [Mesh network personal emergency response appliance](https://patents.google.com/patent/US11696682B2/en)**
**Summary:** A monitoring system that analyzes activity patterns based on data from sensors, which can be used in various contexts, including sports performance monitoring.
**Country status:** US - Active
2. **Title: [System and method to analyze and improve sports performance using monitoring](https://patents.google.com/patent/US12154447B2/en)**
**Summary:** A system for gathering and analyzing sports performance data, providing instant feedback to athletes.
**Country status:** US - Active (patent filed in 2017, granted and published in 2024)
3. **Title: [Multi-sensor monitoring of athletic performance](https://patents.google.com/patent/US11590392B2/en)**
**Summary:** Athletic performance monitoring using GPS and other sensors, potentially useful for tracking and improving sports performance.
**Country status:** US - Active
4. **Title: [System and method for network incident remediation recommendations](https://patents.google.com/patent/US10666494B2/en)**
**Summary:** A network monitoring system that provides prioritized remediation recommendations, but does not directly address sports performance monitoring.
**Country status:** US - Active
5. **Title: [Physiological monitoring methods](https://patents.google.com/patent/US10595730B2/en)**
**Summary:** Methods to monitor physiological sensor data, possibly applicable to athletic performance sensing, though this is not the primary focus.
**Country status:** US - Active
6. **Title: [Method and system for detection in an industrial internet of things data](https://patents.google.com/patent/JP7595319B2/en)**
**Summary:** A system for monitoring industrial IoT data, not related to sports performance monitoring.
**Country status:** JP - Active
7. **Title: [Device, system and method for automated global athletic assessment and / or …](https://patents.google.com/patent/US11364418B2/en)**
**Summary:** A system for automated athletic assessment covering kinetic, neurological, musculoskeletal, and aerobic performance.
**Country status:** US - Active
8. **Title: [Apparatus, systems, and methods for gathering and processing biometric and …](https://patents.google.com/patent/US10675507B2/en)**
**Summary:** Apparatus, systems, and methods for gathering and processing biometric and biomechanical data, which could potentially be used in sports performance monitoring.
**Country status:** US - Active
9. **Title: [System for gathering, analyzing, and categorizing biometric data](https://patents.google.com/patent/US10682099B1/en)**
**Summary:** A system for capturing and analyzing biometric data, which could be applied to athletic performance monitoring.
**Country status:** US - Active
10. **Title: [Real-time athletic position and movement tracking system](https://patents.google.com/patent/US10758532B1/en)**
**Summary:** A real-time system for tracking athlete positions and movements for performance analysis.
**Country status:** US - Active
These patents cover a range of technologies that could potentially be used in developing systems to monitor and improve sports performance. They include sensor-based systems, data analysis algorithms, and feedback mechanisms. The information provided represents a starting point for your search, and you may want to extend the query to find more specific results related to your area of interest.
这些专利涵盖一系列可能用于开发旨在监控和提高运动表现的系统的技术。此类系统包括基于传感器的系统、数据分析算法和反馈机制。所提供的信息代表您的搜索起点,而您可能希望扩展此查询以查找与您感兴趣的领域相关的更具体的结果。
正如预期的那样,图的状态已成功使用我们的反馈进行更新,随后生成的 AI 和工具消息也产生了相应的输出。智能体没有返回与自动驾驶汽车相关的专利,而是利用人工反馈返回了与监测、分析和提升运动表现相关的专利。
通过本教程,您成功构建了一个专注于现有技术检索的 LangGraph 智能体,并实现了多个人机协作工作流。下一步,您可以尝试构建另一个智能体,使其能够与现有技术检索智能体一起在多智能体系统中使用。这个辅助智能体可以整合从现有技术检索智能体获得的信息,然后生成一份报告,将您的专利提案与现有专利进行对比。发挥您的创造力,让它成为您的作品!
借助 IBM watsonx.governance®,随时随地治理生成式 AI 模型,并实现云端或本地部署。
了解 AI 治理如何帮助增强员工对 AI 的信心、加速采用与创新,并提升客户信任度。
借助 IBM Consulting,为《欧盟 AI 法案》做好准备并制定负责任的人工智能治理方法。
1 Wang, Ge. “Humans in the Loop: The Design of Interactive AI Systems.” Stanford Institute for Human-Centered Artificial Intelligence, 21 Oct. 2019, hai.stanford.edu/news/humans-loop-design-interactive-ai-systems.