Solution ready: update agent.py

This commit is contained in:
2026-06-03 10:15:57 +00:00
parent b8e6520641
commit d4e551e277
+5 -13
View File
@@ -1,14 +1,6 @@
"""
HumanintheLoop agent with tool calling.
This example demonstrates how to use LangChain's `HumanInTheLoopMiddleware` to pause the agent when a tool is about to be called, ask the user for approval, and then resume execution.
The agent uses a simple `get_weather` tool that returns a hardcoded weather string. In a real project you would replace it with an API call.
Run:
python agent.py
Make sure you have `OPENAI_API_KEY` set in your environment.
"""
# HumanintheLoop (HITL) middleware example
# interrupt decision types: approve, edit, reject, respond
# respond decision can be used to supply a human reply as tool result
import os
from typing import List, Dict
@@ -17,7 +9,7 @@ from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langchain.tools import BaseTool
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.checkpoint.memory import MemorySaver
from langgraph.types import Command
# --- Tool ---------------------------------------------------------------
@@ -36,7 +28,7 @@ if not api_key:
llm = ChatOpenAI(api_key=api_key, temperature=0.7)
# --- Agent --------------------------------------------------------------
memory = InMemorySaver()
memory = MemorySaver()
agent = create_agent(
model=llm,