Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions api/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,7 @@ def _parse_response(self, response):
msg = res_json.get('message', '')
if msg:
logger.info(f'{self.name}响应消息: {msg}')

# 检查API返回的code字段,判断请求是否成功
code = res_json.get('code', 1)
if code != 1:
error_msg = res_json.get('message', '未知错误')
logger.error(f'{self.name}API返回错误: {error_msg}')
return None


results = res_json.get('results', {})
if not results or not isinstance(results, dict):
logger.error(f'{self.name}查询结果格式错误: API返回结果中results字段格式不正确')
Expand Down Expand Up @@ -608,21 +601,15 @@ def get_api_balance(self, token:str = ""):
temp_headers = self._headers.copy()
temp_headers['Authorization'] = f'Bearer {token}'
try:
res = requests.post(
res = requests.get(
self.balance_api,
headers=temp_headers,
verify=False,
timeout=self._timeout
)
if res.status_code == 200:
res_json = res.json()
code = res_json.get('code', 0)
if code == 1:
return int(res_json.get("balance", 0))
else:
error_msg = res_json.get('message', '未知错误')
logger.error(f'{self.name}获取余额失败: {error_msg}')
return 0
return int(res_json.get("balance", 0))
else:
logger.error(f'{self.name}请求余额接口失败,状态码: {res.status_code}')
return 0
Expand Down Expand Up @@ -668,7 +655,7 @@ def load_config(self) -> None:
self._model = self._conf.get('likeapi_model', None)
self._vision = self._conf.get('likeapi_vision', True)
self._retry = self._conf.get("likeapi_retry", True)
self._retry_times = self._conf.get("likeapi_retry_times", 3)
self._retry_times = int(self._conf.get("likeapi_retry_times", 3))

def _init_tiku(self) -> None:
self.load_config()
Expand Down
6 changes: 4 additions & 2 deletions api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ def clean_res(res):
if isinstance(res, str):
res = [res]
for c in res:
cleaned = re.sub(r'^[A-Za-z]|[.,!?;:,。!?;:]', '', c)
# 仅在字符串长度大于1时才尝试去除开头的字母编号,防止误删单个字母答案
cleaned = re.sub(r'^[A-Za-z]|[.,!?;:,。!?;:]', '', c) if len(c) > 1 else c
cleaned_res.append(cleaned.strip())
Comment on lines +685 to 687
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical regex logic error: punctuation will be stripped from anywhere in the string.

The regex pattern r'^[A-Za-z]|[.,!?;:,。!?;:]' has an unintended behavior. The | (OR) operator means:

  • Match a letter at the start of the string, OR
  • Match punctuation anywhere in the string

This will incorrectly remove punctuation from the middle of answer text, not just from the beginning/end as intended.

🔎 Proposed fix
-                # 仅在字符串长度大于1时才尝试去除开头的字母编号,防止误删单个字母答案
-                cleaned = re.sub(r'^[A-Za-z]|[.,!?;:,。!?;:]', '', c) if len(c) > 1 else c
+                # 仅在字符串长度大于1时才尝试去除开头的字母编号和首尾标点,防止误删单个字母答案
+                cleaned = re.sub(r'^[A-Za-z][\s.]?|[.,!?;:,。!?;:]+$', '', c) if len(c) > 1 else c

This revised pattern:

  • ^[A-Za-z][\s.]? - removes a leading letter optionally followed by space or dot
  • [.,!?;:,。!?;:]+$ - removes trailing punctuation only
  • Prevents removal of punctuation from the middle of text
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# 仅在字符串长度大于1时才尝试去除开头的字母编号,防止误删单个字母答案
cleaned = re.sub(r'^[A-Za-z]|[.,!?;:,。!?;:]', '', c) if len(c) > 1 else c
cleaned_res.append(cleaned.strip())
# 仅在字符串长度大于1时才尝试去除开头的字母编号和首尾标点,防止误删单个字母答案
cleaned = re.sub(r'^[A-Za-z][\s.]?|[.,!?;:,。!?;:]+$', '', c) if len(c) > 1 else c
cleaned_res.append(cleaned.strip())
🧰 Tools
🪛 Ruff (0.14.10)

685-685: Comment contains ambiguous (FULLWIDTH COMMA). Did you mean , (COMMA)?

(RUF003)


686-686: String contains ambiguous (FULLWIDTH COMMA). Did you mean , (COMMA)?

(RUF001)


686-686: String contains ambiguous (FULLWIDTH EXCLAMATION MARK). Did you mean ! (EXCLAMATION MARK)?

(RUF001)


686-686: String contains ambiguous (FULLWIDTH QUESTION MARK). Did you mean ? (QUESTION MARK)?

(RUF001)


686-686: String contains ambiguous (FULLWIDTH SEMICOLON). Did you mean ; (SEMICOLON)?

(RUF001)


686-686: String contains ambiguous (FULLWIDTH COLON). Did you mean : (COLON)?

(RUF001)


return cleaned_res
Expand Down Expand Up @@ -788,6 +789,7 @@ def fetch_response():
is_subsequence(_a, o) # 去掉各种符号和前面ABCD的答案应当是选项的子序列
):
answer += o[:1]
break # 找到匹配项后立即停止,防止重复添加
# 对答案进行排序, 否则会提交失败
answer = "".join(sorted(answer))
# else 如果分割失败那么就直接到下面去随机选
Expand All @@ -804,7 +806,7 @@ def fetch_response():
answer = "true" if self.tiku.judgement_select(res) else "false"
elif q["type"] == "completion":
if isinstance(res, list):
answer = "".join(answer)
answer = "".join(res)
elif isinstance(res, str):
answer = res
else:
Expand Down