From 2533da26a3adc9f74ea474b8a84253cc72b70e67 Mon Sep 17 00:00:00 2001 From: krisograbek Date: Thu, 5 Oct 2023 11:16:24 +0200 Subject: [PATCH] add GPT-4 agent --- LangChainGuides/BasicAgent.ipynb | 216 ++++++++++++++++++++++++++++++- 1 file changed, 213 insertions(+), 3 deletions(-) diff --git a/LangChainGuides/BasicAgent.ipynb b/LangChainGuides/BasicAgent.ipynb index cc07b3e..9125132 100644 --- a/LangChainGuides/BasicAgent.ipynb +++ b/LangChainGuides/BasicAgent.ipynb @@ -71,6 +71,26 @@ "load_dotenv(dotenv_path)" ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'2a5096fc18b65b3fcab02a9bc50c45c5338d9895252875761d92b26dfb2d9181'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getenv(\"SERPAPI_API_KEY\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -215,7 +235,7 @@ } ], "source": [ - "gpt4" + "gpt4.model_name" ] }, { @@ -507,12 +527,12 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from langchain.utilities import SerpAPIWrapper\n", - "from langchain.agents import initialize_agent, Tool\n", + "from langchain.agents import initialize_agent, Tool, AgentType\n", "\n", "search = SerpAPIWrapper()\n", "new_tools = [\n", @@ -732,6 +752,196 @@ "Awesome! Everything is great here!" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating GPT-4 AI Agent\n", + "\n", + "Let's test the same but for GPT-4. Let's try to add the calculator as a tool as well." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.utilities import SerpAPIWrapper\n", + "from langchain.agents import initialize_agent, Tool, AgentType, load_tools\n", + "from langchain.chat_models import ChatOpenAI\n", + "\n", + "search = SerpAPIWrapper()\n", + "\n", + "gpt4 = ChatOpenAI(model=\"gpt-4\", temperature=0)\n", + "\n", + "# create the serp tool\n", + "serp_tool = Tool(\n", + " name=\"Search\",\n", + " func=search.run,\n", + " description=\"useful for when you need to answer questions about current events. You should ask targeted questions\",\n", + " )\n", + "\n", + "# Initialize tools with calculator and the model\n", + "gpt4_tools = load_tools([\"llm-math\"], llm=gpt4)\n", + "# add the serp tool\n", + "gpt4_tools = gpt4_tools + [serp_tool]" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3mThought: I need to find out the current FIFA men's world champion. I can use the Search tool to find this information.\n", + "Action:\n", + "```\n", + "{\n", + " \"action\": \"Search\",\n", + " \"action_input\": \"Current FIFA men's world champion\"\n", + "}\n", + "```\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3mThe reigning champions are Argentina, who won their third title at the 2022 tournament.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer.\n", + "Final Answer: The current FIFA men's world champion is Argentina.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"The current FIFA men's world champion is Argentina.\"" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# initialize GPT-4 Agent\n", + "gpt4agent = initialize_agent(\n", + " gpt4_tools, \n", + " ChatOpenAI(model=\"gpt-4\", temperature=0), \n", + " agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, \n", + " verbose=True, \n", + " # handle_parsing_errors=\"Check your output and make sure it conforms!\"\n", + " )\n", + "\n", + "gpt4agent.run(\"Who is the Current FIFA men's world champion?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3mThought: I need to find out who the current NBA champion is. I can use the Search tool to find this information.\n", + "Action:\n", + "```\n", + "{\n", + " \"action\": \"Search\",\n", + " \"action_input\": \"current NBA champion\"\n", + "}\n", + "```\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3mDenver Nuggets\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mThe search results show that the current NBA champion is the Denver Nuggets.\n", + "Final Answer: The current NBA champion is the Denver Nuggets.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'The current NBA champion is the Denver Nuggets.'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "gpt4agent.run(\"Who is the current NBA champion?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3mThought: I need to find out who the NBA all-time leading scorer is and how many total points he scored. I will use the Search tool to find this information.\n", + "Action:\n", + "```\n", + "{\n", + " \"action\": \"Search\",\n", + " \"action_input\": \"NBA all-time leading scorer and total points\"\n", + "}\n", + "```\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3mLeBron James points tracker: NBA's all-time top scorer closing in on 47,000 career points. Team USA's two-time Olympic gold medallist broke the career NBA regular season points scoring record held by Kareem Abdul-Jabbar. Basketball hero LeBron James is now the NBA's all-time top scorer.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mThe NBA all-time leading scorer is LeBron James. However, the total points mentioned is not specific and seems to be an approximation. I need to find the exact total points scored by LeBron James. I will use the Search tool again to find this information.\n", + "Action:\n", + "```\n", + "{\n", + " \"action\": \"Search\",\n", + " \"action_input\": \"LeBron James total NBA points\"\n", + "}\n", + "```\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3m38,652\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mLeBron James has scored a total of 38,652 points in the NBA. Now I need to calculate 38,652 to the power of 0.42. I will use the Calculator tool for this.\n", + "Action:\n", + "```\n", + "{\n", + " \"action\": \"Calculator\",\n", + " \"action_input\": \"38652^0.42\"\n", + "}\n", + "```\n", + "\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mAnswer: 84.45244506356971\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer. LeBron James is the NBA all-time leading scorer and 38,652 points to the power of 0.42 is approximately 84.45. \n", + "Final Answer: The NBA all-time leading scorer is LeBron James and 38,652 points to the power of 0.42 is approximately 84.45.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'The NBA all-time leading scorer is LeBron James and 38,652 points to the power of 0.42 is approximately 84.45.'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "gpt4agent.run(\"Who is the NBA all-time leading scorer? What's his total points to the power of 0.42?\")" + ] + }, { "cell_type": "markdown", "metadata": {},