Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: bing search response filter (#2519)

Yeuolyd8ab4474b4e5
diff --git a/api/core/tools/entities/tool_entities.py b/api/core/tools/entities/tool_entities.py
index b4579ec65a4..9754698bdd1 100644
--- a/api/core/tools/entities/tool_entities.py
+++ b/api/core/tools/entities/tool_entities.py
@@ -113,7 +113,7 @@ class ToolParameter(BaseModel):
form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
llm_description: Optional[str] = None
required: Optional[bool] = False
- default: Optional[str] = None
+ default: Optional[Union[bool, str, int]] = None
min: Optional[Union[float, int]] = None
max: Optional[Union[float, int]] = None
options: Optional[list[ToolParameterOption]] = None
diff --git a/api/core/tools/provider/builtin/bing/tools/bing_web_search.py b/api/core/tools/provider/builtin/bing/tools/bing_web_search.py
index 7568b733cfa..7b740293dd2 100644
--- a/api/core/tools/provider/builtin/bing/tools/bing_web_search.py
+++ b/api/core/tools/provider/builtin/bing/tools/bing_web_search.py
@@ -1,4 +1,5 @@
from typing import Any, Union
+from urllib.parse import quote
from requests import get
@@ -34,6 +35,18 @@ class BingSearchTool(BuiltinTool):
market = tool_parameters.get('market', 'US')
lang = tool_parameters.get('language', 'en')
+ filter = []
+
+ if tool_parameters.get('enable_computation', False):
+ filter.append('Computation')
+ if tool_parameters.get('enable_entities', False):
+ filter.append('Entities')
+ if tool_parameters.get('enable_news', False):
+ filter.append('News')
+ if tool_parameters.get('enable_related_search', False):
+ filter.append('RelatedSearches')
+ if tool_parameters.get('enable_webpages', False):
+ filter.append('WebPages')
market_code = f'{lang}-{market}'
accept_language = f'{lang},{market_code};q=0.9'
@@ -42,35 +55,72 @@ class BingSearchTool(BuiltinTool):
'Accept-Language': accept_language
}
- params = {
- 'q': query,
- 'mkt': market_code
- }
-
- response = get(server_url, headers=headers, params=params)
+ query = quote(query)
+ server_url = f'{server_url}?q={query}&mkt={market_code}&count={limit}&responseFilter={",".join(filter)}'
+ response = get(server_url, headers=headers)
if response.status_code != 200:
raise Exception(f'Error {response.status_code}: {response.text}')
response = response.json()
- search_results = response['webPages']['value'][:limit]
+ search_results = response['webPages']['value'][:limit] if 'webPages' in response else []
+ related_searches = response['relatedSearches']['value'] if 'relatedSearches' in response else []
+ entities = response['entities']['value'] if 'entities' in response else []
+ news = response['news']['value'] if 'news' in response else []
+ computation = response['computation']['value'] if 'computation' in response else None
if result_type == 'link':
results = []
- for result in search_results:
- results.append(self.create_text_message(
- text=f'{result["name"]}: {result["url"]}'
- ))
+ if search_results:
+ for result in search_results:
+ results.append(self.create_text_message(
+ text=f'{result["name"]}: {result["url"]}'
+ ))
+
+
+ if entities:
+ for entity in entities:
+ results.append(self.create_text_message(
+ text=f'{entity["name"]}: {entity["url"]}'
+ ))
+ if news:
+ for news_item in news:
+ results.append(self.create_text_message(
+ text=f'{news_item["name"]}: {news_item["url"]}'
+ ))
+
+ if related_searches:
+ for related in related_searches:
+ results.append(self.create_text_message(
+ text=f'{related["displayText"]}: {related["webSearchUrl"]}'
+ ))
+
return results
else:
# construct text
text = ''
- for i, result in enumerate(search_results):
- text += f'{i+1}: {result["name"]} - {result["snippet"]}\n'
+ if search_results:
+ for i, result in enumerate(search_results):
+ text += f'{i+1}: {result["name"]} - {result["snippet"]}\n'
+
+ if computation and 'expression' in computation and 'value' in computation:
+ text += '\nComputation:\n'
+ text += f'{computation["expression"]} = {computation["value"]}\n'
+
+ if entities:
+ text += '\nEntities:\n'
+ for entity in entities:
+ text += f'{entity["name"]} - {entity["url"]}\n'
+
+ if news:
+ text += '\nNews:\n'
+ for news_item in news:
+ text += f'{news_item["name"]} - {news_item["url"]}\n'
- text += '\n\nRelated Searches:\n'
- for related in response['relatedSearches']['value']:
- text += f'{related["displayText"]} - {related["webSearchUrl"]}\n'
+ if related_searches:
+ text += '\n\nRelated Searches:\n'
+ for related in related_searches:
+ text += f'{related["displayText"]} - {related["webSearchUrl"]}\n'
return self.create_text_message(text=self.summary(user_id=user_id, content=text))
diff --git a/api/core/tools/provider/builtin/bing/tools/bing_web_search.yaml b/api/core/tools/provider/builtin/bing/tools/bing_web_search.yaml
index 329a83f12fb..6bf64efb999 100644
--- a/api/core/tools/provider/builtin/bing/tools/bing_web_search.yaml
+++ b/api/core/tools/provider/builtin/bing/tools/bing_web_search.yaml
@@ -25,9 +25,74 @@ parameters:
zh_Hans: 用于搜索网页内容
pt_BR: used for searching
llm_description: key words for searching
+ - name: enable_computation
+ type: boolean
+ required: false
+ form: form
+ label:
+ en_US: Enable computation
+ zh_Hans: 启用计算
+ pt_BR: Enable computation
+ human_description:
+ en_US: enable computation
+ zh_Hans: 启用计算
+ pt_BR: enable computation
+ default: false
+ - name: enable_entities
+ type: boolean
+ required: false
+ form: form
+ label:
+ en_US: Enable entities
+ zh_Hans: 启用实体搜索
+ pt_BR: Enable entities
+ human_description:
+ en_US: enable entities
+ zh_Hans: 启用实体搜索
+ pt_BR: enable entities
+ default: true
+ - name: enable_news
+ type: boolean
+ required: false
+ form: form
+ label:
+ en_US: Enable news
+ zh_Hans: 启用新闻搜索
+ pt_BR: Enable news
+ human_description:
+ en_US: enable news
+ zh_Hans: 启用新闻搜索
+ pt_BR: enable news
+ default: false
+ - name: enable_related_search
+ type: boolean
+ required: false
+ form: form
+ label:
+ en_US: Enable related search
+ zh_Hans: 启用相关搜索
+ pt_BR: Enable related search
+ human_description:
+ en_US: enable related search
+ zh_Hans: 启用相关搜索
+ pt_BR: enable related search
+ default: false
+ - name: enable_webpages
+ type: boolean
+ required: false
+ form: form
+ label:
+ en_US: Enable webpages search
+ zh_Hans: 启用网页搜索
+ pt_BR: Enable webpages search
+ human_description:
+ en_US: enable webpages search
+ zh_Hans: 启用网页搜索
+ pt_BR: enable webpages search
+ default: true
- name: limit
type: number
- required: false
+ required: true
form: form
label:
en_US: Limit for results length
@@ -42,7 +107,7 @@ parameters:
default: 5
- name: result_type
type: select
- required: false
+ required: true
label:
en_US: result type
zh_Hans: 结果类型
diff --git a/web/app/components/app/configuration/config/agent/agent-tools/setting-built-in-tool.tsx b/web/app/components/app/configuration/config/agent/agent-tools/setting-built-in-tool.tsx
index 55b554bfcba..e1c99e04326 100644
--- a/web/app/components/app/configuration/config/agent/agent-tools/setting-built-in-tool.tsx
+++ b/web/app/components/app/configuration/config/agent/agent-tools/setting-built-in-tool.tsx
@@ -116,7 +116,7 @@ const SettingBuiltInTool: FC<Props> = ({
</div>
)
- const setttingUI = (
+ const settingUI = (
<Form
value={tempSetting}
onChange={setTempSetting}
@@ -174,7 +174,7 @@ const SettingBuiltInTool: FC<Props> = ({
</div>
: (<div className='flex flex-col h-full'>
<div className='grow h-0 overflow-y-auto px-6'>
- {isInfoActive ? infoUI : setttingUI}
+ {isInfoActive ? infoUI : settingUI}
</div>
{!readonly && !isInfoActive && (
<div className='mt-2 shrink-0 flex justify-end py-4 px-6 space-x-2 rounded-b-[10px] bg-gray-50 border-t border-black/5'>
diff --git a/web/app/components/header/account-setting/model-provider-page/model-modal/Form.tsx b/web/app/components/header/account-setting/model-provider-page/model-modal/Form.tsx
index 365cefc26a5..db436a15478 100644
--- a/web/app/components/header/account-setting/model-provider-page/model-modal/Form.tsx
+++ b/web/app/components/header/account-setting/model-provider-page/model-modal/Form.tsx
@@ -17,6 +17,7 @@ import Input from './Input'
import { SimpleSelect } from '@/app/components/base/select'
import Tooltip from '@/app/components/base/tooltip-plus'
import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
+import Radio from '@/app/components/base/radio'
type FormProps = {
value: FormValue
onChange: (val: FormValue) => void
@@ -47,7 +48,7 @@ const Form: FC<FormProps> = ({
const language = useLanguage()
const [changeKey, setChangeKey] = useState('')
- const handleFormChange = (key: string, val: string) => {
+ const handleFormChange = (key: string, val: string | boolean) => {
if (isEditMode && (key === '__model_type' || key === '__model_name'))
return
@@ -214,6 +215,37 @@ const Form: FC<FormProps> = ({
</div>
)
}
+
+ if (formSchema.type === 'boolean') {
+ const {
+ variable,
+ label,
+ show_on,
+ } = formSchema as CredentialFormSchemaRadio
+
+ if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
+ return null
+
+ return (
+ <div key={variable} className='py-3'>
+ <div className='flex items-center justify-between py-2 text-sm text-gray-900'>
+ <div className='flex items-center space-x-2'>
+ <span>{label[language]}</span>
+ {tooltipContent}
+ </div>
+ <Radio.Group
+ className='flex items-center'
+ value={value[variable] ? 1 : 0}
+ onChange={val => handleFormChange(variable, val === 1)}
+ >
+ <Radio value={1} className='!mr-1'>True</Radio>
+ <Radio value={0}>False</Radio>
+ </Radio.Group>
+ </div>
+ {fieldMoreInfo?.(formSchema)}
+ </div>
+ )
+ }
}
return (
diff --git a/web/app/components/tools/utils/to-form-schema.ts b/web/app/components/tools/utils/to-form-schema.ts
index 50b4fc63e83..f428e9bca46 100644
--- a/web/app/components/tools/utils/to-form-schema.ts
+++ b/web/app/components/tools/utils/to-form-schema.ts
@@ -57,7 +57,7 @@ export const addDefaultValue = (value: Record<string, any>, formSchemas: { varia
const newValues = { ...value }
formSchemas.forEach((formSchema) => {
const itemValue = value[formSchema.variable]
- if (formSchema.default && (value === undefined || itemValue === null || itemValue === ''))
+ if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined))
newValues[formSchema.variable] = formSchema.default
})
return newValues