mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-29 01:48:57 +00:00
human input default (#1911)
This commit is contained in:
parent
b3be83c750
commit
b5667bed9e
@ -1,7 +1,6 @@
|
|||||||
{
|
{
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
@ -13,7 +12,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -26,9 +25,7 @@
|
|||||||
"math_llm = OpenAI(temperature=0.0)\n",
|
"math_llm = OpenAI(temperature=0.0)\n",
|
||||||
"tools = load_tools(\n",
|
"tools = load_tools(\n",
|
||||||
" [\"human\", \"llm-math\"], \n",
|
" [\"human\", \"llm-math\"], \n",
|
||||||
" llm=math_llm, \n",
|
" llm=math_llm,\n",
|
||||||
" prompt_func=lambda s: sys.stdout.write(s + \"\\n\"), \n",
|
|
||||||
" input_func=input,\n",
|
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"agent_chain = initialize_agent(\n",
|
"agent_chain = initialize_agent(\n",
|
||||||
@ -40,7 +37,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
@ -50,7 +46,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -60,243 +56,29 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||||||
"\n",
|
"\u001b[32;1m\u001b[1;3mI don't know Eric Zhu, so I should ask a human for guidance.\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",
|
|
||||||
"Action: Human\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",
|
"\n",
|
||||||
"Observation: \u001b[36;1m\u001b[1;3mYes\u001b[0m\n",
|
"Do you know when Eric Zhu's birthday is?\n",
|
||||||
"Thought:\n",
|
"last week\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
"Observation: \u001b[36;1m\u001b[1;3mlast week\u001b[0m\n",
|
||||||
"Prompt after formatting:\n",
|
"Thought:\u001b[32;1m\u001b[1;3mThat's not very helpful. I should ask for more information.\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: Human\n",
|
||||||
"Action Input: \"Do you know Eric Zhu's birthday?\"\n",
|
"Action Input: \"Do you know the specific date of Eric Zhu's birthday?\"\u001b[0m\n",
|
||||||
"Observation: Yes\n",
|
|
||||||
"Thought:\u001b[0m\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
"Do you know the specific date of Eric Zhu's birthday?\n",
|
||||||
"\u001b[32;1m\u001b[1;3mGreat, now I just need to wait for the human to provide the answer.\n",
|
"august 1st\n",
|
||||||
"Action: Human\n",
|
|
||||||
"Action Input: \"What is Eric Zhu's birthday?\"\u001b[0mWhat is Eric Zhu's birthday?\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"Observation: \u001b[36;1m\u001b[1;3mIt's today\u001b[0m\n",
|
"Observation: \u001b[36;1m\u001b[1;3maugust 1st\u001b[0m\n",
|
||||||
"Thought:\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",
|
||||||
"\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",
|
|
||||||
"Action: Calculator\n",
|
"Action: Calculator\n",
|
||||||
"Action Input: Today's date\u001b[0m\n",
|
"Action Input: \"Is 2021 a leap year?\"\u001b[0m\n",
|
||||||
"\n",
|
"Observation: \u001b[33;1m\u001b[1;3mAnswer: False\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",
|
|
||||||
"\u001b[0m\n",
|
"\u001b[0m\n",
|
||||||
"\n",
|
"Thought:\u001b[32;1m\u001b[1;3mI have all the information I need to answer the original question.\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
"Final Answer: Eric Zhu's birthday is on August 1st and it is not a leap year in 2021.\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",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
]
|
]
|
||||||
@ -304,10 +86,10 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"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": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -315,13 +97,20 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"\n",
|
"\n",
|
||||||
"agent_chain.run(\"What is Eric Zhu's birthday?\")\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": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": ".venv",
|
"display_name": "Python 3 (ipykernel)",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@ -335,9 +124,8 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.2"
|
"version": "3.9.1"
|
||||||
},
|
}
|
||||||
"orig_nbformat": 4
|
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 2
|
"nbformat_minor": 2
|
||||||
|
@ -2,20 +2,27 @@
|
|||||||
|
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
|
from pydantic import Field
|
||||||
|
|
||||||
from langchain.tools.base import BaseTool
|
from langchain.tools.base import BaseTool
|
||||||
|
|
||||||
|
|
||||||
|
def _print_func(text: str) -> None:
|
||||||
|
print("\n")
|
||||||
|
print(text)
|
||||||
|
|
||||||
|
|
||||||
class HumanInputRun(BaseTool):
|
class HumanInputRun(BaseTool):
|
||||||
"""Tool that adds the capability to ask user for input."""
|
"""Tool that adds the capability to ask user for input."""
|
||||||
|
|
||||||
name = "Human"
|
name = "Human"
|
||||||
description = (
|
description = (
|
||||||
"A human. "
|
"You can ask a human for guidance when you think you "
|
||||||
"Useful for clarification, confirmation, or anything other tools cannot help. "
|
"got stuck or you are not sure what to do next. "
|
||||||
"Input should be a question you need help with."
|
"The input should be a question for the human."
|
||||||
)
|
)
|
||||||
prompt_func: Callable[[str], None]
|
prompt_func: Callable[[str], None] = Field(default_factory=lambda: _print_func)
|
||||||
input_func: Callable
|
input_func: Callable = Field(default_factory=lambda: input)
|
||||||
|
|
||||||
def _run(self, query: str) -> str:
|
def _run(self, query: str) -> str:
|
||||||
"""Use the Human input tool."""
|
"""Use the Human input tool."""
|
||||||
|
Loading…
Reference in New Issue
Block a user