mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
289 lines
8.7 KiB
Plaintext
289 lines
8.7 KiB
Plaintext
{
|
||
"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([\"human\", \"ddg-search\"], llm=math_llm, input_func=get_input)"
|
||
]
|
||
},
|
||
{
|
||
"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
|
||
}
|