Explorar o código

chore: improve usage of striping prefix or suffix of string with Ruff 0.6.5 (#8392)

Bowen Liang hai 7 meses
pai
achega
5b98acde2f
Modificáronse 20 ficheiros con 44 adicións e 60 borrados
  1. 1 2
      api/core/model_runtime/model_providers/huggingface_tei/rerank/rerank.py
  2. 2 4
      api/core/model_runtime/model_providers/huggingface_tei/text_embedding/text_embedding.py
  3. 1 2
      api/core/model_runtime/model_providers/jina/rerank/rerank.py
  4. 1 2
      api/core/model_runtime/model_providers/jina/text_embedding/text_embedding.py
  5. 1 2
      api/core/model_runtime/model_providers/siliconflow/rerank/rerank.py
  6. 1 2
      api/core/model_runtime/model_providers/xinference/llm/llm.py
  7. 2 4
      api/core/model_runtime/model_providers/xinference/rerank/rerank.py
  8. 2 4
      api/core/model_runtime/model_providers/xinference/speech2text/speech2text.py
  9. 2 4
      api/core/model_runtime/model_providers/xinference/text_embedding/text_embedding.py
  10. 2 4
      api/core/model_runtime/model_providers/xinference/tts/tts.py
  11. 1 2
      api/core/model_runtime/model_providers/zhipuai/zhipuai_sdk/core/_sse_client.py
  12. 1 1
      api/core/tools/provider/builtin/hap/tools/add_worksheet_record.py
  13. 1 1
      api/core/tools/provider/builtin/hap/tools/delete_worksheet_record.py
  14. 1 1
      api/core/tools/provider/builtin/hap/tools/get_worksheet_fields.py
  15. 1 1
      api/core/tools/provider/builtin/hap/tools/get_worksheet_pivot_data.py
  16. 1 1
      api/core/tools/provider/builtin/hap/tools/list_worksheet_records.py
  17. 1 1
      api/core/tools/provider/builtin/hap/tools/list_worksheets.py
  18. 1 1
      api/core/tools/provider/builtin/hap/tools/update_worksheet_record.py
  19. 20 20
      api/poetry.lock
  20. 1 1
      api/pyproject.toml

+ 1 - 2
api/core/model_runtime/model_providers/huggingface_tei/rerank/rerank.py

@@ -49,8 +49,7 @@ class HuggingfaceTeiRerankModel(RerankModel):
             return RerankResult(model=model, docs=[])
         server_url = credentials["server_url"]
 
-        if server_url.endswith("/"):
-            server_url = server_url[:-1]
+        server_url = server_url.removesuffix("/")
 
         try:
             results = TeiHelper.invoke_rerank(server_url, query, docs)

+ 2 - 4
api/core/model_runtime/model_providers/huggingface_tei/text_embedding/text_embedding.py

@@ -42,8 +42,7 @@ class HuggingfaceTeiTextEmbeddingModel(TextEmbeddingModel):
         """
         server_url = credentials["server_url"]
 
-        if server_url.endswith("/"):
-            server_url = server_url[:-1]
+        server_url = server_url.removesuffix("/")
 
         # get model properties
         context_size = self._get_context_size(model, credentials)
@@ -119,8 +118,7 @@ class HuggingfaceTeiTextEmbeddingModel(TextEmbeddingModel):
         num_tokens = 0
         server_url = credentials["server_url"]
 
-        if server_url.endswith("/"):
-            server_url = server_url[:-1]
+        server_url = server_url.removesuffix("/")
 
         batch_tokens = TeiHelper.invoke_tokenize(server_url, texts)
         num_tokens = sum(len(tokens) for tokens in batch_tokens)

+ 1 - 2
api/core/model_runtime/model_providers/jina/rerank/rerank.py

@@ -48,8 +48,7 @@ class JinaRerankModel(RerankModel):
             return RerankResult(model=model, docs=[])
 
         base_url = credentials.get("base_url", "https://api.jina.ai/v1")
-        if base_url.endswith("/"):
-            base_url = base_url[:-1]
+        base_url = base_url.removesuffix("/")
 
         try:
             response = httpx.post(

+ 1 - 2
api/core/model_runtime/model_providers/jina/text_embedding/text_embedding.py

@@ -44,8 +44,7 @@ class JinaTextEmbeddingModel(TextEmbeddingModel):
             raise CredentialsValidateFailedError("api_key is required")
 
         base_url = credentials.get("base_url", self.api_base)
-        if base_url.endswith("/"):
-            base_url = base_url[:-1]
+        base_url = base_url.removesuffix("/")
 
         url = base_url + "/embeddings"
         headers = {"Authorization": "Bearer " + api_key, "Content-Type": "application/json"}

+ 1 - 2
api/core/model_runtime/model_providers/siliconflow/rerank/rerank.py

@@ -30,8 +30,7 @@ class SiliconflowRerankModel(RerankModel):
             return RerankResult(model=model, docs=[])
 
         base_url = credentials.get("base_url", "https://api.siliconflow.cn/v1")
-        if base_url.endswith("/"):
-            base_url = base_url[:-1]
+        base_url = base_url.removesuffix("/")
         try:
             response = httpx.post(
                 base_url + "/rerank",

+ 1 - 2
api/core/model_runtime/model_providers/xinference/llm/llm.py

@@ -459,8 +459,7 @@ class XinferenceAILargeLanguageModel(LargeLanguageModel):
         if "server_url" not in credentials:
             raise CredentialsValidateFailedError("server_url is required in credentials")
 
-        if credentials["server_url"].endswith("/"):
-            credentials["server_url"] = credentials["server_url"][:-1]
+        credentials["server_url"] = credentials["server_url"].removesuffix("/")
 
         api_key = credentials.get("api_key") or "abc"
 

+ 2 - 4
api/core/model_runtime/model_providers/xinference/rerank/rerank.py

@@ -50,8 +50,7 @@ class XinferenceRerankModel(RerankModel):
         server_url = credentials["server_url"]
         model_uid = credentials["model_uid"]
         api_key = credentials.get("api_key")
-        if server_url.endswith("/"):
-            server_url = server_url[:-1]
+        server_url = server_url.removesuffix("/")
         auth_headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
 
         params = {"documents": docs, "query": query, "top_n": top_n, "return_documents": True}
@@ -98,8 +97,7 @@ class XinferenceRerankModel(RerankModel):
             if "/" in credentials["model_uid"] or "?" in credentials["model_uid"] or "#" in credentials["model_uid"]:
                 raise CredentialsValidateFailedError("model_uid should not contain /, ?, or #")
 
-            if credentials["server_url"].endswith("/"):
-                credentials["server_url"] = credentials["server_url"][:-1]
+            credentials["server_url"] = credentials["server_url"].removesuffix("/")
 
             # initialize client
             client = Client(

+ 2 - 4
api/core/model_runtime/model_providers/xinference/speech2text/speech2text.py

@@ -45,8 +45,7 @@ class XinferenceSpeech2TextModel(Speech2TextModel):
             if "/" in credentials["model_uid"] or "?" in credentials["model_uid"] or "#" in credentials["model_uid"]:
                 raise CredentialsValidateFailedError("model_uid should not contain /, ?, or #")
 
-            if credentials["server_url"].endswith("/"):
-                credentials["server_url"] = credentials["server_url"][:-1]
+            credentials["server_url"] = credentials["server_url"].removesuffix("/")
 
             # initialize client
             client = Client(
@@ -116,8 +115,7 @@ class XinferenceSpeech2TextModel(Speech2TextModel):
         server_url = credentials["server_url"]
         model_uid = credentials["model_uid"]
         api_key = credentials.get("api_key")
-        if server_url.endswith("/"):
-            server_url = server_url[:-1]
+        server_url = server_url.removesuffix("/")
         auth_headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
 
         try:

+ 2 - 4
api/core/model_runtime/model_providers/xinference/text_embedding/text_embedding.py

@@ -45,8 +45,7 @@ class XinferenceTextEmbeddingModel(TextEmbeddingModel):
         server_url = credentials["server_url"]
         model_uid = credentials["model_uid"]
         api_key = credentials.get("api_key")
-        if server_url.endswith("/"):
-            server_url = server_url[:-1]
+        server_url = server_url.removesuffix("/")
         auth_headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
 
         try:
@@ -118,8 +117,7 @@ class XinferenceTextEmbeddingModel(TextEmbeddingModel):
 
             if extra_args.max_tokens:
                 credentials["max_tokens"] = extra_args.max_tokens
-            if server_url.endswith("/"):
-                server_url = server_url[:-1]
+            server_url = server_url.removesuffix("/")
 
             client = Client(
                 base_url=server_url,

+ 2 - 4
api/core/model_runtime/model_providers/xinference/tts/tts.py

@@ -73,8 +73,7 @@ class XinferenceText2SpeechModel(TTSModel):
             if "/" in credentials["model_uid"] or "?" in credentials["model_uid"] or "#" in credentials["model_uid"]:
                 raise CredentialsValidateFailedError("model_uid should not contain /, ?, or #")
 
-            if credentials["server_url"].endswith("/"):
-                credentials["server_url"] = credentials["server_url"][:-1]
+            credentials["server_url"] = credentials["server_url"].removesuffix("/")
 
             extra_param = XinferenceHelper.get_xinference_extra_parameter(
                 server_url=credentials["server_url"],
@@ -189,8 +188,7 @@ class XinferenceText2SpeechModel(TTSModel):
         :param voice: model timbre
         :return: text translated to audio file
         """
-        if credentials["server_url"].endswith("/"):
-            credentials["server_url"] = credentials["server_url"][:-1]
+        credentials["server_url"] = credentials["server_url"].removesuffix("/")
 
         try:
             api_key = credentials.get("api_key")

+ 1 - 2
api/core/model_runtime/model_providers/zhipuai/zhipuai_sdk/core/_sse_client.py

@@ -127,8 +127,7 @@ class SSELineParser:
 
         field, _p, value = line.partition(":")
 
-        if value.startswith(" "):
-            value = value[1:]
+        value = value.removeprefix(" ")
         if field == "data":
             self._data.append(value)
         elif field == "event":

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/add_worksheet_record.py

@@ -30,7 +30,7 @@ class AddWorksheetRecordTool(BuiltinTool):
         elif not host.startswith(("http://", "https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
 
         url = f"{host}/v2/open/worksheet/addRow"
         headers = {"Content-Type": "application/json"}

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/delete_worksheet_record.py

@@ -29,7 +29,7 @@ class DeleteWorksheetRecordTool(BuiltinTool):
         elif not host.startswith(("http://", "https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
 
         url = f"{host}/v2/open/worksheet/deleteRow"
         headers = {"Content-Type": "application/json"}

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/get_worksheet_fields.py

@@ -27,7 +27,7 @@ class GetWorksheetFieldsTool(BuiltinTool):
         elif not host.startswith(("http://", "https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
 
         url = f"{host}/v2/open/worksheet/getWorksheetInfo"
         headers = {"Content-Type": "application/json"}

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/get_worksheet_pivot_data.py

@@ -38,7 +38,7 @@ class GetWorksheetPivotDataTool(BuiltinTool):
         elif not host.startswith(("http://", "https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
 
         url = f"{host}/report/getPivotData"
         headers = {"Content-Type": "application/json"}

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/list_worksheet_records.py

@@ -30,7 +30,7 @@ class ListWorksheetRecordsTool(BuiltinTool):
         elif not (host.startswith("http://") or host.startswith("https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
 
         url_fields = f"{host}/v2/open/worksheet/getWorksheetInfo"
         headers = {"Content-Type": "application/json"}

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/list_worksheets.py

@@ -24,7 +24,7 @@ class ListWorksheetsTool(BuiltinTool):
         elif not (host.startswith("http://") or host.startswith("https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
         url = f"{host}/v1/open/app/get"
 
         result_type = tool_parameters.get("result_type", "")

+ 1 - 1
api/core/tools/provider/builtin/hap/tools/update_worksheet_record.py

@@ -33,7 +33,7 @@ class UpdateWorksheetRecordTool(BuiltinTool):
         elif not host.startswith(("http://", "https://")):
             return self.create_text_message("Invalid parameter Host Address")
         else:
-            host = f"{host[:-1] if host.endswith('/') else host}/api"
+            host = f"{host.removesuffix('/')}/api"
 
         url = f"{host}/v2/open/worksheet/editRow"
         headers = {"Content-Type": "application/json"}

+ 20 - 20
api/poetry.lock

@@ -8003,29 +8003,29 @@ pyasn1 = ">=0.1.3"
 
 [[package]]
 name = "ruff"
-version = "0.6.4"
+version = "0.6.5"
 description = "An extremely fast Python linter and code formatter, written in Rust."
 optional = false
 python-versions = ">=3.7"
 files = [
-    {file = "ruff-0.6.4-py3-none-linux_armv6l.whl", hash = "sha256:c4b153fc152af51855458e79e835fb6b933032921756cec9af7d0ba2aa01a258"},
-    {file = "ruff-0.6.4-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:bedff9e4f004dad5f7f76a9d39c4ca98af526c9b1695068198b3bda8c085ef60"},
-    {file = "ruff-0.6.4-py3-none-macosx_11_0_arm64.whl", hash = "sha256:d02a4127a86de23002e694d7ff19f905c51e338c72d8e09b56bfb60e1681724f"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7862f42fc1a4aca1ea3ffe8a11f67819d183a5693b228f0bb3a531f5e40336fc"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:eebe4ff1967c838a1a9618a5a59a3b0a00406f8d7eefee97c70411fefc353617"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:932063a03bac394866683e15710c25b8690ccdca1cf192b9a98260332ca93408"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:50e30b437cebef547bd5c3edf9ce81343e5dd7c737cb36ccb4fe83573f3d392e"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c44536df7b93a587de690e124b89bd47306fddd59398a0fb12afd6133c7b3818"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:0ea086601b22dc5e7693a78f3fcfc460cceabfdf3bdc36dc898792aba48fbad6"},
-    {file = "ruff-0.6.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0b52387d3289ccd227b62102c24714ed75fbba0b16ecc69a923a37e3b5e0aaaa"},
-    {file = "ruff-0.6.4-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:0308610470fcc82969082fc83c76c0d362f562e2f0cdab0586516f03a4e06ec6"},
-    {file = "ruff-0.6.4-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:803b96dea21795a6c9d5bfa9e96127cc9c31a1987802ca68f35e5c95aed3fc0d"},
-    {file = "ruff-0.6.4-py3-none-musllinux_1_2_i686.whl", hash = "sha256:66dbfea86b663baab8fcae56c59f190caba9398df1488164e2df53e216248baa"},
-    {file = "ruff-0.6.4-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:34d5efad480193c046c86608dbba2bccdc1c5fd11950fb271f8086e0c763a5d1"},
-    {file = "ruff-0.6.4-py3-none-win32.whl", hash = "sha256:f0f8968feea5ce3777c0d8365653d5e91c40c31a81d95824ba61d871a11b8523"},
-    {file = "ruff-0.6.4-py3-none-win_amd64.whl", hash = "sha256:549daccee5227282289390b0222d0fbee0275d1db6d514550d65420053021a58"},
-    {file = "ruff-0.6.4-py3-none-win_arm64.whl", hash = "sha256:ac4b75e898ed189b3708c9ab3fc70b79a433219e1e87193b4f2b77251d058d14"},
-    {file = "ruff-0.6.4.tar.gz", hash = "sha256:ac3b5bfbee99973f80aa1b7cbd1c9cbce200883bdd067300c22a6cc1c7fba212"},
+    {file = "ruff-0.6.5-py3-none-linux_armv6l.whl", hash = "sha256:7e4e308f16e07c95fc7753fc1aaac690a323b2bb9f4ec5e844a97bb7fbebd748"},
+    {file = "ruff-0.6.5-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:932cd69eefe4daf8c7d92bd6689f7e8182571cb934ea720af218929da7bd7d69"},
+    {file = "ruff-0.6.5-py3-none-macosx_11_0_arm64.whl", hash = "sha256:3a8d42d11fff8d3143ff4da41742a98f8f233bf8890e9fe23077826818f8d680"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a50af6e828ee692fb10ff2dfe53f05caecf077f4210fae9677e06a808275754f"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:794ada3400a0d0b89e3015f1a7e01f4c97320ac665b7bc3ade24b50b54cb2972"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:381413ec47f71ce1d1c614f7779d88886f406f1fd53d289c77e4e533dc6ea200"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:52e75a82bbc9b42e63c08d22ad0ac525117e72aee9729a069d7c4f235fc4d276"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:09c72a833fd3551135ceddcba5ebdb68ff89225d30758027280968c9acdc7810"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:800c50371bdcb99b3c1551d5691e14d16d6f07063a518770254227f7f6e8c178"},
+    {file = "ruff-0.6.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e25ddd9cd63ba1f3bd51c1f09903904a6adf8429df34f17d728a8fa11174253"},
+    {file = "ruff-0.6.5-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:7291e64d7129f24d1b0c947ec3ec4c0076e958d1475c61202497c6aced35dd19"},
+    {file = "ruff-0.6.5-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:9ad7dfbd138d09d9a7e6931e6a7e797651ce29becd688be8a0d4d5f8177b4b0c"},
+    {file = "ruff-0.6.5-py3-none-musllinux_1_2_i686.whl", hash = "sha256:005256d977021790cc52aa23d78f06bb5090dc0bfbd42de46d49c201533982ae"},
+    {file = "ruff-0.6.5-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:482c1e6bfeb615eafc5899127b805d28e387bd87db38b2c0c41d271f5e58d8cc"},
+    {file = "ruff-0.6.5-py3-none-win32.whl", hash = "sha256:cf4d3fa53644137f6a4a27a2b397381d16454a1566ae5335855c187fbf67e4f5"},
+    {file = "ruff-0.6.5-py3-none-win_amd64.whl", hash = "sha256:3e42a57b58e3612051a636bc1ac4e6b838679530235520e8f095f7c44f706ff9"},
+    {file = "ruff-0.6.5-py3-none-win_arm64.whl", hash = "sha256:51935067740773afdf97493ba9b8231279e9beef0f2a8079188c4776c25688e0"},
+    {file = "ruff-0.6.5.tar.gz", hash = "sha256:4d32d87fab433c0cf285c3683dd4dae63be05fd7a1d65b3f5bf7cdd05a6b96fb"},
 ]
 
 [[package]]
@@ -10416,4 +10416,4 @@ cffi = ["cffi (>=1.11)"]
 [metadata]
 lock-version = "2.0"
 python-versions = ">=3.10,<3.13"
-content-hash = "726af69ca5a577808dfe76dbce098de77ce358bf64862a4d27309cb1900cea0c"
+content-hash = "9173a56b2efea12804c980511e1465fba43c7a3d83b1ad284ee149851ed67fc5"

+ 1 - 1
api/pyproject.toml

@@ -283,4 +283,4 @@ optional = true
 
 [tool.poetry.group.lint.dependencies]
 dotenv-linter = "~0.5.0"
-ruff = "~0.6.4"
+ruff = "~0.6.5"