Spaces:
Running
Running
File size: 8,989 Bytes
cfd3735 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
{
"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",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.llms import OpenAI\n",
"from langchain.agents import load_tools, initialize_agent\n",
"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",
" 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 (as shown below)."
]
},
{
"cell_type": "code",
"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"
]
},
{
"name": "stdout",
"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": [
"agent_chain.run(\"What's my friend Eric's surname?\")\n",
"# Answer with 'Zhu'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"tools = load_tools(\n",
" [\"human\", \"ddg-search\"], \n",
" llm=math_llm,\n",
" input_func=get_input\n",
")"
]
},
{
"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": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mI should ask a human for guidance\n",
"Action: Human\n",
"Action Input: \"Can you help me attribute a quote?\"\u001b[0m\n",
"\n",
"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"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" vini\n",
" vidi\n",
" vici\n",
" q\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"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",
"Action: Human\n",
"Action Input: \"The quote is 'Veni, vidi, vici'\"\u001b[0m\n",
"\n",
"The quote is 'Veni, vidi, vici'\n",
"Insert your text. Enter 'q' or press Ctrl-D (or Ctrl-Z on Windows) to end.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" oh who said it \n",
" q\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"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",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'Julius Caesar said the quote \"Veni, vidi, vici\" which means \"I came, I saw, I conquered\".'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_chain.run(\"I need help attributing a quote\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"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",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|