{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyMl2q/nwBstRfIi1uOWucfB", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# LangChain Agent 示例" ], "metadata": { "id": "GIUvp6uXWAcn" } }, { "cell_type": "markdown", "source": [ "## 准备环境" ], "metadata": { "id": "h4Ps0wdmWFGh" } }, { "cell_type": "markdown", "source": [ "1. 安装langchain版本0.0.235" ], "metadata": { "id": "E8OKcwR6VdHL" } }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "yDa7VdF9SlAm" }, "outputs": [], "source": [ "!pip install -q -U langchain==0.0.235" ] }, { "cell_type": "markdown", "source": [ "2. 安装duckduckgo-search包\n", " \n", " 在使用DuckDuckGoSearchRun这个类时会需要这个python包。" ], "metadata": { "id": "WgipamGdVnYL" } }, { "cell_type": "code", "source": [ "!pip install -q -U duckduckgo-search" ], "metadata": { "id": "faW8Qfw1TJaf" }, "execution_count": 17, "outputs": [] }, { "cell_type": "markdown", "source": [ "## 示例代码" ], "metadata": { "id": "i0wXx4wfWIjF" } }, { "cell_type": "markdown", "source": [ "### Tool" ], "metadata": { "id": "lEPqka5QWcph" } }, { "cell_type": "markdown", "source": [ "1. 通过工具类创建工具实例" ], "metadata": { "id": "xnhIUB4rVTmZ" } }, { "cell_type": "code", "source": [ "from langchain.tools import DuckDuckGoSearchRun\n", "\n", "search = DuckDuckGoSearchRun()\n", "search.run(\"Who is winner of FIFA worldcup 2018?\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 202 }, "id": "u7Qj56OFTMus", "outputId": "f011f0da-5687-4978-cbc6-bcf2e5fd0930" }, "execution_count": 7, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\"The 2018 FIFA World Cup was the 21st FIFA World Cup, the quadrennial world championship for national football teams organized by FIFA.It took place in Russia from 14 June to 15 July 2018, after the country was awarded the hosting rights in 2010. It was the eleventh time the championships had been held in Europe, and the first time they were held in Eastern Europe. Who won the last World Cup at Russia 2018? Revisiting France's championship run | Sporting News EVE LEI 12:00p +100 CHE ARS 12:00p -160 WHU MCI 12:00p -475 FUL LIV 12:00p -360 MUN BHA Thu,... World Cup 2018: The Winners and Losers. Luka Modric and Kylian Mbappe during the World Cup final. Jewel Samad/Agence France-Presse — Getty Images. France won the World Cup, but that is far from ... Brazil have won it five times, while Germany and Italy have four titles each. Argentina, France and Uruguay have each won the World Cup twice, while England and Spain have each won it once. Post... Didier Deschamps was the manager of the French team that won the 2018 FIFA World Cup, making him the third individual to win the title both as a player and a manager. Mario Zagallo (Brazil) and Franz Beckenbauer (Germany) have also achieved the feat.\"" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 7 } ] }, { "cell_type": "markdown", "source": [ "2. 通过辅助函数 `load_tools` 加载" ], "metadata": { "id": "8yK-vVhIVV8L" } }, { "cell_type": "code", "source": [ "from langchain.agents import load_tools\n", "\n", "tools = load_tools(['ddg-search'])\n", "search = tools[0]\n", "search.run(\"Who is winner of FIFA worldcup 2018?\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 202 }, "id": "gHfGofDRV4Nn", "outputId": "22e3433d-38fb-4d7f-ef4e-c4d7a16b2c89" }, "execution_count": 11, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\"The 2018 FIFA World Cup was the 21st FIFA World Cup, the quadrennial world championship for national football teams organized by FIFA.It took place in Russia from 14 June to 15 July 2018, after the country was awarded the hosting rights in 2010. It was the eleventh time the championships had been held in Europe, and the first time they were held in Eastern Europe. FIFA World Cup Qatar 2022™. World Cup memories of a French superfan. 6 May 2020. 2018 FIFA World Cup Russia™. #WorldCupAtHome: Electric Mbappe helps France win seven-goal thriller. 9 Apr 2020 ... Who won the last World Cup at Russia 2018? Revisiting France's championship run | Sporting News EVE LEI 12:00p +100 CHE ARS 12:00p -160 WHU MCI 12:00p -475 FUL LIV 12:00p -360 MUN BHA Thu,... World Cup 2018: The Winners and Losers. Luka Modric and Kylian Mbappe during the World Cup final. Jewel Samad/Agence France-Presse — Getty Images. France won the World Cup, but that is far from ... Antoine Griezmann, Paul Pogba and Kylian Mbappé each scored in the final against their unexpected foe, Croatia. The 19-year-old Mbappé became the first teenager to score in a World Cup final...\"" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 11 } ] }, { "cell_type": "markdown", "source": [ "3. 查看 `LangChain` 内置支持的工具列表" ], "metadata": { "id": "zuNvBf2uWSMv" } }, { "cell_type": "code", "source": [ "from langchain.agents import get_all_tool_names\n", "\n", "get_all_tool_names()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "pWjMoK-daWl8", "outputId": "8fc38cde-4038-44e3-cc06-8c9a2e870180" }, "execution_count": 12, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['python_repl',\n", " 'requests',\n", " 'requests_get',\n", " 'requests_post',\n", " 'requests_patch',\n", " 'requests_put',\n", " 'requests_delete',\n", " 'terminal',\n", " 'sleep',\n", " 'wolfram-alpha',\n", " 'google-search',\n", " 'google-search-results-json',\n", " 'searx-search-results-json',\n", " 'bing-search',\n", " 'metaphor-search',\n", " 'ddg-search',\n", " 'google-serper',\n", " 'google-serper-results-json',\n", " 'serpapi',\n", " 'twilio',\n", " 'searx-search',\n", " 'wikipedia',\n", " 'arxiv',\n", " 'pupmed',\n", " 'human',\n", " 'awslambda',\n", " 'sceneXplain',\n", " 'graphql',\n", " 'openweathermap-api',\n", " 'dataforseo-api-search',\n", " 'dataforseo-api-search-json',\n", " 'news-api',\n", " 'tmdb-api',\n", " 'podcast-api',\n", " 'pal-math',\n", " 'pal-colored-objects',\n", " 'llm-math',\n", " 'open-meteo-api']" ] }, "metadata": {}, "execution_count": 12 } ] }, { "cell_type": "markdown", "source": [ "### Agent" ], "metadata": { "id": "IITv5SLpcNi8" } }, { "cell_type": "markdown", "source": [ "#### 额外的环境准备\n", "\n", "这里我们会用到OpenAI的模型,因此安装python包 `openai` 。" ], "metadata": { "id": "X6fBRd3MWkvZ" } }, { "cell_type": "code", "source": [ "!pip install -q -U openai" ], "metadata": { "id": "3EkfTNMEdLGl" }, "execution_count": 18, "outputs": [] }, { "cell_type": "markdown", "source": [ "一个 `Agent` 的例子" ], "metadata": { "id": "q8y0lMWIWt4m" } }, { "cell_type": "code", "source": [ "from langchain.agents import load_tools\n", "from langchain.agents import initialize_agent\n", "from langchain.agents import AgentType\n", "from langchain.llms import OpenAI\n", "\n", "import os\n", "os.environ['OPENAI_API_KEY'] = \"\"\n", "\n", "llm = OpenAI(temperature=0)\n", "tools = load_tools([\"ddg-search\", \"llm-math\"], llm=llm)\n", "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n", "agent.run(\"What is the height difference between Eiffel Tower and Taiwan 101 Tower?\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 407 }, "id": "1rkToli6cSlT", "outputId": "447ab4e9-845e-4bfa-d173-09a04c95c21a" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m I need to find out the heights of both towers.\n", "Action: duckduckgo_search\n", "Action Input: \"Eiffel Tower height\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mIt was the first structure in the world to surpass both the 200-metre and 300-metre mark in height. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Eiffel's concept of a 300-metre (984-foot) tower built almost entirely of open-lattice wrought iron aroused amazement, skepticism, and no little opposition on aesthetic grounds. When completed, the tower served as the entrance gateway to the exposition. Built for the 1889 World's Fair in Paris, the Eiffel Tower is a 1,000-foot tall wrought iron tower, considered an architectural wonder and one of the world's most recognizable structures. The Eiffel Tower reached the height of 330 meters (1,083 feet) on March 15, 2022 thanks to this successful and dizzying operation carried out by helicopter. Which was a first ! Browse our resources on the links between the Tower and radio. Discover how did radio save the Tower. All about the Tower's Scientific Uses Height of the Eiffel Tower. The main structure of the is Eiffel Tower is 300 meters tall, although the height including antennas is 324 meters. This height is roughly equivalent to that of an 81-storey building. Interestingly, the Eiffel Tower can shrink by 6 inches during cold temperatures. The tower has a square base that measures 125 meters ...\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to find out the height of Taiwan 101 Tower.\n", "Action: duckduckgo_search\n", "Action Input: \"Taiwan 101 Tower height\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mDesigned by C.Y. Lee & Partners, a local architectural firm, the skyscraper has 101 stories and reaches a height, including the spire, of 1,667 feet (508 metres). At the time of its official opening in October 2004, it was the world's tallest building, having surpassed the Petronas Twin Towers in Kuala Lumpur, Malaysia. Taipei 101 is the tallest building in Taiwan. The elevators of Taipei 101 that transport passengers from the 5th to the 89th floor in 37 seconds (attaining 60.6 km/h ... The height of 101 floors commemorates the renewal of time: the new century that arrived as the tower was built (100+1) and all the new years that follow (1 January = 1-01 ... Last updated: Sep 1, 2021 • 3 min read Taipei 101, located in Taiwan, is one of the tallest high-rise buildings in the world. Visitors can explore the building that includes a large shopping mall, world-class restaurants, and an outdoor observation deck. Learn From the Best Food Design & Style Arts & Entertainment Music Business Sports & Gaming With a height of 1667 feet or 508 meters, Taipei 101 has the distinction of being the third tallest building in the world, and tallest outside the Middle East. Its spire adds to its height, and the total makes it higher than the Kuala Lumpur Petronas Twin Towers. The Taipei 101 tower in Taipei, Taiwan, was the world's tallest building from 2004 until 2010 when it was beaten out by Dubai's impressive Burj Khalifa. Regardless, Taipei 101 is still considered the tallest green building in the world for its innovative and energy-saving design. Even the 2015-2016 New Year's Eve firework's show was nature ...\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the heights of both towers, so I can calculate the difference.\n", "Action: Calculator\n", "Action Input: 330 - 508\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3mAnswer: -178\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", "Final Answer: The height difference between Eiffel Tower and Taiwan 101 Tower is 178 meters.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "'The height difference between Eiffel Tower and Taiwan 101 Tower is 178 meters.'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 15 } ] } ] }