Building a Local Knowledge Base for Web Novels: A Practical Guide to LLM API Integration
This guide explains how to build a local knowledge base for web novels by keeping data and retrieval on your own machine while calling large language models through cloud APIs. It covers a four-layer architecture, how to route tasks between reasoning-oriented and general-purpose chat models, chunking and metadata strategies for long documents, hybrid vector and keyword retrieval, prompt constraints and response logging to reduce hallucination, and practical handling of cost, rate limits, and stability. You will learn a resource-efficient setup that runs on an ordinary laptop and scales as your text collection grows.
1. First, a key distinction: a local knowledge base does not require a locally deployed model
When creators set up a private knowledge base, their first instinct is often to “download the model to a local machine.” That instinct needs correcting. “Local” in the context of a knowledge base means keeping the data and the retrieval pipeline on your own machine, not moving multi-billion-parameter model weights onto it.
The reason is the hard boundary imposed by resource constraints. Current mainstream open-source LLMs come in different parameter sizes, and parameter count directly determines VRAM usage and inference throughput. Consumer-grade GPUs typically offer anywhere from a few GB to a little over twenty GB of VRAM. Running a distilled small-parameter version is feasible, but performance degrades noticeably on tasks such as long-context understanding, multi-hop reasoning, and mapping character relationships. A full-parameter model, meanwhile, demands far more VRAM and bandwidth than an ordinary workstation can provide. The conclusion is clear: unless you have a professional-grade inference cluster, keep the model in the cloud as a service and use local resources for data governance and retrieval.
This division of labor brings three direct benefits:
- Locally, you can store as many long documents as you want, without being limited by the context window of a web chat interface;
- You avoid unresponsive periods caused by peak-hour queuing and rate limiting;
- Data preprocessing, index building, and result archiving are all under your control, making version management and traceability easier.
2. Overall architecture: a four-layer structure
A usable local web-novel knowledge base can be abstracted into four layers:
- Data layer: raw text (novels, setting collections, outlines, character cards), cleaned plain text, and split fragments.
- Index layer: a vector index plus a keyword inverted index, with the two complementing each other.
- Model layer: inference capabilities provided by cloud APIs, routed to different models by task type.
- Application layer: retrieval-based Q&A, setting consistency checks, character relationship extraction, and style-imitation writing.
A skeletal example of the call flow is shown below (pseudocode that only expresses the calling and retry logic):
def ask_llm(client, model, messages, max_retry=3):for i in range(max_retry):try:resp = client.chat.completions.create(model=model, # reasoning-oriented or general-purpose chatmessages=messages,temperature=0.3, # lower for factual lookup, higher for creative divergence)return resp.choices[0].message.contentexcept Exception as e:if i == max_retry - 1:raisetime.sleep(2 ** i) # exponential backoff to avoid an avalanche
3. Model division of labor: choosing between reasoning and chat models
One valuable observation from the original practice is that different model tiers suit different tasks. That experience can be generalized into a common principle.
- Reasoning-oriented models: strong at multi-step derivation, foreshadowing payoff, and logical closure checks, but more prone to “overstepping” during generation, meaning they add settings that do not exist in the source text. They are suitable for analytical tasks, and you must require them to cite the source passage.
- General-purpose chat models: more constrained and more “restrained,” offering a more stable experience for pure fact lookup, entry summarization, and terminology explanation. They are a good default backend for retrieval-based Q&A.
- High-speed tiers: some platforms offer faster but more expensive service tiers, which are essentially a queuing advantage from resource isolation. They are worth the investment for interactive scenarios (asking questions while writing) but offer poor cost-efficiency for batch offline tasks.
Engineering recommendation: implement model routing at the application layer, sending “look up a setting” to the chat model and “find a contradiction” to the reasoning model, rather than forcing a single model to handle everything.
4. Long-document processing: chunking, vectorization, and hybrid retrieval
The defining trait of web-novel text is extreme length, dense references, and many proper nouns. Feeding an entire book directly is neither economical nor accurate, so a retrieval-augmented approach is required.
For chunking strategy, split along semantic boundaries rather than a fixed character count: use chapters as the first-level split and scene transitions as the second-level split, keeping each fragment to a few hundred characters with 10%–15% overlap to avoid broken cross-fragment references. Attach metadata to each fragment:
{"doc": "some work","chapter": 37,"characters": ["protagonist", "supporting character A"],"tags": ["worldbuilding", "faction settings"],"text": "..."}
Hybrid retrieval is the key. Pure vector retrieval excels at semantic similarity but often struggles with low-frequency proper terms such as character names, place names, and move names; keyword retrieval is the opposite. Fusing the two result sets before passing them to the model typically yields a significantly higher recall rate than either path alone. Fusion can use a simple weighted sum:
score = α * normalize(vector_sim) + (1 - α) * normalize(bm25_score)
Start α at 0.5 and adjust it according to the density of proper nouns.
5. Prompts and result validation
The most common failure mode in retrieval augmentation is the model “completing” rather than “citing.” Ways to constrain this include: stating explicitly in the system prompt to “answer only based on the given fragments, and output not found when there is no basis”; requiring a source identifier for each item; and performing a second sampling review of key conclusions.
Switch the temperature parameter by task: use 0.1–0.3 for factual lookup and around 0.8 for style continuation. Also, always keep raw response logs so you can later determine whether retrieval failed to recall or the model failed to follow instructions.
6. Cost, rate limits, and stability
The main risk of cloud calls is queuing and rate limiting. Key practices:
- Implement exponential backoff retries on the client, distinguishing retryable errors (rate limits, timeouts) from non-retryable ones (invalid parameters);
- Run batch tasks during off-peak hours and route interactive tasks to the high-speed tier;
- Cache results for high-frequency repeated questions, keyed by content hash;
- Set a daily call limit and alerts to prevent abnormal loops from consuming your quota;
- Abstract a unified calling interface so the model layer is replaceable, avoiding tight coupling to any single service.
7. Summary
The right shape for a local web-novel knowledge base is “data local, compute in the cloud, hybrid retrieval, and division of labor among models.” First govern the text with semantic chunking and metadata, then use dual-path recall from vectors and keywords to ensure proper nouns are not lost, and finally suppress model hallucination through prompt constraints and log review. This approach does not depend on a high-end GPU, can be deployed on an ordinary laptop, and scales smoothly as the text volume grows.
