Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e8b7652
[E-4] 15-Agent / 05-IterationFunction(Human-in-the-loop) [Title] Iter…
BaBetterB Jan 13, 2025
272637e
[E-4] 15-Agent / 05-IterationFunction(Human-in-the-loop) [Title] Iter…
BaBetterB Jan 13, 2025
3b3107f
[E-4] 15-Agent / 05-IterationFunction(Human-in-the-loop) [Title] Iter…
BaBetterB Jan 13, 2025
74e38bd
[E-4] 15-Agent / 05-IterationFunction(Human-in-the-loop) [Title] Iter…
BaBetterB Jan 13, 2025
0c8f3d8
[E-4] 15-Agent / 05-IterationFunction(Human-in-the-loop) [Title] Iter…
BaBetterB Jan 13, 2025
f79fc65
[E-4] 15-Agent / 05-Iteration-HumanInTheLoop [Title] Iteration-HumanI…
BaBetterB Jan 13, 2025
e8fbb06
[E-4] 15-Agent / 05-Iteration-HumanInTheLoop [Title] Iteration-HumanI…
BaBetterB Jan 13, 2025
8c123ca
Merge remote-tracking branch 'upstream/main'
BaBetterB Jan 13, 2025
d9f5bf3
file delete
BaBetterB Jan 13, 2025
566952a
Merge branch 'LangChain-OpenTutorial:main' into main
BaBetterB Jan 15, 2025
85854ff
[E-4] 15-Agent / 05-Iteration-HumanInTheLoop [Title] Iteration-HumanI…
BaBetterB Jan 15, 2025
46a0600
[E-4] 13-LCEL / 10-Binding [Title] Binding [Version] Proofread and re…
BaBetterB Jan 16, 2025
5af1aca
Merge branch 'LangChain-OpenTutorial:main' into main
BaBetterB Jan 16, 2025
f1ecedb
Merge branch 'LangChain-OpenTutorial:main' into main
BaBetterB Jan 16, 2025
97791c8
Revert "[E-4] 13-LCEL / 10-Binding [Title] Binding [Version] Proofrea…
BaBetterB Jan 17, 2025
873196c
Merge branch 'main' of https://github.com/BaBetterB/LangChain-OpenTut…
BaBetterB Jan 17, 2025
fa5144b
Merge remote-tracking branch 'upstream/main'
BaBetterB Jan 17, 2025
8bc9bc2
[E-4] 15-Agent / 05-Iteration-HumanInTheLoop [Title] Iteration-human-…
BaBetterB Jan 17, 2025
e19117f
[E-4] 15-Agent / 05-Iteration-HumanInTheLoop [Title] Iteration-human-…
BaBetterB Jan 17, 2025
ed0c21a
[E-4] 15-Agent / 05-Iteration-HumanInTheLoop [Title] Iteration-human-…
BaBetterB Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
362 changes: 362 additions & 0 deletions 15-Agent/05-Iteration-HumanInTheLoop.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Iteration-human-in-the-loop\n",
"\n",
"- Author: [Wonyoung Lee](https://github.com/BaBetterB)\n",
"- Peer Review: \n",
"- This is a part of [LangChain Open Tutorial](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial)\n",
"\n",
"[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/BaBetterB/LangChain-OpenTutorial/blob/main/15-Agent/05-Iteration-HumanInTheLoop.ipynb)\n",
"[![Open in GitHub](https://img.shields.io/badge/Open%20in%20GitHub-181717?style=flat-square&logo=github&logoColor=white)](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial/blob/main/07-TextSplitter/04-SemanticChunker.ipynb)\n",
"\n",
"\n",
"## Overview\n",
"\n",
"This tutorial covers the functionality of repeating the agent's execution process or receiving user input to decide whether to proceed during intermediate steps. \n",
"\n",
"The feature of asking the user whether to continue during the agent's execution process is called human-in-the-loop . \n",
"\n",
"The `iter()` method creates an iterator that allows you to step through the agent's execution process step-by-step.\n",
"\n",
"\n",
"### Table of Contents\n",
"\n",
"- [Overview](#overview)\n",
"- [Environement Setup](#environment-setup)\n",
"- [AgentExecutor](#agentexecutor)\n",
"\n",
"\n",
"\n",
"### References\n",
"\n",
"\n",
"- [LangChain ChatOpenAI API reference](https://python.langchain.com/api_reference/openai/chat_models/langchain_openai.chat_models.base.ChatOpenAI.html)\n",
"- [LangChain AgentExecutor API reference](https://python.langchain.com/api_reference/langchain/agents/langchain.agents.agent.AgentExecutor.html)\n",
"- [LangSmith API reference](https://docs.smith.langchain.com/)\n",
"\n",
"----\n",
"\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Environment Setup\n",
"\n",
"Set up the environment. You may refer to [Environment Setup](https://wikidocs.net/257836) for more details.\n",
"\n",
"**[Note]**\n",
"- `langchain-opentutorial` is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials. \n",
"- You can checkout the [ `langchain-opentutorial` ](https://github.com/LangChain-OpenTutorial/langchain-opentutorial-pypi) for more details."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load sample text and output the content."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"[notice] A new release of pip is available: 24.2 -> 24.3.1\n",
"[notice] To update, run: python.exe -m pip install --upgrade pip\n"
]
}
],
"source": [
"%%capture --no-stderr\n",
"%pip install langchain-opentutorial"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Install required packages\n",
"from langchain_opentutorial import package\n",
"\n",
"\n",
"package.install(\n",
" [\n",
" \"langsmith\",\n",
" \"langchain\",\n",
" \"langchain_core\",\n",
" \"langchain_community\",\n",
" \"load_dotenv\",\n",
" \"langchain_openai\",\n",
" ],\n",
" verbose=False,\n",
" upgrade=False,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Environment variables have been set successfully.\n"
]
}
],
"source": [
"# Set environment variables\n",
"from langchain_opentutorial import set_env\n",
"\n",
"set_env(\n",
" {\n",
" \"OPENAI_API_KEY\": \"\",\n",
" \"LANGCHAIN_API_KEY\": \"\",\n",
" \"LANGCHAIN_TRACING_V2\": \"true\",\n",
" \"LANGCHAIN_ENDPOINT\": \"https://api.smith.langchain.com\",\n",
" \"LANGCHAIN_PROJECT\": \"Iteration-human-in-the-loop\", # title\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can alternatively set `OPENAI_API_KEY` in `.env` file and load it.\n",
"\n",
"[Note] This is not necessary if you've already set `OPENAI_API_KEY` in previous steps."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Configuration File for Managing API Keys as Environment Variables\n",
"from dotenv import load_dotenv\n",
"\n",
"# Load API Key Information\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, define the tool."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import tool\n",
"\n",
"\n",
"@tool\n",
"def add_function(a: float, b: float) -> float:\n",
" \"\"\"Adds two numbers together.\"\"\"\n",
"\n",
" return a + b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, define an agent that performs addition calculations using `add_function`."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain.agents import create_tool_calling_agent, AgentExecutor\n",
"\n",
"# Define tools\n",
"tools = [add_function]\n",
"\n",
"# Create LLM\n",
"gpt = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"# Create prompt\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant.\"\n",
" \"Please avoid LaTeX-style formatting and use plain symbols.\",\n",
" ),\n",
" (\"human\", \"{input}\"),\n",
" MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n",
" ]\n",
")\n",
"\n",
"# Create Agent\n",
"gpt_agent = create_tool_calling_agent(gpt, tools, prompt)\n",
"\n",
"# Create AgentExecutor\n",
"agent_executor = AgentExecutor(\n",
" agent=gpt_agent,\n",
" tools=tools,\n",
" verbose=False,\n",
" max_iterations=10,\n",
" handle_parsing_errors=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## AgentExecutor\n",
"\n",
"This method creates an iterator (`AgentExecutorIterator` ) that allows you to step through the agent's execution process.\n",
"\n",
"**Function Description**\n",
"The `iter()` method returns an `AgentExecutorIterator` object that provides sequential access to each step the agent takes until reaching the final output.\n",
"\n",
"**Key Features**\n",
"- **Step-by-step execution access** : Enables you to examine the agent's execution process step-by-step.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Flow Overview**\n",
"\n",
"To perform the addition calculation for `\"114.5 + 121.2 + 34.2 + 110.1\"`, the steps are executed as follows:\n",
"\n",
"1. 114.5 + 121.2 = 235.7\n",
"2. 235.7 + 34.2 = 269.9\n",
"3. 269.9 + 110.1 = 380.0\n",
"\n",
"You can observe each step in this calculation process.\n",
"\n",
"During this process, the system displays the intermediate calculation results to the user and asks if they want to continue. (**Human-in-the-loop**)\n",
"\n",
"If the user inputs anything other than 'y', the iteration stops.\n",
"\n",
"In practice, while calculating 114.5 + 121.2 = 235.7, 34.2 + 110.1 = 144.3 is also calculated simultaneously.\n",
"\n",
"Then, the result of 235.7 + 144.3 = 380.0 is calculated as the second step.\n",
"\n",
"This process can be observed when `verbose=True` is set in the `AgentExecutor`.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tool Name: add_function, Execution Result: 235.7\n",
"Tool Name: add_function, Execution Result: 380.0\n",
"Final result: The result of 114.5 + 121.2 + 34.2 + 110.1 is 380.0.\n"
]
}
],
"source": [
"# Define the user input question\n",
"question = \"What is the result of 114.5 + 121.2 + 34.2 + 110.1?\"\n",
"\n",
"\n",
"# Flag to track if the calculation is stopped\n",
"calculation_stopped = False\n",
"\n",
"# Use AgentExecutor's iter() method to run step-by-step execution\n",
"for step in agent_executor.iter({\"input\": question}):\n",
" # Access each calculation step through intermediate_step\n",
" if output := step.get(\"intermediate_step\"):\n",
" action, value = output[0]\n",
"\n",
" # Print the result of each calculation step\n",
" if action.tool == \"add_function\":\n",
" print(f\"Tool Name: {action.tool}, Execution Result: {value}\")\n",
"\n",
" # Ask the user whether to continue\n",
" while True:\n",
" _continue = input(\"Do you want to continue? (y/n):\").strip().lower()\n",
" if _continue in [\"y\", \"n\"]:\n",
" if _continue == \"n\":\n",
" print(f\"Calculation stopped. Last computed result: {value}\")\n",
" calculation_stopped = True # Set flag to indicate calculation stop\n",
" break # Break from the loop to stop calculation\n",
" break # Break the inner while loop after valid input\n",
" else:\n",
" print(\"Invalid input! Please enter 'y' or 'n'.\")\n",
"\n",
" # Exit the iteration if the calculation is stopped\n",
" if calculation_stopped:\n",
" break\n",
"\n",
"# Print the final result\n",
"if \"output\" in step:\n",
" print(f\"Final result: {step['output']}\")\n",
"else:\n",
" print(f\"Final result (from last computation): {value}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "langchain-opentutorial-HDS-w_h3-py3.11",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading