:代码 Embedding 策略——原始代码反而比注释增强更好)
假设和现实注释比代码更像自然语言,所以向量模型理解起来应该更好。大多数工程师设计代码 Embedding 时都从这个假设出发。实验数据给出了不同的答案。实验设计数据集:27 个 Python 函数,跨 5 个业务模块(认证、数据库、缓存、支付、通知)三种策略,使用同一个 Embedding 模型(BAAI/bge-large-zh-v1.5):Strategy A:原始代码 嵌入完整的函数体——变量名、库调用、控制流全部保留 Strategy B:签名 + 注释(注释增强) 只嵌入 def 行 + docstring,去掉所有实现代码 例: def validate_jwt_token(token: str) """Decode and validate a JWT token. Returns payload if valid.""" Strategy C:混合(注释 + 代码) 把注释作为前缀,附加完整代码体 例: # Decode and validate a JWT token. Returns payload if valid. def validate_jwt_token(token: str): payload = jwt.decode(token, SECRET_KEY, ...) ...三个策略用同一个模型,差异仅来自输入格式,排除了模型质量的干扰。评估指标:Recall@3 和 Recall@5(12 个自然语言查询 × ground truth 相关函数)运行结果Strategy Recall@3 Recall@5 vs A ──────────────────────────── ────────── ────────── ────── A_raw_code 0.889 0.958 base B_comment_enhanced 0.847 0.917 -0.041 C_hybrid 0.847 0.917 -0.041Strategy A 最好。B 和 C 打平,都比 A 低 0.041。逐查询分析(Recall@5)Query A B C ────────────────────────────────────────────────── ────── ────── ────── verify user identity and check JWT token validity 1.00 0.50 0.50 ← encrypt and store user password securely 1.00 1.00 1.00 generate JWT access token for authenticated user 1.00 1.00 1.00 check if user has permission to perform an action 1.00 1.00 1.00 store and retrieve data from Redis cache 1.00 1.00 1.00 limit how many times a user can call an API 1.00 1.00 1.00 execute SQL query safely against the database 1.00 1.00 1.00 process payment and create Stripe charge 0.50 0.50 0.50