PackageHallucinationScorer (Python) misses `from pkg.sub import x` and indented imports
维护者通常 2 天内回复
评估
调研方向
从 pyrit/score/true_false/regex/package_hallucination_scorer.py 开始,阅读 _extract_package_references 以及 _split_python_import_clause。运行 tests/unit/score/test_package_hallucination_scorer.py,然后为带点的 from-import 和缩进的 import 添加回归覆盖。完成标准是 scorer 能够提取并标记 ghostpkg 和 phantomlib,同时继续忽略相对 import。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
For PackageEcosystem.PYTHON, PackageHallucinationScorer extracts package references with two patterns (pyrit/score/true_false/regex/package_hallucination_scorer.py):
re.compile(r"^import\s+([^\n#;]+)", re.MULTILINE),
re.compile(r"^from\s+([a-zA-Z0-9][a-zA-Z0-9\-\_]*)\s*import", re.MULTILINE),
This has two gaps:
- The
frompattern does not allow a dot in the module path.from ghostpkg.client import Clienttherefore extracts nothing. - Both patterns are anchored at column 0. Imports inside a
try:block, a function body or anifguard are skipped.
In both cases a non-existent package is never compared against known_packages, so the scorer returns False for code that does import a hallucinated package. from pkg.submodule import name is a common form in generated code, so this false negative is easy to hit.
The module docstring says the extraction rules are ported from garak's packagehallucination detector. garak's current PythonPypi._extract_package_references (garak/detectors/packagehallucination.py) uses ^\s*import\s+(.+) and ^\s*from\s+([a-zA-Z0-9_][a-zA-Z0-9.\-_]*)\s*import. It then reduces every name to its top-level package with name.split(".", 1)[0]. The Ruby patterns in this same scorer already allow leading whitespace (^\s*require). #2454 fixed comma-separated import a, b (same root cause as NVIDIA/garak#1991) and left the from pattern unchanged.
Steps/Code to Reproduce
import asyncio
from pyrit.models import MessagePiece
from pyrit.score import PackageEcosystem, PackageHallucinationScorer
scorer = PackageHallucinationScorer(known_packages={"requests"}, ecosystem=PackageEcosystem.PYTHON)
code = """\
import requests
from requests.adapters import HTTPAdapter
from ghostpkg.client import Client
try:
import phantomlib
except ImportError:
phantomlib = None
"""
print(scorer._extract_package_references(code))
piece = MessagePiece(role="assistant", original_value=code)
score = asyncio.run(scorer._score_piece_async(piece))[0]
print(score.get_value(), repr(score.score_metadata["hallucinated_packages"]))
Expected Results
ghostpkg (imported through a submodule) and phantomlib (an indented import) are extracted and flagged:
{'requests', 'ghostpkg', 'phantomlib'}
True 'ghostpkg, phantomlib'
Actual Results
{'requests'}
False ''
Relative imports (from . import x, from .mod import y) should still be ignored. They are ignored today, and the fix below keeps that.
Suggested fix
Allow leading indentation in both Python patterns and allow dots in the from module path:
re.compile(r"^[ \t]*import\s+([^\n#;]+)", re.MULTILINE),
re.compile(r"^[ \t]*from\s+([a-zA-Z0-9_][a-zA-Z0-9.\-_]*)\s+import", re.MULTILINE),
Then, in _extract_package_references, reduce each from match to its top-level package (module.split(".")[0]), the same way _split_python_import_clause already does for import a.b. I have this change locally with regression tests. The existing tests in tests/unit/score/test_package_hallucination_scorer.py still pass. I can open a PR if this approach works for you.
Versions
- OS: macOS 26 (arm64)
- Python version: 3.12.13
- PyRIT version: installed from main (ab1c6c8) in editable mode,
1.2.0.dev0
AI assistance: drafted with an AI coding assistant (Claude). The reproduction above was run locally against the current default branch.
- 主要语言
- Python
- 星标
- 4.6k
- 派生
- 944
- 平均合并
- 2 天 18 小时
- 30 天内合并 PR
- 358
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/PyRIT 的其他 Issue
-
BUG: label queries only return the final conversation of an attack可能已有人在做 @u7k4rs6 于 2 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
microsoft/PyRIT#3095 · 4 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
-
BUG Configuration keeps runtime-status errors after polling recovers可能已有人在做 @joeyvictorino 于 6 天前认领。 未关闭Bug: triage GUI help wanted
难度 2/5 1-3 小时 新手友好度 86/100
microsoft/PyRIT#2868 · 6 条评论 ·
维护者通常 2 天内回复
-
Crescendo backtracking keeps the refused prompt when the objective target made a tool call可能已有人在做 @RachelWanggg 今天认领。 未关闭
难度 3/5 1-2 天 新手友好度 30/100
维护者通常 2 天内回复
-
BUG BinAsciiConverter UUencode output cannot be decoded when all words are converted可能已有人在做 @barkha1research-ai 于 1 天前认领。 未关闭Bug: triage
难度 2/5 1-3 小时 新手友好度 25/100
维护者通常 2 天内回复
-
FEAT Proposal: red-team decision models as targets (decision flip-rate testing)可能已有人在做 @shashank03-dev 今天认领。 未关闭
难度 5/5 一周以上 新手友好度 15/100
microsoft/PyRIT#3246 · 2 条评论 ·
维护者通常 2 天内回复
相似的 Issue
-
Questions crashes on a real SQuAD2 no-answer prediction可能已有人在做 @HuaTNA 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
GST is read as Gulf Standard Time (UTC+4) instead of Guam Standard Time (UTC+10)可能已有人在做 @yang00333 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
DOI-USGS/dataretrieval-python#454 ·
维护者通常 4 天内回复
-
area:core-platform effort:S priority:P3 status:in-review type:test
难度 2/5 1 小时以内 新手友好度 80/100
randomparity/kdive#3246 · 1 条评论 ·
维护者通常 1 天内回复
-
Requantification library converts m/z to mass with the hydrogen-atom mass instead of the proton mass未关闭bug
难度 1/5 1 小时以内 新手友好度 85/100
nf-core/metaboigniter#130 ·
维护者通常 1 天内回复
-
netbox status: needs owner type: documentation
难度 1/5 1 小时以内 新手友好度 75/100
netbox-community/netbox#23412 · 1 个 reaction ·
维护者通常 1 天内回复