Building a General AI Chatbot System: Architecture and Containerized Deployment Practices

    This article explores the architecture and engineering practices behind a general AI chatbot system built with containerized deployment. From front-end interaction to back-end orchestration and stability assurance, it details a complete technical solution. Readers will gain insights into dual-token authentication, SSE streaming for real-time dialogue, multimodal processing, rate limiting, asynchronous task queues, and standardized engineering workflows within a monorepo architecture.

    With the rapid advancement of large language model (LLM) technology, building an AI chatbot system capable of handling high concurrency, supporting multimodal interactions, and remaining easy to maintain has become a key focus for developers and enterprises. This article delves into the architectural design and engineering practices of a general AI chatbot system based on containerized deployment, providing a comprehensive technical solution covering front-end interaction, back-end orchestration, and system stability assurance.

    1. Overall Architecture and Monorepo Management

    The system is positioned as a general AI chatbot platform accessible via H5 and mini-programs, utilizing a monorepo architecture for both front-end and back-end. This approach simplifies the Continuous Integration/Continuous Deployment (CI/CD) pipeline and reduces the communication overhead of cross-repository collaboration.

    • Front-end Layer: Developed using a mainstream responsive framework with a strongly typed language, paired with a modern bundler. The UI layer leverages mature component libraries and preprocessors to ensure visual consistency and smooth interaction across different platforms.
    • Back-end Layer: Provides API services using a lightweight Node.js web framework, with data persistence handled by a lightweight embedded relational database to lower the deployment barrier. A layered architecture (routing, controllers, services, and data access layer) achieves high decoupling of business logic.
    • Containerized Deployment: The system provides independent build scripts for front-end and back-end container images. Containerization ensures environment isolation, one-click deployment, and consistency across different runtime environments.

    2. Implementation of Core Interaction Capabilities

    2.1 Dual-Token Silent Refresh Mechanism

    To balance security and user experience, the system implements a silent refresh mechanism based on dual tokens (access token and refresh token). A short-lived access token handles daily API authentication, while a long-lived refresh token obtains new credentials when the access token expires.

    The front-end encapsulates a unified request interceptor that automatically injects the access token before each request. Upon receiving a 401 Unauthorized status, the interceptor suspends current and subsequent requests, using the refresh token to request a new access token from the authentication endpoint. Once successful, the suspended requests are replayed sequentially. This design is completely transparent to the business layer, ensuring users do not experience any login interruptions.

    2.2 SSE Streaming AI Dialogue and Session Management

    Traditional HTTP requests often suffer from long delays when processing LLM generation tasks. The system utilizes Server-Sent Events (SSE) to enable streaming dialogue interaction, significantly reducing the time to first token.

    On the back end, the LLM’s streaming output capability pushes text slices to the front end in real-time. The front-end listens for specific event types:

    • delta event: Receives incremental text and renders it in real-time to create a typewriter effect.
    • thinking event: Displays prompt information about the model’s thought process, enhancing interaction transparency.
    • done event: Marks the end of the current generation round and triggers subsequent session persistence logic.

    Furthermore, the system supports a complete session lifecycle, including session list display, history retrieval, session deletion, message liking, and assistant message regeneration. For the regeneration feature, the system overwrites the previous response on the front end while updating the back-end database state using an optimistic locking mechanism to ensure data consistency.

    2.3 Multimodal Interaction: Image and Voice Processing

    To enrich interaction methods, the system integrates image upload and voice dialogue capabilities.

    • Image Upload: The front-end uploads image streams via form submission. The back-end validates the format, renames the file, stores it persistently, and returns an accessible URL for front-end display.
    • Voice Interaction: The front-end leverages native browser Media Recorder and Web Audio APIs to capture microphone audio and standardize it into WAV format. The standardized audio file is uploaded to the back-end, which does not perform speech recognition directly but adds it to an asynchronous queue. Background worker nodes consume the queue, utilizing a lightweight local C++ speech recognition engine to convert speech to text, and then feed the result to the LLM for dialogue processing. This asynchronous design prevents interface blocking caused by lengthy speech recognition tasks.

    3. Back-end Performance and Stability Assurance

    3.1 Fixed Window Rate Limiting via In-Memory Database

    To prevent malicious attacks or abnormal high-frequency calls from overwhelming the system, the back-end introduces a fixed window rate limiting mechanism based on a high-performance in-memory key-value database for critical endpoints (such as login, AI dialogue, and speech recognition).

    The core logic leverages the high-speed read/write capabilities of the in-memory database to maintain a request counter within a time window for each user or IP address. When a request arrives, the counter increments. If the counter exceeds the preset threshold within the window, the request is rejected directly, and a rate limit prompt is returned. This mechanism is simple to implement with minimal overhead, effectively protecting underlying LLM computing resources from abuse.

    3.2 Asynchronous Orchestration via Task Queue

    The system incorporates a task queue library based on an in-memory database, specifically for handling time-consuming, non-blocking core tasks, such as the aforementioned speech recognition and temporary file cleanup.

    The task queue adopts a producer-consumer model. The HTTP interface controller, acting as the producer, simply pushes task metadata into the queue and returns an immediate response, ensuring high throughput. Background worker nodes, acting as consumers, are deployed independently and process tasks sequentially on a first-in, first-out (FIFO) basis. For periodic tasks like temporary file cleanup, the system schedules cron jobs to push them into the queue, ensuring a healthy lifecycle for server storage space.

    4. Containerization and Engineering Standards

    In terms of engineering, the system’s directory structure is clearly defined, following the principle of separation of concerns. The front-end directory focuses on view rendering, state management, and native capability encapsulation. The back-end directory is subdivided into client encapsulation, routing control, business services, data access layers, and task queue definitions.

    By writing independent container image build scripts, the front-end project compiles source code into static assets during the build phase, which are then hosted by a web server. The back-end project packages the runtime environment and dependencies into an independent runnable container instance. This containerized deployment approach not only facilitates rapid orchestration and scaling on various cloud-native platforms but also provides a standardized foundation for integrating monitoring, alerting, and logging services.

    5. Conclusion

    Building a production-ready AI chatbot system requires more than just integrating powerful LLM capabilities; it tests comprehensive architectural design and optimization. Through a series of technical practices—including dual-token authentication, SSE streaming, asynchronous task queues, API rate limiting, and containerized deployment—the system discussed in this article achieves a highly available, easily scalable, and multimodal AI chatbot platform architecture. These general technical solutions and design concepts provide valuable references for developers implementing similar AI applications.