diff --git a/langchain/jupyter/agents/openai_function.ipynb b/langchain/jupyter/agents/openai_function.ipynb index 3f80eb9d..9f62ef62 100644 --- a/langchain/jupyter/agents/openai_function.ipynb +++ b/langchain/jupyter/agents/openai_function.ipynb @@ -18,12 +18,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "Requirement already satisfied: langchain in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.2.7)\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: langchain in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.2.12)\n", "Requirement already satisfied: PyYAML>=5.3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (6.0.1)\n", "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (2.0.31)\n", "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (3.9.5)\n", "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (4.0.3)\n", - "Requirement already satisfied: langchain-core<0.3.0,>=0.2.12 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.2.13)\n", + "Requirement already satisfied: langchain-core<0.3.0,>=0.2.27 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.2.28)\n", "Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (0.2.0)\n", "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.1.85)\n", "Requirement already satisfied: numpy<2,>=1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (1.26.4)\n", @@ -35,18 +36,18 @@ "Requirement already satisfied: frozenlist>=1.1.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n", - "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (1.33)\n", - "Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (23.2)\n", + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (1.33)\n", + "Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (23.2)\n", + "Requirement already satisfied: typing-extensions>=4.7 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (4.12.2)\n", "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.5)\n", "Requirement already satisfied: annotated-types>=0.4.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.14.6 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (2.14.6)\n", - "Requirement already satisfied: typing-extensions>=4.6.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (4.12.2)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (2.2.2)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (2024.2.2)\n", "Requirement already satisfied: greenlet!=0.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n", - "Requirement already satisfied: jsonpointer>=1.9 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.12->langchain) (3.0.0)\n" + "Requirement already satisfied: jsonpointer>=1.9 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.27->langchain) (3.0.0)\n" ] } ], @@ -92,7 +93,7 @@ "outputs": [], "source": [ "from langchain.schema import SystemMessage\n", - "from langchain.agents import OpenAIFunctionsAgent\n", + "from langchain.agents import OpenAIFunctionsAgent, create_openai_functions_agent\n", "\n", "system_message = SystemMessage(content=\"你是非常强大的AI助手,但在计算单词长度方面不擅长。\")\n", "prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)" @@ -103,23 +104,24 @@ "execution_count": 5, "id": "ef0874c6-8076-4536-9533-bb9f69f450fe", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python310\\site-packages\\langchain_core\\_api\\deprecation.py:139: LangChainDeprecationWarning: The class `OpenAIFunctionsAgent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use create_openai_functions_agent instead.\n", - " warn_deprecated(\n" - ] - } - ], + "outputs": [], "source": [ - "agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)" + "# agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)#过时" ] }, { "cell_type": "code", "execution_count": 6, + "id": "0532b789-455c-4188-ba32-dba615131b2e", + "metadata": {}, + "outputs": [], + "source": [ + "agent = create_openai_functions_agent(llm=llm, tools=tools, prompt=prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, "id": "e7061c25-d090-4b52-a137-ba7545759241", "metadata": {}, "outputs": [], @@ -132,7 +134,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "4f0ae154-5cb9-44cf-91d6-cc80308dcffc", "metadata": {}, "outputs": [ @@ -158,18 +160,18 @@ "{'input': '单词“educa”中有多少个字母?', 'output': '单词“educa”中有5个字母。'}" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent_executor.invoke(\"单词“educa”中有多少个字母?\")" + "agent_executor.invoke({\"input\":\"单词“educa”中有多少个字母?\"})" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "2070d22f-c9d9-4346-b41d-e9beecb40625", "metadata": {}, "outputs": [], @@ -185,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "849ebeac-aa8c-4ae8-b36c-c81a120147fb", "metadata": {}, "outputs": [], @@ -197,18 +199,19 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "id": "9af06fad-4f85-4411-b4f8-4fca7a45e09b", "metadata": {}, "outputs": [], "source": [ - "agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)\n", + "# agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt) # 过时\n", + "agent = create_openai_functions_agent(llm=llm, tools=tools, prompt=prompt)\n", "agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "id": "fc912215-61d3-4ce1-9a48-b795cf445bde", "metadata": {}, "outputs": [ @@ -237,18 +240,18 @@ " 'output': '单词“educa”中有5个字母。'}" ] }, - "execution_count": 11, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent_executor.invoke(\"单词“educa”中有多少个字母?\")" + "agent_executor.invoke({\"input\":\"单词“educa”中有多少个字母?\"})" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 15, "id": "d3f96ac9-b548-4908-9c5e-d58118ed6a9a", "metadata": {}, "outputs": [ @@ -259,7 +262,7 @@ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或在线搜索以确认该单词的存在。\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或搜索引擎来确认这个单词的存在。\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -271,17 +274,17 @@ " 'chat_history': [HumanMessage(content='单词“educa”中有多少个字母?'),\n", " AIMessage(content='单词“educa”中有5个字母。'),\n", " HumanMessage(content='那是一个真实的单词吗?'),\n", - " AIMessage(content='抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或在线搜索以确认该单词的存在。')],\n", - " 'output': '抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或在线搜索以确认该单词的存在。'}" + " AIMessage(content='抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或搜索引擎来确认这个单词的存在。')],\n", + " 'output': '抱歉,我无法确定“educa”是否是一个真实的单词。您可以查阅词典或搜索引擎来确认这个单词的存在。'}" ] }, - "execution_count": 12, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent_executor.invoke(\"那是一个真实的单词吗?\")" + "agent_executor.invoke({\"input\":\"那是一个真实的单词吗?\"})" ] }, { diff --git a/langchain/jupyter/agents/react.ipynb b/langchain/jupyter/agents/react.ipynb index 0d50131f..bbceabc7 100644 --- a/langchain/jupyter/agents/react.ipynb +++ b/langchain/jupyter/agents/react.ipynb @@ -22,12 +22,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "Requirement already satisfied: langchain in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.2.7)\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: langchain in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.2.12)\n", "Requirement already satisfied: PyYAML>=5.3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (6.0.1)\n", "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (2.0.31)\n", "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (3.9.5)\n", "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (4.0.3)\n", - "Requirement already satisfied: langchain-core<0.3.0,>=0.2.12 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.2.13)\n", + "Requirement already satisfied: langchain-core<0.3.0,>=0.2.27 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.2.28)\n", "Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (0.2.0)\n", "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (0.1.85)\n", "Requirement already satisfied: numpy<2,>=1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain) (1.26.4)\n", @@ -39,18 +40,18 @@ "Requirement already satisfied: frozenlist>=1.1.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n", - "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (1.33)\n", - "Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (23.2)\n", + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (1.33)\n", + "Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (23.2)\n", + "Requirement already satisfied: typing-extensions>=4.7 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.27->langchain) (4.12.2)\n", "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.5)\n", "Requirement already satisfied: annotated-types>=0.4.0 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.14.6 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (2.14.6)\n", - "Requirement already satisfied: typing-extensions>=4.6.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1->langchain) (4.12.2)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (2.2.2)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchain) (2024.2.2)\n", "Requirement already satisfied: greenlet!=0.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n", - "Requirement already satisfied: jsonpointer>=1.9 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.12->langchain) (3.0.0)\n" + "Requirement already satisfied: jsonpointer>=1.9 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.27->langchain) (3.0.0)\n" ] } ], @@ -60,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "id": "cd59da96-dda8-42c8-9c82-8460cf3aa75d", "metadata": {}, "outputs": [], @@ -68,7 +69,7 @@ "import os\n", "\n", "# 更换为自己的 Serp API KEY\n", - "os.environ[\"SERPAPI_API_KEY\"] = \"1e3c93e0753ac224098370cd71da86150c6609caba6b5aaa307e04b72a5006e1\"" + "os.environ[\"SERPAPI_API_KEY\"] = \"*********替换成你的SerpAPI************\"" ] }, { @@ -103,32 +104,71 @@ "id": "d89d569f-5313-42f3-8f1a-ea45fb1ea035", "metadata": {}, "source": [ - "### 使用语言模型(GPT-3)" + "##### 使用语言模型(GPT-3.5)" ] }, { "cell_type": "code", - "execution_count": 5, - "id": "70af3538-18c4-4c64-99a7-96b32038d31b", + "execution_count": 11, + "id": "adfdf945-fa2b-4c9d-ae2b-4a859c3beeb0", "metadata": {}, "outputs": [ { - "name": "stderr", + "name": "stdout", "output_type": "stream", "text": [ - "C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python310\\site-packages\\langchain_core\\_api\\deprecation.py:139: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.\n", - " warn_deprecated(\n" + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: langchainhub in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (0.1.20)\n", + "Requirement already satisfied: packaging<25,>=23.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchainhub) (23.2)\n", + "Requirement already satisfied: requests<3,>=2 in c:\\users\\lenovo\\appdata\\roaming\\python\\python310\\site-packages (from langchainhub) (2.32.3)\n", + "Requirement already satisfied: types-requests<3.0.0.0,>=2.31.0.2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchainhub) (2.31.0.6)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchainhub) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchainhub) (3.6)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchainhub) (2.2.2)\n", + "Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests<3,>=2->langchainhub) (2024.2.2)\n", + "Requirement already satisfied: types-urllib3 in c:\\programdata\\anaconda3\\envs\\langchain\\lib\\site-packages (from types-requests<3.0.0.0,>=2.31.0.2->langchainhub) (1.26.25.14)\n" ] } ], "source": [ - "# 实例化 ZERO_SHOT_REACT Agent\n", - "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)" + "!pip install langchainhub" ] }, { "cell_type": "code", "execution_count": 6, + "id": "e561a1a7-2413-46b5-93e9-905ce7092849", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_openai import ChatOpenAI\n", + "\n", + "llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "70af3538-18c4-4c64-99a7-96b32038d31b", + "metadata": {}, + "outputs": [], + "source": [ + "# 实例化 ZERO_SHOT_REACT Agent\n", + "# agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) #过时\n", + "from langchain import hub\n", + "from langchain.agents import AgentExecutor, create_react_agent, create_json_agent\n", + "from langchain.globals import set_debug, set_verbose\n", + "# set_debug(True)\n", + "set_verbose(True)\n", + "prompt = hub.pull(\"hwchase17/react\")\n", + "\n", + "agent = create_react_agent(llm, tools, prompt)\n", + "agent_executor = AgentExecutor(agent=agent, tools=tools)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, "id": "484bba88-885b-4630-a9a9-b2ff6241df83", "metadata": {}, "outputs": [ @@ -139,16 +179,12 @@ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m I should use the search engine to find information about Leonardo DiCaprio's girlfriend.\n", + "\u001b[32;1m\u001b[1;3mI need to find out who is Leonardo DiCaprio's girlfriend and then calculate 0.43 raised to her current age.\n", "Action: Search\n", - "Action Input: \"Leonardo DiCaprio girlfriend\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mVittoria Ceretti\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m Now I need to use the calculator to calculate 0.43 times the age of Vittoria Ceretti.\n", + "Action Input: \"Leonardo DiCaprio girlfriend\"\u001b[0m\u001b[36;1m\u001b[1;3m[{'title': 'Leonardo DiCaprio Suffers Jellyfish Sting During Swim With GF Vittoria Ceretti', 'link': 'https://www.tmz.com/2024/08/05/leonardo-dicaprio-jellyfish-sting-swimming-yacht-italy-vittoria-ceretti/', 'source': 'TMZ', 'date': '22 hours ago', 'thumbnail': 'https://serpapi.com/searches/66b23925a5ad6d180b99ea11/images/7db95bf161919ea5f1437993aeba4501fe9fba0acebd51c0.jpeg'}, {'title': \"Leonardo DiCaprio's girlfriend Vittoria Ceretti tends to his jellyfish sting during Italian getaway\", 'link': 'https://timesofindia.indiatimes.com/entertainment/english/hollywood/news/leonardo-dicaprios-girlfriend-vittoria-ceretti-tends-to-his-jellyfish-sting-during-italian-getaway/articleshow/112321056.cms', 'source': 'Times of India', 'date': '2 hours ago', 'thumbnail': 'https://serpapi.com/searches/66b23925a5ad6d180b99ea11/images/7db95bf161919ea5cc24078b67e8880b2a2065d86299c6fa.jpeg'}, {'title': 'Vittoria Ceretti Tends to Boyfriend Leonardo DiCaprio After He Was Seemingly Stung by Jelly Fish on Yacht Trip', 'link': 'https://people.com/leonardo-dicaprio-and-girlfriend-vittoria-ceretti-vacation-in-italy-and-spend-some-time-on-star-studded-yacht-8690463', 'source': 'People', 'date': '19 hours ago', 'thumbnail': 'https://serpapi.com/searches/66b23925a5ad6d180b99ea11/images/7db95bf161919ea541824bea9924d255b388ee4b94430297.jpeg'}, {'title': 'Leonardo DiCaprio, 49, gets a jellyfish sting while at sea with bikini-clad girlfriend Vittoria Ceretti, 26, a', 'link': 'https://www.dailymail.co.uk/tvshowbiz/article-13711627/Leonardo-DiCaprio-gets-jellyfish-sting-Vittoria-Ceretti.html', 'source': 'Daily Mail Online', 'date': '18 hours ago', 'thumbnail': 'https://serpapi.com/searches/66b23925a5ad6d180b99ea11/images/7db95bf161919ea53904a654108da52820108789c1eba021.jpeg'}, {'title': \"Leonardo DiCaprio's girlfriend Vittoria Ceretti tends to him after jellyfish sting in Italy\", 'link': 'https://www.hindustantimes.com/entertainment/hollywood/leonardo-dicaprio-girlfriend-vittoria-ceretti-tends-to-him-after-jellyfish-sting-italy-see-pic-101722908491066.html', 'source': 'Hindustan Times', 'date': '13 hours ago', 'thumbnail': 'https://serpapi.com/searches/66b23925a5ad6d180b99ea11/images/7db95bf161919ea527cbadd3e3a65364753b1fe0e1644e19.jpeg'}]\u001b[0m\u001b[32;1m\u001b[1;3mI found out that Leonardo DiCaprio's girlfriend is Vittoria Ceretti. Now I need to calculate 0.43 raised to her current age.\n", "Action: Calculator\n", - "Action Input: 0.43 * 23\u001b[0m\n", - "Observation: \u001b[33;1m\u001b[1;3mAnswer: 9.89\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", - "Final Answer: 9.89\u001b[0m\n", + "Action Input: 0.43, current age of Vittoria Ceretti\u001b[0m\u001b[33;1m\u001b[1;3mAnswer: 0.43\u001b[0m\u001b[32;1m\u001b[1;3mI now know the final answer.\n", + "Final Answer: 0.43\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -156,16 +192,16 @@ { "data": { "text/plain": [ - "{'input': '谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?', 'output': '9.89'}" + "{'input': '谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?', 'output': '0.43'}" ] }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "agent.invoke(\"谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?\")" + "agent_executor.invoke({\"input\":\"谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?\"})" ] }, { @@ -178,7 +214,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "448c877f-577b-4277-a155-51223dc3d949", "metadata": {}, "outputs": [ @@ -189,42 +225,18 @@ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mQuestion: 谁是莱昂纳多·迪卡普里奥的女朋友?\n", - "Thought: To find out who Leonardo DiCaprio's current girlfriend is, I will need to use a search engine as this information can change frequently and is dependent on current events or recent reports.\n", - "Action:\n", - "```\n", - "{\n", - " \"action\": \"Search\",\n", - " \"action_input\": \"Leonardo DiCaprio current girlfriend 2023\"\n", - "}\n", - "```\n", - "\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mVittoria Ceretti\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mThought: Now that I know Leonardo DiCaprio's current girlfriend is Vittoria Ceretti, I need to find out her age to calculate the 0.43rd power of it.\n", + "\u001b[32;1m\u001b[1;3mThought: The question is asking for the current girlfriend of Leonardo DiCaprio and then to calculate the 0.43 power of her age. To answer the first part, I need to find out who Leonardo DiCaprio's current girlfriend is, which is likely to be a piece of publicly available information. For the second part, once I have her age, I will use the Calculator tool to compute the 0.43 power of that number.\n", + "\n", + "Action: Search\n", + "Action Input: Leonardo DiCaprio current girlfriend 2023\u001b[0m\u001b[36;1m\u001b[1;3mVittoria Ceretti\u001b[0m\u001b[32;1m\u001b[1;3mNow that I know Leonardo DiCaprio's current girlfriend is Vittoria Ceretti, I need to find out her age to calculate the 0.43 power of it.\n", + "\n", + "Action: Search\n", + "Action Input: Vittoria Ceretti age\u001b[0m\u001b[36;1m\u001b[1;3m26 years\u001b[0m\u001b[32;1m\u001b[1;3mNow that I know Vittoria Ceretti is 26 years old, I can calculate the 0.43 power of her age using the Calculator tool.\n", "\n", - "Action:\n", - "```\n", - "{\n", - " \"action\": \"Search\",\n", - " \"action_input\": \"Vittoria Ceretti age\"\n", - "}\n", - "```\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m26 years\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mNow that I know Vittoria Ceretti is 26 years old, I can calculate the 0.43rd power of her age.\n", + "Action: Calculator\n", + "Action Input: 26 ** 0.43\u001b[0m\u001b[33;1m\u001b[1;3mAnswer: 4.059182145592686\u001b[0m\u001b[32;1m\u001b[1;3mI now know the final answer to the second part of the question. The 0.43 power of Vittoria Ceretti's age, who is 26 years old, is approximately 4.06.\n", "\n", - "Action:\n", - "```\n", - "{\n", - " \"action\": \"Calculator\",\n", - " \"action_input\": {\n", - " \"expression\": \"26^0.43\"\n", - " }\n", - "}\n", - "```\n", - "\u001b[0m\n", - "Observation: \u001b[33;1m\u001b[1;3mAnswer: 4.059182145592686\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer to the second part of the question.\n", - "Final Answer: Vittoria Ceretti's age to the power of 0.43 is approximately 4.06.\u001b[0m\n", + "Final Answer: Leonardo DiCaprio's current girlfriend is Vittoria Ceretti, and the 0.43 power of her age, which is 26, is approximately 4.06.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -233,10 +245,10 @@ "data": { "text/plain": [ "{'input': '谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?',\n", - " 'output': \"Vittoria Ceretti's age to the power of 0.43 is approximately 4.06.\"}" + " 'output': \"Leonardo DiCaprio's current girlfriend is Vittoria Ceretti, and the 0.43 power of her age, which is 26, is approximately 4.06.\"}" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -245,8 +257,13 @@ "from langchain_openai import ChatOpenAI\n", "\n", "chat_model = ChatOpenAI(model=\"gpt-4-1106-preview\", temperature=0)\n", - "agent = initialize_agent(tools, chat_model, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n", - "agent.invoke(\"谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?\")" + "# agent = initialize_agent(tools, chat_model, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n", + "# agent.invoke(\"谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?\")\n", + "prompt = hub.pull(\"hwchase17/react\")\n", + "\n", + "agent = create_react_agent(chat_model, tools, prompt)\n", + "agent_executor = AgentExecutor(agent=agent, tools=tools)\n", + "agent_executor.invoke({\"input\":\"谁是莱昂纳多·迪卡普里奥的女朋友?她现在年龄的0.43次方是多少?\"})" ] }, {