From 3145995ed9b70644ea767a674a1f5417a878207d Mon Sep 17 00:00:00 2001 From: Qingchuan Hao Date: Tue, 3 Sep 2024 05:11:32 +0800 Subject: [PATCH] community[patch]: BingSearchResults returns raw snippets as artifact(#23304) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Returns an array of results which is more specific and easier for later use. Tested locally: ``` resp = tool.invoke("what's the weather like in Shanghai?") for item in resp: print(item) ``` returns ``` {'snippet': 'Shanghai, Shanghai, China Weather Forecast, with current conditions, wind, air quality, and what to expect for the next 3 days.', 'title': 'Shanghai, Shanghai, China Weather Forecast | AccuWeather', 'link': 'https://www.accuweather.com/en/cn/shanghai/106577/weather-forecast/106577'} {'snippet': '5. 99 / 87 °F. 6. 99 / 86 °F. 7. Detailed forecast for 14 days. Need some help? Current weather in Shanghai and forecast for today, tomorrow, and next 14 days.', 'title': 'Weather for Shanghai, Shanghai Municipality, China - timeanddate.com', 'link': 'https://www.timeanddate.com/weather/china/shanghai'} {'snippet': 'Shanghai - Weather warnings issued 14-day forecast. Weather warnings issued. Forecast - Shanghai. Day by day forecast. Last updated Friday at 01:05. Tonight, ... Temperature feels like 34 ...', 'title': 'Shanghai - BBC Weather', 'link': 'https://www.bbc.com/weather/1796236'} {'snippet': 'Current weather in Shanghai, Shanghai, China. Check current conditions in Shanghai, Shanghai, China with radar, hourly, and more.', 'title': 'Shanghai, Shanghai, China Current Weather | AccuWeather', 'link': 'https://www.accuweather.com/en/cn/shanghai/106577/current-weather/106577'} 13-Day Beijing, Xi'an, Chengdu, Shanghai Chinese Language and Culture Immersion Tour. Shanghai in September. Average daily temperature range: 23–29°C (73–84°F) Average rainy days: 10. Average sunny days: 20. September ushers in pleasant autumn weather, making it one of the best months to visit Shanghai. Weather in Shanghai: Climate, Seasons, and Average Monthly Temperature. Shanghai has a subtropical maritime monsoon climate, meaning high humidity and lots of rain. Hot muggy summers, cool falls, cold winters with little snow, and warm springs are the norm. Midsummer through early fall is the best time to visit Shanghai. Shanghai, Shanghai, China Weather Forecast, with current conditions, wind, air quality, and what to expect for the next 3 days. 1165. 45.9. 121. Winter, from December to February, is quite cold: the average January temperature is 5 °C (41 °F). There may be cold periods, with highs around 5 °C (41 °F) or below, and occasionally, even snow can fall. The temperature dropped to -10 °C (14 °F) in January 1977 and to -7 °C (19.5 °F) in January 2016. 5. 99 / 87 °F. 6. 99 / 86 °F. 7. Detailed forecast for 14 days. Need some help? Current weather in Shanghai and forecast for today, tomorrow, and next 14 days. Everything you need to know about today's weather in Shanghai, Shanghai, China. High/Low, Precipitation Chances, Sunrise/Sunset, and today's Temperature History. Shanghai - Weather warnings issued 14-day forecast. Weather warnings issued. Forecast - Shanghai. Day by day forecast. Last updated Friday at 01:05. Tonight, ... Temperature feels like 34 ... Shanghai 14 Day Extended Forecast. Weather Today Weather Hourly 14 Day Forecast Yesterday/Past Weather Climate (Averages) Currently: 84 °F. Passing clouds. (Weather station: Shanghai Hongqiao Airport, China). See more current weather. Current weather in Shanghai, Shanghai, China. Check current conditions in Shanghai, Shanghai, China with radar, hourly, and more. Shanghai Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the Shanghai area. ``` --------- Co-authored-by: Bagatur --- .../tools/bing_search/tool.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/libs/community/langchain_community/tools/bing_search/tool.py b/libs/community/langchain_community/tools/bing_search/tool.py index 63bac6191fb..9c05405f825 100644 --- a/libs/community/langchain_community/tools/bing_search/tool.py +++ b/libs/community/langchain_community/tools/bing_search/tool.py @@ -1,6 +1,6 @@ """Tool for the Bing search API.""" -from typing import Optional +from typing import Dict, List, Literal, Optional, Tuple from langchain_core.callbacks import CallbackManagerForToolRun from langchain_core.tools import BaseTool @@ -51,7 +51,7 @@ class BingSearchResults(BaseTool): Invocation with args: .. code-block:: python - tool.invoke("what is the weather in SF?") + tool.invoke({"query": "what is the weather in SF?"}) .. code-block:: python @@ -65,7 +65,12 @@ class BingSearchResults(BaseTool): .. code-block:: python - ToolMessage(content="[{'snippet': 'Get the latest weather forecast for San Francisco, CA, including temperature, RealFeel, and chance of precipitation. Find out how the weather will affect your plans and activities in the city of ...', 'title': 'San Francisco, CA Weather Forecast | AccuWeather', 'link': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629'}, {'snippet': 'Radar. Be prepared with the most accurate 10-day forecast for San Francisco, CA with highs, lows, chance of precipitation from The Weather Channel and Weather.com.', 'title': '10-Day Weather Forecast for San Francisco, CA - The Weather Channel', 'link': 'https://weather.com/weather/tenday/l/San+Francisco+CA+USCA0987:1:US'}, {'snippet': 'Tropical Storm Ernesto Forms; Fire Weather Concerns in the Great Basin: Hot Temperatures Return to the South-Central U.S. ... San Francisco CA 37.77°N 122.41°W (Elev. 131 ft) Last Update: 2:21 pm PDT Aug 12, 2024. Forecast Valid: 6pm PDT Aug 12, 2024-6pm PDT Aug 19, 2024 .', 'title': 'National Weather Service', 'link': 'https://forecast.weather.gov/zipcity.php?inputstring=San+Francisco,CA'}, {'snippet': 'Current weather in San Francisco, CA. Check current conditions in San Francisco, CA with radar, hourly, and more.', 'title': 'San Francisco, CA Current Weather | AccuWeather', 'link': 'https://www.accuweather.com/en/us/san-francisco/94103/current-weather/347629'}]", name='bing_search_results_json', tool_call_id='1') + ToolMessage( + content="[{'snippet': 'Get the latest weather forecast for San Francisco, CA, including temperature, RealFeel, and chance of precipitation. Find out how the weather will affect your plans and activities in the city of ...', 'title': 'San Francisco, CA Weather Forecast | AccuWeather', 'link': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629'}, {'snippet': 'Radar. Be prepared with the most accurate 10-day forecast for San Francisco, CA with highs, lows, chance of precipitation from The Weather Channel and Weather.com.', 'title': '10-Day Weather Forecast for San Francisco, CA - The Weather Channel', 'link': 'https://weather.com/weather/tenday/l/San+Francisco+CA+USCA0987:1:US'}, {'snippet': 'Tropical Storm Ernesto Forms; Fire Weather Concerns in the Great Basin: Hot Temperatures Return to the South-Central U.S. ... San Francisco CA 37.77°N 122.41°W (Elev. 131 ft) Last Update: 2:21 pm PDT Aug 12, 2024. Forecast Valid: 6pm PDT Aug 12, 2024-6pm PDT Aug 19, 2024 .', 'title': 'National Weather Service', 'link': 'https://forecast.weather.gov/zipcity.php?inputstring=San+Francisco,CA'}, {'snippet': 'Current weather in San Francisco, CA. Check current conditions in San Francisco, CA with radar, hourly, and more.', 'title': 'San Francisco, CA Current Weather | AccuWeather', 'link': 'https://www.accuweather.com/en/us/san-francisco/94103/current-weather/347629'}]", + artifact=[{'snippet': 'Get the latest weather forecast for San Francisco, CA, including temperature, RealFeel, and chance of precipitation. Find out how the weather will affect your plans and activities in the city of ...', 'title': 'San Francisco, CA Weather Forecast | AccuWeather', 'link': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629'}, {'snippet': 'Radar. Be prepared with the most accurate 10-day forecast for San Francisco, CA with highs, lows, chance of precipitation from The Weather Channel and Weather.com.', 'title': '10-Day Weather Forecast for San Francisco, CA - The Weather Channel', 'link': 'https://weather.com/weather/tenday/l/San+Francisco+CA+USCA0987:1:US'}, {'snippet': 'Tropical Storm Ernesto Forms; Fire Weather Concerns in the Great Basin: Hot Temperatures Return to the South-Central U.S. ... San Francisco CA 37.77°N 122.41°W (Elev. 131 ft) Last Update: 2:21 pm PDT Aug 12, 2024. Forecast Valid: 6pm PDT Aug 12, 2024-6pm PDT Aug 19, 2024 .', 'title': 'National Weather Service', 'link': 'https://forecast.weather.gov/zipcity.php?inputstring=San+Francisco,CA'}, {'snippet': 'Current weather in San Francisco, CA. Check current conditions in San Francisco, CA with radar, hourly, and more.', 'title': 'San Francisco, CA Current Weather | AccuWeather', 'link': 'https://www.accuweather.com/en/us/san-francisco/94103/current-weather/347629'}], + name='bing_search_results_json', + tool_call_id='1' + ) """ # noqa: E501 @@ -73,15 +78,21 @@ class BingSearchResults(BaseTool): description: str = ( "A wrapper around Bing Search. " "Useful for when you need to answer questions about current events. " - "Input should be a search query. Output is a JSON array of the query results" + "Input should be a search query. Output is an array of the query results." ) num_results: int = 4 + """Max search results to return, default is 4.""" api_wrapper: BingSearchAPIWrapper + response_format: Literal["content_and_artifact"] = "content_and_artifact" def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, - ) -> str: + ) -> Tuple[str, List[Dict]]: """Use the tool.""" - return str(self.api_wrapper.results(query, self.num_results)) + try: + results = self.api_wrapper.results(query, self.num_results) + return str(results), results + except Exception as e: + return repr(e), []