Please add humanintheloop after checkweather in the following code:import operator from…
- User
Please add human_in_the_loop after check_weather in the following code:import operator from typing import TypedDict from typing_extensions import Annotated from langgraph.graph import StateGraph, END
Define our state class State(TypedDict): messages: Annotated[list, operator.add] location: str weather: str
Create our graph workflow = StateGraph(State)
Define our nodes def greet(state): print("state:",state) return {"messages": [("ai", "Hello! I'm your weather assistant. Where are you located?")]}
def get_location(state): print("state:",state) return {"location": state["messages"][-1][1]}
def check_weather(state): print("state:",state)
In a real app, we'd call a weather API here
weather = "sunny" if "new york" in state["location"].lower() else "rainy" return {"weather": weather}
def report_weather(state): print("state:",state) return {"messages": [ ("ai", f"The weather in {state['location']} is {state['weather']}. Can I help you with anything else?")]}
Add nodes to our graph workflow.add_node("greet", greet) workflow.add_node("get_location", get_location) workflow.add_node("check_weather", check_weather) workflow.add_node("report_weather", report_weather)
Connect our nodes workflow.set_entry_point("greet") workflow.add_edge("greet", "get_location") workflow.add_edge("get_location", "check_weather") workflow.add_edge("check_weather", "report_weather") workflow.add_edge("report_weather", END)
Compile our graph app = workflow.compile()
Run our app inputs = {"messages": [("human", "Hi, I'd like to check the weather.")]} for output in app.stream(inputs): for key, value in output.items(): print(f"{key}: {value}")
Assistant
Okay, to add a human-in-the-loop (HITL) step after
check_weather, we'll use LangGraph's built-in interruption mechanism. This requires a checkpointer. We'll use a simple in-memory checkpointer for this example.Here's how we'll modify the code:
- Import
MemorySaver: This is needed for the interruption to work, as the graph needs to persist its state when interrupted. - Instantiate
MemorySaver: Create an instance of it. - Compile with
interrupt_after: When compiling the graph, we'll specifyinterrupt_after=["check_weather"]and pass thecheckpointer. - Modify the execution loop:
- The first run of
app.stream()will execute nodes up to and includingcheck_weather, then pause. - We'll then explicitly get the current state using
app.get_state(config). - Present the information (e.g., the determined weather) to the "human".
- Get input from the human.
- Optionally, update the state based on human input using
app.update_state(config, {"key_to_update": new_value}). - Resume the graph by calling
app.stream(None, config=config). TheNonetells LangGraph to continue from the interrupted state.
- The first run of
Here's the updated code:
import operator from typing import TypedDict from typing_extensions import Annotated from langgraph.graph import StateGraph, END from langgraph.checkpoint.memory import MemorySaver # Import MemorySaver # Define our state class State(TypedDict): messages: Annotated[list, operator.add] location: str weather: str # Create our graph workflow = StateGraph(State) # Define our nodes def greet(state): print("state (greet):", state) return {"messages": [("ai", "Hello! I'm your weather assistant. Where are you located?")]} def get_location(state): print("state (get_location):", state) # Assuming the last human message contains the location human_messages = [msg[1] for msg in state["messages"] if msg[0] == "human"] if human_messages: return {"location": human_messages[-1]} # Get the content of the last human message return {"location": "Unknown"} # Fallback if no human message found (shouldn't happen in this flow) def check_weather(state): print("state (check_weather):", state) # In a real app, we'd call a weather API here weather = "sunny" if "new york" in state["location"].lower() else "rainy" print(f"AI determined weather for {state['location']} as: {weather}") return {"weather": weather} # This is our new human-in-the-loop step. # In this specific setup with interrupt_after, this node isn't strictly necessary # as the interruption happens *after* check_weather and *before* report_weather. # The HITL logic will be in the main execution block. # However, if you wanted a dedicated node for HITL, you'd define it here. # For now, we'll handle HITL outside the graph nodes using the interrupt. def report_weather(state): print("state (report_weather):", state) return {"messages": [ ("ai", f"The weather in {state['location']} is {state['weather']}. Can I help you with anything else?")]} # Add nodes to our graph workflow.add_node("greet", greet) workflow.add_node("get_location", get_location) workflow.add_node("check_weather", check_weather) workflow.add_node("report_weather", report_weather) # Connect our nodes workflow.set_entry_point("greet") workflow.add_edge("greet", "get_location") workflow.add_edge("get_location", "check_weather") # The graph will interrupt AFTER check_weather. # If resumed, it will then go to report_weather. workflow.add_edge("check_weather", "report_weather") workflow.add_edge("report_weather", END) # Compile our graph with interruption memory_saver = MemorySaver() app = workflow.compile( checkpointer=memory_saver, interrupt_after=["check_weather"] # Interrupt after check_weather node completes ) # Run our app # We need a configurable thread_id for the checkpointer to work with interruptions thread_id = "my-weather-thread-1" config = {"configurable": {"thread_id": thread_id}} print("--- Starting Weather Assistant ---") inputs = {"messages": [("human", "Hi, I'd like to check the weather for New York.")]} # Provide location directly # Initial run - will execute up to the interruption point print("\n--- Running graph until interruption ---") for output in app.stream(inputs, config=config): for key, value in output.items(): print(f"Output from node '{key}':") if isinstance(value, dict) and 'messages' in value and isinstance(value['messages'], list): for role, msg_content in value['messages']: print(f" {role}: {msg_content}") else: print(f" {value}") print("---") print("\n--- Graph execution paused for Human-in-the-Loop ---") # Get the state at the point of interruption current_state = app.get_state(config) print(f"Current state before HITL: {current_state.values}") determined_weather = current_state.values.get("weather", "unknown") current_location = current_state.values.get("location", "unknown") print(f"\nThe AI has determined the weather for '{current_location}' is: {determined_weather}") human_approval = input("Is this correct? (yes/no/override <new_weather>): ").strip().lower() if human_approval == "yes": print("Proceeding with the determined weather.") # No state update needed, just resume elif human_approval.startswith("override "): try: new_weather = human_approval.split(" ", 1)[1] print(f"Overriding weather to: {new_weather}") app.update_state(config, {"weather": new_weather}) except IndexError: print("Invalid override format. Proceeding with original weather.") else: # "no" or anything else not "yes" or "override" print("Human intervention indicated an issue or chose not to proceed with reporting.") print("Stopping the process here as per human input.") # To stop, we simply don't resume the stream. # If you wanted to route to a different part of the graph, # you'd need more complex conditional edges and state updates. # For now, we'll just end. import sys sys.exit() print("\n--- Resuming graph execution ---") # Resume the graph from the interruption point # Pass None as the first argument to stream to indicate resumption for output in app.stream(None, config=config): for key, value in output.items(): print(f"Output from node '{key}':") if isinstance(value, dict) and 'messages' in value and isinstance(value['messages'], list): for role, msg_content in value['messages']: print(f" {role}: {msg_content}") else: print(f" {value}") print("---") print("\n--- Weather Assistant Finished ---") # You can inspect the final state final_state = app.get_state(config) print(f"\nFinal state: {final_state.values}")Explanation of Changes:
MemorySaver: Essential forinterrupt_afterto function. It allows LangGraph to save the state of the graph when it's paused.app = workflow.compile(checkpointer=memory_saver, interrupt_after=["check_weather"]): This tells LangGraph to pause execution right after thecheck_weathernode has finished.config = {"configurable": {"thread_id": thread_id}}: A uniquethread_idis required when using a checkpointer so that LangGraph can manage different conversation states.- First
app.stream(inputs, config=config): This runs the graph. It will executegreet, thenget_location, thencheck_weather. Aftercheck_weatherfinishes, the stream will end because of theinterrupt_afterinstruction. app.get_state(config): Retrieves the current state of the graph for the giventhread_id. This state will include theweatherdetermined bycheck_weather.- Human Interaction (
input(...)): This is where you prompt the human. app.update_state(config, {"weather": new_weather}): If the human provides a correction, this function updates the graph's state for the currentthread_id.- Second
app.stream(None, config=config):- Passing
Noneas the first argument tellsapp.streamto resume execution from the last interruption point for the givenconfig(which contains thethread_id). - The graph will then proceed to the next node connected to
check_weather, which isreport_weather.
- Passing
get_locationfix: I've slightly adjustedget_locationto correctly extract the location from the last human message in themessageslist. I also updated the initial input to provide the location directly for simplicity in this example, as the HITL is focused on weather verification.
This setup provides a clear point for human intervention after the weather has been checked but before it's reported.
- Import
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.