chore: apply ruff's pyupgrade linter rules to modernize Python code with targeted version (#2419)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List
|
||||
from typing import Any
|
||||
|
||||
from core.tools.entities.common_entities import I18nObject
|
||||
from core.tools.entities.tool_bundle import ApiBasedToolBundle
|
||||
@@ -83,10 +83,10 @@ class ApiBasedToolProviderController(ToolProviderController):
|
||||
def app_type(self) -> ToolProviderType:
|
||||
return ToolProviderType.API_BASED
|
||||
|
||||
def _validate_credentials(self, tool_name: str, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, tool_name: str, credentials: dict[str, Any]) -> None:
|
||||
pass
|
||||
|
||||
def validate_parameters(self, tool_name: str, tool_parameters: Dict[str, Any]) -> None:
|
||||
def validate_parameters(self, tool_name: str, tool_parameters: dict[str, Any]) -> None:
|
||||
pass
|
||||
|
||||
def _parse_tool_bundle(self, tool_bundle: ApiBasedToolBundle) -> ApiTool:
|
||||
@@ -117,7 +117,7 @@ class ApiBasedToolProviderController(ToolProviderController):
|
||||
'parameters' : tool_bundle.parameters if tool_bundle.parameters else [],
|
||||
})
|
||||
|
||||
def load_bundled_tools(self, tools: List[ApiBasedToolBundle]) -> List[ApiTool]:
|
||||
def load_bundled_tools(self, tools: list[ApiBasedToolBundle]) -> list[ApiTool]:
|
||||
"""
|
||||
load bundled tools
|
||||
|
||||
@@ -128,7 +128,7 @@ class ApiBasedToolProviderController(ToolProviderController):
|
||||
|
||||
return self.tools
|
||||
|
||||
def get_tools(self, user_id: str, tenant_id: str) -> List[ApiTool]:
|
||||
def get_tools(self, user_id: str, tenant_id: str) -> list[ApiTool]:
|
||||
"""
|
||||
fetch tools from database
|
||||
|
||||
@@ -139,10 +139,10 @@ class ApiBasedToolProviderController(ToolProviderController):
|
||||
if self.tools is not None:
|
||||
return self.tools
|
||||
|
||||
tools: List[Tool] = []
|
||||
tools: list[Tool] = []
|
||||
|
||||
# get tenant api providers
|
||||
db_providers: List[ApiToolProvider] = db.session.query(ApiToolProvider).filter(
|
||||
db_providers: list[ApiToolProvider] = db.session.query(ApiToolProvider).filter(
|
||||
ApiToolProvider.tenant_id == tenant_id,
|
||||
ApiToolProvider.name == self.identity.name
|
||||
).all()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
from typing import Any, Dict, List
|
||||
from typing import Any
|
||||
|
||||
from core.tools.entities.common_entities import I18nObject
|
||||
from core.tools.entities.tool_entities import ToolParameter, ToolParameterOption, ToolProviderType
|
||||
@@ -16,21 +16,21 @@ class AppBasedToolProviderEntity(ToolProviderController):
|
||||
def app_type(self) -> ToolProviderType:
|
||||
return ToolProviderType.APP_BASED
|
||||
|
||||
def _validate_credentials(self, tool_name: str, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, tool_name: str, credentials: dict[str, Any]) -> None:
|
||||
pass
|
||||
|
||||
def validate_parameters(self, tool_name: str, tool_parameters: Dict[str, Any]) -> None:
|
||||
def validate_parameters(self, tool_name: str, tool_parameters: dict[str, Any]) -> None:
|
||||
pass
|
||||
|
||||
def get_tools(self, user_id: str) -> List[Tool]:
|
||||
db_tools: List[PublishedAppTool] = db.session.query(PublishedAppTool).filter(
|
||||
def get_tools(self, user_id: str) -> list[Tool]:
|
||||
db_tools: list[PublishedAppTool] = db.session.query(PublishedAppTool).filter(
|
||||
PublishedAppTool.user_id == user_id,
|
||||
).all()
|
||||
|
||||
if not db_tools or len(db_tools) == 0:
|
||||
return []
|
||||
|
||||
tools: List[Tool] = []
|
||||
tools: list[Tool] = []
|
||||
|
||||
for db_tool in db_tools:
|
||||
tool = {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import os.path
|
||||
from typing import List
|
||||
|
||||
from yaml import FullLoader, load
|
||||
|
||||
@@ -9,12 +8,12 @@ position = {}
|
||||
|
||||
class BuiltinToolProviderSort:
|
||||
@staticmethod
|
||||
def sort(providers: List[UserToolProvider]) -> List[UserToolProvider]:
|
||||
def sort(providers: list[UserToolProvider]) -> list[UserToolProvider]:
|
||||
global position
|
||||
if not position:
|
||||
tmp_position = {}
|
||||
file_path = os.path.join(os.path.dirname(__file__), '..', '_position.yaml')
|
||||
with open(file_path, 'r') as f:
|
||||
with open(file_path) as f:
|
||||
for pos, val in enumerate(load(f, Loader=FullLoader)):
|
||||
tmp_position[val] = pos
|
||||
position = tmp_position
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.azuredalle.tools.dalle3 import DallE3Tool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class AzureDALLEProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
DallE3Tool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from base64 import b64decode
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from openai import AzureOpenAI
|
||||
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class DallE3Tool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.bing.tools.bing_web_search import BingSearchTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class BingProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
BingSearchTool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from requests import get
|
||||
|
||||
@@ -11,8 +11,8 @@ class BingSearchTool(BuiltinTool):
|
||||
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import io
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@@ -8,8 +8,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class BarChartTool(BuiltinTool):
|
||||
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]]:
|
||||
data = tool_parameters.get('data', '')
|
||||
if not data:
|
||||
return self.create_text_message('Please input data')
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import io
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class LinearChartTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
data = tool_parameters.get('data', '')
|
||||
if not data:
|
||||
return self.create_text_message('Please input data')
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import io
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class PieChartTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
data = tool_parameters.get('data', '')
|
||||
if not data:
|
||||
return self.create_text_message('Please input data')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.dalle.tools.dalle2 import DallE2Tool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class DALLEProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
DallE2Tool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from base64 import b64decode
|
||||
from os.path import join
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
@@ -11,8 +11,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class DallE2Tool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from base64 import b64decode
|
||||
from os.path import join
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
@@ -11,8 +11,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class DallE3Tool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import json
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import requests
|
||||
|
||||
@@ -8,7 +8,7 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class GaodeRepositoriesTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
from urllib.parse import quote
|
||||
|
||||
import requests
|
||||
@@ -10,7 +10,7 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class GihubRepositoriesTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.google.tools.google_search import GoogleSearchTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class GoogleProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
GoogleSearchTool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
import sys
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from serpapi import GoogleSearch
|
||||
|
||||
@@ -48,7 +48,7 @@ class SerpAPI:
|
||||
res = search.get_dict()
|
||||
return res
|
||||
|
||||
def get_params(self, query: str) -> Dict[str, str]:
|
||||
def get_params(self, query: str) -> dict[str, str]:
|
||||
"""Get parameters for SerpAPI."""
|
||||
_params = {
|
||||
"api_key": self.serpapi_api_key,
|
||||
@@ -148,8 +148,8 @@ class SerpAPI:
|
||||
class GoogleSearchTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.maths.tools.eval_expression import EvaluateExpressionTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class MathsProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
EvaluateExpressionTool().invoke(
|
||||
user_id='',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import numexpr as ne
|
||||
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class EvaluateExpressionTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.stablediffusion.tools.stable_diffusion import StableDiffusionTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class StableDiffusionProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
StableDiffusionTool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -3,7 +3,7 @@ import json
|
||||
from base64 import b64decode, b64encode
|
||||
from copy import deepcopy
|
||||
from os.path import join
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from httpx import get, post
|
||||
from PIL import Image
|
||||
@@ -60,8 +60,8 @@ DRAW_TEXT_OPTIONS = {
|
||||
|
||||
|
||||
class StableDiffusionTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
@@ -141,7 +141,7 @@ class StableDiffusionTool(BuiltinTool):
|
||||
height=height,
|
||||
steps=steps)
|
||||
|
||||
def validate_models(self) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
def validate_models(self) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
validate models
|
||||
"""
|
||||
@@ -168,7 +168,7 @@ class StableDiffusionTool(BuiltinTool):
|
||||
def img2img(self, base_url: str, lora: str, image_binary: bytes,
|
||||
prompt: str, negative_prompt: str,
|
||||
width: int, height: int, steps: int) \
|
||||
-> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
-> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
generate image
|
||||
"""
|
||||
@@ -207,7 +207,7 @@ class StableDiffusionTool(BuiltinTool):
|
||||
return self.create_text_message('Failed to generate image')
|
||||
|
||||
def text2img(self, base_url: str, lora: str, prompt: str, negative_prompt: str, width: int, height: int, steps: int) \
|
||||
-> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
-> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
generate image
|
||||
"""
|
||||
@@ -239,7 +239,7 @@ class StableDiffusionTool(BuiltinTool):
|
||||
except Exception as e:
|
||||
return self.create_text_message('Failed to generate image')
|
||||
|
||||
def get_runtime_parameters(self) -> List[ToolParameter]:
|
||||
def get_runtime_parameters(self) -> list[ToolParameter]:
|
||||
parameters = [
|
||||
ToolParameter(name='prompt',
|
||||
label=I18nObject(en_US='Prompt', zh_Hans='Prompt'),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.time.tools.current_time import CurrentTimeTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class WikiPediaProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
CurrentTimeTool().invoke(
|
||||
user_id='',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from pytz import timezone as pytz_timezone
|
||||
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class CurrentTimeTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from base64 import b64decode
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from httpx import post
|
||||
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class VectorizerTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
@@ -56,7 +56,7 @@ class VectorizerTool(BuiltinTool):
|
||||
meta={'mime_type': 'image/svg+xml'})
|
||||
]
|
||||
|
||||
def get_runtime_parameters(self) -> List[ToolParameter]:
|
||||
def get_runtime_parameters(self) -> list[ToolParameter]:
|
||||
"""
|
||||
override the runtime parameters
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.vectorizer.tools.vectorizer import VectorizerTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class VectorizerProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
VectorizerTool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.errors import ToolInvokeError
|
||||
@@ -8,8 +8,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
class WebscraperTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.webscraper.tools.webscraper import WebscraperTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class WebscraperProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
WebscraperTool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from langchain import WikipediaAPIWrapper
|
||||
from langchain.tools import WikipediaQueryRun
|
||||
@@ -14,8 +14,8 @@ class WikipediaInput(BaseModel):
|
||||
class WikiPediaSearchTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from httpx import get
|
||||
|
||||
@@ -12,8 +12,8 @@ class WolframAlphaTool(BuiltinTool):
|
||||
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: Dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.wolframalpha.tools.wolframalpha import WolframAlphaTool
|
||||
@@ -6,7 +6,7 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
|
||||
|
||||
class GoogleProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
WolframAlphaTool().fork_tool_runtime(
|
||||
meta={
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import pandas as pd
|
||||
from requests.exceptions import HTTPError, ReadTimeout
|
||||
@@ -10,8 +10,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class YahooFinanceAnalyticsTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
import yfinance
|
||||
from requests.exceptions import HTTPError, ReadTimeout
|
||||
@@ -8,8 +8,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class YahooFinanceSearchTickerTool(BuiltinTool):
|
||||
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
|
||||
'''
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from requests.exceptions import HTTPError, ReadTimeout
|
||||
from yfinance import Ticker
|
||||
@@ -8,8 +8,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class YahooFinanceSearchTickerTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Union
|
||||
from typing import Any, Union
|
||||
|
||||
from googleapiclient.discovery import build
|
||||
|
||||
@@ -8,8 +8,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class YoutubeVideosAnalyticsTool(BuiltinTool):
|
||||
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
|
||||
"""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import importlib
|
||||
from abc import abstractmethod
|
||||
from os import listdir, path
|
||||
from typing import Any, Dict, List
|
||||
from typing import Any
|
||||
|
||||
from yaml import FullLoader, load
|
||||
|
||||
@@ -28,7 +28,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
provider = self.__class__.__module__.split('.')[-1]
|
||||
yaml_path = path.join(path.dirname(path.realpath(__file__)), 'builtin', provider, f'{provider}.yaml')
|
||||
try:
|
||||
with open(yaml_path, 'r') as f:
|
||||
with open(yaml_path) as f:
|
||||
provider_yaml = load(f.read(), FullLoader)
|
||||
except:
|
||||
raise ToolProviderNotFoundError(f'can not load provider yaml for {provider}')
|
||||
@@ -43,7 +43,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
'credentials_schema': provider_yaml['credentials_for_provider'] if 'credentials_for_provider' in provider_yaml else None,
|
||||
})
|
||||
|
||||
def _get_builtin_tools(self) -> List[Tool]:
|
||||
def _get_builtin_tools(self) -> list[Tool]:
|
||||
"""
|
||||
returns a list of tools that the provider can provide
|
||||
|
||||
@@ -58,7 +58,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
tool_files = list(filter(lambda x: x.endswith(".yaml") and not x.startswith("__"), listdir(tool_path)))
|
||||
tools = []
|
||||
for tool_file in tool_files:
|
||||
with open(path.join(tool_path, tool_file), "r") as f:
|
||||
with open(path.join(tool_path, tool_file)) as f:
|
||||
# get tool name
|
||||
tool_name = tool_file.split(".")[0]
|
||||
tool = load(f.read(), FullLoader)
|
||||
@@ -78,7 +78,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
self.tools = tools
|
||||
return tools
|
||||
|
||||
def get_credentials_schema(self) -> Dict[str, ToolProviderCredentials]:
|
||||
def get_credentials_schema(self) -> dict[str, ToolProviderCredentials]:
|
||||
"""
|
||||
returns the credentials schema of the provider
|
||||
|
||||
@@ -98,7 +98,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
credentials = self.credentials_schema.copy()
|
||||
return UserToolProviderCredentials(credentials=credentials)
|
||||
|
||||
def get_tools(self) -> List[Tool]:
|
||||
def get_tools(self) -> list[Tool]:
|
||||
"""
|
||||
returns a list of tools that the provider can provide
|
||||
|
||||
@@ -112,7 +112,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
"""
|
||||
return next(filter(lambda x: x.identity.name == tool_name, self.get_tools()), None)
|
||||
|
||||
def get_parameters(self, tool_name: str) -> List[ToolParameter]:
|
||||
def get_parameters(self, tool_name: str) -> list[ToolParameter]:
|
||||
"""
|
||||
returns the parameters of the tool
|
||||
|
||||
@@ -142,7 +142,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
"""
|
||||
return ToolProviderType.BUILT_IN
|
||||
|
||||
def validate_parameters(self, tool_id: int, tool_name: str, tool_parameters: Dict[str, Any]) -> None:
|
||||
def validate_parameters(self, tool_id: int, tool_name: str, tool_parameters: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the parameters of the tool and set the default value if needed
|
||||
|
||||
@@ -151,7 +151,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
"""
|
||||
tool_parameters_schema = self.get_parameters(tool_name)
|
||||
|
||||
tool_parameters_need_to_validate: Dict[str, ToolParameter] = {}
|
||||
tool_parameters_need_to_validate: dict[str, ToolParameter] = {}
|
||||
for parameter in tool_parameters_schema:
|
||||
tool_parameters_need_to_validate[parameter.name] = parameter
|
||||
|
||||
@@ -166,7 +166,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
raise ToolParameterValidationError(f'parameter {parameter} should be string')
|
||||
|
||||
elif parameter_schema.type == ToolParameter.ToolParameterType.NUMBER:
|
||||
if not isinstance(tool_parameters[parameter], (int, float)):
|
||||
if not isinstance(tool_parameters[parameter], int | float):
|
||||
raise ToolParameterValidationError(f'parameter {parameter} should be number')
|
||||
|
||||
if parameter_schema.min is not None and tool_parameters[parameter] < parameter_schema.min:
|
||||
@@ -211,7 +211,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
|
||||
tool_parameters[parameter] = default_value
|
||||
|
||||
def validate_credentials_format(self, credentials: Dict[str, Any]) -> None:
|
||||
def validate_credentials_format(self, credentials: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the format of the credentials of the provider and set the default value if needed
|
||||
|
||||
@@ -221,7 +221,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
if credentials_schema is None:
|
||||
return
|
||||
|
||||
credentials_need_to_validate: Dict[str, ToolProviderCredentials] = {}
|
||||
credentials_need_to_validate: dict[str, ToolProviderCredentials] = {}
|
||||
for credential_name in credentials_schema:
|
||||
credentials_need_to_validate[credential_name] = credentials_schema[credential_name]
|
||||
|
||||
@@ -266,7 +266,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
|
||||
credentials[credential_name] = default_value
|
||||
|
||||
def validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the credentials of the provider
|
||||
|
||||
@@ -280,7 +280,7 @@ class BuiltinToolProviderController(ToolProviderController):
|
||||
self._validate_credentials(credentials)
|
||||
|
||||
@abstractmethod
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the credentials of the provider
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -16,10 +16,10 @@ from core.tools.tool.tool import Tool
|
||||
|
||||
class ToolProviderController(BaseModel, ABC):
|
||||
identity: Optional[ToolProviderIdentity] = None
|
||||
tools: Optional[List[Tool]] = None
|
||||
credentials_schema: Optional[Dict[str, ToolProviderCredentials]] = None
|
||||
tools: Optional[list[Tool]] = None
|
||||
credentials_schema: Optional[dict[str, ToolProviderCredentials]] = None
|
||||
|
||||
def get_credentials_schema(self) -> Dict[str, ToolProviderCredentials]:
|
||||
def get_credentials_schema(self) -> dict[str, ToolProviderCredentials]:
|
||||
"""
|
||||
returns the credentials schema of the provider
|
||||
|
||||
@@ -37,7 +37,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
return UserToolProviderCredentials(credentials=credentials)
|
||||
|
||||
@abstractmethod
|
||||
def get_tools(self) -> List[Tool]:
|
||||
def get_tools(self) -> list[Tool]:
|
||||
"""
|
||||
returns a list of tools that the provider can provide
|
||||
|
||||
@@ -54,7 +54,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_parameters(self, tool_name: str) -> List[ToolParameter]:
|
||||
def get_parameters(self, tool_name: str) -> list[ToolParameter]:
|
||||
"""
|
||||
returns the parameters of the tool
|
||||
|
||||
@@ -75,7 +75,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
"""
|
||||
return ToolProviderType.BUILT_IN
|
||||
|
||||
def validate_parameters(self, tool_id: int, tool_name: str, tool_parameters: Dict[str, Any]) -> None:
|
||||
def validate_parameters(self, tool_id: int, tool_name: str, tool_parameters: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the parameters of the tool and set the default value if needed
|
||||
|
||||
@@ -84,7 +84,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
"""
|
||||
tool_parameters_schema = self.get_parameters(tool_name)
|
||||
|
||||
tool_parameters_need_to_validate: Dict[str, ToolParameter] = {}
|
||||
tool_parameters_need_to_validate: dict[str, ToolParameter] = {}
|
||||
for parameter in tool_parameters_schema:
|
||||
tool_parameters_need_to_validate[parameter.name] = parameter
|
||||
|
||||
@@ -99,7 +99,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
raise ToolParameterValidationError(f'parameter {parameter} should be string')
|
||||
|
||||
elif parameter_schema.type == ToolParameter.ToolParameterType.NUMBER:
|
||||
if not isinstance(tool_parameters[parameter], (int, float)):
|
||||
if not isinstance(tool_parameters[parameter], int | float):
|
||||
raise ToolParameterValidationError(f'parameter {parameter} should be number')
|
||||
|
||||
if parameter_schema.min is not None and tool_parameters[parameter] < parameter_schema.min:
|
||||
@@ -144,7 +144,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
|
||||
tool_parameters[parameter] = default_value
|
||||
|
||||
def validate_credentials_format(self, credentials: Dict[str, Any]) -> None:
|
||||
def validate_credentials_format(self, credentials: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the format of the credentials of the provider and set the default value if needed
|
||||
|
||||
@@ -154,7 +154,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
if credentials_schema is None:
|
||||
return
|
||||
|
||||
credentials_need_to_validate: Dict[str, ToolProviderCredentials] = {}
|
||||
credentials_need_to_validate: dict[str, ToolProviderCredentials] = {}
|
||||
for credential_name in credentials_schema:
|
||||
credentials_need_to_validate[credential_name] = credentials_schema[credential_name]
|
||||
|
||||
@@ -198,7 +198,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
|
||||
credentials[credential_name] = default_value
|
||||
|
||||
def validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the credentials of the provider
|
||||
|
||||
@@ -212,7 +212,7 @@ class ToolProviderController(BaseModel, ABC):
|
||||
self._validate_credentials(credentials)
|
||||
|
||||
@abstractmethod
|
||||
def _validate_credentials(self, credentials: Dict[str, Any]) -> None:
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the credentials of the provider
|
||||
|
||||
|
||||
Reference in New Issue
Block a user