mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
85e8423312
Make BingSearchResults tool name OpenAI functions compatible (can't have spaces). Fixes #16368
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""Tool for the Bing search API."""
|
|
|
|
from typing import Optional
|
|
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
from langchain_core.tools import BaseTool
|
|
|
|
from langchain_community.utilities.bing_search import BingSearchAPIWrapper
|
|
|
|
|
|
class BingSearchRun(BaseTool):
|
|
"""Tool that queries the Bing search API."""
|
|
|
|
name: str = "bing_search"
|
|
description: str = (
|
|
"A wrapper around Bing Search. "
|
|
"Useful for when you need to answer questions about current events. "
|
|
"Input should be a search query."
|
|
)
|
|
api_wrapper: BingSearchAPIWrapper
|
|
|
|
def _run(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the tool."""
|
|
return self.api_wrapper.run(query)
|
|
|
|
|
|
class BingSearchResults(BaseTool):
|
|
"""Tool that queries the Bing Search API and gets back json."""
|
|
|
|
name: str = "bing_search_results_json"
|
|
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"
|
|
)
|
|
num_results: int = 4
|
|
api_wrapper: BingSearchAPIWrapper
|
|
|
|
def _run(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the tool."""
|
|
return str(self.api_wrapper.results(query, self.num_results))
|