2023-03-23 03:14:52 +00:00
|
|
|
{
|
|
|
|
"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,
|
2023-03-23 03:14:52 +00:00
|
|
|
"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",
|
|
|
|
"\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",
|
2023-03-23 03:14:52 +00:00
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"agent_chain = initialize_agent(\n",
|
|
|
|
" tools,\n",
|
|
|
|
" llm,\n",
|
|
|
|
" agent=\"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,
|
2023-03-23 03:14:52 +00:00
|
|
|
"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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"\n",
|
2023-03-23 03:30:45 +00:00
|
|
|
"Do you know when Eric Zhu's birthday is?\n",
|
|
|
|
"last week\n",
|
2023-03-23 03:14:52 +00:00
|
|
|
"\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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"\n",
|
2023-03-23 03:30:45 +00:00
|
|
|
"Do you know the specific date of Eric Zhu's birthday?\n",
|
|
|
|
"august 1st\n",
|
2023-03-23 03:14:52 +00:00
|
|
|
"\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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"\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",
|
2023-03-23 03:14:52 +00:00
|
|
|
"\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:14:52 +00:00
|
|
|
]
|
|
|
|
},
|
2023-03-23 03:30:45 +00:00
|
|
|
"execution_count": 3,
|
2023-03-23 03:14:52 +00:00
|
|
|
"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:14:52 +00:00
|
|
|
]
|
2023-03-23 03:30:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": []
|
2023-03-23 03:14:52 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
2023-03-23 03:30:45 +00:00
|
|
|
"display_name": "Python 3 (ipykernel)",
|
2023-03-23 03:14:52 +00:00
|
|
|
"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"
|
|
|
|
}
|
2023-03-23 03:14:52 +00:00
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|