mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-07-24 05:54:26 +08:00
fix: bing search response filter (#2519)
This commit is contained in:
parent
1ecbd95adf
commit
d8ab4474b4
@ -113,7 +113,7 @@ class ToolParameter(BaseModel):
|
|||||||
form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
|
form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
|
||||||
llm_description: Optional[str] = None
|
llm_description: Optional[str] = None
|
||||||
required: Optional[bool] = False
|
required: Optional[bool] = False
|
||||||
default: Optional[str] = None
|
default: Optional[Union[bool, str, int]] = None
|
||||||
min: Optional[Union[float, int]] = None
|
min: Optional[Union[float, int]] = None
|
||||||
max: Optional[Union[float, int]] = None
|
max: Optional[Union[float, int]] = None
|
||||||
options: Optional[list[ToolParameterOption]] = None
|
options: Optional[list[ToolParameterOption]] = None
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from typing import Any, Union
|
from typing import Any, Union
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
from requests import get
|
from requests import get
|
||||||
|
|
||||||
@ -34,6 +35,18 @@ class BingSearchTool(BuiltinTool):
|
|||||||
|
|
||||||
market = tool_parameters.get('market', 'US')
|
market = tool_parameters.get('market', 'US')
|
||||||
lang = tool_parameters.get('language', 'en')
|
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}'
|
market_code = f'{lang}-{market}'
|
||||||
accept_language = f'{lang},{market_code};q=0.9'
|
accept_language = f'{lang},{market_code};q=0.9'
|
||||||
@ -42,35 +55,72 @@ class BingSearchTool(BuiltinTool):
|
|||||||
'Accept-Language': accept_language
|
'Accept-Language': accept_language
|
||||||
}
|
}
|
||||||
|
|
||||||
params = {
|
query = quote(query)
|
||||||
'q': query,
|
server_url = f'{server_url}?q={query}&mkt={market_code}&count={limit}&responseFilter={",".join(filter)}'
|
||||||
'mkt': market_code
|
response = get(server_url, headers=headers)
|
||||||
}
|
|
||||||
|
|
||||||
response = get(server_url, headers=headers, params=params)
|
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise Exception(f'Error {response.status_code}: {response.text}')
|
raise Exception(f'Error {response.status_code}: {response.text}')
|
||||||
|
|
||||||
response = response.json()
|
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':
|
if result_type == 'link':
|
||||||
results = []
|
results = []
|
||||||
for result in search_results:
|
if search_results:
|
||||||
results.append(self.create_text_message(
|
for result in search_results:
|
||||||
text=f'{result["name"]}: {result["url"]}'
|
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
|
return results
|
||||||
else:
|
else:
|
||||||
# construct text
|
# construct text
|
||||||
text = ''
|
text = ''
|
||||||
for i, result in enumerate(search_results):
|
if search_results:
|
||||||
text += f'{i+1}: {result["name"]} - {result["snippet"]}\n'
|
for i, result in enumerate(search_results):
|
||||||
|
text += f'{i+1}: {result["name"]} - {result["snippet"]}\n'
|
||||||
|
|
||||||
text += '\n\nRelated Searches:\n'
|
if computation and 'expression' in computation and 'value' in computation:
|
||||||
for related in response['relatedSearches']['value']:
|
text += '\nComputation:\n'
|
||||||
text += f'{related["displayText"]} - {related["webSearchUrl"]}\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))
|
return self.create_text_message(text=self.summary(user_id=user_id, content=text))
|
||||||
|
@ -25,9 +25,74 @@ parameters:
|
|||||||
zh_Hans: 用于搜索网页内容
|
zh_Hans: 用于搜索网页内容
|
||||||
pt_BR: used for searching
|
pt_BR: used for searching
|
||||||
llm_description: key words 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
|
- name: limit
|
||||||
type: number
|
type: number
|
||||||
required: false
|
required: true
|
||||||
form: form
|
form: form
|
||||||
label:
|
label:
|
||||||
en_US: Limit for results length
|
en_US: Limit for results length
|
||||||
@ -42,7 +107,7 @@ parameters:
|
|||||||
default: 5
|
default: 5
|
||||||
- name: result_type
|
- name: result_type
|
||||||
type: select
|
type: select
|
||||||
required: false
|
required: true
|
||||||
label:
|
label:
|
||||||
en_US: result type
|
en_US: result type
|
||||||
zh_Hans: 结果类型
|
zh_Hans: 结果类型
|
||||||
|
@ -116,7 +116,7 @@ const SettingBuiltInTool: FC<Props> = ({
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
||||||
const setttingUI = (
|
const settingUI = (
|
||||||
<Form
|
<Form
|
||||||
value={tempSetting}
|
value={tempSetting}
|
||||||
onChange={setTempSetting}
|
onChange={setTempSetting}
|
||||||
@ -174,7 +174,7 @@ const SettingBuiltInTool: FC<Props> = ({
|
|||||||
</div>
|
</div>
|
||||||
: (<div className='flex flex-col h-full'>
|
: (<div className='flex flex-col h-full'>
|
||||||
<div className='grow h-0 overflow-y-auto px-6'>
|
<div className='grow h-0 overflow-y-auto px-6'>
|
||||||
{isInfoActive ? infoUI : setttingUI}
|
{isInfoActive ? infoUI : settingUI}
|
||||||
</div>
|
</div>
|
||||||
{!readonly && !isInfoActive && (
|
{!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'>
|
<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'>
|
||||||
|
@ -17,6 +17,7 @@ import Input from './Input'
|
|||||||
import { SimpleSelect } from '@/app/components/base/select'
|
import { SimpleSelect } from '@/app/components/base/select'
|
||||||
import Tooltip from '@/app/components/base/tooltip-plus'
|
import Tooltip from '@/app/components/base/tooltip-plus'
|
||||||
import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
|
import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
|
||||||
|
import Radio from '@/app/components/base/radio'
|
||||||
type FormProps = {
|
type FormProps = {
|
||||||
value: FormValue
|
value: FormValue
|
||||||
onChange: (val: FormValue) => void
|
onChange: (val: FormValue) => void
|
||||||
@ -47,7 +48,7 @@ const Form: FC<FormProps> = ({
|
|||||||
const language = useLanguage()
|
const language = useLanguage()
|
||||||
const [changeKey, setChangeKey] = useState('')
|
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'))
|
if (isEditMode && (key === '__model_type' || key === '__model_name'))
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -214,6 +215,37 @@ const Form: FC<FormProps> = ({
|
|||||||
</div>
|
</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 (
|
return (
|
||||||
|
@ -57,7 +57,7 @@ export const addDefaultValue = (value: Record<string, any>, formSchemas: { varia
|
|||||||
const newValues = { ...value }
|
const newValues = { ...value }
|
||||||
formSchemas.forEach((formSchema) => {
|
formSchemas.forEach((formSchema) => {
|
||||||
const itemValue = value[formSchema.variable]
|
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
|
newValues[formSchema.variable] = formSchema.default
|
||||||
})
|
})
|
||||||
return newValues
|
return newValues
|
||||||
|
Loading…
x
Reference in New Issue
Block a user