Lightweight autonomous agent with latent-conditioned behavior control
Nắng AI không phải chatbot.
Nó là một hệ thống kết hợp:
- LLM generation (Qwen 2.5 3B / Hermes 3 8B, 4-bit quantized)
- World Model (Dreamer / RSSM)
- Behavior control qua latent state
- Tool system + safety layers
- Self-evaluation + reflection engine
LLM không tự quyết định hoàn toàn.
Behavior bị điều khiển bởi:
- latent state (h, z) từ RSSM
- reward signal từ environment
- self-evaluation từng turn
Thay vì prompt thêm "hãy bình tĩnh" — hệ thống điều khiển behavior trực tiếp qua latent state injection.
User Input
↓
Embedding + Sentiment Scoring
↓
ConversationEnv (state vector)
↓
RSSM World Model (h, z latent)
↓
LatentAdapter → soft prefix tokens → LLM
↓
Response (streaming)
↓
Self-Eval + Reflection + Reward
↓
Memory (ChromaDB) + Metrics logging
- Load LLM 4-bit quantized
- Shared embedding model (không load 2 lần)
- Thread-safe generation với RLock
- Hybrid tool routing (regex fast-path + LLM fallback)
- Streaming token output
- Latent state:
h(deterministic) +z(stochastic) - Encode emotional dynamics qua turns
- Drive behavior thông qua latent conditioning
(h, z)→ adapter → soft prefix tokens → LLM- Không dùng prompt text, không cần finetune LLM
- Điều khiển behavior trực tiếp ở token level
- State = embedding vector + meta signals
- Action space:
[calm, warm, concerned, tool_use, tool_skip, memory_deep] - Reward: sentiment shift + task success + memory usefulness
- Không phải toy env — dùng trực tiếp trên conversation thật
- ChromaDB storage với async embedding (non-blocking)
- Deduplication (cosine > 0.95)
- Importance filtering + recency decay
- Failure memory riêng cho tool errors
- Memory → reward shaping
- Embedding-based (không keyword matching)
- Top-k cosine mean
- Context-aware scoring
ToolGuard: cooldown, permission checkToolbox: web search, read file, visit URL, ADB phone controlToolVerifier: LLM tự verify output (VALID / INVALID / UNCERTAIN)
- LLM tự chấm: relevance, tone, safety
- Rule-based fallback: generic detection, repetition, persona break
- Không dùng LLM — dùng embedding similarity
- Semantic similarity + contradiction detection + length anomaly
- Dùng latent state (h, z) để check consistency
- Nếu lệch → regenerate response
- Track: personality consistency, stress coherence, latent dynamics, reward/loss
- FastAPI + WebSocket streaming
- Thread pool riêng: LLM / tools / audio
- Semaphore giới hạn tool execution
- TTS:
edge-tts - STT:
speech_recognition - Async loop (không lag UI)
Latent-conditioned behavior
Không phải prompt = "be calm" mà là latent state → control LLM
Deferred reward system Reward không tính ngay — inject vào turn sau, giống RL thực
Non-blocking architecture Embedding async, tool async, audio async, LLM lock-safe
Safety layers Tool Guard → Tool Verifier → Hallucination Detector → Self-Evaluator
Research-ready Metrics logging, reflection experiments, latent conditioning modes
- RSSM chưa fully trained
- Evaluation signal còn noisy
- Chưa có long-term learning pipeline
- Không phải AGI
# config.py
NangConfig.RESEARCH_MODE = True # bật logging + metrics
NangConfig.BASELINE_MODE = True # chỉ log latent, không inject
NangConfig.BASELINE_MODE = False # inject latent vào LLM (experimental)Khi bật Research Mode:
- Latent adapter hoạt động
- Reflection engine chạy
- Metrics logging ghi đầy đủ
- UI hiển thị: Reward, Stress, Consistency, Decision, Hallucination
pip install -r requirements.txt
python main.pyMở trình duyệt: http://localhost:8000
NắngAI/
├── main.py # FastAPI server + WebSocket
├── brain.py # LLM orchestrator + generation
├── soul.py # RSSM World Model (Dreamer)
├── env.py # Conversation environment
├── memory.py # Long-term memory (ChromaDB)
├── latent_adapter.py # Latent → LLM bridge
├── sentiment.py # Embedding-based sentiment
├── tools.py # Tool system + ToolGuard
├── tool_verifier.py # LLM-based tool output verification
├── self_evaluator.py # Self-evaluation engine
├── hallucination_detector.py # Embedding-based hallucination check
├── reflection.py # Reflection + regeneration
├── metrics.py # Research metrics tracking
├── audio.py # TTS + STT
├── config.py # Configuration
├── utils.py # Utilities
└── index.html # Web UI
- ~5.8k lines of core logic (~7–8k including UI & infrastructure)
- 17 modules
- Hardware: RTX 3050 6GB
- Models: Qwen 2.5 3B / Hermes 3 8B (4-bit quantized)
Không cần train model lớn hơn.
Chỉ cần:
- control behavior tốt hơn
- hiểu state tốt hơn
- phản hồi thông minh hơn
Reusing ideas is fine. Reusing code without attribution is not.
CC BY-NC 4.0 — Free cho cá nhân và nghiên cứu, cấm thương mại.
Bạn được phép: sử dụng, chỉnh sửa, chia sẻ với điều kiện ghi nguồn.
Bạn không được phép: sử dụng cho mục đích thương mại dưới bất kỳ hình thức nào.
Long — Vũng Tàu, Việt Nam
Tự học, không có background IT chính quy. Build bằng cách làm thật, fail thật, fix thật.
⚠️ Note: This is a research-oriented system focused on behavior control, not a full-scale training framework. RSSM conditioning is experimental.