Building a Local Web Novel Knowledge Base with a Large Model API
This article explains why calling a cloud API is often more practical than self-hosting an open-source large model for personal users and small teams, and walks through the full workflow: account and API key setup, client configuration, knowledge base construction with chunking and vectorization, retrieval-augmented generation with citations, cost and stability control, and troubleshooting. You will learn how to keep the knowledge base and client local while sending only relevant text fragments to the model, improving long-document handling, response reliability, and engineering control.
1. Why Call an API Instead of Self-Hosting
Many readers assume that data is only safe when it stays local, so they try to deploy an open-source large model on their own machine. The engineering reality is different: a full-parameter model demands a great deal of VRAM, memory bandwidth, and cooling, which consumer-grade GPUs can rarely provide. Distilled small-parameter versions are friendlier on resources, but they degrade noticeably in long-text understanding, complex reasoning, and instruction following. When processing web novels—often hundreds of thousands of characters with interwoven character relationships—they tend to contradict themselves or omit key settings.
For most individual users and small teams, a more pragmatic route is to keep only the knowledge base and client locally and move model inference to a cloud API. This brings three benefits:
- Better long-text capacity: Local files are no longer limited by the context window of a web chat box. The client splits and retrieves content first, sending only relevant fragments to the model.
- Improved stability: Web entry points often queue or rate-limit during peak hours. API calls may slow down but usually still return a result instead of no response at all.
- Engineering control: Call parameters, retry strategies, caching, and logging are all under your control, which makes reproduction and iteration easier.
If strict compliance requirements do apply, the correct approach is to adopt a common private deployment pattern—wrapping your own inference service with general capabilities such as container platforms and object storage—rather than forcing a small model onto low-spec hardware.
2. Preparation: Account, Key, and Client
The overall process has four steps, executed in order.
- Register a cloud provider account and complete identity verification. Many platforms offer a certain amount of free call quota on first registration, which can be used to verify that the pipeline works.
- Create an API key. A key is like a password: keep it only in local environment variables or a secret management file, and never hard-code it or commit it to a repository. Use separate keys for different projects so they can be revoked individually and usage can be attributed.
- Choose a model tier. The same platform usually offers both “reasoning-oriented” and “chat-oriented” models. The former excels at divergence and logical deduction, making it suitable for mapping character relationships and analyzing foreshadowing; the latter is more restrained and stable, suitable for reference lookup and summary extraction. If the platform offers a higher-priority channel with faster responses, you can upgrade as needed, but note that it usually does not include the free quota.
- Install a general-purpose chat client. An open-source or cross-platform desktop client is fine, but make sure the version is on a recent branch; older versions often lack key features such as knowledge bases and vector retrieval.
3. Integration: Turning an API into a Local Capability
In the client’s model service settings, add a custom provider and fill in the endpoint base URL, key, and model identifiers provided by the platform. An illustrative configuration (it only shows what the fields mean and does not represent any specific API):
{"provider": "custom-openai-compatible","base_url": "https://<your-endpoint>/v1","api_key": "${LLM_API_KEY}","models": ["reasoning-model", "chat-model"],"temperature": 0.3,"max_tokens": 4096,"timeout_seconds": 180}
A few parameters that are easy to get wrong:
- temperature: Lower it for knowledge base queries (0.2–0.4) to improve factual consistency; raise it for creative continuation.
- timeout: For long documents, extend the timeout to minutes and pair it with client-side retries.
- max_tokens: Estimate it as “retrieved fragment length + expected answer length” to avoid truncated answers.
4. Knowledge Base Construction: Chunking, Vectorization, and Indexing
This is the step that determines quality; most “off-topic answers” come from a crude chunking strategy.
- Document parsing: Convert TXT, EPUB, and PDF to plain text first, then strip headers, footers, chapter numbers, and repeated blank lines.
- Structured chunking: Prefer splitting by the volume–chapter–section hierarchy, with 500–1000 characters per chunk and 80–120 characters of overlap, so a character’s introduction and dialogue are not cut to opposite ends of the same chunk.
- Metadata tagging: Attach the work title, chapter number, and timeline tags to each chunk. At retrieval time, filter by metadata first and then rank, which significantly reduces noise.
- Vectorization: Use the platform’s general text embedding model to generate embeddings in batches, and store the mapping between vectors and source text locally; when files change, recompute only the modified chapters.
- Hybrid retrieval: Vector retrieval handles semantic similarity, while keyword retrieval handles exact matches of proper nouns; fuse both result sets before passing them to the model.
5. Retrieval-Augmented Generation: Answers with Sources
Fix the Q&A flow as four steps: question rewriting → hybrid retrieval of Top-K → reranking and deduplication → generation with citations. Explicitly instruct in the prompt to “answer only from the given fragments, and state directly when information is missing,” which effectively suppresses fabrication.
You are a reference retrieval assistant. Answer using only the content in [Reference].If the reference is insufficient, reply "Not mentioned in the reference."Output format: conclusion + source (work title/chapter number).[Reference] {{retrieved_chunks}}[Question] {{question}}
Advanced practices include building an answer cache for high-frequency questions; sampling multiple times for the same question and running a consistency check; and generating key points for a long answer first, then having the model expand them, which reduces pressure on a single call.
6. Cost and Stability Control
- Separate task tiers: Use lightweight models for summarization and extraction; reserve reasoning models for complex reasoning.
- Compress context: Pre-filter locally by keywords and send only necessary fragments; token consumption often drops several times over.
- Caching and batching: Build indexes offline in batches to avoid repeated interactive calls.
- Failure fallback: Set exponential backoff retries and a fallback model; write call logs and latency to a local logging service to determine whether the issue is rate limiting or networking.
7. Troubleshooting Common Issues
If the client reports a connection failure, first check whether the endpoint base URL and key match. If answers are vague, the chunking is probably too coarse or Top-K is too small. If responses are slow, switch to the higher-priority channel or move to asynchronous batch processing. If the model “forgets” long documents, check whether only a summary was sent instead of source fragments. Iterating on this workflow lets a local web novel knowledge base reliably support setting research, character relationship mapping, and writing assistance.
