mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-11 03:46:52 +08:00
chore: apply flake8-comprehensions Ruff rules to improve collection comprehensions (#5652)
Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -105,15 +105,15 @@ class BingSearchTool(BuiltinTool):
|
||||
|
||||
|
||||
def validate_credentials(self, credentials: dict[str, Any], tool_parameters: dict[str, Any]) -> None:
|
||||
key = credentials.get('subscription_key', None)
|
||||
key = credentials.get('subscription_key')
|
||||
if not key:
|
||||
raise Exception('subscription_key is required')
|
||||
|
||||
server_url = credentials.get('server_url', None)
|
||||
server_url = credentials.get('server_url')
|
||||
if not server_url:
|
||||
server_url = self.url
|
||||
|
||||
query = tool_parameters.get('query', None)
|
||||
query = tool_parameters.get('query')
|
||||
if not query:
|
||||
raise Exception('query is required')
|
||||
|
||||
@@ -170,7 +170,7 @@ class BingSearchTool(BuiltinTool):
|
||||
if not server_url:
|
||||
server_url = self.url
|
||||
|
||||
query = tool_parameters.get('query', None)
|
||||
query = tool_parameters.get('query')
|
||||
if not query:
|
||||
raise Exception('query is required')
|
||||
|
||||
|
||||
@@ -16,12 +16,12 @@ class BarChartTool(BuiltinTool):
|
||||
data = data.split(';')
|
||||
|
||||
# if all data is int, convert to int
|
||||
if all([i.isdigit() for i in data]):
|
||||
if all(i.isdigit() for i in data):
|
||||
data = [int(i) for i in data]
|
||||
else:
|
||||
data = [float(i) for i in data]
|
||||
|
||||
axis = tool_parameters.get('x_axis', None) or None
|
||||
axis = tool_parameters.get('x_axis') or None
|
||||
if axis:
|
||||
axis = axis.split(';')
|
||||
if len(axis) != len(data):
|
||||
|
||||
@@ -17,14 +17,14 @@ class LinearChartTool(BuiltinTool):
|
||||
return self.create_text_message('Please input data')
|
||||
data = data.split(';')
|
||||
|
||||
axis = tool_parameters.get('x_axis', None) or None
|
||||
axis = tool_parameters.get('x_axis') or None
|
||||
if axis:
|
||||
axis = axis.split(';')
|
||||
if len(axis) != len(data):
|
||||
axis = None
|
||||
|
||||
# if all data is int, convert to int
|
||||
if all([i.isdigit() for i in data]):
|
||||
if all(i.isdigit() for i in data):
|
||||
data = [int(i) for i in data]
|
||||
else:
|
||||
data = [float(i) for i in data]
|
||||
|
||||
@@ -16,10 +16,10 @@ class PieChartTool(BuiltinTool):
|
||||
if not data:
|
||||
return self.create_text_message('Please input data')
|
||||
data = data.split(';')
|
||||
categories = tool_parameters.get('categories', None) or None
|
||||
categories = tool_parameters.get('categories') or None
|
||||
|
||||
# if all data is int, convert to int
|
||||
if all([i.isdigit() for i in data]):
|
||||
if all(i.isdigit() for i in data):
|
||||
data = [int(i) for i in data]
|
||||
else:
|
||||
data = [float(i) for i in data]
|
||||
|
||||
@@ -37,10 +37,10 @@ class GaodeRepositoriesTool(BuiltinTool):
|
||||
apikey=self.runtime.credentials.get('api_key')))
|
||||
weatherInfo_data = weatherInfo_response.json()
|
||||
if weatherInfo_response.status_code == 200 and weatherInfo_data.get('info') == 'OK':
|
||||
contents = list()
|
||||
contents = []
|
||||
if len(weatherInfo_data.get('forecasts')) > 0:
|
||||
for item in weatherInfo_data['forecasts'][0]['casts']:
|
||||
content = dict()
|
||||
content = {}
|
||||
content['date'] = item.get('date')
|
||||
content['week'] = item.get('week')
|
||||
content['dayweather'] = item.get('dayweather')
|
||||
|
||||
@@ -39,10 +39,10 @@ class GihubRepositoriesTool(BuiltinTool):
|
||||
f"q={quote(query)}&sort=stars&per_page={top_n}&order=desc")
|
||||
response_data = response.json()
|
||||
if response.status_code == 200 and isinstance(response_data.get('items'), list):
|
||||
contents = list()
|
||||
contents = []
|
||||
if len(response_data.get('items')) > 0:
|
||||
for item in response_data.get('items'):
|
||||
content = dict()
|
||||
content = {}
|
||||
updated_at_object = datetime.strptime(item['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
|
||||
content['owner'] = item['owner']['login']
|
||||
content['name'] = item['name']
|
||||
|
||||
@@ -26,11 +26,11 @@ class JinaReaderTool(BuiltinTool):
|
||||
if 'api_key' in self.runtime.credentials and self.runtime.credentials.get('api_key'):
|
||||
headers['Authorization'] = "Bearer " + self.runtime.credentials.get('api_key')
|
||||
|
||||
target_selector = tool_parameters.get('target_selector', None)
|
||||
target_selector = tool_parameters.get('target_selector')
|
||||
if target_selector is not None and target_selector != '':
|
||||
headers['X-Target-Selector'] = target_selector
|
||||
|
||||
wait_for_selector = tool_parameters.get('wait_for_selector', None)
|
||||
wait_for_selector = tool_parameters.get('wait_for_selector')
|
||||
if wait_for_selector is not None and wait_for_selector != '':
|
||||
headers['X-Wait-For-Selector'] = wait_for_selector
|
||||
|
||||
@@ -43,7 +43,7 @@ class JinaReaderTool(BuiltinTool):
|
||||
if tool_parameters.get('gather_all_images_at_the_end', False):
|
||||
headers['X-With-Images-Summary'] = 'true'
|
||||
|
||||
proxy_server = tool_parameters.get('proxy_server', None)
|
||||
proxy_server = tool_parameters.get('proxy_server')
|
||||
if proxy_server is not None and proxy_server != '':
|
||||
headers['X-Proxy-Url'] = proxy_server
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ class JinaSearchTool(BuiltinTool):
|
||||
if tool_parameters.get('gather_all_images_at_the_end', False):
|
||||
headers['X-With-Images-Summary'] = 'true'
|
||||
|
||||
proxy_server = tool_parameters.get('proxy_server', None)
|
||||
proxy_server = tool_parameters.get('proxy_server')
|
||||
if proxy_server is not None and proxy_server != '':
|
||||
headers['X-Proxy-Url'] = proxy_server
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ class GoogleTool(BuiltinTool):
|
||||
google_domain = tool_parameters.get("google_domain", "google.com")
|
||||
gl = tool_parameters.get("gl", "us")
|
||||
hl = tool_parameters.get("hl", "en")
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
api_key = self.runtime.credentials['searchapi_api_key']
|
||||
result = SearchAPI(api_key).run(query, result_type=result_type, num=num, google_domain=google_domain, gl=gl, hl=hl, location=location)
|
||||
|
||||
@@ -72,11 +72,11 @@ class GoogleJobsTool(BuiltinTool):
|
||||
"""
|
||||
query = tool_parameters['query']
|
||||
result_type = tool_parameters['result_type']
|
||||
is_remote = tool_parameters.get("is_remote", None)
|
||||
is_remote = tool_parameters.get("is_remote")
|
||||
google_domain = tool_parameters.get("google_domain", "google.com")
|
||||
gl = tool_parameters.get("gl", "us")
|
||||
hl = tool_parameters.get("hl", "en")
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
ltype = 1 if is_remote else None
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ class GoogleNewsTool(BuiltinTool):
|
||||
google_domain = tool_parameters.get("google_domain", "google.com")
|
||||
gl = tool_parameters.get("gl", "us")
|
||||
hl = tool_parameters.get("hl", "en")
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
api_key = self.runtime.credentials['searchapi_api_key']
|
||||
result = SearchAPI(api_key).run(query, result_type=result_type, num=num, google_domain=google_domain, gl=gl, hl=hl, location=location)
|
||||
|
||||
@@ -107,7 +107,7 @@ class SearXNGSearchTool(BuiltinTool):
|
||||
if not host:
|
||||
raise Exception('SearXNG api is required')
|
||||
|
||||
query = tool_parameters.get('query', None)
|
||||
query = tool_parameters.get('query')
|
||||
if not query:
|
||||
return self.create_text_message('Please input query')
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ class GetMarkdownTool(BuiltinTool):
|
||||
Invoke the SerplyApi tool.
|
||||
"""
|
||||
url = tool_parameters["url"]
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
api_key = self.runtime.credentials["serply_api_key"]
|
||||
result = SerplyApi(api_key).run(url, location=location)
|
||||
|
||||
@@ -55,7 +55,7 @@ class SerplyApi:
|
||||
f"Employer: {job['employer']}",
|
||||
f"Location: {job['location']}",
|
||||
f"Link: {job['link']}",
|
||||
f"""Highest: {", ".join([h for h in job["highlights"]])}""",
|
||||
f"""Highest: {", ".join(list(job["highlights"]))}""",
|
||||
"---",
|
||||
])
|
||||
)
|
||||
@@ -78,7 +78,7 @@ class JobSearchTool(BuiltinTool):
|
||||
query = tool_parameters["query"]
|
||||
gl = tool_parameters.get("gl", "us")
|
||||
hl = tool_parameters.get("hl", "en")
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
api_key = self.runtime.credentials["serply_api_key"]
|
||||
result = SerplyApi(api_key).run(query, gl=gl, hl=hl, location=location)
|
||||
|
||||
@@ -80,7 +80,7 @@ class NewsSearchTool(BuiltinTool):
|
||||
query = tool_parameters["query"]
|
||||
gl = tool_parameters.get("gl", "us")
|
||||
hl = tool_parameters.get("hl", "en")
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
api_key = self.runtime.credentials["serply_api_key"]
|
||||
result = SerplyApi(api_key).run(query, gl=gl, hl=hl, location=location)
|
||||
|
||||
@@ -83,7 +83,7 @@ class ScholarSearchTool(BuiltinTool):
|
||||
query = tool_parameters["query"]
|
||||
gl = tool_parameters.get("gl", "us")
|
||||
hl = tool_parameters.get("hl", "en")
|
||||
location = tool_parameters.get("location", None)
|
||||
location = tool_parameters.get("location")
|
||||
|
||||
api_key = self.runtime.credentials["serply_api_key"]
|
||||
result = SerplyApi(api_key).run(query, gl=gl, hl=hl, location=location)
|
||||
|
||||
@@ -38,7 +38,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
|
||||
super().__init__(**{
|
||||
'identity': provider_yaml['identity'],
|
||||
'credentials_schema': provider_yaml['credentials_for_provider'] if 'credentials_for_provider' in provider_yaml else None,
|
||||
'credentials_schema': provider_yaml.get('credentials_for_provider', None),
|
||||
})
|
||||
|
||||
def _get_builtin_tools(self) -> list[Tool]:
|
||||
|
||||
@@ -159,8 +159,8 @@ class ApiTool(Tool):
|
||||
for content_type in self.api_bundle.openapi['requestBody']['content']:
|
||||
headers['Content-Type'] = content_type
|
||||
body_schema = self.api_bundle.openapi['requestBody']['content'][content_type]['schema']
|
||||
required = body_schema['required'] if 'required' in body_schema else []
|
||||
properties = body_schema['properties'] if 'properties' in body_schema else {}
|
||||
required = body_schema.get('required', [])
|
||||
properties = body_schema.get('properties', {})
|
||||
for name, property in properties.items():
|
||||
if name in parameters:
|
||||
# convert type
|
||||
|
||||
@@ -90,7 +90,7 @@ class DatasetRetrieverTool(Tool):
|
||||
"""
|
||||
invoke dataset retriever tool
|
||||
"""
|
||||
query = tool_parameters.get('query', None)
|
||||
query = tool_parameters.get('query')
|
||||
if not query:
|
||||
return self.create_text_message(text='please input query')
|
||||
|
||||
|
||||
@@ -209,7 +209,7 @@ class ToolManager:
|
||||
|
||||
if parameter_rule.type == ToolParameter.ToolParameterType.SELECT:
|
||||
# check if tool_parameter_config in options
|
||||
options = list(map(lambda x: x.value, parameter_rule.options))
|
||||
options = [x.value for x in parameter_rule.options]
|
||||
if parameter_value is not None and parameter_value not in options:
|
||||
raise ValueError(
|
||||
f"tool parameter {parameter_rule.name} value {parameter_value} not in options {options}")
|
||||
|
||||
@@ -21,10 +21,7 @@ class ApiBasedToolSchemaParser:
|
||||
extra_info = extra_info if extra_info is not None else {}
|
||||
|
||||
# set description to extra_info
|
||||
if 'description' in openapi['info']:
|
||||
extra_info['description'] = openapi['info']['description']
|
||||
else:
|
||||
extra_info['description'] = ''
|
||||
extra_info['description'] = openapi['info'].get('description', '')
|
||||
|
||||
if len(openapi['servers']) == 0:
|
||||
raise ToolProviderNotFoundError('No server found in the openapi yaml.')
|
||||
@@ -95,8 +92,8 @@ class ApiBasedToolSchemaParser:
|
||||
# parse body parameters
|
||||
if 'schema' in interface['operation']['requestBody']['content'][content_type]:
|
||||
body_schema = interface['operation']['requestBody']['content'][content_type]['schema']
|
||||
required = body_schema['required'] if 'required' in body_schema else []
|
||||
properties = body_schema['properties'] if 'properties' in body_schema else {}
|
||||
required = body_schema.get('required', [])
|
||||
properties = body_schema.get('properties', {})
|
||||
for name, property in properties.items():
|
||||
tool = ToolParameter(
|
||||
name=name,
|
||||
@@ -105,14 +102,14 @@ class ApiBasedToolSchemaParser:
|
||||
zh_Hans=name
|
||||
),
|
||||
human_description=I18nObject(
|
||||
en_US=property['description'] if 'description' in property else '',
|
||||
zh_Hans=property['description'] if 'description' in property else ''
|
||||
en_US=property.get('description', ''),
|
||||
zh_Hans=property.get('description', '')
|
||||
),
|
||||
type=ToolParameter.ToolParameterType.STRING,
|
||||
required=name in required,
|
||||
form=ToolParameter.ToolParameterForm.LLM,
|
||||
llm_description=property['description'] if 'description' in property else '',
|
||||
default=property['default'] if 'default' in property else None,
|
||||
llm_description=property.get('description', ''),
|
||||
default=property.get('default', None),
|
||||
)
|
||||
|
||||
# check if there is a type
|
||||
@@ -149,7 +146,7 @@ class ApiBasedToolSchemaParser:
|
||||
server_url=server_url + interface['path'],
|
||||
method=interface['method'],
|
||||
summary=interface['operation']['description'] if 'description' in interface['operation'] else
|
||||
interface['operation']['summary'] if 'summary' in interface['operation'] else None,
|
||||
interface['operation'].get('summary', None),
|
||||
operation_id=interface['operation']['operationId'],
|
||||
parameters=parameters,
|
||||
author='',
|
||||
|
||||
@@ -283,7 +283,7 @@ def strip_control_characters(text):
|
||||
# [Cn]: Other, Not Assigned
|
||||
# [Co]: Other, Private Use
|
||||
# [Cs]: Other, Surrogate
|
||||
control_chars = set(['Cc', 'Cf', 'Cn', 'Co', 'Cs'])
|
||||
control_chars = {'Cc', 'Cf', 'Cn', 'Co', 'Cs'}
|
||||
retained_chars = ['\t', '\n', '\r', '\f']
|
||||
|
||||
# Remove non-printing control characters
|
||||
|
||||
Reference in New Issue
Block a user