Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bing search #2375

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/core/tools/provider/builtin/_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

position = {
'google': 1,
'bing': 2,
'wikipedia': 2,
'dalle': 3,
'webscraper': 4,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions api/core/tools/provider/builtin/bing/bing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
from core.tools.errors import ToolProviderCredentialValidationError

from core.tools.provider.builtin.bing.tools.bing_web_search import BingSearchTool

from typing import Any, Dict, List

class BingProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
try:
BingSearchTool().fork_tool_runtime(
meta={
"credentials": credentials,
}
).invoke(
user_id='',
tool_parameters={
"query": "test",
"result_type": "link"
},
)
except Exception as e:
raise ToolProviderCredentialValidationError(str(e))
45 changes: 45 additions & 0 deletions api/core/tools/provider/builtin/bing/bing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
identity:
author: Dify
name: bing
label:
en_US: Bing
zh_Hans: Bing
pt_BR: Bing
description:
en_US: Bing Search
zh_Hans: Bing 搜索
pt_BR: Bing Search
icon: icon.png
credentials_for_provider:
subscription_key:
type: secret-input
required: true
label:
en_US: Bing subscription key
zh_Hans: Bing subscription key
pt_BR: Bing subscription key
placeholder:
en_US: Please input your Bing subscription key
zh_Hans: 请输入你的 Bing subscription key
pt_BR: Please input your Bing subscription key
help:
en_US: Get your Bing subscription key from Bing
zh_Hans: 从 Bing 获取您的 Bing subscription key
pt_BR: Get your Bing subscription key from Bing
url: https://www.microsoft.com/cognitive-services/en-us/bing-web-search-api
server_url:
type: text-input
required: false
label:
en_US: Bing endpoint
zh_Hans: Bing endpoint
pt_BR: Bing endpoint
placeholder:
en_US: Please input your Bing endpoint
zh_Hans: 请输入你的 Bing 端点
pt_BR: Please input your Bing endpoint
help:
en_US: An endpoint is like "https://api.bing.microsoft.com/v7.0/search"
zh_Hans: 例如 "https://api.bing.microsoft.com/v7.0/search"
pt_BR: An endpoint is like "https://api.bing.microsoft.com/v7.0/search"
default: https://api.bing.microsoft.com/v7.0/search
61 changes: 61 additions & 0 deletions api/core/tools/provider/builtin/bing/tools/bing_web_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from core.tools.tool.builtin_tool import BuiltinTool
from core.tools.entities.tool_entities import ToolInvokeMessage

from typing import Any, Dict, List, Union
from os import path
from requests import get

class BingSearchTool(BuiltinTool):
url = 'https://api.bing.microsoft.com/v7.0/search'

def _invoke(self,
user_id: str,
tool_parameters: Dict[str, Any],
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
"""
invoke tools
"""

key = self.runtime.credentials.get('subscription_key', None)
if not key:
raise Exception('subscription_key is required')

server_url = self.runtime.credentials.get('server_url', None)
if not server_url:
server_url = self.url

query = tool_parameters.get('query', None)
if not query:
raise Exception('query is required')

market = tool_parameters.get('market', 'US')
lang = tool_parameters.get('language', 'en')

market_code = f'{lang}-{market}'
accept_language = f'{lang},{market_code};q=0.9'
headers = {
'Ocp-Apim-Subscription-Key': key,
'Accept-Language': accept_language
}

params = {
'q': query,
'mkt': market_code
}

response = get(server_url, headers=headers, params=params)

if response.status_code != 200:
raise Exception(f'Error {response.status_code}: {response.text}')

response = response.json()
# get the first 5 results
search_results = response['webPages']['value'][:5]
results = []
for result in search_results:
results.append(self.create_text_message(
text=f'{result["name"]}: {result["url"]}'
))

return results

Loading
Loading