icl#245
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the LongContext-ICL-Annotation pipeline to support Ascend NPU environments, batch processing, and multi-service deployment. It introduces robust API client error handling, optimized prompt construction, and retry mechanisms for failed requests. The review feedback highlights several critical and medium-severity improvements, including PEP 8 style guide compliance (such as using is not None and implicit boolean checks), fixing incorrect type hints, and refactoring fragile string parsing logic to be more robust.
| except APIConnectionError as e: | ||
| print(f"连接失败: {e}") | ||
| print("请检查 base_url 是否配置正确,或者网络是否正常。") | ||
| raise e | ||
| except AuthenticationError as e: | ||
| print(f"鉴权失败: {e}") | ||
| print("请检查 API Key 是否填写正确。") | ||
| raise e | ||
| except APIError as e: | ||
| error = str(e) | ||
| if "inappropriate" in error: | ||
| return '<output_answer>Output data may contain inappropriate content</output_answer>', False | ||
| else: | ||
| raise e | ||
| except BadRequestError as e: | ||
| # Error code: 400 - Input data may contain inappropriate content. | ||
| error = str(e) | ||
| if "inappropriate" in error: | ||
| return '<output_answer>Input data may contain inappropriate content</output_answer>', False | ||
| else: | ||
| raise e | ||
| except Exception as e: | ||
| error = str(e) | ||
| if "inappropriate" in error: | ||
| return '<output_answer>Output data may contain inappropriate content</output_answer>', False | ||
| else: | ||
| raise e |
There was a problem hiding this comment.
Any transient network error, rate limit, or temporary API issue will raise an exception and crash the entire batch run, losing all progress. It is highly recommended to handle these exceptions gracefully by logging the error and returning None, False, allowing the retry logic in main_batch.py to handle it later.
except APIConnectionError as e:
print(f"连接失败: {e}")
print("请检查 base_url 是否配置正确,或者网络是否正常。")
return None, False
except AuthenticationError as e:
print(f"鉴权失败: {e}")
print("请检查 API Key 是否填写正确。")
return None, False
except APIError as e:
error = str(e)
if "inappropriate" in error:
return '<output_answer>Output data may contain inappropriate content</output_answer>', False
else:
print(f"API错误: {e}")
return None, False
except BadRequestError as e:
error = str(e)
if "inappropriate" in error:
return '<output_answer>Input data may contain inappropriate content</output_answer>', False
else:
print(f"请求错误: {e}")
return None, False
except Exception as e:
error = str(e)
if "inappropriate" in error:
return '<output_answer>Output data may contain inappropriate content</output_answer>', False
else:
print(f"未知错误: {e}")
return None, False| elif getattr(delta, 'content', None): | ||
| content = delta.content | ||
|
|
||
| if content != None: |
There was a problem hiding this comment.
According to PEP 8, comparisons to singletons like None should always be done with is or is not, never the equality operators.
| if content != None: | |
| if content is not None: |
References
- Comparisons to singletons like None should always be done with is or is not, never the equality operators. (link)
| print('text, state = ', text, state) | ||
|
|
||
| # 若格式不符合要求,则多请求几次尽量提高成功率 | ||
| if state == False: |
There was a problem hiding this comment.
According to PEP 8, comparisons to boolean values should be written implicitly (e.g., if not state: instead of if state == False:).
| if state == False: | |
| if not state: |
References
- Comparisons to True or False should be written implicitly, never using equality operators. (link)
| print('text, state = ', text, state) | ||
|
|
||
| # 若格式不符合要求,则多请求一次尽量提高成功率 | ||
| if state == False: |
There was a problem hiding this comment.
According to PEP 8, comparisons to boolean values should be written implicitly (e.g., if not state: instead of if state == False:).
| if state == False: | |
| if not state: |
References
- Comparisons to True or False should be written implicitly, never using equality operators. (link)
| if answer == None: | ||
| return None, False |
There was a problem hiding this comment.
According to PEP 8, comparisons to singletons like None should always be done with is or is not, never the equality operators.
| if answer == None: | |
| return None, False | |
| if answer is None: | |
| return None, False |
References
- Comparisons to singletons like None should always be done with is or is not, never the equality operators. (link)
| outputs.append(data) | ||
| # 检查是否满足特定条件 | ||
| data_len = len(data['prediction']) if data['prediction'] != None else 0 | ||
| if data['state'] == False or data_len < min_len or data_len > max_len: |
There was a problem hiding this comment.
According to PEP 8, comparisons to boolean values should be written implicitly (e.g., not data['state'] instead of data['state'] == False).
| if data['state'] == False or data_len < min_len or data_len > max_len: | |
| if not data['state'] or data_len < min_len or data_len > max_len: |
References
- Comparisons to True or False should be written implicitly, never using equality operators. (link)
| # next | ||
| i += 1 | ||
|
|
||
| if len(checks.values()) == 0: |
| indexs = [ i for i, item in enumerate(test_samples) if item['id'] in check_ids ] | ||
| for k in range(0, len(indexs), req_concurrency_size): | ||
| sample_idxs = indexs[k : k + req_concurrency_size] |
There was a problem hiding this comment.
The variable name indexs is misspelled (should be indices or indexes). Additionally, checking membership in checks directly is more idiomatic and direct than calling checks.keys().
| indexs = [ i for i, item in enumerate(test_samples) if item['id'] in check_ids ] | |
| for k in range(0, len(indexs), req_concurrency_size): | |
| sample_idxs = indexs[k : k + req_concurrency_size] | |
| indices = [ i for i, item in enumerate(test_samples) if item['id'] in checks ] | |
| for k in range(0, len(indices), req_concurrency_size): | |
| sample_idxs = indices[k : k + req_concurrency_size] |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: huangnie <980484578@qq.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: huangnie <980484578@qq.com>
No description provided.