Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
b814f0b7
Unverified
Commit
b814f0b7
authored
Feb 04, 2024
by
Yeuoly
Committed by
GitHub
Feb 04, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: bing search (#2375)
parent
65bec16f
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
610 additions
and
0 deletions
+610
-0
_positions.py
api/core/tools/provider/builtin/_positions.py
+1
-0
icon.png
api/core/tools/provider/builtin/bing/_assets/icon.png
+0
-0
bing.py
api/core/tools/provider/builtin/bing/bing.py
+23
-0
bing.yaml
api/core/tools/provider/builtin/bing/bing.yaml
+45
-0
bing_web_search.py
...core/tools/provider/builtin/bing/tools/bing_web_search.py
+61
-0
bing_web_search.yaml
...re/tools/provider/builtin/bing/tools/bing_web_search.yaml
+480
-0
No files found.
api/core/tools/provider/builtin/_positions.py
View file @
b814f0b7
...
@@ -4,6 +4,7 @@ from core.tools.entities.user_entities import UserToolProvider
...
@@ -4,6 +4,7 @@ from core.tools.entities.user_entities import UserToolProvider
position
=
{
position
=
{
'google'
:
1
,
'google'
:
1
,
'bing'
:
2
,
'wikipedia'
:
2
,
'wikipedia'
:
2
,
'dalle'
:
3
,
'dalle'
:
3
,
'webscraper'
:
4
,
'webscraper'
:
4
,
...
...
api/core/tools/provider/builtin/bing/_assets/icon.png
0 → 100644
View file @
b814f0b7
4.52 KB
api/core/tools/provider/builtin/bing/bing.py
0 → 100644
View file @
b814f0b7
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
))
api/core/tools/provider/builtin/bing/bing.yaml
0 → 100644
View file @
b814f0b7
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
api/core/tools/provider/builtin/bing/tools/bing_web_search.py
0 → 100644
View file @
b814f0b7
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
\ No newline at end of file
api/core/tools/provider/builtin/bing/tools/bing_web_search.yaml
0 → 100644
View file @
b814f0b7
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment