langchain/docs/modules/agents/implementations/react.ipynb

111 lines
3.9 KiB
Plaintext
Raw Normal View History

2022-11-22 14:16:26 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "82140df0",
"metadata": {},
"source": [
"# ReAct\n",
"\n",
"This notebook showcases using an agent to implement the ReAct logic."
]
},
{
"cell_type": "code",
"execution_count": 4,
2022-11-22 14:16:26 +00:00
"id": "4e272b47",
"metadata": {},
"outputs": [],
"source": [
"from langchain import OpenAI, Wikipedia\n",
"from langchain.agents import initialize_agent, Tool\n",
"from langchain.agents.react.base import DocstoreExplorer\n",
"docstore=DocstoreExplorer(Wikipedia())\n",
"tools = [\n",
" Tool(\n",
" name=\"Search\",\n",
" func=docstore.search,\n",
" description=\"useful for when you need to ask with search\"\n",
2022-11-22 14:16:26 +00:00
" ),\n",
" Tool(\n",
" name=\"Lookup\",\n",
" func=docstore.lookup,\n",
" description=\"useful for when you need to ask with lookup\"\n",
2022-11-22 14:16:26 +00:00
" )\n",
"]\n",
"\n",
2022-12-11 07:17:19 +00:00
"llm = OpenAI(temperature=0, model_name=\"text-davinci-002\")\n",
2022-11-23 02:02:20 +00:00
"react = initialize_agent(tools, llm, agent=\"react-docstore\", verbose=True)"
2022-11-22 14:16:26 +00:00
]
},
{
"cell_type": "code",
"execution_count": 5,
2022-11-22 14:16:26 +00:00
"id": "8078c8f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-12-11 07:17:19 +00:00
"\n",
"\n",
2022-12-26 13:46:38 +00:00
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
2022-12-19 02:51:23 +00:00
"\u001b[32;1m\u001b[1;3m\n",
2022-12-26 13:46:38 +00:00
"Thought 1: I need to search David Chanoff and find the U.S. Navy admiral he collaborated\n",
"with.\n",
2022-12-11 07:17:19 +00:00
"Action 1: Search[David Chanoff]\u001b[0m\n",
"Observation 1: \u001b[36;1m\u001b[1;3mDavid Chanoff is a noted author of non-fiction work. His work has typically involved collaborations with the principal protagonist of the work concerned. His collaborators have included; Augustus A. White, Joycelyn Elders, Đoàn Văn Toại, William J. Crowe, Ariel Sharon, Kenneth Good and Felix Zandman. He has also written about a wide range of subjects including literary history, education and foreign for The Washington Post, The New Republic and The New York Times Magazine. He has published more than twelve books.\u001b[0m\n",
"Thought 2:\u001b[32;1m\u001b[1;3m The U.S. Navy admiral David Chanoff collaborated with is William J. Crowe.\n",
"Action 2: Search[William J. Crowe]\u001b[0m\n",
"Observation 2: \u001b[36;1m\u001b[1;3mWilliam James Crowe Jr. (January 2, 1925 October 18, 2007) was a United States Navy admiral and diplomat who served as the 11th chairman of the Joint Chiefs of Staff under Presidents Ronald Reagan and George H. W. Bush, and as the ambassador to the United Kingdom and Chair of the Intelligence Oversight Board under President Bill Clinton.\u001b[0m\n",
2022-12-19 02:51:23 +00:00
"Thought 3:\u001b[32;1m\u001b[1;3m The President William J. Crowe served as the ambassador to the United Kingdom under is Bill Clinton.\n",
2022-12-11 07:17:19 +00:00
"Action 3: Finish[Bill Clinton]\u001b[0m\n",
2022-12-26 13:46:38 +00:00
"\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
2022-11-22 14:16:26 +00:00
]
2022-12-11 07:17:19 +00:00
},
{
"data": {
"text/plain": [
"'Bill Clinton'"
]
},
"execution_count": 5,
2022-12-11 07:17:19 +00:00
"metadata": {},
"output_type": "execute_result"
2022-11-22 14:16:26 +00:00
}
],
"source": [
"question = \"Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?\"\n",
"react.run(question)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
2022-11-22 14:16:26 +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",
"version": "3.9.1"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
2022-11-22 14:16:26 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}