From d8ab4474b4e5a987595ce1c3825d5d20efda798d Mon Sep 17 00:00:00 2001 From: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Date: Thu, 22 Feb 2024 13:06:55 +0800 Subject: [PATCH] fix: bing search response filter (#2519) --- api/core/tools/entities/tool_entities.py | 2 +- .../builtin/bing/tools/bing_web_search.py | 82 +++++++++++++++---- .../builtin/bing/tools/bing_web_search.yaml | 69 +++++++++++++++- .../agent-tools/setting-built-in-tool.tsx | 4 +- .../model-provider-page/model-modal/Form.tsx | 34 +++++++- .../components/tools/utils/to-form-schema.ts | 2 +- 6 files changed, 170 insertions(+), 23 deletions(-) diff --git a/api/core/tools/entities/tool_entities.py b/api/core/tools/entities/tool_entities.py index b4579ec65a..9754698bdd 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 7568b733cf..7b740293dd 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' - text += '\n\nRelated Searches:\n' - for related in response['relatedSearches']['value']: - text += f'{related["displayText"]} - {related["webSearchUrl"]}\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' + + 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 329a83f12f..6bf64efb99 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 55b554bfcb..e1c99e0432 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 = ({ ) - const setttingUI = ( + const settingUI = (
= ({ : (
- {isInfoActive ? infoUI : setttingUI} + {isInfoActive ? infoUI : settingUI}
{!readonly && !isInfoActive && (
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 365cefc26a..db436a1547 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 = ({ 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 = ({
) } + + 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 ( +
+
+
+ {label[language]} + {tooltipContent} +
+ handleFormChange(variable, val === 1)} + > + True + False + +
+ {fieldMoreInfo?.(formSchema)} +
+ ) + } } return ( diff --git a/web/app/components/tools/utils/to-form-schema.ts b/web/app/components/tools/utils/to-form-schema.ts index 50b4fc63e8..f428e9bca4 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, 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