Spaces:
Runtime error
Runtime error
[Delta-02] Drill C retry/backoff 또는 graceful fallback 동작 확인 테스트 코드 추가 ver.2
Browse files- app/embedding_utils.py +12 -2
- app/main.py +12 -2
app/embedding_utils.py
CHANGED
|
@@ -3,10 +3,19 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from app.config import OPENAI_API_KEY, OPENAI_EMBEDDING_MODEL
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Cross-encoder 리랭커 (첫 호출 시 모델 다운로드)
|
| 12 |
reranker = CrossEncoder("BAAI/bge-reranker-v2-m3")
|
|
@@ -54,6 +63,7 @@ def _is_transient_error(e: Exception) -> bool:
|
|
| 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(
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from openai import OpenAI, RateLimitError, APITimeoutError, APIConnectionError
|
| 5 |
from sentence_transformers import CrossEncoder
|
| 6 |
+
from tenacity import (
|
| 7 |
+
retry,
|
| 8 |
+
wait_exponential,
|
| 9 |
+
stop_after_attempt,
|
| 10 |
+
retry_if_exception,
|
| 11 |
+
before_sleep_log,
|
| 12 |
+
)
|
| 13 |
+
import logging
|
| 14 |
from app.config import OPENAI_API_KEY, OPENAI_EMBEDDING_MODEL
|
| 15 |
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
client = OpenAI(api_key=OPENAI_API_KEY, timeout=1.0)
|
| 19 |
|
| 20 |
# Cross-encoder 리랭커 (첫 호출 시 모델 다운로드)
|
| 21 |
reranker = CrossEncoder("BAAI/bge-reranker-v2-m3")
|
|
|
|
| 63 |
retry=retry_if_exception(_is_transient_error),
|
| 64 |
wait=wait_exponential(multiplier=1, min=2, max=30),
|
| 65 |
stop=stop_after_attempt(3),
|
| 66 |
+
before_sleep=before_sleep_log(logger, logging.WARNING),
|
| 67 |
)
|
| 68 |
def _openai_embed(text: str) -> List[float]:
|
| 69 |
response = client.embeddings.create(
|
app/main.py
CHANGED
|
@@ -32,14 +32,23 @@ from openai import (
|
|
| 32 |
APITimeoutError,
|
| 33 |
APIConnectionError,
|
| 34 |
)
|
| 35 |
-
from tenacity import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
import hashlib
|
| 37 |
import time
|
| 38 |
from concurrent.futures import ThreadPoolExecutor
|
| 39 |
|
|
|
|
|
|
|
| 40 |
app = FastAPI(title="DocuFlow AI Agent")
|
| 41 |
|
| 42 |
-
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 43 |
|
| 44 |
|
| 45 |
@app.get("/health")
|
|
@@ -61,6 +70,7 @@ def _is_transient_error(e: Exception) -> bool:
|
|
| 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)
|
|
|
|
| 32 |
APITimeoutError,
|
| 33 |
APIConnectionError,
|
| 34 |
)
|
| 35 |
+
from tenacity import (
|
| 36 |
+
retry,
|
| 37 |
+
wait_exponential,
|
| 38 |
+
stop_after_attempt,
|
| 39 |
+
retry_if_exception,
|
| 40 |
+
before_sleep_log,
|
| 41 |
+
)
|
| 42 |
+
import logging
|
| 43 |
import hashlib
|
| 44 |
import time
|
| 45 |
from concurrent.futures import ThreadPoolExecutor
|
| 46 |
|
| 47 |
+
logger = logging.getLogger(__name__)
|
| 48 |
+
|
| 49 |
app = FastAPI(title="DocuFlow AI Agent")
|
| 50 |
|
| 51 |
+
client = OpenAI(api_key=OPENAI_API_KEY, timeout=1.0)
|
| 52 |
|
| 53 |
|
| 54 |
@app.get("/health")
|
|
|
|
| 70 |
retry=retry_if_exception(_is_transient_error),
|
| 71 |
wait=wait_exponential(multiplier=1, min=2, max=30),
|
| 72 |
stop=stop_after_attempt(3),
|
| 73 |
+
before_sleep=before_sleep_log(logger, logging.WARNING),
|
| 74 |
)
|
| 75 |
def _call_llm(messages: list, model: str) -> str:
|
| 76 |
response = client.chat.completions.create(model=model, messages=messages)
|