2525import sys
2626import time
2727import traceback
28+ import uuid
2829from dataclasses import dataclass , field
2930from typing import Optional
3031
3132import aiohttp
3233from tqdm .asyncio import tqdm
3334
34- AIOHTTP_TIMEOUT = aiohttp .ClientTimeout (total = 6 * 60 * 60 )
35+ AIOHTTP_TIMEOUT = aiohttp .ClientTimeout (total = int ( os . environ . get ( "AIOHTTP_TIMEOUT" , 6 * 60 * 60 )) )
3536
3637
3738@dataclass
3839class RequestFuncInput :
3940 """Input for requesting LLMs via API"""
4041
41- no : str
42+ no : int
4243 prompt : str
4344 history_QA : Optional [dict ]
4445 hyper_parameters : dict
@@ -299,7 +300,7 @@ async def handle_non_stream_response(
299300 # arrival_time:
300301 output .arrival_time = []
301302
302- has_text = output .generated_text . strip ( ) or output .reasoning_content . strip ( )
303+ has_text = bool ( output .generated_text ) or bool ( output .reasoning_content )
303304
304305 has_tool = bool (output .tool_calls )
305306
@@ -415,6 +416,11 @@ async def async_request_eb_openai_chat_completions(
415416 "Authorization" : f"Bearer { os .environ .get ('OPENAI_API_KEY' )} " ,
416417 }
417418
419+ if request_func_input .session_id is not None :
420+ headers ["X-SMG-Routing-Key" ] = f"{ request_func_input .session_id } "
421+ if request_func_input .session_id is not None and request_func_input .turn_idx is not None :
422+ headers ["X-Request-Id" ] = f"{ request_func_input .session_id } :{ request_func_input .turn_idx } "
423+
418424 output = RequestFuncOutput ()
419425 output .prompt_len = 0
420426 output .no = request_func_input .no
@@ -617,7 +623,7 @@ async def async_request_eb_openai_chat_completions(
617623 # 新增metrics统计,计算首token过滤空包
618624 output .metrics = metrics_summary (metrics_list , token_timestamps [1 :])
619625
620- has_text = output .generated_text . strip ( ) or output .reasoning_content . strip ( )
626+ has_text = bool ( output .generated_text ) or bool ( output .reasoning_content )
621627 has_tool = getattr (output , "tool_calls" , None )
622628
623629 # 如果前面已经有服务端错误,保留原错误
@@ -662,6 +668,7 @@ async def async_request_eb_openai_chat_completions(
662668 if not output .success or output .output_tokens == 0 :
663669 with open ("error_output.txt" , "a" ) as f :
664670 f .write (str (output ) + "\n " )
671+ print ("####error response:" , output )
665672 if pbar :
666673 pbar .update (1 )
667674 if request_func_input .debug :
@@ -724,10 +731,10 @@ async def async_request_eb_openai_chat_completions_multi_turn(
724731 request_func_input : RequestFuncInput ,
725732 pbar : Optional [tqdm ] = None ,
726733):
727- # yaml中或数据集中带tools才走工具调用逻辑
734+ # 只有显式指定 enable_tools=True 时才走工具调用逻辑,否则走SWE模式(直接用数据集拼接多轮)
728735 json_data = request_func_input .json_data or {}
729736 hyper = request_func_input .hyper_parameters or {}
730- enable_tools = bool (json_data .get ("tools " ) or hyper .get ("tools " ))
737+ enable_tools = bool (json_data .get ("enable_tools " ) or hyper .get ("enable_tools " ))
731738
732739 outputs = []
733740
@@ -766,6 +773,7 @@ async def async_request_eb_openai_chat_completions_multi_turn(
766773
767774 # 只创建一次 session
768775 session_start = time .perf_counter ()
776+ session_uuid = uuid .uuid4 ().hex
769777 connector = aiohttp .TCPConnector (
770778 limit = 0 ,
771779 limit_per_host = 0 ,
@@ -784,6 +792,8 @@ async def async_request_eb_openai_chat_completions_multi_turn(
784792 round_input = copy .deepcopy (request_func_input )
785793 round_input .history_QA = history
786794 round_input .no = f"{ round_input .no } _{ prompt_no } "
795+ round_input .session_id = f"{ session_uuid } :{ request_func_input .no } "
796+ round_input .turn_idx = prompt_no
787797 if use_token_ids :
788798 if len (input_ids_all ) == 0 :
789799 # 拼接token_ids模式,首轮token_ids
@@ -982,17 +992,17 @@ async def async_request_eb_openai_chat_completions_multi_turn(
982992 print (f"Warning { prompt_no } exceed max_loop={ max_loop } , force stop tool loop" )
983993
984994 else :
985- # 无tools
986- history .append (
987- {
988- "role" : "assistant" ,
989- "content" : output .generated_text ,
990- }
991- )
995+ # 无tools(SWE模式):不追加模型实际返回,直接用数据集里的assistant回复拼接多轮
996+ pass
992997
993998 prompt_no += 1
994999 elif message ["role" ] == "assistant" :
995- continue
1000+ if enable_tools :
1001+ # 工具调用模式:跳过数据集里的assistant消息,使用模型实际返回
1002+ continue
1003+ else :
1004+ # SWE模式:直接用数据集里的assistant回复拼接多轮
1005+ history .append (message )
9961006 else :
9971007 history .append (message )
9981008
0 commit comments