6565def _assess_output_completeness (text : str ) -> float :
6666 """
6767 智能评估输出完整性的高级算法
68-
68+
6969 使用多种启发式方法来检测输出是否被截断:
7070 1. 结构化标记完整性检查
71- 2. 句子完整性分析
71+ 2. 句子完整性分析
7272 3. 代码块完整性验证
7373 4. 预期内容元素检查
74-
74+
7575 Returns:
7676 float: 完整性分数 (0.0-1.0),越高表示越完整
7777 """
7878 if not text or len (text .strip ()) < 100 :
7979 return 0.0
80-
80+
8181 score = 0.0
8282 factors = 0
83-
83+
8484 # 1. 基本长度检查 (权重: 0.2)
8585 if len (text ) > 5000 : # 期望的最小输出长度
8686 score += 0.2
8787 elif len (text ) > 2000 :
8888 score += 0.1
8989 factors += 1
90-
90+
9191 # 2. 结构完整性检查 (权重: 0.3)
9292 structure_indicators = [
93- "## 1." , "## 2." , "## 3." , # 章节标题
94- "```" , "file_structure" , "implementation" ,
95- "algorithm" , "method" , "function"
93+ "## 1." ,
94+ "## 2." ,
95+ "## 3." , # 章节标题
96+ "```" ,
97+ "file_structure" ,
98+ "implementation" ,
99+ "algorithm" ,
100+ "method" ,
101+ "function" ,
96102 ]
97- structure_count = sum (1 for indicator in structure_indicators if indicator .lower () in text .lower ())
103+ structure_count = sum (
104+ 1 for indicator in structure_indicators if indicator .lower () in text .lower ()
105+ )
98106 if structure_count >= 6 :
99107 score += 0.3
100108 elif structure_count >= 3 :
101109 score += 0.15
102110 factors += 1
103-
111+
104112 # 3. 句子完整性检查 (权重: 0.2)
105- lines = text .strip ().split (' \n ' )
113+ lines = text .strip ().split (" \n " )
106114 if lines :
107115 last_line = lines [- 1 ].strip ()
108116 # 检查最后一行是否是完整的句子或结构化内容
109- if (last_line .endswith (('.' , ':' , '```' , '!' , '?' )) or
110- last_line .startswith (('##' , '-' , '*' , '`' )) or
111- len (last_line ) < 10 ): # 很短的行可能是列表项
117+ if (
118+ last_line .endswith (("." , ":" , "```" , "!" , "?" ))
119+ or last_line .startswith (("##" , "-" , "*" , "`" ))
120+ or len (last_line ) < 10
121+ ): # 很短的行可能是列表项
112122 score += 0.2
113- elif len (last_line ) > 50 and not last_line .endswith (('.' , ':' , '```' , '!' , '?' )):
123+ elif len (last_line ) > 50 and not last_line .endswith (
124+ ("." , ":" , "```" , "!" , "?" )
125+ ):
114126 # 长行但没有适当结尾,可能被截断
115127 score += 0.05
116128 factors += 1
117-
129+
118130 # 4. 代码实现计划完整性 (权重: 0.3)
119131 implementation_keywords = [
120- "file structure" , "architecture" , "implementation" ,
121- "requirements" , "dependencies" , "setup" , "main" ,
122- "class" , "function" , "method" , "algorithm"
132+ "file structure" ,
133+ "architecture" ,
134+ "implementation" ,
135+ "requirements" ,
136+ "dependencies" ,
137+ "setup" ,
138+ "main" ,
139+ "class" ,
140+ "function" ,
141+ "method" ,
142+ "algorithm" ,
123143 ]
124- impl_count = sum (1 for keyword in implementation_keywords if keyword .lower () in text .lower ())
144+ impl_count = sum (
145+ 1 for keyword in implementation_keywords if keyword .lower () in text .lower ()
146+ )
125147 if impl_count >= 8 :
126148 score += 0.3
127149 elif impl_count >= 4 :
128150 score += 0.15
129151 factors += 1
130-
152+
131153 return min (score , 1.0 ) # 确保不超过1.0
132154
133155
134156def _adjust_params_for_retry (params : RequestParams , retry_count : int ) -> RequestParams :
135157 """
136158 动态调整请求参数以提高成功率
137-
159+
138160 基于重试次数智能调整参数:
139161 - 增加token限制
140162 - 调整temperature
141163 - 优化其他参数
142164 """
143165 # 基础token增量:每次重试增加更多tokens
144166 token_increment = 4096 * (retry_count + 1 )
145- new_max_tokens = min (params .max_tokens + token_increment , 32768 ) # 不超过32K的合理限制
146-
167+ new_max_tokens = min (
168+ params .max_tokens + token_increment , 32768
169+ ) # 不超过32K的合理限制
170+
147171 # 随着重试次数增加,降低temperature以获得更一致的输出
148172 new_temperature = max (params .temperature - (retry_count * 0.1 ), 0.1 )
149-
173+
150174 print (f"🔧 Adjusting parameters for retry { retry_count + 1 } :" )
151175 print (f" Token limit: { params .max_tokens } → { new_max_tokens } " )
152176 print (f" Temperature: { params .temperature } → { new_temperature } " )
153-
177+
154178 return RequestParams (
155179 max_tokens = new_max_tokens ,
156180 temperature = new_temperature ,
@@ -483,13 +507,15 @@ async def run_code_analyzer(
483507 # 分段模式:可以使用更高的token限制,因为输入已经被优化
484508 max_tokens_limit = 16384 # 使用更高限制,因为分段减少了输入复杂性
485509 temperature = 0.2 # 稍微降低temperature以提高一致性
486- print ("🧠 Using SEGMENTED mode: Higher token limit (16384) with optimized inputs" )
510+ print (
511+ "🧠 Using SEGMENTED mode: Higher token limit (16384) with optimized inputs"
512+ )
487513 else :
488514 # 传统模式:使用保守的token限制并启用增量生成
489515 max_tokens_limit = 12288 # 中等限制,为聚合输出留出空间
490516 temperature = 0.3
491517 print ("🧠 Using TRADITIONAL mode: Moderate token limit (12288)" )
492-
518+
493519 enhanced_params = RequestParams (
494520 max_tokens = max_tokens_limit ,
495521 temperature = temperature ,
@@ -509,33 +535,39 @@ async def run_code_analyzer(
509535 # 智能输出完整性检查和重试机制
510536 max_retries = 3
511537 retry_count = 0
512-
538+
513539 while retry_count < max_retries :
514540 try :
515- print (f"🚀 Attempting code analysis (attempt { retry_count + 1 } /{ max_retries } )" )
541+ print (
542+ f"🚀 Attempting code analysis (attempt { retry_count + 1 } /{ max_retries } )"
543+ )
516544 result = await code_aggregator_agent .generate_str (
517545 message = message , request_params = enhanced_params
518546 )
519-
547+
520548 # 检查输出完整性的高级指标
521549 completeness_score = _assess_output_completeness (result )
522550 print (f"📊 Output completeness score: { completeness_score :.2f} /1.0" )
523-
551+
524552 if completeness_score >= 0.8 : # 输出被认为是完整的
525- print (f"✅ Code analysis completed successfully (length: { len (result )} chars)" )
553+ print (
554+ f"✅ Code analysis completed successfully (length: { len (result )} chars)"
555+ )
526556 return result
527557 else :
528- print (f"⚠️ Output appears truncated (score: { completeness_score :.2f} ), retrying with enhanced parameters..." )
558+ print (
559+ f"⚠️ Output appears truncated (score: { completeness_score :.2f} ), retrying with enhanced parameters..."
560+ )
529561 # 动态调整参数进行重试
530562 enhanced_params = _adjust_params_for_retry (enhanced_params , retry_count )
531563 retry_count += 1
532-
564+
533565 except Exception as e :
534566 print (f"❌ Error in code analysis attempt { retry_count + 1 } : { e } " )
535567 retry_count += 1
536568 if retry_count >= max_retries :
537569 raise
538-
570+
539571 # 如果所有重试都失败,返回最后一次的结果
540572 print (f"⚠️ Returning potentially incomplete result after { max_retries } attempts" )
541573 return result
0 commit comments