How zgc-llm-safety/trafficllm works
Project Type and Purpose
• TrafficLLM is an open‐source framework for adapting large-language models (ChatGLM2, GLM4, LLaMA2) to network traffic analysis tasks (detection & generation).
• Core features: traffic-domain tokenization, dual-stage tuning (instruction + traffic data), and extensible parameter-efficient fine-tuning (EA-PEFT).
Main Components
A. Data Layer
• Raw Traffic Datasets (PCAP files)
• Instruction Datasets (JSON of human instructions)
B. Preprocessing Layer
• preprocess_dataset.py, flow_data_preprocess.py, packet_data_preprocess.py
• Output: structured JSON datasets for tuning & testing
C. Tokenization Layer
• traffic_tokenizer.py
• Extends base‐model tokenizer to handle traffic segments & special tokens
D. Dual-Stage Tuning Pipeline
1. Stage 1 (Instruction Tuning)
• Script: dual-stage-tuning/trafficllm_stage1.sh & main.py
• Learns LLM to understand natural-language traffic tasks
2. Stage 2 (Task-Specific Traffic Tuning)
• Script: dual-stage-tuning/trafficllm_stage2.sh & trainer_seq2seq.py
• Teaches model to map traffic patterns to labels or generate pcap payloads
E. EA-PEFT Extension Module
• EA-PEFT/ea-peft.py
• Dynamically updates or registers new task adapters with low overhead
• Produces separate PEFT checkpoints per traffic task
F. Model Store
• Base models: models/chatglm2/chatglm2-6b/, Adapt2GLM4/...
• PEFT checkpoints: stored under models/chatglm2/peft/<task>/checkpoint-*/
G. Evaluation Component
• evaluation.py
• Consumes base + PEFT models plus test JSON & label files to compute metrics
H. Inference & Serving Layer
• inference.py (CLI mode)
• trafficllm_server.py (Streamlit web server)
• config.json: registers model_path, peft_set, task mapping, preprompts
Relationships & Data Flow
• Raw Data → Preprocessing → Processed JSON
• Processed JSON → Tokenizer → Tokenized Input
• Tokenized Input + Base Model → Stage 1 Tuning → Instruction PEFT Adapter
• Stage 1 Model → Stage 2 Tuning (per-task) → Task-Specific PEFT Adapters
• All PEFT Adapters registered via config.json
• Inference Module loads Base Model + selected PEFT Adapter → Serves predictions
• Evaluation Module loads same model combinations → Computes detection/generation metrics
• EA-PEFT Module may extend/update PEFT Adapters by feeding in new traffic data
Architectural Patterns & Design Principles
• Pipeline Pattern: clear linear flow from raw data to deployment
• Modularization: each layer (preprocess, tokenize, tune, serve) is independent
• Plugin/Adapter Pattern: PEFT adapters encapsulate task-specific behavior
• Configuration-driven: config.json drives model & adapter selection at runtime
• Reusability & Extensibility: add new tasks by adding new PEFT checkpoints and updating config.json
Putting It All Together
• Leftmost column: Data Layer (Raw Traffic, Instructions)
• Next column: Preprocessing → Tokenization
• Center column: Dual-Stage Tuning (Stage 1 and Stage 2) plus EA-PEFT extension
• Below/alongside: Model Store (base + PEFT checkpoints)
• Rightmost column: Inference & Serving + Evaluation