update gemini
Browse files- rag_core/llm.py +32 -19
rag_core/llm.py
CHANGED
|
@@ -1,40 +1,53 @@
|
|
| 1 |
import requests
|
| 2 |
import logging
|
| 3 |
import time
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
LLM_ENDPOINT = "https://vietcat-gemma34b.hf.space/purechat"
|
| 6 |
|
| 7 |
def generate_answer(prompt: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 8 |
max_retries = 3
|
| 9 |
-
timeout =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
for attempt in range(1, max_retries + 1):
|
| 12 |
try:
|
| 13 |
-
logging.info(f"📡 Gửi request đến
|
| 14 |
-
|
| 15 |
-
prompt_length = len(prompt.split())
|
| 16 |
-
response = requests.post(
|
| 17 |
-
LLM_ENDPOINT,
|
| 18 |
-
json={"prompt": prompt, "max_tokens": prompt_length},
|
| 19 |
-
timeout=timeout
|
| 20 |
-
)
|
| 21 |
response.raise_for_status()
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
except requests.exceptions.Timeout as e:
|
| 25 |
logging.warning(f"⚠️ Timeout ở lần {attempt}: {e}")
|
| 26 |
if attempt < max_retries:
|
| 27 |
-
timeout *= 2
|
| 28 |
continue
|
| 29 |
else:
|
| 30 |
logging.error("❌ Lỗi timeout sau 3 lần retry.")
|
| 31 |
-
return "Lỗi timeout khi gửi tới
|
| 32 |
-
|
| 33 |
except Exception as e:
|
| 34 |
-
logging.warning(f"⚠️ Lỗi khi gửi request tới
|
| 35 |
if attempt < max_retries:
|
| 36 |
-
time.sleep(1)
|
| 37 |
continue
|
| 38 |
else:
|
| 39 |
-
logging.error("❌ Lỗi khi gửi tới
|
| 40 |
-
return "Lỗi khi gửi tới
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import logging
|
| 3 |
import time
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
LLM_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
def generate_answer(prompt: str) -> str:
|
| 10 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 11 |
+
if not api_key:
|
| 12 |
+
return "Thiếu biến môi trường GEMINI_API_KEY."
|
| 13 |
max_retries = 3
|
| 14 |
+
timeout = 60 # Gemini Flash rất nhanh, timeout 1 phút là đủ
|
| 15 |
+
|
| 16 |
+
payload = {
|
| 17 |
+
"contents": [
|
| 18 |
+
{
|
| 19 |
+
"parts": [
|
| 20 |
+
{"text": prompt}
|
| 21 |
+
]
|
| 22 |
+
}
|
| 23 |
+
]
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
headers = {"Content-Type": "application/json"}
|
| 27 |
+
url = f"{LLM_ENDPOINT}?key={api_key}"
|
| 28 |
|
| 29 |
for attempt in range(1, max_retries + 1):
|
| 30 |
try:
|
| 31 |
+
logging.info(f"📡 Gửi request đến Gemini 2.5 Flash tại {LLM_ENDPOINT}")
|
| 32 |
+
response = requests.post(url, json=payload, headers=headers, timeout=timeout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
response.raise_for_status()
|
| 34 |
+
data = response.json()
|
| 35 |
+
# Lấy text trả về
|
| 36 |
+
return data["candidates"][0]["content"]["parts"][0]["text"]
|
| 37 |
except requests.exceptions.Timeout as e:
|
| 38 |
logging.warning(f"⚠️ Timeout ở lần {attempt}: {e}")
|
| 39 |
if attempt < max_retries:
|
| 40 |
+
timeout *= 2
|
| 41 |
continue
|
| 42 |
else:
|
| 43 |
logging.error("❌ Lỗi timeout sau 3 lần retry.")
|
| 44 |
+
return "Lỗi timeout khi gửi tới Gemini."
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
+
logging.warning(f"⚠️ Lỗi khi gửi request tới Gemini (lần {attempt}): {e}")
|
| 47 |
if attempt < max_retries:
|
| 48 |
+
time.sleep(1)
|
| 49 |
continue
|
| 50 |
else:
|
| 51 |
+
logging.error("❌ Lỗi khi gửi tới Gemini sau 3 lần thử.")
|
| 52 |
+
return "Lỗi khi gửi tới Gemini."
|
| 53 |
+
return "Lỗi không xác định khi gửi tới Gemini."
|