Open-Source Chat AI Bot Architecture and Containerized Deployment Practices

    This article provides a comprehensive guide to the architecture and containerized deployment of open-source chat AI bots. It breaks down the three-layer architecture—message access, communication protocol, and AI logic—and demonstrates how to deploy a bot using Docker Compose. Furthermore, it outlines essential operational practices, including plugin sourcing, cost management, logging, and security compliance, to help developers build stable and maintainable AI chatbots.

    1. Understanding the Three-Layer Architecture

    A chat AI system is typically divided into three layers. Each layer is responsible for a single function, and layers communicate with each other through predefined interfaces.

    Message Access Layer: This layer handles logging into instant messaging platforms, sending and receiving messages, and converting platform-specific events into a unified format. Differences in proprietary protocols across platforms are abstracted at this level. The access layer usually runs as an independent process, exposing a WebSocket or HTTP port.

    Communication Protocol Layer: This layer defines the data structures for messages, events, and actions. Taking the OneBot 11 specification as an example, it defines JSON fields for message events and calling interfaces for actions like sending messages. As long as both parties adhere to the same specification, the access implementation can be replaced without modifying the business code.

    AI Logic and Plugin Layer: This is the core of the bot. It receives events, determines whether a reply is needed, calls the Large Language Model (LLM) to generate content, maintains context for each session, and implements features like translation, drawing, scheduled tasks, and knowledge base Q&A through plugins. Python frameworks like NoneBot2 belong to this layer; they typically support multiple model services and can be switched via configuration.

    Understanding this layered architecture makes troubleshooting much easier. If a message is not coming in, check the access layer first. If the message comes in but there is no reply, check the logic layer. If both are normal but they cannot connect, check the protocol address and network.

    2. Why Choose Containerized Deployment

    Manual deployment requires installing runtimes one by one, resolving dependency version conflicts, and handling port occupancy. Moving to a new server means starting all over again. Containerization writes the access service, bot framework, and their network relationships into a single orchestration file, allowing the entire system to be launched with a single command.

    The main benefits of containerization are fourfold:

    • Environment Consistency: Dependencies are packaged into the image. Services that run locally will also run on the server.
    • Version-Controlled Configuration: Orchestration files and environment variable templates can be committed to Git, and migration only requires copying the directory.
    • Fault Isolation: If the access service crashes, the bot framework continues to run, and restart strategies can be applied independently.
    • Simple Upgrades and Rollbacks: Upgrading is as simple as modifying the image tag. If issues arise, simply revert to the old tag.

    3. Deployment Steps and Example Orchestration

    The following solution involves two containers. The message adapter container communicates with the platform; for this example, we assume it provides a WebSocket service on port 3001 within the internal network. The bot container connects to this port and provides a management interface. Both containers only join a custom network; the access port is not mapped to the host machine and is not exposed to the public internet.

    First, create a .env file in the project directory and write the model key and model name. Do not commit this file to the version control repository:

    1. LLM_API_KEY=replace_with_your_key
    2. LLM_MODEL=replace_with_your_chosen_model_name

    Then, create a docker-compose.yml file:

    1. services:
    2. message-adapter:
    3. image: your-adapter-image:tag # Replace with your chosen access service image
    4. restart: unless-stopped
    5. volumes:
    6. - ./adapter-data:/app/data
    7. networks:
    8. - botnet
    9. ai-bot:
    10. image: your-bot-framework-image:tag # Replace with your chosen bot framework image
    11. restart: unless-stopped
    12. ports:
    13. - "127.0.0.1:8080:8080"
    14. env_file:
    15. - .env
    16. environment:
    17. - ADAPTER_WS=ws://message-adapter:3001
    18. volumes:
    19. - ./bot-data:/app/data
    20. depends_on:
    21. - message-adapter
    22. networks:
    23. - botnet
    24. networks:
    25. botnet:
    26. driver: bridge

    Here are a few points to note. The management port is bound only to 127.0.0.1. For remote access, you should use an SSH tunnel or add a reverse proxy with authentication; do not expose it directly. The hostname message-adapter in ADAPTER_WS is the internal Compose service name, which containers use to resolve the address. Environment variable names should align with the documentation of your chosen image; the names in the example need to be replaced accordingly.

    The startup and verification steps are as follows:

    1. Run docker compose up -d to bring up all services. The first run will download images, which may take some time depending on network speed.
    2. Run docker compose ps to confirm both containers are running.
    3. Run docker compose logs -f message-adapter to confirm the access service is listening on port 3001 and see prompts for a successful login or waiting for a QR code scan.
    4. Access http://127.0.0.1:8080 via an SSH tunnel to complete the framework’s account or model configuration.
    5. Send a test message from another account in a group chat or private chat to confirm the message reaches the bot and a reply is received.

    If you do not see the listening information in step 3, check the port configuration in the image documentation. If you do not receive messages in step 5, sequentially check the ADAPTER_WS address, the login status of the access service, and the framework logs.

    4. Plugins, Cost Control, and Operations

    After deployment, the long-term stable operation of the bot depends on the following factors:

    Plugin Sources: Only install plugins from official repositories or actively maintained, open-source projects. Before installation, check the plugin code for network requests, file reads/writes, and environment variable reads—especially to see if it sends keys to third-party addresses. Check for updates regularly and verify security patches in the changelogs.

    Cost Control: LLMs are billed based on usage volume. It is recommended to set a maximum length for single-round replies, limit the number of contextual messages in a session, and require group chat messages to include trigger keywords or @mention the bot before invoking the model. This prevents irrelevant messages from repeatedly consuming your quota.

    Logs and Alerts: Enable framework logging and output logs to a mounted directory or a logging service. Avoid printing full keys or user privacy information in logs. Focus on three metrics: container restart count, model API timeout count, and daily invocation volume. If restart counts increase abnormally in a short period, or if invocations exceed budget thresholds, the maintainer should be notified immediately.

    Permissions and Secrets: Model keys should be granted only the necessary quota and model access, and rotated regularly. Only open essential ports on the server. Back up the bot-data and adapter-data directories, as login credentials must be re-scanned if lost.

    Platform Compliance: Bot accounts should comply with the rules of their respective platforms, control sending frequencies, and avoid mass messaging or marketing promotion. Positioning the bot for internal tools, knowledge Q&A, and efficiency assistance scenarios will reduce the risk of account suspension.

    5. Conclusion

    Open-source chat AI bots typically consist of an access layer, a protocol layer, and a logic layer, with clear responsibilities for each, making replacement and troubleshooting straightforward. Containerized deployment ensures environment reproducibility and trackable configuration. Readers can first follow the steps in this article to complete a minimal viable deployment. Once the message pipeline is confirmed to be connected, you can gradually add plugins, cost limitations, and monitoring alerts. A bot built this way will run stably and be easy to migrate to other platforms or switch models.