Skip to content

Commit 480b9ac

Browse files
CopilotMte90
andcommitted
Add curl command generation on embedding timeout
Co-authored-by: Mte90 <403283+Mte90@users.noreply.github.com>
1 parent 7f27423 commit 480b9ac

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

ai/openai.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,34 @@ 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+
139167
def _log_request_start(self, request_id: str, file_path: str, chunk_index: int, chunk_len: int):
140168
_embedding_logger.debug(
141169
"Embedding request START",
@@ -244,7 +272,26 @@ def embed_text(self, text: str, file_path: str = "<unknown>", chunk_index: int =
244272
except requests.Timeout as e:
245273
elapsed = time.perf_counter() - start
246274
err_msg = f"Timeout after {elapsed:.2f}s: {e}"
247-
_embedding_logger.error("Embedding API Timeout", extra={"request_id": request_id, "error": str(e)})
275+
276+
# Generate and print curl command for debugging
277+
curl_command = self._generate_curl_command(self.api_url, dict(self.session.headers), payload)
278+
_embedding_logger.error(
279+
"Embedding API Timeout",
280+
extra={
281+
"request_id": request_id,
282+
"error": str(e),
283+
"elapsed_s": elapsed,
284+
"curl_command": curl_command
285+
}
286+
)
287+
# Also print to console for easy debugging
288+
print(f"\n{'='*80}")
289+
print(f"Embedding request timed out after {elapsed:.2f}s")
290+
print(f"Request ID: {request_id}")
291+
print(f"File: {file_path}, Chunk: {chunk_index}")
292+
print(f"\nDebug with this curl command:")
293+
print(curl_command)
294+
print(f"{'='*80}\n")
248295
except requests.RequestException as e:
249296
elapsed = time.perf_counter() - start
250297
err_msg = f"RequestException after {elapsed:.2f}s: {e}\n{traceback.format_exc()}"

0 commit comments

Comments
 (0)