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 (HITL) middleware example
HumanintheLoop agent with tool calling. # interrupt decision types: approve, edit, reject, respond
# respond decision can be used to supply a human reply as tool result
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.
"""
import os import os
from typing import List, Dict from typing import List, Dict
@@ -17,7 +9,7 @@ from langchain_openai import ChatOpenAI
from langchain.agents import create_agent from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware from langchain.agents.middleware import HumanInTheLoopMiddleware
from langchain.tools import BaseTool from langchain.tools import BaseTool
from langgraph.checkpoint.memory import InMemorySaver from langgraph.checkpoint.memory import MemorySaver
from langgraph.types import Command from langgraph.types import Command
# --- Tool --------------------------------------------------------------- # --- Tool ---------------------------------------------------------------
@@ -36,7 +28,7 @@ if not api_key:
llm = ChatOpenAI(api_key=api_key, temperature=0.7) llm = ChatOpenAI(api_key=api_key, temperature=0.7)
# --- Agent -------------------------------------------------------------- # --- Agent --------------------------------------------------------------
memory = InMemorySaver() memory = MemorySaver()
agent = create_agent( agent = create_agent(
model=llm, model=llm,