How Group Chat Bots Work: Architecture and Knowledge Base Setup

    This article explains how to build a group chat Q&A bot that turns scattered chat history into a searchable knowledge source. It walks through a four-stage architecture—message ingestion, intent understanding, retrieval, and generation—and covers polling-based message access, query cleanup, document parsing and chunking, BM25-style full-text indexing, prompt constraints that keep answers grounded in retrieved text, group-to-knowledge-base mapping, observability, and common pitfalls. The approach stays lightweight, using full-text retrieval first so the whole loop can run on ordinary hardware.

    1. The Problem: Answers Exist but Cannot Be Reused

    In internal collaboration, technical discussion groups and product support groups accumulate a large amount of useful information. The problem is that this information lives in fragmented conversation streams. New members ask the same questions, experienced members give the same answers, and operations and support staff spend hours copying and pasting. The real bottleneck is not that nobody knows the answer, but that answers are scattered across chat logs with no mechanism to accumulate, search, and reuse them.

    A group chat bot is an engineering fix for exactly this gap. It turns a group from a one-off conversation into a persistent entry point for querying knowledge. Users install nothing: they trigger the bot inside the group in a specific way and get an answer grounded in existing material.

    2. Overall Architecture: Four Stages Forming a Loop

    A working group chat Q&A bot usually chains four stages together:

    1. Ingestion: join the target group with a logged-in account and receive message events.
    2. Understanding: detect the trigger intent, strip irrelevant context, and extract the actual question.
    3. Retrieval: locate the most relevant text passages in a self-hosted knowledge base.
    4. Generation: pass the retrieved results to a large language model as a constraint, produce a readable answer, and post it back to the group.

    Once these four layers are decoupled, each one can be replaced and tuned independently, instead of piling all the logic into a single script.

    3. Ingestion Layer: How Messages Get Into the Program

    The starting point for a group chat bot is getting the messages. There are two common approaches: integrating through the platform’s official open capabilities, or polling messages using a desktop client login session. The latter is simple to implement and friendly to ordinary machines, but one constraint must be stated clearly: any automation should be premised on respecting platform rules and obtaining informed consent from group members. Account security and compliant use come first.

    The core of polling mode is a scheduled loop that pulls incremental messages at a fixed interval and compares them against the locally processed position to avoid consuming the same message twice:

    1. while running:
    2. messages = fetch_new_messages(since=cursor)
    3. for msg in messages:
    4. cursor = msg.id
    5. if should_handle(msg):
    6. handle(msg)
    7. sleep(interval_seconds)

    Three engineering details need extra handling: deduplication (keyed on the unique message ID), ordering guarantees (sort by timestamp before processing), and keeping long-running tasks from blocking the main loop (put model calls in a queue and execute them asynchronously).

    4. Understanding Layer: From Natural Language to a Searchable Question

    Group messages are noisy, and searching with the raw sentence usually gives poor results. Three cleanup steps are recommended:

    • Trigger detection: decide whether the bot was explicitly invoked, for example by a specific prefix or a mention marker in the message.
    • Context stripping: remove greetings, emoji, and quoted content, keeping only the question itself.
    • Query normalization: remove extra whitespace and punctuation, and apply synonym expansion where necessary to improve recall.

    The goal here is not deep understanding. It is to hand the retrieval stage a clean, focused query string.

    5. Knowledge Layer: Document Parsing and Full-Text Indexing

    Knowledge base quality sets the ceiling on answer quality. A practical pipeline looks like this:

    1. Upload material: support common document formats such as plain text, rich text documents, and portable document format files.
    2. Parse and extract: pull the body text page by page or paragraph by paragraph, discarding headers, footers, and repeated layout noise.
    3. Split into chunks: prioritize semantic completeness, typically a few hundred characters per chunk, with a small overlap between chunks so answers are not cut off.
    4. Build the index: create an inverted index over the chunks to support keyword matching and relevance scoring.

    The recommended approach here is the classic probabilistic retrieval framework based on term frequency and inverse document frequency, commonly known in the industry as a BM25-style algorithm. The advantages are clear: no vector database, no extra embedding service, low resource usage, and enough capacity for internal scenarios where documents number in the thousands to tens of thousands of chunks. The scoring logic can be abstracted as: the more a term appears in a given chunk and the rarer it is across the whole corpus, the higher that chunk scores.

    1. score(query, block) = Σ idf(term) * tf(term, block) / (k1 + tf(term, block))

    If semantic-level matching is needed later, vector retrieval can be layered on top of the same index for hybrid ranking, but there is no need to pull in heavy dependencies at the start.

    6. Generation Layer: Making the Model Answer Only From the Given Material

    After retrieval, hand the matched chunks to the large language model as the single source of truth, and require it to state clearly when the material is insufficient. Key prompt constraints: answer only from the provided passages, cite the chunk numbers used, and do not speculate about information that does not appear.

    1. You are a knowledge base assistant. Answer the question using only the material below; if the material is insufficient, state directly that no supporting evidence was found.
    2. Material:
    3. [1] ...
    4. [2] ...
    5. Question: {user question}

    On the calling side, keep the model service address, key, model name, number of retrieved chunks, timeout, and retry count all externalized as configuration items so the setup is easy to move between environments.

    7. Mapping Groups to Knowledge Bases

    Different groups have different knowledge boundaries, so a many-to-one group-to-knowledge-base mapping is needed: each group is bound to its own document collection, with no cross-contamination. Also provide an on/off switch so the bot responds only in explicitly enabled groups and does not disturb unrelated ones. After going live, the bot should fetch the group list automatically and support selecting groups from an interface.

    8. Observability and Operations

    A service that runs long term needs a dashboard and logs: login status, running status, document count, chunk count, and the matched passages and latency for each Q&A turn. Structured log output is recommended so a complete Q&A chain can be traced by session identifier. To handle higher concurrency, split polling and generation into separate processes, introduce a message queue, use object storage to keep the original documents, publish uniformly through a container platform, and centralize monitoring with a logging service and alerting. Baidu AI Cloud offers corresponding product categories for these general-purpose capabilities.

    9. Common Pitfalls

    • Chunks that are too coarse produce generic answers; chunks that are too fine lose context.
    • No deduplication, so the same question gets answered repeatedly.
    • No trigger whitelist, so the bot interferes with normal discussion.
    • Keys hardcoded in source code, creating a leak risk.
    • No failure fallback, so the group gets no feedback at all when the model times out.

    10. Conclusion

    A group chat bot is not an account that can chat. It is a pipeline that converts unstructured discussion into a searchable asset: ingest messages, clean up questions, index documents, constrain generation. Keeping the technology choices lightweight—getting the loop working with full-text retrieval first, then strengthening it step by step based on results—is a reliable path to a fast deployment on ordinary hardware.