Spaces:
Runtime error
Runtime error
[Delta-02] Drill C retry/backoff 대응 코드 추가
Browse files- app/embedding_utils.py +7 -5
- app/main.py +25 -5
app/embedding_utils.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from typing import List
|
| 2 |
import nltk
|
| 3 |
import numpy as np
|
| 4 |
-
from openai import OpenAI, RateLimitError
|
| 5 |
from sentence_transformers import CrossEncoder
|
| 6 |
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception
|
| 7 |
from app.config import OPENAI_API_KEY, OPENAI_EMBEDDING_MODEL
|
|
@@ -41,17 +41,19 @@ def _simple_split_by_chars(
|
|
| 41 |
return chunks
|
| 42 |
|
| 43 |
|
| 44 |
-
def
|
| 45 |
-
"""
|
|
|
|
|
|
|
| 46 |
if isinstance(e, RateLimitError):
|
| 47 |
return e.code != "insufficient_quota"
|
| 48 |
return False
|
| 49 |
|
| 50 |
|
| 51 |
@retry(
|
| 52 |
-
retry=retry_if_exception(
|
| 53 |
wait=wait_exponential(multiplier=1, min=2, max=30),
|
| 54 |
-
stop=stop_after_attempt(
|
| 55 |
)
|
| 56 |
def _openai_embed(text: str) -> List[float]:
|
| 57 |
response = client.embeddings.create(
|
|
|
|
| 1 |
from typing import List
|
| 2 |
import nltk
|
| 3 |
import numpy as np
|
| 4 |
+
from openai import OpenAI, RateLimitError, APITimeoutError, APIConnectionError
|
| 5 |
from sentence_transformers import CrossEncoder
|
| 6 |
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception
|
| 7 |
from app.config import OPENAI_API_KEY, OPENAI_EMBEDDING_MODEL
|
|
|
|
| 41 |
return chunks
|
| 42 |
|
| 43 |
|
| 44 |
+
def _is_transient_error(e: Exception) -> bool:
|
| 45 |
+
"""타임아웃·연결 오류·rate limit(quota 부족 제외)을 재시도 대상으로 판단"""
|
| 46 |
+
if isinstance(e, (APITimeoutError, APIConnectionError)):
|
| 47 |
+
return True
|
| 48 |
if isinstance(e, RateLimitError):
|
| 49 |
return e.code != "insufficient_quota"
|
| 50 |
return False
|
| 51 |
|
| 52 |
|
| 53 |
@retry(
|
| 54 |
+
retry=retry_if_exception(_is_transient_error),
|
| 55 |
wait=wait_exponential(multiplier=1, min=2, max=30),
|
| 56 |
+
stop=stop_after_attempt(3),
|
| 57 |
)
|
| 58 |
def _openai_embed(text: str) -> List[float]:
|
| 59 |
response = client.embeddings.create(
|
app/main.py
CHANGED
|
@@ -25,7 +25,13 @@ from .config import (
|
|
| 25 |
)
|
| 26 |
|
| 27 |
import pdfplumber
|
| 28 |
-
from openai import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception
|
| 30 |
import hashlib
|
| 31 |
import time
|
|
@@ -42,17 +48,19 @@ async def health_check():
|
|
| 42 |
return {"status": "healthy"}
|
| 43 |
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
"""
|
|
|
|
|
|
|
| 47 |
if isinstance(e, RateLimitError):
|
| 48 |
return e.code != "insufficient_quota"
|
| 49 |
return False
|
| 50 |
|
| 51 |
|
| 52 |
@retry(
|
| 53 |
-
retry=retry_if_exception(
|
| 54 |
wait=wait_exponential(multiplier=1, min=2, max=30),
|
| 55 |
-
stop=stop_after_attempt(
|
| 56 |
)
|
| 57 |
def _call_llm(messages: list, model: str) -> str:
|
| 58 |
response = client.chat.completions.create(model=model, messages=messages)
|
|
@@ -239,6 +247,12 @@ async def upload_document(file: UploadFile = File(...)):
|
|
| 239 |
status_code=500,
|
| 240 |
detail="서버 설정 오류가 발생했습니다. 관리자에게 문의해주세요.",
|
| 241 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
except Exception as e:
|
| 243 |
print(f"오류 발생: {str(e)}")
|
| 244 |
raise HTTPException(status_code=500, detail=str(e))
|
|
@@ -374,6 +388,12 @@ async def question_answering(question: Dict[str, str] = Body(...)):
|
|
| 374 |
status_code=500,
|
| 375 |
detail="서버 설정 오류가 발생했습니다. 관리자에게 문의해주세요.",
|
| 376 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
except RateLimitError as e:
|
| 378 |
if e.code == "insufficient_quota":
|
| 379 |
raise HTTPException(
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
import pdfplumber
|
| 28 |
+
from openai import (
|
| 29 |
+
OpenAI,
|
| 30 |
+
RateLimitError,
|
| 31 |
+
AuthenticationError,
|
| 32 |
+
APITimeoutError,
|
| 33 |
+
APIConnectionError,
|
| 34 |
+
)
|
| 35 |
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception
|
| 36 |
import hashlib
|
| 37 |
import time
|
|
|
|
| 48 |
return {"status": "healthy"}
|
| 49 |
|
| 50 |
|
| 51 |
+
def _is_transient_error(e: Exception) -> bool:
|
| 52 |
+
"""타임아웃·연결 오류·rate limit(quota 부족 제외)을 재시도 대상으로 판단"""
|
| 53 |
+
if isinstance(e, (APITimeoutError, APIConnectionError)):
|
| 54 |
+
return True
|
| 55 |
if isinstance(e, RateLimitError):
|
| 56 |
return e.code != "insufficient_quota"
|
| 57 |
return False
|
| 58 |
|
| 59 |
|
| 60 |
@retry(
|
| 61 |
+
retry=retry_if_exception(_is_transient_error),
|
| 62 |
wait=wait_exponential(multiplier=1, min=2, max=30),
|
| 63 |
+
stop=stop_after_attempt(3),
|
| 64 |
)
|
| 65 |
def _call_llm(messages: list, model: str) -> str:
|
| 66 |
response = client.chat.completions.create(model=model, messages=messages)
|
|
|
|
| 247 |
status_code=500,
|
| 248 |
detail="서버 설정 오류가 발생했습니다. 관리자에게 문의해주세요.",
|
| 249 |
)
|
| 250 |
+
except APITimeoutError:
|
| 251 |
+
print("오류 발생: OpenAI API 요청 시간 초과")
|
| 252 |
+
raise HTTPException(
|
| 253 |
+
status_code=504,
|
| 254 |
+
detail="요청 처리 시간이 초과되었습니다. 잠시 후 다시 시도해주세요.",
|
| 255 |
+
)
|
| 256 |
except Exception as e:
|
| 257 |
print(f"오류 발생: {str(e)}")
|
| 258 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 388 |
status_code=500,
|
| 389 |
detail="서버 설정 오류가 발생했습니다. 관리자에게 문의해주세요.",
|
| 390 |
)
|
| 391 |
+
except APITimeoutError:
|
| 392 |
+
print("오류 발생: OpenAI API 요청 시간 초과")
|
| 393 |
+
raise HTTPException(
|
| 394 |
+
status_code=504,
|
| 395 |
+
detail="요청 처리 시간이 초과되었습니다. 잠시 후 다시 시도해주세요.",
|
| 396 |
+
)
|
| 397 |
except RateLimitError as e:
|
| 398 |
if e.code == "insufficient_quota":
|
| 399 |
raise HTTPException(
|