Skip to content

Commit

Permalink
[wip] add react agent (#6865)
Browse files Browse the repository at this point in the history

Co-authored-by: Simon Suo <simonsdsuo@gmail.com>
  • Loading branch information
jerryjliu and Disiok authored Jul 12, 2023
1 parent aa56f21 commit 74a4f41
Show file tree
Hide file tree
Showing 21 changed files with 1,222 additions and 15 deletions.
224 changes: 224 additions & 0 deletions docs/examples/agent/react_agent.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "e8ac1778-0585-43c9-9dad-014d13d7460d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from llama_index.agent import ReActAgent\n",
"from llama_index.llms import OpenAI, ChatMessage\n",
"from llama_index.tools import BaseTool, FunctionTool"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "26472aaf-1a12-49f9-9fe6-cbf41dd15f88",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def multiply(a: int, b: int) -> int:\n",
" \"\"\"Multiple two integers and returns the result integer\"\"\"\n",
" return a * b\n",
"\n",
"\n",
"multiply_tool = FunctionTool.from_defaults(fn=multiply)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "df78ae85-bcf7-44c1-87ee-f301e646db20",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def add(a: int, b: int) -> int:\n",
" \"\"\"Add two integers and returns the result integer\"\"\"\n",
" return a + b\n",
"\n",
"\n",
"add_tool = FunctionTool.from_defaults(fn=add)"
]
},
{
"cell_type": "markdown",
"id": "8b6c519f-6d74-4251-b38e-b8395a96d43b",
"metadata": {},
"source": [
"## gpt-3.5-turbo-0613"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7ab300f1-b054-46d9-b1c8-dbcd0d538e5a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"llm = OpenAI(model=\"gpt-3.5-turbo-0613\")\n",
"agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "69bb1aa9-1ea3-4c88-a4f3-239b76392aa5",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[38;5;200m\u001b[1;3mThought: I can use the tools to perform the multiplication and addition operations.\n",
"Action: multiply\n",
"Action Input: {'a': 2, 'b': 4}\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 8\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3mThought: Now I need to add 2 to the result of the multiplication.\n",
"Action: add\n",
"Action Input: {'a': 8, 'b': 2}\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 10\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3mResponse: 10\n",
"\u001b[0m10\n"
]
}
],
"source": [
"response = agent.chat(\"What is 2+2*4\")\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "6d112393-ac6e-45d4-a58e-fec9745a79fc",
"metadata": {},
"source": [
"## gpt-4"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1bb7d49b-404c-4a46-9a84-1f7bb8792991",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"llm = OpenAI(model=\"gpt-4\")\n",
"agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "277f21d3-4f62-430e-b0fc-7561c64084d0",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[38;5;200m\u001b[1;3mThought: I need to use the tools to help me answer the question. I will first multiply 2 and 4, then add the result to 2.\n",
"Action: multiply\n",
"Action Input: {'a': 2, 'b': 4}\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 8\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3mThought: Now that I have the result of 2*4, I need to add this result to 2.\n",
"Action: add\n",
"Action Input: {'a': 2, 'b': 8}\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 10\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3mResponse: 10\n",
"\u001b[0m10\n"
]
}
],
"source": [
"response = agent.chat(\"What is 2+2*4\")\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "fa9ffdae-962b-447d-a54a-624d5208aee8",
"metadata": {
"tags": []
},
"source": [
"## text-davinci-003"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e402e2dc-0537-49d5-b399-3831114691e6",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"llm = OpenAI(model=\"text-davinci-003\")\n",
"agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "f42bec92-5294-48a2-8a16-8a1d1a0cc908",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[38;5;200m\u001b[1;3mThought: I need to use a tool to help me answer the question.\n",
"Action: add\n",
"Action Input: {'a': 2, 'b': 8}\n",
"\u001b[0m\u001b[36;1m\u001b[1;3mObservation: 10\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3mResponse: 10\n",
"\u001b[0m10\n"
]
}
],
"source": [
"response = agent.chat(\"What is 2+2*4\")\n",
"print(response)"
]
}
],
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 74a4f41

Please sign in to comment.