This tutorial covers the functionality of repeating the agent's execution process or receiving user input to decide whether to proceed during intermediate steps.
The feature of asking the user whether to continue during the agent's execution process is called human-in-the-loop .
The iter() method creates an iterator that allows you to step through the agent's execution process step-by-step.
# Set environment variables
from langchain_opentutorial import set_env
set_env(
{
"OPENAI_API_KEY": "",
"LANGCHAIN_API_KEY": "",
"LANGCHAIN_TRACING_V2": "true",
"LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
"LANGCHAIN_PROJECT": "Iteration-human-in-the-loop", # title
}
)
Environment variables have been set successfully.
You can alternatively set OPENAI_API_KEY in .env file and load it.
[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.
# Configuration File for Managing API Keys as Environment Variables
from dotenv import load_dotenv
# Load API Key Information
load_dotenv(override=True)
True
First, define the tool.
from langchain.agents import tool
@tool
def add_function(a: float, b: float) -> float:
"""Adds two numbers together."""
return a + b
Next, define an agent that performs addition calculations using add_function.
This method creates an iterator (AgentExecutorIterator ) that allows you to step through the agent's execution process.
Function Description The iter() method returns an AgentExecutorIterator object that provides sequential access to each step the agent takes until reaching the final output.
Key Features
Step-by-step execution access : Enables you to examine the agent's execution process step-by-step.
Flow Overview
To perform the addition calculation for "114.5 + 121.2 + 34.2 + 110.1", the steps are executed as follows:
114.5 + 121.2 = 235.7
235.7 + 34.2 = 269.9
269.9 + 110.1 = 380.0
You can observe each step in this calculation process.
During this process, the system displays the intermediate calculation results to the user and asks if they want to continue. (Human-in-the-loop)
If the user inputs anything other than 'y', the iteration stops.
In practice, while calculating 114.5 + 121.2 = 235.7, 34.2 + 110.1 = 144.3 is also calculated simultaneously.
Then, the result of 235.7 + 144.3 = 380.0 is calculated as the second step.
This process can be observed when verbose=True is set in the AgentExecutor.
# Define the user input question
question = "What is the result of 114.5 + 121.2 + 34.2 + 110.1?"
# Flag to track if the calculation is stopped
calculation_stopped = False
# Use AgentExecutor's iter() method to run step-by-step execution
for step in agent_executor.iter({"input": question}):
# Access each calculation step through intermediate_step
if output := step.get("intermediate_step"):
action, value = output[0]
# Print the result of each calculation step
if action.tool == "add_function":
print(f"Tool Name: {action.tool}, Execution Result: {value}")
# Ask the user whether to continue
while True:
_continue = input("Do you want to continue? (y/n):").strip().lower()
if _continue in ["y", "n"]:
if _continue == "n":
print(f"Calculation stopped. Last computed result: {value}")
calculation_stopped = True # Set flag to indicate calculation stop
break # Break from the loop to stop calculation
break # Break the inner while loop after valid input
else:
print("Invalid input! Please enter 'y' or 'n'.")
# Exit the iteration if the calculation is stopped
if calculation_stopped:
break
# Print the final result
if "output" in step:
print(f"Final result: {step['output']}")
else:
print(f"Final result (from last computation): {value}")
Tool Name: add_function, Execution Result: 235.7
Tool Name: add_function, Execution Result: 380.0
Final result: The result of 114.5 + 121.2 + 34.2 + 110.1 is 380.0.