init project
Browse files- rag_core/embedder.py +18 -7
rag_core/embedder.py
CHANGED
|
@@ -1,11 +1,22 @@
|
|
| 1 |
import requests
|
|
|
|
|
|
|
| 2 |
from rag_core.utils import log_timed
|
| 3 |
|
| 4 |
@log_timed("gửi API tạo embedding")
|
| 5 |
-
def get_embedding(text: str):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import time
|
| 3 |
+
import logging
|
| 4 |
from rag_core.utils import log_timed
|
| 5 |
|
| 6 |
@log_timed("gửi API tạo embedding")
|
| 7 |
+
def get_embedding(text: str, retries: int = 3):
|
| 8 |
+
for i in range(retries):
|
| 9 |
+
try:
|
| 10 |
+
response = requests.post(
|
| 11 |
+
"https://vietcat-phobertnode.hf.space/embed",
|
| 12 |
+
json={"text": text},
|
| 13 |
+
timeout=30 # Tăng từ 10 lên 30 giây
|
| 14 |
+
)
|
| 15 |
+
response.raise_for_status() # Nếu không 200 -> raise exception
|
| 16 |
+
return response.json()["embedding"]
|
| 17 |
+
except requests.exceptions.RequestException as e:
|
| 18 |
+
logging.warning(f"Lỗi embedding (lần {i+1}/{retries}): {e}")
|
| 19 |
+
if i < retries - 1:
|
| 20 |
+
time.sleep(2) # Đợi 2s rồi thử lại
|
| 21 |
+
else:
|
| 22 |
+
raise RuntimeError(f"Không thể lấy embedding sau {retries} lần thử.")
|