@@ -83,7 +83,7 @@ def build_payload(model: str, your_name: str, chat_messages: list[dict], tempera
8383 payload ["temperature" ] = temperature
8484 return payload
8585
86- def send_request (payload : dict , api_url : str , api_key : str , verbose : bool = False ) -> str :
86+ def send_request (payload : dict , api_url : str , api_key : str , verbose : bool , timeout : int ) -> str :
8787 headers = {
8888 "Content-Type" : "application/json" ,
8989 "Authorization" : f"Bearer { api_key } " ,
@@ -99,7 +99,7 @@ def send_request(payload: dict, api_url: str, api_key: str, verbose: bool=False)
9999 )
100100
101101 try :
102- with urllib .request .urlopen (req , timeout = 10 ) as resp :
102+ with urllib .request .urlopen (req , timeout = timeout ) as resp :
103103 body = resp .read ().decode ("utf-8" )
104104 result = json .loads (body )
105105 except urllib .error .HTTPError as e :
@@ -140,8 +140,13 @@ def main():
140140 help = "Sampling temperature (e.g., 0.2)." )
141141 parser .add_argument ("-v" , "--verbose" , action = "store_true" ,
142142 help = "Print request payload to stderr." )
143+ parser .add_argument ("-T" , "--timeout" , type = int , default = 10 ,
144+ help = "Network timeout in seconds (default: 10)." )
143145 args = parser .parse_args ()
144146
147+ if args .timeout <= 0 :
148+ print ("Error: -T / --timeout must be > 0 seconds." , file = sys .stderr )
149+ sys .exit (1 )
145150
146151 # Resolve service -> api_url, api_key_env, and default model
147152 service = args .service .strip ()
@@ -165,7 +170,7 @@ def main():
165170 lines = read_chat_file (args .chat_completion )
166171 your_name , chat_messages = parse_chat (lines )
167172 payload = build_payload (model , your_name , chat_messages , args .temperature )
168- reply = send_request (payload , api_url , api_key , verbose = args .verbose )
173+ reply = send_request (payload , api_url , api_key , verbose = args .verbose , timeout = args . timeout )
169174 reply_without_name = reply .split (":" , 1 )[1 ].strip () if ":" in reply else reply
170175 print (reply_without_name )
171176
0 commit comments