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-05-01 01:59:11 +00:00
"metadata": {
"tags": []
},
2023-03-23 03:14:52 +00:00
"outputs": [],
"source": [
"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",
2023-03-23 03:14:52 +00:00
"\n",
"llm = ChatOpenAI(temperature=0.0)\n",
"math_llm = OpenAI(temperature=0.0)\n",
"tools = load_tools(\n",
2023-06-16 18:52:56 +00:00
" [\"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",
2023-04-04 04:56:20 +00:00
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
2023-03-23 03:14:52 +00:00
" verbose=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the above code you can see the tool takes input directly from command line.\n",
2023-05-01 01:59:11 +00:00
"You can customize `prompt_func` and `input_func` according to your need (as shown below)."
2023-03-23 03:14:52 +00:00
]
},
{
"cell_type": "code",
2023-05-01 01:59:11 +00:00
"execution_count": 2,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mI don't know Eric's surname, so I should ask a human for guidance.\n",
"Action: Human\n",
"Action Input: \"What is Eric's surname?\"\u001b[0m\n",
"\n",
"What is Eric's surname?\n"
]
},
{
2023-05-09 14:42:47 +00:00
"name": "stdout",
2023-05-01 01:59:11 +00:00
"output_type": "stream",
"text": [
" Zhu\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Observation: \u001b[36;1m\u001b[1;3mZhu\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mI now know Eric's surname is Zhu.\n",
"Final Answer: Eric's surname is Zhu.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\"Eric's surname is Zhu.\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-05-09 14:42:47 +00:00
"agent_chain.run(\"What's my friend Eric's surname?\")\n",
2023-05-01 01:59:11 +00:00
"# Answer with 'Zhu'"
]
},
{
"cell_type": "markdown",
2023-03-23 03:14:52 +00:00
"metadata": {},
2023-05-01 01:59:11 +00:00
"source": [
"## Configuring the Input Function\n",
"\n",
"By default, the `HumanInputRun` tool uses the python `input` function to get input from the user.\n",
"You can customize the input_func to be anything you'd like.\n",
"For instance, if you want to accept multi-line input, you could do the following:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def get_input() -> str:\n",
" print(\"Insert your text. Enter 'q' or press Ctrl-D (or Ctrl-Z on Windows) to end.\")\n",
" contents = []\n",
" while True:\n",
" try:\n",
" line = input()\n",
" except EOFError:\n",
" break\n",
" if line == \"q\":\n",
" break\n",
" contents.append(line)\n",
" return \"\\n\".join(contents)\n",
"\n",
"\n",
"# You can modify the tool when loading\n",
2023-06-16 18:52:56 +00:00
"tools = load_tools([\"human\", \"ddg-search\"], llm=math_llm, input_func=get_input)"
2023-05-01 01:59:11 +00:00
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Or you can directly instantiate the tool\n",
"from langchain.tools import HumanInputRun\n",
"\n",
"tool = HumanInputRun(input_func=get_input)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"agent_chain = initialize_agent(\n",
" tools,\n",
" llm,\n",
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
" verbose=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"tags": []
},
2023-03-23 03:14:52 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
2023-05-01 01:59:11 +00:00
"\u001b[32;1m\u001b[1;3mI should ask a human for guidance\n",
2023-03-23 03:14:52 +00:00
"Action: Human\n",
2023-05-01 01:59:11 +00:00
"Action Input: \"Can you help me attribute a quote?\"\u001b[0m\n",
2023-03-23 03:14:52 +00:00
"\n",
2023-05-01 01:59:11 +00:00
"Can you help me attribute a quote?\n",
"Insert your text. Enter 'q' or press Ctrl-D (or Ctrl-Z on Windows) to end.\n"
]
},
{
2023-05-09 14:42:47 +00:00
"name": "stdout",
2023-05-01 01:59:11 +00:00
"output_type": "stream",
"text": [
" vini\n",
" vidi\n",
" vici\n",
" q\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-03-23 03:14:52 +00:00
"\n",
2023-05-01 01:59:11 +00:00
"Observation: \u001b[36;1m\u001b[1;3mvini\n",
"vidi\n",
"vici\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mI need to provide more context about the quote\n",
2023-03-23 03:14:52 +00:00
"Action: Human\n",
2023-05-01 01:59:11 +00:00
"Action Input: \"The quote is 'Veni, vidi, vici'\"\u001b[0m\n",
2023-03-23 03:14:52 +00:00
"\n",
2023-05-01 01:59:11 +00:00
"The quote is 'Veni, vidi, vici'\n",
"Insert your text. Enter 'q' or press Ctrl-D (or Ctrl-Z on Windows) to end.\n"
]
},
{
2023-05-09 14:42:47 +00:00
"name": "stdout",
2023-05-01 01:59:11 +00:00
"output_type": "stream",
"text": [
" oh who said it \n",
" q\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-03-23 03:14:52 +00:00
"\n",
2023-05-01 01:59:11 +00:00
"Observation: \u001b[36;1m\u001b[1;3moh who said it \u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mI can use DuckDuckGo Search to find out who said the quote\n",
"Action: DuckDuckGo Search\n",
"Action Input: \"Who said 'Veni, vidi, vici'?\"\u001b[0m\n",
"Observation: \u001b[33;1m\u001b[1;3mUpdated on September 06, 2019. \"Veni, vidi, vici\" is a famous phrase said to have been spoken by the Roman Emperor Julius Caesar (100-44 BCE) in a bit of stylish bragging that impressed many of the writers of his day and beyond. The phrase means roughly \"I came, I saw, I conquered\" and it could be pronounced approximately Vehnee, Veedee ... Veni, vidi, vici (Classical Latin: [weː niː wiː diː wiː kiː ], Ecclesiastical Latin: [ˈ veni ˈ vidi ˈ vitʃi]; \"I came; I saw; I conquered\") is a Latin phrase used to refer to a swift, conclusive victory.The phrase is popularly attributed to Julius Caesar who, according to Appian, used the phrase in a letter to the Roman Senate around 47 BC after he had achieved a quick victory in his short ... veni, vidi, vici Latin quotation from Julius Caesar ve· ni, vi· di, vi· ci ˌwā-nē ˌwē-dē ˈ wē-kē ˌvā-nē ˌvē-dē ˈ vē-chē : I came, I saw, I conquered Articles Related to veni, vidi, vici 'In Vino Veritas' and Other Latin... Dictionary Entries Near veni, vidi, vici Venite veni, vidi, vici Venizélos See More Nearby Entries Cite this Entry Style The simplest explanation for why veni, vidi, vici is a popular saying is that it comes from Julius Caesar, one of history's most famous figures, and has a simple, strong meaning: I'm powerful and fast. But it's not just the meaning that makes the phrase so powerful. Caesar was a gifted writer, and the phrase makes use of Latin grammar to ... One of the best known and most frequently quoted Latin expression, veni, vidi, vici may be found hundreds of times throughout the centuries used as an expression of triumph. The words are said to have been used by Caesar as he was enjoying a triumph.\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3mI now know the final answer\n",
"Final Answer: Julius Caesar said the quote \"Veni, vidi, vici\" which means \"I came, I saw, I conquered\".\u001b[0m\n",
2023-03-23 03:14:52 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
2023-05-01 01:59:11 +00:00
"'Julius Caesar said the quote \"Veni, vidi, vici\" which means \"I came, I saw, I conquered\".'"
2023-03-23 03:14:52 +00:00
]
},
2023-05-01 01:59:11 +00:00
"execution_count": 12,
2023-03-23 03:14:52 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-05-01 01:59:11 +00:00
"agent_chain.run(\"I need help attributing a quote\")"
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-05-01 01:59:11 +00:00
"version": "3.11.2"
2023-03-23 03:30:45 +00:00
}
2023-03-23 03:14:52 +00:00
},
"nbformat": 4,
2023-05-01 01:59:11 +00:00
"nbformat_minor": 4
2023-03-23 03:14:52 +00:00
}