langchain/docs/modules/agents/tools/examples/human_tools.ipynb

134 lines
3.6 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Human as a tool\n",
"\n",
"Human are AGI so they can certainly be used as a tool to help out AI agent \n",
"when it is confused."
]
},
{
"cell_type": "code",
2023-03-23 03:30:45 +00:00
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.llms import OpenAI\n",
"from langchain.agents import load_tools, initialize_agent\n",
2023-04-04 14:21:50 +00:00
"from langchain.agents import AgentType\n",
"\n",
"llm = ChatOpenAI(temperature=0.0)\n",
"math_llm = OpenAI(temperature=0.0)\n",
"tools = load_tools(\n",
" [\"human\", \"llm-math\"], \n",
2023-03-23 03:30:45 +00:00
" llm=math_llm,\n",
")\n",
"\n",
"agent_chain = initialize_agent(\n",
" tools,\n",
" llm,\n",
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
" verbose=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the above code you can see the tool takes input directly from command line.\n",
"You can customize `prompt_func` and `input_func` according to your need."
]
},
{
"cell_type": "code",
2023-03-23 03:30:45 +00:00
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
2023-03-23 03:30:45 +00:00
"\u001b[32;1m\u001b[1;3mI don't know Eric Zhu, so I should ask a human for guidance.\n",
"Action: Human\n",
2023-03-23 03:30:45 +00:00
"Action Input: \"Do you know when Eric Zhu's birthday is?\"\u001b[0m\n",
"\n",
2023-03-23 03:30:45 +00:00
"Do you know when Eric Zhu's birthday is?\n",
"last week\n",
"\n",
2023-03-23 03:30:45 +00:00
"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",
2023-03-23 03:30:45 +00:00
"Action Input: \"Do you know the specific date of Eric Zhu's birthday?\"\u001b[0m\n",
"\n",
2023-03-23 03:30:45 +00:00
"Do you know the specific date of Eric Zhu's birthday?\n",
"august 1st\n",
"\n",
2023-03-23 03:30:45 +00:00
"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",
2023-03-23 03:30:45 +00:00
"Action Input: \"Is 2021 a leap year?\"\u001b[0m\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: False\n",
"\u001b[0m\n",
2023-03-23 03:30:45 +00:00
"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"
]
},
{
"data": {
"text/plain": [
2023-03-23 03:30:45 +00:00
"\"Eric Zhu's birthday is on August 1st and it is not a leap year in 2021.\""
]
},
2023-03-23 03:30:45 +00:00
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"agent_chain.run(\"What is Eric Zhu's birthday?\")\n",
2023-03-23 03:30:45 +00:00
"# Answer with \"last week\""
]
2023-03-23 03:30:45 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
2023-03-23 03:30:45 +00:00
"display_name": "Python 3 (ipykernel)",
"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",
2023-03-23 03:30:45 +00:00
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}