Skip to content

Commit

Permalink
fix: bing search response filter (langgenius#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly authored Feb 22, 2024
1 parent 1ecbd95 commit d8ab447
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 23 deletions.
2 changes: 1 addition & 1 deletion api/core/tools/entities/tool_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ToolParameterForm(Enum):
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
Expand Down
82 changes: 66 additions & 16 deletions api/core/tools/provider/builtin/bing/tools/bing_web_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Union
from urllib.parse import quote

from requests import get

Expand Down Expand Up @@ -34,6 +35,18 @@ def _invoke(self,

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'
Expand All @@ -42,35 +55,72 @@ def _invoke(self,
'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))
69 changes: 67 additions & 2 deletions api/core/tools/provider/builtin/bing/tools/bing_web_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,7 +107,7 @@ parameters:
default: 5
- name: result_type
type: select
required: false
required: true
label:
en_US: result type
zh_Hans: 结果类型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const SettingBuiltInTool: FC<Props> = ({
</div>
)

const setttingUI = (
const settingUI = (
<Form
value={tempSetting}
onChange={setTempSetting}
Expand Down Expand Up @@ -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'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion web/app/components/tools/utils/to-form-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d8ab447

Please sign in to comment.