Tổng quan

Gemma 4 E4B Local LLM Service

Dịch vụ LLM thế hệ mới chạy local với Google Gemma 4 E4B Instruct (BitsAndBytes INT4 quantized). LLM chính của hệ thống, thay thế hoàn toàn Gemma2 9B kể từ ngày 04/04/2026. Hỗ trợ thinking mode (chain-of-thought), 128K context và native system role.

Đặc điểm kỹ thuật

Model Google Gemma 4 E4B Instruct (google/gemma-4-E4B-it)
Kiến trúc Gemma4ForConditionalGeneration (text + image multimodal)
Quantization BitsAndBytes INT4 (~9.81 GiB loaded)
Framework vLLM 0.19.0 + transformers 5.6.0.dev (from source)
Architecture ports vLLM backend :8092, FastAPI gateway :8044
GPU RTX 3060 12GB (CUDA_VISIBLE_DEVICES=3)
VRAM Usage ~10GB (model weights + KV cache, gpu_memory_utilization=0.97)
Context Window 128K (vLLM deployed with max_model_len=4096 for stability)
Ngôn ngữ 140+ ngôn ngữ, bao gồm tiếng Việt và tiếng Anh
Thêm ngày 04/04/2026 — thay thế gemma2_service

So sánh với Gemma2

Tính năng Gemma2 9B (cũ) Gemma4 E4B (mới)
Parameters 9B (AWQ INT4) 4.5B effective / 8B total (BnB INT4)
Context 4,096 tokens 128K tokens (deployed: 4K)
Thinking mode ❌ Không có ✅ Built-in chain-of-thought
System role Hỗ trợ hạn chế ✅ Native support
Multimodal ❌ Text only ✅ Text + Image
GPU RTX 3060 CVD=1 (port 8042) RTX 3060 CVD=3 (port 8044)
API /gemma2/ /gemma4/
OpenAI compat ✅ (drop-in replacement)
Migration từ Gemma2

Thay pnt.badt.vn/gemma2/v1 bằng pnt.badt.vn/gemma4/v1 và đổi model name thành gemma-4-e4b-it. API tương thích hoàn toàn.

Tính năng nổi bật

Thinking Mode

Chain-of-thought reasoning tích hợp sẵn, bật bằng enable_thinking: true

128K Context

Context window 128K tokens — gấp 32 lần Gemma2

140+ Ngôn ngữ

Tiếng Việt chất lượng cao, đa ngôn ngữ vượt trội

Multimodal

Text + Image — hỗ trợ phân tích hình ảnh y tế

OpenAI API

Tương thích hoàn toàn với OpenAI Python/JS SDK

Local & Bảo mật

Xử lý 100% local, dữ liệu không ra ngoài

API Endpoints

Base URL

https://pnt.badt.vn/gemma4

Danh sách Endpoints

GET /health

Health check — trả về trạng thái service và vLLM backend.

GET /status

Service status, thông tin model và GPU.

POST /chat

Simple chat endpoint — tương thích ngược với gemma2_service. Hỗ trợ enable_thinking.

Request body

{
  "prompt": "string (required)",
  "max_tokens": 1024,
  "temperature": 1.0,
  "system_prompt": "string (optional)",
  "enable_thinking": false
}
POST /v1/chat/completions

OpenAI-compatible chat completions API.

Request body

{
  "model": "gemma-4-e4b-it",
  "messages": [
    {"role": "system", "content": "..."},
    {"role": "user", "content": "..."}
  ],
  "temperature": 1.0,
  "max_tokens": 1024,
  "top_p": 0.95,
  "top_k": 64,
  "enable_thinking": false
}
GET /v1/models

List available models (OpenAI format).

Xác thực

Bearer Token Authentication Required

API này yêu cầu Bearer token trong Authorization header. Liên hệ admin để nhận API token.

Cách dùng

Authorization: Bearer YOUR_API_TOKEN
Lưu ý bảo mật
  • Không chia sẻ token công khai hoặc commit vào source code
  • Dùng environment variables để lưu token
  • Rate limiting áp dụng để đảm bảo công bằng

Ví dụ sử dụng

1. Simple Chat (cURL)

curl -X POST https://pnt.badt.vn/gemma4/chat \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Triệu chứng của bệnh viêm phổi là gì?",
    "max_tokens": 300,
    "temperature": 0.7
  }'

Response

{
  "response": "Triệu chứng của bệnh viêm phổi thường bao gồm:\n\n1. Ho có đờm (có thể có máu)\n2. Sốt cao...",
  "model": "google/gemma-4-E4B-it",
  "elapsed_time": "3.12s",
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 95,
    "total_tokens": 110
  }
}

2. Chat với System Prompt (Python)

