Toilatop1sever commited on
Commit
31decae
·
verified ·
1 Parent(s): 71067ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -18,10 +18,10 @@ app.add_middleware(
18
  MODEL_REPO = "unsloth/Qwen3-4B-GGUF"
19
  MODEL_FILE = "Qwen3-4B-Q4_K_M.gguf"
20
 
21
- MAX_HISTORY = 6
22
  MAX_CTX = 4096
23
- MAX_TOKENS = 4096
24
- THREADS = 2
25
 
26
  llm: Optional[Llama] = None
27
 
@@ -39,12 +39,15 @@ async def startup_event():
39
  model_path = MODEL_FILE,
40
  n_ctx = MAX_CTX,
41
  n_threads = THREADS,
42
- n_batch = 8192,
43
- n_ubatch = 512,
 
44
  n_gpu_layers = 0,
45
  verbose = False,
46
  use_mmap = True,
47
  use_mlock = False,
 
 
48
  )
49
  print("Model ready!")
50
 
@@ -61,12 +64,16 @@ class ChatRequest(BaseModel):
61
  top_p: float = 0.9
62
 
63
  def build_messages(req: ChatRequest) -> list:
64
- system = req.system_prompt or "/no_think bạn trợ lý AI, trả lời bằng tiếng Việt."
 
65
  msgs = [{"role": "system", "content": system}]
66
- for msg in req.history[-(MAX_HISTORY * 2):]:
 
 
67
  if msg.role in ("user", "assistant") and msg.content.strip():
68
  if msgs[-1]["role"] != msg.role:
69
  msgs.append({"role": msg.role, "content": msg.content.strip()})
 
70
  if msgs[-1]["role"] == "user":
71
  msgs.pop()
72
  msgs.append({"role": "user", "content": req.prompt.strip()})
@@ -75,7 +82,7 @@ def build_messages(req: ChatRequest) -> list:
75
  @app.post("/chat")
76
  async def chat(req: ChatRequest):
77
  if llm is None:
78
- raise HTTPException(503, "Model chưa sẵn sàng, thử lại sau!")
79
  if not req.prompt.strip():
80
  raise HTTPException(400, "Prompt trống")
81
  if len(req.prompt) > 4000:
@@ -111,11 +118,7 @@ async def chat(req: ChatRequest):
111
 
112
  @app.get("/")
113
  async def root():
114
- return {
115
- "status" : "ok" if llm else "loading",
116
- "model" : MODEL_FILE,
117
- "message" : "Model ready!" if llm else "Model đang tải...",
118
- }
119
 
120
  @app.get("/health")
121
  async def health():
 
18
  MODEL_REPO = "unsloth/Qwen3-4B-GGUF"
19
  MODEL_FILE = "Qwen3-4B-Q4_K_M.gguf"
20
 
21
+ MAX_HISTORY = 4 # Giảm từ 6 xuống 4 để bớt prefill
22
  MAX_CTX = 4096
23
+ MAX_TOKENS = 2048 # Giảm nếu không cần sinh dài
24
+ THREADS = 2 # Giữ nguyên vì 2 vCPU
25
 
26
  llm: Optional[Llama] = None
27
 
 
39
  model_path = MODEL_FILE,
40
  n_ctx = MAX_CTX,
41
  n_threads = THREADS,
42
+ n_threads_batch = THREADS, # Thêm nếu llama_cpp hỗ trợ
43
+ n_batch = 256, # Giảm mạnh từ 8192 -> 256
44
+ n_ubatch = 128, # Giảm từ 512 -> 128
45
  n_gpu_layers = 0,
46
  verbose = False,
47
  use_mmap = True,
48
  use_mlock = False,
49
+ flash_attn = True, # Bật flash attention nếu phiên bản hỗ trợ
50
+ logits_all = False, # Không cần logits, tiết kiệm
51
  )
52
  print("Model ready!")
53
 
 
64
  top_p: float = 0.9
65
 
66
  def build_messages(req: ChatRequest) -> list:
67
+ # Prompt system ngắn gọn hơn một chút
68
+ system = req.system_prompt or "Bạn là trợ lý AI, trả lời bằng tiếng Việt ."
69
  msgs = [{"role": "system", "content": system}]
70
+ # Giữ lại tối đa 4 tin nhắn gần nhất (2 lượt)
71
+ recent_history = req.history[-(MAX_HISTORY * 2):]
72
+ for msg in recent_history:
73
  if msg.role in ("user", "assistant") and msg.content.strip():
74
  if msgs[-1]["role"] != msg.role:
75
  msgs.append({"role": msg.role, "content": msg.content.strip()})
76
+ # Loại bỏ user cuối nếu trùng, rồi thêm prompt mới
77
  if msgs[-1]["role"] == "user":
78
  msgs.pop()
79
  msgs.append({"role": "user", "content": req.prompt.strip()})
 
82
  @app.post("/chat")
83
  async def chat(req: ChatRequest):
84
  if llm is None:
85
+ raise HTTPException(503, "Model chưa sẵn sàng")
86
  if not req.prompt.strip():
87
  raise HTTPException(400, "Prompt trống")
88
  if len(req.prompt) > 4000:
 
118
 
119
  @app.get("/")
120
  async def root():
121
+ return {"status": "ok" if llm else "loading", "model": MODEL_FILE}
 
 
 
 
122
 
123
  @app.get("/health")
124
  async def health():