跪拜 Guibai
← All articles
Frontend · Backend · Interview

A Local 27B Model Builds a 5,816-Entity Knowledge Graph from a 120-Chapter Classic

By Moment ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A 27B model on a laptop can now produce a citation-backed knowledge graph from a full-length novel in half a day, with every relation traceable to source text. That makes local graph construction a realistic option for privacy-sensitive or iterative document analysis, and gives a concrete baseline for memory, token, and cost tradeoffs.

Summary

A local run of Qwen 27B (via Ollama, NVFP4 quantized) turned all 120 chapters of Dream of the Red Chamber into a knowledge graph with 5,816 entities, 5,165 relations, 1,951 attributes, and 10,145 evidence passages. The pipeline splits the text by chapter, extracts entities and relations with a 32K context window, then reviews each candidate against original text before aligning identities across chapters. The total wall time from import to publication was 12 hours 27 minutes on a MacBook Pro with 48GB unified memory.

The work is part of DocFlow, a full-stack project combining agents, RAG, and knowledge graphs. The graph feeds an agent that can answer cross-chapter questions with explicit source citations. A key test case was distinguishing Jia Baoyu from Zhen Baoyu, two nearly identical names that must not collapse into one node.

The article also details memory sizing: BF16 weights would need ~54GB, so quantization plus a ~2GiB KV cache at 32K context is what makes it fit. Token usage totaled 2.98M input and 1.53M output across 806 model calls. The same load on DeepSeek-flash would cost roughly ¥9 to ¥18, but the local choice keeps the full corpus on-device and allows rule changes to be re-run directly.

Takeaways
— 5,816 entities and 5,165 relations extracted from 120 chapters, with 1,951 attributes and 10,145 evidence passages.
— Used qwen3.8:27b-mlx, an ~18GB NVFP4-quantized package, on 48GB unified memory; BF16 weights would need ~54GB.
— Three-stage pipeline: chapter split, local extraction, then evidence and identity review, then cross-chapter identity alignment.
— The 32K-token KV cache alone is ~2GiB for this model's 16 layers, 4 KV heads, and head dimension 256.
— 806 successful model calls: 597 extraction calls (2.15M input, 1.29M output tokens) and 209 review calls (826K input, 244K output).
— Full run took 12 hours 27 minutes from import to publication.
— Equivalent DeepSeek-flash cost would be ¥9.11 idle or ¥18.22 peak for the same token counts.
— Ollama served on 127.0.0.1:11434; requests use /api/chat with model field qwen3.8:27b-mlx, think:false, temperature 0, num_ctx 32768.
Conclusions

The three-stage process makes identity disambiguation the real bottleneck, not entity extraction: the same person has multiple names, different people share names, and model guesses need a dedicated reconciliation pass plus human-verifiable evidence.

The cost comparison with DeepSeek-flash (¥9-18 for the whole job) shows the local choice is not about saving money; it is about keeping the full corpus on-device and being able to re-run the pipeline whenever extraction rules change.

The 12.5-hour wall time is dominated by orchestration and post-processing, not raw generation: 4.5M total tokens across 806 calls implies per-call latency is modest, so batch throughput and review logic set the practical ceiling.

Concepts & terms
KV cache
Memory used to store key-value tensors from attention layers during generation. The article estimates a 32K-token context alone costs about 2 GiB for its specific 16-layer, 4-KV-head, head-dim-256 configuration.
GraphRAG
A Microsoft-style retrieval approach that combines structured graph queries with associated source text placed directly into the model's prompt context, letting an agent answer cross-document questions with citations.
NVFP4
A 4-bit floating-point quantization format used by the MLX/Apple Silicon build of Qwen; the model package is roughly 18GB versus ~54GB for BF16 weights.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