@@ -136,6 +136,68 @@ def __init__(self,
136136 self .session .headers .update ({"Authorization" : f"Bearer { self .api_key } " })
137137 self .session .headers .update ({"Content-Type" : "application/json" })
138138
139+ def _generate_curl_command (self , url : str , headers : Dict [str , str ], payload : Dict [str , Any ]) -> str :
140+ """
141+ Generate a curl command for debugging purposes.
142+ Masks the API key for security.
143+ """
144+ # Start with basic curl command
145+ curl_parts = ["curl" , "-X" , "POST" , f"'{ url } '" ]
146+
147+ # Add headers
148+ for key , value in headers .items ():
149+ if key .lower () == "authorization" and value :
150+ # Mask the API key for security
151+ if value .startswith ("Bearer " ):
152+ masked_value = f"Bearer <API_KEY_MASKED>"
153+ else :
154+ masked_value = "<API_KEY_MASKED>"
155+ curl_parts .append (f"-H '{ key } : { masked_value } '" )
156+ else :
157+ curl_parts .append (f"-H '{ key } : { value } '" )
158+
159+ # Add data payload
160+ payload_json = json .dumps (payload )
161+ # Escape single quotes in the JSON for shell compatibility
162+ payload_json_escaped = payload_json .replace ("'" , "'\\ ''" )
163+ curl_parts .append (f"-d '{ payload_json_escaped } '" )
164+
165+ return " \\ \n " .join (curl_parts )
166+
167+ def _save_curl_script (self , curl_command : str , request_id : str , file_path : str , chunk_index : int ) -> Optional [str ]:
168+ """
169+ Save curl command to a bash script in /tmp for debugging.
170+ Returns the path to the generated script, or None if save failed.
171+ """
172+ try :
173+ import tempfile
174+ # Create a unique filename based on request_id
175+ script_name = f"embedding_debug_{ request_id [:8 ]} .sh"
176+ script_path = os .path .join ("/tmp" , script_name )
177+
178+ # Generate script content with shebang and comments
179+ script_content = f"""#!/bin/bash
180+ # Embedding request debug script
181+ # Request ID: { request_id }
182+ # File: { file_path }
183+ # Chunk: { chunk_index }
184+ # Generated: { time .strftime ('%Y-%m-%d %H:%M:%S' )}
185+
186+ { curl_command }
187+ """
188+
189+ with open (script_path , 'w' ) as f :
190+ f .write (script_content )
191+
192+ # Make the script executable
193+ os .chmod (script_path , 0o755 )
194+
195+ return script_path
196+ except Exception as e :
197+ _embedding_logger .warning (f"Failed to save curl debug script: { e } " )
198+ return None
199+
200+
139201 def _log_request_start (self , request_id : str , file_path : str , chunk_index : int , chunk_len : int ):
140202 _embedding_logger .debug (
141203 "Embedding request START" ,
@@ -244,7 +306,38 @@ def embed_text(self, text: str, file_path: str = "<unknown>", chunk_index: int =
244306 except requests .Timeout as e :
245307 elapsed = time .perf_counter () - start
246308 err_msg = f"Timeout after { elapsed :.2f} s: { e } "
247- _embedding_logger .error ("Embedding API Timeout" , extra = {"request_id" : request_id , "error" : str (e )})
309+
310+ # Generate curl command for debugging
311+ curl_command = self ._generate_curl_command (self .api_url , dict (self .session .headers ), payload )
312+
313+ # Save to bash script in /tmp if DEBUG is enabled
314+ script_path = None
315+ if CFG .get ("debug" ):
316+ script_path = self ._save_curl_script (curl_command , request_id , file_path , chunk_index )
317+
318+ _embedding_logger .error (
319+ "Embedding API Timeout" ,
320+ extra = {
321+ "request_id" : request_id ,
322+ "error" : str (e ),
323+ "elapsed_s" : elapsed ,
324+ "curl_command" : curl_command ,
325+ "debug_script" : script_path
326+ }
327+ )
328+
329+ # Print to console for easy debugging
330+ print (f"\n { '=' * 80 } " )
331+ print (f"Embedding request timed out after { elapsed :.2f} s" )
332+ print (f"Request ID: { request_id } " )
333+ print (f"File: { file_path } , Chunk: { chunk_index } " )
334+ if script_path :
335+ print (f"\n Debug script saved to: { script_path } " )
336+ print (f"Run with: bash { script_path } " )
337+ else :
338+ print (f"\n Debug with this curl command:" )
339+ print (curl_command )
340+ print (f"{ '=' * 80 } \n " )
248341 except requests .RequestException as e :
249342 elapsed = time .perf_counter () - start
250343 err_msg = f"RequestException after { elapsed :.2f} s: { e } \n { traceback .format_exc ()} "
0 commit comments