From c5e0acf6f090afa38a61590c0b999cdad4cf5d9f Mon Sep 17 00:00:00 2001 From: Qingchuan Hao Date: Tue, 18 Jun 2024 06:08:52 +0800 Subject: [PATCH] docs: add bing search integration to agent (#22929) - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ --- .../docs/integrations/tools/bing_search.ipynb | 207 +++++++++++++++--- 1 file changed, 177 insertions(+), 30 deletions(-) diff --git a/docs/docs/integrations/tools/bing_search.ipynb b/docs/docs/integrations/tools/bing_search.ipynb index 1a54765d51..e441f64bb6 100644 --- a/docs/docs/integrations/tools/bing_search.ipynb +++ b/docs/docs/integrations/tools/bing_search.ipynb @@ -6,16 +6,17 @@ "source": [ "# Bing Search\n", "\n", - ">[Microsoft Bing](https://www.bing.com/), commonly referred to as `Bing` or `Bing Search`, is a web search engine owned and operated by `Microsoft`." + "[Microsoft Bing](https://www.bing.com/), commonly referred to as `Bing` or `Bing Search`, is a web search engine owned and operated by `Microsoft`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook goes over how to use the bing search component.\n", + "## Setup\n", + "Following the [instruction](https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/create-bing-search-service-resource) to create Azure Bing Search v7 service, and get the subscription key\n", "\n", - "Then we will need to set some environment variables." + "The integration lives in the `langchain-community` package." ] }, { @@ -24,24 +25,25 @@ "metadata": {}, "outputs": [], "source": [ - "%pip install --upgrade --quiet langchain-community" + "%pip install -U langchain-community" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ + "import getpass\n", "import os\n", "\n", - "os.environ[\"BING_SUBSCRIPTION_KEY\"] = \"\"\n", + "os.environ[\"BING_SUBSCRIPTION_KEY\"] = getpass.getpass()\n", "os.environ[\"BING_SEARCH_URL\"] = \"https://api.bing.microsoft.com/v7.0/search\"" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -50,25 +52,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "search = BingSearchAPIWrapper()" + "search = BingSearchAPIWrapper(k=4)" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Thanks to the flexibility of Python and the powerful ecosystem of packages, the Azure CLI supports features such as autocompletion (in shells that support it), persistent credentials, JMESPath result parsing, lazy initialization, network-less unit tests, and more. Building an open-source and cross-platform Azure CLI with Python by Dan Taylor. Python releases by version number: Release version Release date Click for more. Python 3.11.1 Dec. 6, 2022 Download Release Notes. Python 3.10.9 Dec. 6, 2022 Download Release Notes. Python 3.9.16 Dec. 6, 2022 Download Release Notes. Python 3.8.16 Dec. 6, 2022 Download Release Notes. Python 3.7.16 Dec. 6, 2022 Download Release Notes. In this lesson, we will look at the += operator in Python and see how it works with several simple examples.. The operator ‘+=’ is a shorthand for the addition assignment operator.It adds two values and assigns the sum to a variable (left operand). W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more. This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well. For a description of standard objects and modules, see The Python Standard ... Python is a general-purpose, versatile, and powerful programming language. It's a great first language because Python code is concise and easy to read. Whatever you want to do, python can do it. From web development to machine learning to data science, Python is the language for you. To install Python using the Microsoft Store: Go to your Start menu (lower left Windows icon), type "Microsoft Store", select the link to open the store. Once the store is open, select Search from the upper-right menu and enter "Python". Select which version of Python you would like to use from the results under Apps. Under the “Python Releases for Mac OS X” heading, click the link for the Latest Python 3 Release - Python 3.x.x. As of this writing, the latest version was Python 3.8.4. Scroll to the bottom and click macOS 64-bit installer to start the download. When the installer is finished downloading, move on to the next step. Step 2: Run the Installer'" + "'Python is a versatile and powerful language that lets you work quickly and integrate systems more effectively. Learn how to get started, download the latest version, access documentation, find jobs, and join the Python community. Python is a popular programming language for various purposes. Find the latest version of Python for different operating systems, download release notes, and learn about the development process. Learn Python, a popular programming language for web applications, with examples, exercises, and references. Get certified by completing the PYTHON course at W3Schools. Learn the basic concepts and features of Python, a powerful and easy to learn programming language. The tutorial covers topics such as data structures, modules, classes, exceptions, input and output, and more. Learn why and how to use Python, a popular and easy-to-learn programming language. Find installation guides, tutorials, documentation, resources and FAQs for beginners and experienced programmers. Learn about Python, a high-level, general-purpose programming language with a focus on code readability and multiple paradigms. Find out its history, design, features, libraries, implementations, popularity, uses, and influences. Real Python offers tutorials, books, courses, and news for Python developers of all skill levels. Whether you want to learn Python basics, web development, data science, or machine learning, you can find useful articles and code examples here. Learn how to install, use, and extend Python 3.12.3, a popular programming language. Find tutorials, library references, API guides, FAQs, and more. Python is a powerful, fast, friendly and open-source language that runs everywhere. Learn how to get started, explore applications, join the community and access the latest news and events. Learn the basics of Python programming language with examples of numbers, text, variables, and operators. This tutorial covers the syntax, types, and features of Python for beginners.'" ] }, - "execution_count": 23, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -87,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -96,16 +98,16 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Thanks to the flexibility of Python and the powerful ecosystem of packages, the Azure CLI supports features such as autocompletion (in shells that support it), persistent credentials, JMESPath result parsing, lazy initialization, network-less unit tests, and more. Building an open-source and cross-platform Azure CLI with Python by Dan Taylor.'" + "'Python is a versatile and powerful language that lets you work quickly and integrate systems more effectively. Learn how to get started, download the latest version, access documentation, find jobs, and join the Python community.'" ] }, - "execution_count": 25, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -134,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -143,27 +145,30 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[{'snippet': 'Lady Alice. Pink Lady apples aren’t the only lady in the apple family. Lady Alice apples were discovered growing, thanks to bees pollinating, in Washington. They are smaller and slightly more stout in appearance than other varieties. Their skin color appears to have red and yellow stripes running from stem to butt.',\n", - " 'title': '25 Types of Apples - Jessica Gavin',\n", - " 'link': 'https://www.jessicagavin.com/types-of-apples/'},\n", - " {'snippet': 'Apples can do a lot for you, thanks to plant chemicals called flavonoids. And they have pectin, a fiber that breaks down in your gut. If you take off the apple’s skin before eating it, you won ...',\n", - " 'title': 'Apples: Nutrition & Health Benefits - WebMD',\n", - " 'link': 'https://www.webmd.com/food-recipes/benefits-apples'},\n", - " {'snippet': 'Apples boast many vitamins and minerals, though not in high amounts. However, apples are usually a good source of vitamin C. Vitamin C. Also called ascorbic acid, this vitamin is a common ...',\n", + "[{'snippet': 'Learn about the nutrients, antioxidants, and potential health effects of apples. Find out how apples may help with weight loss, diabetes, heart disease, and cancer.',\n", " 'title': 'Apples 101: Nutrition Facts and Health Benefits',\n", " 'link': 'https://www.healthline.com/nutrition/foods/apples'},\n", - " {'snippet': 'Weight management. The fibers in apples can slow digestion, helping one to feel greater satisfaction after eating. After following three large prospective cohorts of 133,468 men and women for 24 years, researchers found that higher intakes of fiber-rich fruits with a low glycemic load, particularly apples and pears, were associated with the least amount of weight gain over time.',\n", - " 'title': 'Apples | The Nutrition Source | Harvard T.H. Chan School of Public Health',\n", - " 'link': 'https://www.hsph.harvard.edu/nutritionsource/food-features/apples/'}]" + " {'snippet': 'Learn how apples can improve your health with their fiber, antioxidants, and phytochemicals. Find out the best types of apples for different purposes, how to buy and store them, and what side effects to watch out for.',\n", + " 'title': 'Apples: Nutrition and Health Benefits - WebMD',\n", + " 'link': 'https://www.webmd.com/food-recipes/benefits-apples'},\n", + " {'snippet': 'Apples are nutritious, filling, and versatile fruits that may lower your risk of various diseases. Learn how apples can support your weight loss, heart health, gut health, and brain health with scientific evidence.',\n", + " 'title': '10 Impressive Health Benefits of Apples',\n", + " 'link': 'https://www.healthline.com/nutrition/10-health-benefits-of-apples'},\n", + " {'snippet': 'An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica).Apple trees are cultivated worldwide and are the most widely grown species in the genus Malus.The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found.Apples have been grown for thousands of years in Eurasia and were introduced ...',\n", + " 'title': 'Apple - Wikipedia',\n", + " 'link': 'https://en.wikipedia.org/wiki/Apple'},\n", + " {'snippet': 'Learn about the most popular and diverse apples in the world, from ambrosia to winesap, with photos and descriptions. Find out their origins, flavors, uses, and nutritional benefits in this comprehensive guide to apples.',\n", + " 'title': '29 Types Of Apples From A to Z (With Photos!) - Live Eat Learn',\n", + " 'link': 'https://www.liveeatlearn.com/types-of-apples/'}]" ] }, - "execution_count": 27, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -171,6 +176,148 @@ "source": [ "search.results(\"apples\", 5)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tool Usage" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "BingSearchResults(api_wrapper=BingSearchAPIWrapper(bing_subscription_key='', bing_search_url='https://api.bing.microsoft.com/v7.0/search', k=10, search_kwargs={}))" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "\n", + "from langchain_community.tools.bing_search import BingSearchResults\n", + "from langchain_community.utilities import BingSearchAPIWrapper\n", + "\n", + "api_wrapper = BingSearchAPIWrapper()\n", + "tool = BingSearchResults(api_wrapper=api_wrapper)\n", + "tool" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'snippet': 'This chart shows the 14 day weather trend for 33.74°N 89.59°E with daily weather symbols, minimum and maximum temperatures, precipitation amount and probability.. The deviance is coloured within the temperature graph. The stronger the ups and downs, the more uncertain the forecast will be.', 'title': '14 Day Weather 33.74°N 89.59°E - meteoblue', 'link': 'https://www.meteoblue.com/en/weather/14-days/33.739N89.594E6216_Asia%2FShanghai'}\n", + "{'snippet': 'Get the monthly weather forecast for Huangpu District, Shanghai, China, including daily high/low, historical averages, to help you plan ahead.', 'title': 'Huangpu District, Shanghai, China Monthly Weather | AccuWeather', 'link': 'https://www.accuweather.com/en/cn/huangpu-district/60782/june-weather/60782'}\n", + "{'snippet': 'Shanghai Hongqiao Airport is 60 miles from 31°31'42.7"N, 120°24'12.7"E, so the actual climate in 31°31'42.7"N, 120°24'12.7"E can vary a bit. Based on weather reports collected during 1992–2021. Showing: All Year January February March April May June July August September October November December', 'title': 'Climate & Weather Averages in 31°31'42.7"N, 120°24'12.7"E, China', 'link': 'https://www.timeanddate.com/weather/@31.52853,120.40355/climate'}\n", + "{'snippet': 'Air Quality gives information using weather conditions, pollutants, and research from The Weather Channel and weather.com ... Today's Air Quality-Shanghai, People's Republic of China. 76.', 'title': 'Shanghai, People's Republic of China Weather', 'link': 'https://weather.com/forecast/air-quality/l/80415bb74d7ded500f89b569c51da53325719ddea6e1394485ad846e812e61d2'}\n" + ] + } + ], + "source": [ + "# .invoke wraps utility.results\n", + "response = tool.invoke(\"What is the weather in Shanghai\")\n", + "for item in list(response):\n", + " print(item)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Chaining\n", + "\n", + "We show here how to use it as part of an [agent](/docs/tutorials/agents). We use the OpenAI Functions Agent, so we will need to setup and install the required dependencies for that. We will also use [LangSmith Hub](https://smith.langchain.com/hub) to pull the prompt from, so we will need to install that." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# you need a model to use in the chain\n", + "%pip install --upgrade --quiet langchain langchain-openai langchainhub langchain-community" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m\n", + "Invoking: `bing_search_results_json` with `{'query': 'latest burning man floods'}`\n", + "\n", + "\n", + "\u001b[0m\u001b[36;1m\u001b[1;3m[{'snippet': 'Some festivalgoers have shared stories of their successful 6-mile hikes away from Burning Man. The worst of the rain Sunday is expected between 12 p.m. and 4 p.m. local time (3 p.m. to 7 p.m. ET ...', 'title': 'Live updates: Burning Man festival rain strands thousands in ... - CNN', 'link': 'https://www.cnn.com/us/live-news/nevada-desert-burning-man-weather-rain-09-03-23/index.html'}, {'snippet': 'Black Rock Forest, where around 70,000 Burning Man attendees are gathered for the festival, is in the northwest. "Flash flooding caused by excessive rainfall" is possible in parts of eastern ...', 'title': 'Burning Man flooding keeps thousands stranded at Nevada site as ...', 'link': 'https://www.nbcnews.com/news/us-news/live-blog/live-updates-burning-man-flooding-keeps-thousands-stranded-nevada-site-rcna103193'}, {'snippet': 'Thousands of Burning Man attendees finally made their mass exodus after intense rain over the weekend flooded camp sites and filled them with thick, ankle-deep mud – stranding more than 70,000 ...', 'title': 'Burning Man attendees make a mass exodus after a dramatic weekend ... - CNN', 'link': 'https://www.cnn.com/2023/09/05/us/burning-man-storms-shelter-exodus-tuesday/index.html'}, {'snippet': 'You’re going to get stuck,” hosts on Burning Man Information Radio, broadcasting from within the event, told festivalgoers early on Sept. 4. According to NBC News, the Pershing County Sheriff ...', 'title': 'Burning Man attendees make mass exodus after being stranded in ... - TODAY', 'link': 'https://www.today.com/news/what-is-burning-man-flood-death-rcna103231'}]\u001b[0m\u001b[32;1m\u001b[1;3mThe latest Burning Man festival experienced heavy rain and flooding, which resulted in thousands of festivalgoers being stranded. Some attendees had to hike for miles to safety. The rain caused flash flooding in parts of the festival site, including the Black Rock Forest where the event takes place. Campsites were flooded, and thick mud made movement difficult. Eventually, after the intense rain over the weekend, attendees were able to make a mass exodus from the festival. The Pershing County Sheriff's Office warned festivalgoers about the flooding and encouraged them to leave for safety.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "{'input': 'What happened in the latest burning man floods?',\n", + " 'output': \"The latest Burning Man festival experienced heavy rain and flooding, which resulted in thousands of festivalgoers being stranded. Some attendees had to hike for miles to safety. The rain caused flash flooding in parts of the festival site, including the Black Rock Forest where the event takes place. Campsites were flooded, and thick mud made movement difficult. Eventually, after the intense rain over the weekend, attendees were able to make a mass exodus from the festival. The Pershing County Sheriff's Office warned festivalgoers about the flooding and encouraged them to leave for safety.\"}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import getpass\n", + "import os\n", + "\n", + "from langchain import hub\n", + "from langchain.agents import AgentExecutor, create_openai_functions_agent\n", + "from langchain_openai import AzureChatOpenAI\n", + "\n", + "os.environ[\"AZURE_OPENAI_API_KEY\"] = getpass.getpass()\n", + "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://.openai.azure.com/\"\n", + "os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"2023-06-01-preview\"\n", + "os.environ[\"AZURE_OPENAI_DEPLOYMENT_NAME\"] = \"\"\n", + "\n", + "instructions = \"\"\"You are an assistant.\"\"\"\n", + "base_prompt = hub.pull(\"langchain-ai/openai-functions-template\")\n", + "prompt = base_prompt.partial(instructions=instructions)\n", + "llm = AzureChatOpenAI(\n", + " openai_api_key=os.environ[\"AZURE_OPENAI_API_KEY\"],\n", + " azure_endpoint=os.environ[\"AZURE_OPENAI_ENDPOINT\"],\n", + " azure_deployment=os.environ[\"AZURE_OPENAI_DEPLOYMENT_NAME\"],\n", + " openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n", + ")\n", + "tool = BingSearchResults(api_wrapper=api_wrapper)\n", + "tools = [tool]\n", + "agent = create_openai_functions_agent(llm, tools, prompt)\n", + "agent_executor = AgentExecutor(\n", + " agent=agent,\n", + " tools=tools,\n", + " verbose=True,\n", + ")\n", + "agent_executor.invoke({\"input\": \"What happened in the latest burning man floods?\"})" + ] } ], "metadata": { @@ -189,7 +336,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.14" }, "vscode": { "interpreter": {