diff --git a/docs/modules/agents/examples/human_tools.ipynb b/docs/modules/agents/examples/human_tools.ipynb index 49fdc65759..498c471c7d 100644 --- a/docs/modules/agents/examples/human_tools.ipynb +++ b/docs/modules/agents/examples/human_tools.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -13,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -26,9 +25,7 @@ "math_llm = OpenAI(temperature=0.0)\n", "tools = load_tools(\n", " [\"human\", \"llm-math\"], \n", - " llm=math_llm, \n", - " prompt_func=lambda s: sys.stdout.write(s + \"\\n\"), \n", - " input_func=input,\n", + " llm=math_llm,\n", ")\n", "\n", "agent_chain = initialize_agent(\n", @@ -40,7 +37,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -50,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -60,243 +56,29 @@ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\n", - "\n", - "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", - "Prompt after formatting:\n", - "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n", - "\n", - "Human: A human. Useful for clarification, confirmation, or anything other tools cannot help.Input should be a question you need help with.\n", - "Calculator: Useful for when you need to answer questions about math.\n", - "\n", - "Use the following format:\n", - "\n", - "Question: the input question you must answer\n", - "Thought: you should always think about what to do\n", - "Action: the action to take, should be one of [Human, Calculator]\n", - "Action Input: the input to the action\n", - "Observation: the result of the action\n", - "... (this Thought/Action/Action Input/Observation can repeat N times)\n", - "Thought: I now know the final answer\n", - "Final Answer: the final answer to the original input question\n", - "\n", - "Begin!\n", - "\n", - "Question: What is Eric Zhu's birthday?\n", - "Thought:\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mI don't have any information on Eric Zhu's birthday, so I'll need to ask a human.\n", + "\u001b[32;1m\u001b[1;3mI don't know Eric Zhu, so I should ask a human for guidance.\n", "Action: Human\n", - "Action Input: \"Do you know Eric Zhu's birthday?\"\u001b[0mDo you know Eric Zhu's birthday?\n", + "Action Input: \"Do you know when Eric Zhu's birthday is?\"\u001b[0m\n", "\n", - "Observation: \u001b[36;1m\u001b[1;3mYes\u001b[0m\n", - "Thought:\n", + "Do you know when Eric Zhu's birthday is?\n", + "last week\n", "\n", - "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", - "Prompt after formatting:\n", - "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n", - "\n", - "Human: A human. Useful for clarification, confirmation, or anything other tools cannot help.Input should be a question you need help with.\n", - "Calculator: Useful for when you need to answer questions about math.\n", - "\n", - "Use the following format:\n", - "\n", - "Question: the input question you must answer\n", - "Thought: you should always think about what to do\n", - "Action: the action to take, should be one of [Human, Calculator]\n", - "Action Input: the input to the action\n", - "Observation: the result of the action\n", - "... (this Thought/Action/Action Input/Observation can repeat N times)\n", - "Thought: I now know the final answer\n", - "Final Answer: the final answer to the original input question\n", - "\n", - "Begin!\n", - "\n", - "Question: What is Eric Zhu's birthday?\n", - "Thought:I don't have any information on Eric Zhu's birthday, so I'll need to ask a human.\n", + "Observation: \u001b[36;1m\u001b[1;3mlast week\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mThat's not very helpful. I should ask for more information.\n", "Action: Human\n", - "Action Input: \"Do you know Eric Zhu's birthday?\"\n", - "Observation: Yes\n", - "Thought:\u001b[0m\n", + "Action Input: \"Do you know the specific date of Eric Zhu's birthday?\"\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mGreat, now I just need to wait for the human to provide the answer.\n", - "Action: Human\n", - "Action Input: \"What is Eric Zhu's birthday?\"\u001b[0mWhat is Eric Zhu's birthday?\n", + "Do you know the specific date of Eric Zhu's birthday?\n", + "august 1st\n", "\n", - "Observation: \u001b[36;1m\u001b[1;3mIt's today\u001b[0m\n", - "Thought:\n", - "\n", - "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", - "Prompt after formatting:\n", - "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n", - "\n", - "Human: A human. Useful for clarification, confirmation, or anything other tools cannot help.Input should be a question you need help with.\n", - "Calculator: Useful for when you need to answer questions about math.\n", - "\n", - "Use the following format:\n", - "\n", - "Question: the input question you must answer\n", - "Thought: you should always think about what to do\n", - "Action: the action to take, should be one of [Human, Calculator]\n", - "Action Input: the input to the action\n", - "Observation: the result of the action\n", - "... (this Thought/Action/Action Input/Observation can repeat N times)\n", - "Thought: I now know the final answer\n", - "Final Answer: the final answer to the original input question\n", - "\n", - "Begin!\n", - "\n", - "Question: What is Eric Zhu's birthday?\n", - "Thought:I don't have any information on Eric Zhu's birthday, so I'll need to ask a human.\n", - "Action: Human\n", - "Action Input: \"Do you know Eric Zhu's birthday?\"\n", - "Observation: Yes\n", - "Thought:Great, now I just need to wait for the human to provide the answer.\n", - "Action: Human\n", - "Action Input: \"What is Eric Zhu's birthday?\"\n", - "Observation: It's today\n", - "Thought:\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mI need to confirm if the human is sure about the answer.\n", - "Action: Human\n", - "Action Input: \"Are you sure it's today?\"\u001b[0mAre you sure it's today?\n", - "\n", - "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n", - "Thought:\n", - "\n", - "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", - "Prompt after formatting:\n", - "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n", - "\n", - "Human: A human. Useful for clarification, confirmation, or anything other tools cannot help.Input should be a question you need help with.\n", - "Calculator: Useful for when you need to answer questions about math.\n", - "\n", - "Use the following format:\n", - "\n", - "Question: the input question you must answer\n", - "Thought: you should always think about what to do\n", - "Action: the action to take, should be one of [Human, Calculator]\n", - "Action Input: the input to the action\n", - "Observation: the result of the action\n", - "... (this Thought/Action/Action Input/Observation can repeat N times)\n", - "Thought: I now know the final answer\n", - "Final Answer: the final answer to the original input question\n", - "\n", - "Begin!\n", - "\n", - "Question: What is Eric Zhu's birthday?\n", - "Thought:I don't have any information on Eric Zhu's birthday, so I'll need to ask a human.\n", - "Action: Human\n", - "Action Input: \"Do you know Eric Zhu's birthday?\"\n", - "Observation: Yes\n", - "Thought:Great, now I just need to wait for the human to provide the answer.\n", - "Action: Human\n", - "Action Input: \"What is Eric Zhu's birthday?\"\n", - "Observation: It's today\n", - "Thought:I need to confirm if the human is sure about the answer.\n", - "Action: Human\n", - "Action Input: \"Are you sure it's today?\"\n", - "Observation: \n", - "Thought:\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mThe human confirmed that it's today, but I still need to verify it with a calculator.\n", + "Observation: \u001b[36;1m\u001b[1;3maugust 1st\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mNow that I have the date, I can check if it's a leap year or not.\n", "Action: Calculator\n", - "Action Input: Today's date\u001b[0m\n", - "\n", - "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n", - "Today's date\n", - "\n", - "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", - "Prompt after formatting:\n", - "\u001b[32;1m\u001b[1;3mTranslate a math problem into Python code that can be executed in Python 3 REPL. Use the output of running this code to answer the question.\n", - "\n", - "Question: ${Question with math problem.}\n", - "```python\n", - "${Code that solves the problem and prints the solution}\n", - "```\n", - "```output\n", - "${Output of running the code}\n", - "```\n", - "Answer: ${Answer}\n", - "\n", - "Begin.\n", - "\n", - "Question: What is 37593 * 67?\n", - "\n", - "```python\n", - "print(37593 * 67)\n", - "```\n", - "```output\n", - "2518731\n", - "```\n", - "Answer: 2518731\n", - "\n", - "Question: Today's date\n", + "Action Input: \"Is 2021 a leap year?\"\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3mAnswer: False\n", "\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m\n", - "```python\n", - "import datetime\n", - "\n", - "print(datetime.date.today())\n", - "```\n", - "\u001b[0m\n", - "Answer: \u001b[33;1m\u001b[1;3m2023-03-22\n", - "\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\n", - "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2023-03-22\n", - "\u001b[0m\n", - "Thought:\n", - "\n", - "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", - "Prompt after formatting:\n", - "\u001b[32;1m\u001b[1;3mAnswer the following questions as best you can. You have access to the following tools:\n", - "\n", - "Human: A human. Useful for clarification, confirmation, or anything other tools cannot help.Input should be a question you need help with.\n", - "Calculator: Useful for when you need to answer questions about math.\n", - "\n", - "Use the following format:\n", - "\n", - "Question: the input question you must answer\n", - "Thought: you should always think about what to do\n", - "Action: the action to take, should be one of [Human, Calculator]\n", - "Action Input: the input to the action\n", - "Observation: the result of the action\n", - "... (this Thought/Action/Action Input/Observation can repeat N times)\n", - "Thought: I now know the final answer\n", - "Final Answer: the final answer to the original input question\n", - "\n", - "Begin!\n", - "\n", - "Question: What is Eric Zhu's birthday?\n", - "Thought:I don't have any information on Eric Zhu's birthday, so I'll need to ask a human.\n", - "Action: Human\n", - "Action Input: \"Do you know Eric Zhu's birthday?\"\n", - "Observation: Yes\n", - "Thought:Great, now I just need to wait for the human to provide the answer.\n", - "Action: Human\n", - "Action Input: \"What is Eric Zhu's birthday?\"\n", - "Observation: It's today\n", - "Thought:I need to confirm if the human is sure about the answer.\n", - "Action: Human\n", - "Action Input: \"Are you sure it's today?\"\n", - "Observation: \n", - "Thought:The human confirmed that it's today, but I still need to verify it with a calculator.\n", - "Action: Calculator\n", - "Action Input: Today's date\n", - "Observation: Answer: 2023-03-22\n", - "\n", - "Thought:\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mI now know the final answer\n", - "Final Answer: Eric Zhu's birthday is March 22, 2023.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mI have all the information I need to answer the original question.\n", + "Final Answer: Eric Zhu's birthday is on August 1st and it is not a leap year in 2021.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -304,10 +86,10 @@ { "data": { "text/plain": [ - "\"Eric Zhu's birthday is March 22, 2023.\"" + "\"Eric Zhu's birthday is on August 1st and it is not a leap year in 2021.\"" ] }, - "execution_count": 13, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -315,13 +97,20 @@ "source": [ "\n", "agent_chain.run(\"What is Eric Zhu's birthday?\")\n", - "# Answer with \"it is today.\"" + "# Answer with \"last week\"" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -335,9 +124,8 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" - }, - "orig_nbformat": 4 + "version": "3.9.1" + } }, "nbformat": 4, "nbformat_minor": 2 diff --git a/langchain/tools/human/tool.py b/langchain/tools/human/tool.py index 1b710c0a03..de2cce81cf 100644 --- a/langchain/tools/human/tool.py +++ b/langchain/tools/human/tool.py @@ -2,20 +2,27 @@ from typing import Callable +from pydantic import Field + from langchain.tools.base import BaseTool +def _print_func(text: str) -> None: + print("\n") + print(text) + + class HumanInputRun(BaseTool): """Tool that adds the capability to ask user for input.""" name = "Human" description = ( - "A human. " - "Useful for clarification, confirmation, or anything other tools cannot help. " - "Input should be a question you need help with." + "You can ask a human for guidance when you think you " + "got stuck or you are not sure what to do next. " + "The input should be a question for the human." ) - prompt_func: Callable[[str], None] - input_func: Callable + prompt_func: Callable[[str], None] = Field(default_factory=lambda: _print_func) + input_func: Callable = Field(default_factory=lambda: input) def _run(self, query: str) -> str: """Use the Human input tool."""