chore(api/core): apply ruff reformatting (#7624)

This commit is contained in:
Bowen Liang
2024-09-10 17:00:20 +08:00
committed by GitHub
parent 178730266d
commit 2cf1187b32
724 changed files with 21180 additions and 21123 deletions

View File

@@ -10,27 +10,29 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
class GoogleProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
try:
if credentials['api_key'] is None:
credentials['api_key'] = ''
if credentials["api_key"] is None:
credentials["api_key"] = ""
else:
result = JinaReaderTool().fork_tool_runtime(
runtime={
"credentials": credentials,
}
).invoke(
user_id='',
tool_parameters={
"url": "https://example.com",
},
)[0]
result = (
JinaReaderTool()
.fork_tool_runtime(
runtime={
"credentials": credentials,
}
)
.invoke(
user_id="",
tool_parameters={
"url": "https://example.com",
},
)[0]
)
message = json.loads(result.message)
if message['code'] != 200:
raise ToolProviderCredentialValidationError(message['message'])
if message["code"] != 200:
raise ToolProviderCredentialValidationError(message["message"])
except Exception as e:
raise ToolProviderCredentialValidationError(str(e))
def _get_tool_labels(self) -> list[ToolLabelEnum]:
return [
ToolLabelEnum.SEARCH, ToolLabelEnum.PRODUCTIVITY
]
return [ToolLabelEnum.SEARCH, ToolLabelEnum.PRODUCTIVITY]

View File

@@ -9,26 +9,25 @@ from core.tools.tool.builtin_tool import BuiltinTool
class JinaReaderTool(BuiltinTool):
_jina_reader_endpoint = 'https://r.jina.ai/'
_jina_reader_endpoint = "https://r.jina.ai/"
def _invoke(self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
"""
invoke tools
invoke tools
"""
url = tool_parameters['url']
url = tool_parameters["url"]
headers = {
'Accept': 'application/json'
}
headers = {"Accept": "application/json"}
if 'api_key' in self.runtime.credentials and self.runtime.credentials.get('api_key'):
headers['Authorization'] = "Bearer " + self.runtime.credentials.get('api_key')
if "api_key" in self.runtime.credentials and self.runtime.credentials.get("api_key"):
headers["Authorization"] = "Bearer " + self.runtime.credentials.get("api_key")
request_params = tool_parameters.get('request_params')
if request_params is not None and request_params != '':
request_params = tool_parameters.get("request_params")
if request_params is not None and request_params != "":
try:
request_params = json.loads(request_params)
if not isinstance(request_params, dict):
@@ -36,40 +35,40 @@ class JinaReaderTool(BuiltinTool):
except (json.JSONDecodeError, ValueError) as e:
raise ValueError(f"Invalid request_params: {e}")
target_selector = tool_parameters.get('target_selector')
if target_selector is not None and target_selector != '':
headers['X-Target-Selector'] = target_selector
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')
if wait_for_selector is not None and wait_for_selector != '':
headers['X-Wait-For-Selector'] = wait_for_selector
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
if tool_parameters.get('image_caption', False):
headers['X-With-Generated-Alt'] = 'true'
if tool_parameters.get("image_caption", False):
headers["X-With-Generated-Alt"] = "true"
if tool_parameters.get('gather_all_links_at_the_end', False):
headers['X-With-Links-Summary'] = 'true'
if tool_parameters.get("gather_all_links_at_the_end", False):
headers["X-With-Links-Summary"] = "true"
if tool_parameters.get('gather_all_images_at_the_end', False):
headers['X-With-Images-Summary'] = 'true'
if tool_parameters.get("gather_all_images_at_the_end", False):
headers["X-With-Images-Summary"] = "true"
proxy_server = tool_parameters.get('proxy_server')
if proxy_server is not None and proxy_server != '':
headers['X-Proxy-Url'] = proxy_server
proxy_server = tool_parameters.get("proxy_server")
if proxy_server is not None and proxy_server != "":
headers["X-Proxy-Url"] = proxy_server
if tool_parameters.get('no_cache', False):
headers['X-No-Cache'] = 'true'
if tool_parameters.get("no_cache", False):
headers["X-No-Cache"] = "true"
max_retries = tool_parameters.get('max_retries', 3)
max_retries = tool_parameters.get("max_retries", 3)
response = ssrf_proxy.get(
str(URL(self._jina_reader_endpoint + url)),
headers=headers,
params=request_params,
timeout=(10, 60),
max_retries=max_retries
max_retries=max_retries,
)
if tool_parameters.get('summary', False):
if tool_parameters.get("summary", False):
return self.create_text_message(self.summary(user_id, response.text))
return self.create_text_message(response.text)

View File

@@ -8,44 +8,39 @@ from core.tools.tool.builtin_tool import BuiltinTool
class JinaSearchTool(BuiltinTool):
_jina_search_endpoint = 'https://s.jina.ai/'
_jina_search_endpoint = "https://s.jina.ai/"
def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
query = tool_parameters['query']
query = tool_parameters["query"]
headers = {
'Accept': 'application/json'
}
headers = {"Accept": "application/json"}
if 'api_key' in self.runtime.credentials and self.runtime.credentials.get('api_key'):
headers['Authorization'] = "Bearer " + self.runtime.credentials.get('api_key')
if "api_key" in self.runtime.credentials and self.runtime.credentials.get("api_key"):
headers["Authorization"] = "Bearer " + self.runtime.credentials.get("api_key")
if tool_parameters.get('image_caption', False):
headers['X-With-Generated-Alt'] = 'true'
if tool_parameters.get("image_caption", False):
headers["X-With-Generated-Alt"] = "true"
if tool_parameters.get('gather_all_links_at_the_end', False):
headers['X-With-Links-Summary'] = 'true'
if tool_parameters.get("gather_all_links_at_the_end", False):
headers["X-With-Links-Summary"] = "true"
if tool_parameters.get('gather_all_images_at_the_end', False):
headers['X-With-Images-Summary'] = 'true'
if tool_parameters.get("gather_all_images_at_the_end", False):
headers["X-With-Images-Summary"] = "true"
proxy_server = tool_parameters.get('proxy_server')
if proxy_server is not None and proxy_server != '':
headers['X-Proxy-Url'] = proxy_server
proxy_server = tool_parameters.get("proxy_server")
if proxy_server is not None and proxy_server != "":
headers["X-Proxy-Url"] = proxy_server
if tool_parameters.get('no_cache', False):
headers['X-No-Cache'] = 'true'
if tool_parameters.get("no_cache", False):
headers["X-No-Cache"] = "true"
max_retries = tool_parameters.get('max_retries', 3)
max_retries = tool_parameters.get("max_retries", 3)
response = ssrf_proxy.get(
str(URL(self._jina_search_endpoint + query)),
headers=headers,
timeout=(10, 60),
max_retries=max_retries
str(URL(self._jina_search_endpoint + query)), headers=headers, timeout=(10, 60), max_retries=max_retries
)
return self.create_text_message(response.text)

View File

@@ -6,33 +6,29 @@ from core.tools.tool.builtin_tool import BuiltinTool
class JinaTokenizerTool(BuiltinTool):
_jina_tokenizer_endpoint = 'https://tokenize.jina.ai/'
_jina_tokenizer_endpoint = "https://tokenize.jina.ai/"
def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
) -> ToolInvokeMessage:
content = tool_parameters['content']
body = {
"content": content
}
content = tool_parameters["content"]
body = {"content": content}
headers = {
'Content-Type': 'application/json'
}
headers = {"Content-Type": "application/json"}
if 'api_key' in self.runtime.credentials and self.runtime.credentials.get('api_key'):
headers['Authorization'] = "Bearer " + self.runtime.credentials.get('api_key')
if "api_key" in self.runtime.credentials and self.runtime.credentials.get("api_key"):
headers["Authorization"] = "Bearer " + self.runtime.credentials.get("api_key")
if tool_parameters.get('return_chunks', False):
body['return_chunks'] = True
if tool_parameters.get('return_tokens', False):
body['return_tokens'] = True
if tokenizer := tool_parameters.get('tokenizer'):
body['tokenizer'] = tokenizer
if tool_parameters.get("return_chunks", False):
body["return_chunks"] = True
if tool_parameters.get("return_tokens", False):
body["return_tokens"] = True
if tokenizer := tool_parameters.get("tokenizer"):
body["tokenizer"] = tokenizer
response = ssrf_proxy.post(
self._jina_tokenizer_endpoint,