Skip to content

icl#245

Open
huangnie2020 wants to merge 4 commits into
FlagAI-Open:mainfrom
huangnie2020:lca
Open

icl#245
huangnie2020 wants to merge 4 commits into
FlagAI-Open:mainfrom
huangnie2020:lca

Conversation

@huangnie2020

Copy link
Copy Markdown

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment on lines +157 to +183
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to PEP 8, comparisons to singletons like None should always be done with is or is not, never the equality operators.

Suggested change
if content != None:
if content is not None:
References
  1. 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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to PEP 8, comparisons to boolean values should be written implicitly (e.g., if not state: instead of if state == False:).

Suggested change
if state == False:
if not state:
References
  1. Comparisons to True or False should be written implicitly, never using equality operators. (link)

print('text, state = ', text, state)

# 若格式不符合要求,则多请求一次尽量提高成功率
if state == False:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to PEP 8, comparisons to boolean values should be written implicitly (e.g., if not state: instead of if state == False:).

Suggested change
if state == False:
if not state:
References
  1. Comparisons to True or False should be written implicitly, never using equality operators. (link)

Comment on lines +216 to +217
if answer == None:
return None, False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to PEP 8, comparisons to singletons like None should always be done with is or is not, never the equality operators.

Suggested change
if answer == None:
return None, False
if answer is None:
return None, False
References
  1. Comparisons to singletons like None should always be done with is or is not, never the equality operators. (link)

Comment thread openseek/competition/LongContext-ICL-Annotation/src/method.py Outdated
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to PEP 8, comparisons to boolean values should be written implicitly (e.g., not data['state'] instead of data['state'] == False).

Suggested change
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
  1. Comparisons to True or False should be written implicitly, never using equality operators. (link)

# next
i += 1

if len(checks.values()) == 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Checking if a dictionary is empty can be done more pythonically by evaluating the dictionary directly (e.g., if not checks: instead of if len(checks.values()) == 0:).

Suggested change
if len(checks.values()) == 0:
if not checks:

Comment on lines +264 to +266
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]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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().

Suggested change
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]

Comment thread openseek/competition/LongContext-ICL-Annotation/src/main_batch.py Outdated
huangnie2020 and others added 4 commits May 27, 2026 17:09
Signed-off-by: huangnie <980484578@qq.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>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: huangnie <980484578@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant