Skip to content

Commit

Permalink
chore: improve usage of striping prefix or suffix of string with Ruff…
Browse files Browse the repository at this point in the history
… 0.6.5 (langgenius#8392)
  • Loading branch information
bowenliang123 authored and JunXu01 committed Nov 9, 2024
1 parent 14e39dd commit 417e784
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def _invoke(
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def _invoke(
"""
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)
Expand Down Expand Up @@ -119,8 +118,7 @@ def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int
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)
Expand Down
3 changes: 1 addition & 2 deletions api/core/model_runtime/model_providers/jina/rerank/rerank.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def _invoke(
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def _invoke(
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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def _invoke(
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",
Expand Down
3 changes: 1 addition & 2 deletions api/core/model_runtime/model_providers/xinference/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,7 @@ def _generate(
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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def _invoke(
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}
Expand Down Expand Up @@ -98,8 +97,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
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(
Expand Down Expand Up @@ -116,8 +115,7 @@ def _speech2text_invoke(
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def _invoke(
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:
Expand Down Expand Up @@ -118,8 +117,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:

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,
Expand Down
6 changes: 2 additions & 4 deletions api/core/model_runtime/model_providers/xinference/tts/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
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"],
Expand Down Expand Up @@ -189,8 +188,7 @@ def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str
: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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ def decode_line(self, line: str):

field, _p, value = line.partition(":")

if value.startswith(" "):
value = value[1:]
value = value.removeprefix(" ")
if field == "data":
self._data.append(value)
elif field == "event":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _invoke(
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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _invoke(
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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _invoke(
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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _invoke(
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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _invoke(
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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _invoke(
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", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _invoke(
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"}
Expand Down
40 changes: 20 additions & 20 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,4 @@ optional = true

[tool.poetry.group.lint.dependencies]
dotenv-linter = "~0.5.0"
ruff = "~0.6.4"
ruff = "~0.6.5"

0 comments on commit 417e784

Please sign in to comment.