Building a Local Knowledge Base for Web Novels: A Practical Guide to LLM API Integration

    This article explains how to build a local knowledge base for Chinese web novels using cloud-based LLM APIs rather than running models locally. It covers a four-layer architecture (access, storage, retrieval, and application), strategies for chunking long documents and extracting structured metadata, hybrid retrieval that combines keyword and vector search, and practical methods for cost control and stability such as caching, tiered models, timeouts, and budget guardrails. It also highlights common pitfalls and a four-week implementation roadmap.

    1. Why a “Local Knowledge Base” Does Not Mean “Running a Model Locally”

    When people first approach a private knowledge base, they often conflate “data stored locally” with “model running locally.” These are two independent concerns: the first determines whether data leaves your environment, while the second determines where inference compute lives. For individual creators and small teams, keeping long-form text local while offloading inference to a cloud API is often the most cost-effective combination.

    Running a full, large-parameter model on a consumer-grade GPU is not advisable, and the reason is straightforward: VRAM capacity and bandwidth dictate the model size you can support. Distilled, smaller-parameter versions tend to degrade noticeably on tasks such as long-range reasoning, character relationship tracking, and foreshadowing consistency checks, often producing contradictions and setting drift. If you cannot run the full version, a cloud API is the more pragmatic choice.

    The API model offers three core benefits:

    • Long documents do not have to be fragmented by web interface input limits; local files can be fed in batches.
    • You are not affected by server-side peak-hour queues; failed requests can be retried instead of simply hanging.
    • Engineering steps such as retrieval, chunking, caching, and logging can be consolidated into a reusable pipeline.

    2. Overall Architecture: A Four-Layer Structure

    A usable local knowledge base for web novels typically has four layers:

    1. Access layer: a unified wrapper for API calls that handles authentication, timeouts, retries, rate limiting, and streaming output.
    2. Storage layer: raw text, chunks, vector indexes, and metadata are persisted separately.
    3. Retrieval layer: hybrid recall combining keyword and vector search, followed by reranking.
    4. Application layer: setting queries, character relationship mapping, chapter summaries, style imitation, and consistency checks.

    A skeletal access layer is shown below (pseudocode, structure only):

    1. def call_llm(messages, temperature=0.3, max_tokens=2048):
    2. for attempt in range(3):
    3. try:
    4. return client.chat.create(
    5. model=MODEL_NAME,
    6. messages=messages,
    7. temperature=temperature,
    8. max_tokens=max_tokens,
    9. )
    10. except TimeoutError:
    11. time.sleep(2 ** attempt)
    12. raise RuntimeError("upstream unavailable")

    It is good practice to make the model name, temperature, and maximum output length fully configurable so you can switch between tasks: use a low temperature for restrained Q&A and a higher temperature for more divergent continuation.

    3. Long-Document Chunking: The First Dividing Line for Knowledge Base Quality

    Web novel text has a distinct profile: individual chapters are short, but the total volume is huge, and settings are scattered across hundreds of thousands of words. The chunking strategy directly determines recall quality.

    • Split along natural chapter boundaries, keeping the chapter title as metadata.
    • Keep a small overlap for each chunk (for example, one or two sentences before and after) to avoid cutting off information across boundaries.
    • Extract “setting-type” content (cultivation tiers, geography, organizations, items) into structured entries separately.
    • Metadata should include at least: work title, volume/chapter number, timeline position, and list of characters appearing.

    Example of a structured entry:

    1. {
    2. "entity": "A Certain Sect",
    3. "type": "organization",
    4. "attributes": {"rank": "top-tier", "base": "Eastern Region"},
    5. "source": {"chapter": 137, "time_order": 137}
    6. }

    4. Retrieval Design: Hybrid Recall Beats a Single Path

    Pure vector retrieval excels at semantic similarity but is weak at exact matching of proper nouns, move names, and character names; pure keyword retrieval is the opposite. In practice, the more robust approach is to run both in parallel and then fuse and rerank:

    1. The keyword channel recalls chunks with exact matches.
    2. The vector channel recalls semantically related setting descriptions.
    3. Fuse and deduplicate, then pass the top few to the model.
    4. Have the model cite its sources in the answer for easy manual verification.

    Consistency checking is a high-value use case for the knowledge base: submit the “current chapter” together with “retrieved historical settings,” and require the model to output only a list of conflicts, not rewrite suggestions. This significantly reduces false positives.

    5. Cost and Stability: Keeping Uncertainty Under Control

    • Cache first: persist and reuse retrieval results and conversation summaries for identical questions.
    • Tiered models: use lightweight models for summaries and extraction, and more capable models for complex reasoning.
    • Timeouts and backoff: the default timeout should not be too short; pair it with exponential backoff retries.
    • Budget guardrails: set daily or per-project call limits and alerts.
    • Traceable results: save request fingerprints and outputs to make it easy to reproduce issues.

    If you use a cloud provider’s general capabilities, you can store vector indexes and raw files in object storage and a managed retrieval service respectively, use a message queue to smooth out traffic spikes, and use logging and monitoring with alerts to observe the call chain. These are common industry categories rather than proprietary interfaces.

    6. Common Pitfalls

    • Treating “can connect to the API” as “the knowledge base is usable,” while ignoring chunking and metadata.
    • Feeding an entire book at once, exceeding the context window and diluting key information.
    • Relying only on semantic retrieval, leading to mismatched names of people and places.
    • Not recording sources, so answers cannot be verified and you still end up reading the whole text manually.

    7. Implementation Roadmap

    In week one, get the access layer working with single-file Q&A; in week two, complete chunking, indexing, and metadata; in week three, add hybrid recall and conflict detection; in week four, fill in caching, budget, and monitoring. At this pace, even an individual reader can end up with a stable, verifiable, and continuously growing local web novel knowledge base at a controllable cost.