import requests
import os

url = "https://pnt.badt.vn/gemma4/chat"
headers = {
    "Authorization": f"Bearer {os.getenv('GEMMA4_API_TOKEN')}",
    "Content-Type": "application/json"
}
data = {
    "prompt": "Bệnh nhân 45 tuổi, ho kéo dài 3 tuần. Hướng chẩn đoán?",
    "system_prompt": "Bạn là bác sĩ nội khoa. Trả lời ngắn gọn, chuyên nghiệp.",
    "max_tokens": 400,
    "temperature": 0.7
}

response = requests.post(url, json=data, headers=headers)
result = response.json()
print(result["response"])

3. Health Check

curl https://pnt.badt.vn/gemma4/health \
  -H "Authorization: Bearer YOUR_API_TOKEN"

Response

{
  "status": "healthy",
  "service": "gemma4_service",
  "vllm_server": "healthy",
  "model": "google/gemma-4-E4B-it",
  "timestamp": "2026-04-04T15:43:27.807199"
}

Thinking Mode (Chain-of-Thought)

🧠 Tính năng độc quyền của Gemma 4

Khi bật enable_thinking: true, model sẽ suy nghĩ từng bước trước khi đưa ra câu trả lời — tương tự o1/DeepSeek Thinking. Hữu ích cho các câu hỏi lâm sàng phức tạp, chẩn đoán phân biệt, lập kế hoạch điều trị.

Ví dụ Thinking Mode

curl -X POST https://pnt.badt.vn/gemma4/chat \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Bệnh nhân 60 tuổi, đái tháo đường type 2, vừa nhập viện với đau ngực. Hướng xử trí ưu tiên?",
    "system_prompt": "Bạn là bác sĩ nội tim mạch.",
    "max_tokens": 800,
    "temperature": 0.7,
    "enable_thinking": true
  }'
Khi nào nên dùng Thinking Mode?
  • Chẩn đoán phân biệt phức tạp
  • Lập kế hoạch điều trị nhiều bước
  • Giải bài toán lâm sàng có nhiều yếu tố
  • Phân tích ca lâm sàng cho giảng dạy

Lưu ý: Thinking mode tốn thêm tokens và thời gian (~2-3x).

OpenAI Compatible API

Gemma4 service hỗ trợ đầy đủ OpenAI-compatible API — drop-in replacement cho gemma2_service:

Python với OpenAI SDK

from openai import OpenAI
import os

client = OpenAI(
    api_key="not-needed",
    base_url="https://pnt.badt.vn/gemma4/v1",
    default_headers={
        "Authorization": f"Bearer {os.getenv('GEMMA4_API_TOKEN')}"
    }
)

response = client.chat.completions.create(
    model="gemma-4-e4b-it",
    messages=[
        {"role": "system", "content": "Bạn là bác sĩ AI chuyên y học nội khoa."},
        {"role": "user", "content": "Triệu chứng suy tim sung huyết?"}
    ],
    temperature=0.7,
    max_tokens=400
)

print(response.choices[0].message.content)

JavaScript với OpenAI SDK

import OpenAI from 'openai';

const client = new OpenAI({
    apiKey: 'not-needed',
    baseURL: 'https://pnt.badt.vn/gemma4/v1',
    defaultHeaders: {
        'Authorization': `Bearer ${process.env.GEMMA4_API_TOKEN}`
    }
});

const response = await client.chat.completions.create({
    model: 'gemma-4-e4b-it',
    messages: [
        { role: 'system', content: 'Bạn là trợ lý y tế AI.' },
        { role: 'user', content: 'Xin chào!' }
    ],
    max_tokens: 200
});

console.log(response.choices[0].message.content);
Migration từ gemma2_service

Chỉ cần đổi base URL và model name:

- base_url = "https://pnt.badt.vn/gemma2/v1"
- model = "gemma-2-9b-it"
+ base_url = "https://pnt.badt.vn/gemma4/v1"
+ model = "gemma-4-e4b-it"

Rate Limits

Fair Usage Policy
  • Max concurrent requests: 2 (vLLM --max-num-seqs 2)
  • Request timeout: 600 giây
  • Max tokens per request: 4096 (vLLM --max-model-len 4096)
  • Max prompt size: 10MB

Thinking mode tăng thời gian xử lý — đặt timeout phù hợp.

Error Codes

Code Error Description
400 Bad Request Tham số request không hợp lệ
401 Unauthorized Bearer token thiếu hoặc không hợp lệ
422 Validation Error Request validation failed
500 Internal Server Error Lỗi model server
503 Service Unavailable vLLM backend chưa sẵn sàng
504 Gateway Timeout Request timeout (>600s)

Tài liệu bổ sung