Commit 5fe080db authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

chore: clean up scratch files and prepare production DB

parent 159099af
chatbot_canifa @ f9b10ebe
Subproject commit f9b10ebef38a7af9ffb488678084f0ebac581afc
......@@ -52,3 +52,6 @@ def update_sqlite_colors():
if __name__ == "__main__":
update_sqlite_colors()
import subprocess
import logging
import itertools
import threading
logger = logging.getLogger(__name__)
# Round-robin xoay vòng qua 7 model để tận dụng hết quota
MODELS = [
"gemini-3.1-flash-lite-preview", # 3.1 lite # 2.5 lite
"gemini-3-flash-preview", # 3.0 flash
"gemini-3.1-pro-preview", # 3.1 pro
"gemini-2.5-pro", # 2.5 pro
"gemma-4-31b-it", # Gemma 4 31B
"gemma-4-26b-a4b-it", # Gemma 4 26B
]
_model_cycle = itertools.cycle(MODELS)
_model_lock = threading.Lock()
def _next_model():
with _model_lock:
return next(_model_cycle)
def run_gemini_cli(prompt: str, model: str = None) -> str:
"""
Wrapper gọi trực tiếp Gemini CLI của hệ thống qua subprocess.
Sử dụng cờ -p (hoặc --prompt) để chạy ở chế độ non-interactive (headless).
Tự động xoay vòng model nếu không truyền model cụ thể.
"""
try:
import shutil
import os
chosen_model = model or _next_model()
# Tìm đường dẫn thực tế của lệnh gemini.CMD
gemini_cmd = shutil.which("gemini")
if not gemini_cmd:
return "Error: Không tìm thấy lệnh gemini"
# Tìm đường dẫn file JS gốc của Gemini CLI
gemini_js = os.path.join(os.path.dirname(gemini_cmd), "node_modules", "@google", "gemini-cli", "bundle", "gemini.js")
# Gọi trực tiếp qua Node.js (không qua shell CMD) để không bị lỗi khi prompt chứa dấu ngoặc kép JSON
result = subprocess.run(
["node", gemini_js, "-p", prompt, "--model", chosen_model],
capture_output=True,
text=True,
encoding="utf-8",
check=True
)
logger.info(f"Dùng model: {chosen_model}")
return result.stdout.strip()
except subprocess.CalledProcessError as e:
logger.error(f"Lỗi khi gọi Gemini CLI: {e.stderr}")
print(f"Error output: {e.stderr}")
return ""
except FileNotFoundError:
logger.error("Không tìm thấy lệnh 'gemini'. Hãy chắc chắn Gemini CLI đã được cài đặt và nằm trong PATH.")
return ""
if __name__ == "__main__":
print("Đang test Gemini CLI Wrapper...")
test_prompt = "Xin chào, hãy trả lời ngắn gọn trong 1 câu: bạn có thể sinh JSON được không?"
print(f"Prompt: {test_prompt}")
response = run_gemini_cli(test_prompt)
print("\n--- KẾT QUẢ TỪ GEMINI CLI ---")
print(response)
print("-----------------------------")
version = 1
revision = 3
requires-python = ">=3.14"
requires-python = ">=3.12"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment