How memochou1993/gpt-ai-assistant works
Project type and purpose
• This is a Node.js-based serverless chat-bot application (“GPT AI Assistant”) that bridges the LINE Messaging API with the OpenAI API (and optionally SerpAPI) to let end users chat with an AI assistant over LINE.
• It’s delivered as a Docker/Vercel function with webhook endpoints, middleware for signature validation, business logic layers (commands, handlers, actions), and integration with external AI services.
File structure
• Top level: Dockerfile, docker-compose.yaml, vercel.json, .env.example, config files, CI/config templates.
• “api/index.js”: the HTTP entry point (receives LINE webhook callbacks).
• “middleware/”: request-validation layer (LINE signature validator).
• “app/”: core application with layered subdirectories:
• commands/ – Command objects implementing the “Command” pattern (e.g., analyze-*, bot-*, sum-*, sys-*).
• actions/ – Domain/business-rule implementations (action.js, message.js).
• models/ – Domain entities (Bot, Event, Source).
• history/ – Message history persistence logic.
• prompt/ – Prompt-building utilities.
• repository/ – Data-access layer for ‘Source’ entities.
• messages/ – Message-object builders (template, text, image).
• context.js – Request context builder.
• index.js – Bootstrapper for wiring commands, handlers, services.
• “services/”: external API clients (OpenAI, Line, SerpAPI, Vercel).
• “storage/”: low-level persistence module (e.g. key-value store or file).
• “utils/”: helper functions (fetch-, generate-, validate-, reply-).
• “config/”, “constants/”, “locales/”: configuration, command constants, i18n.
• “tests/”: unit tests per handler/command.
README for additional insights
• Confirms use of OpenAI models (gpt-3.5, gpt-4, gpt-4o, Whisper) and support for LINE audio.
• Mentions deployment via Vercel and Docker for local development.
Main components & their interactions
a. External Actors & Services:
• End User (LINE mobile app)
• LINE Messaging API (webhook sender & reply receiver)
• OpenAI API (chat/completion, image, transcription)
• SerpAPI (optional web search)
• Vercel Runtime / Docker container (hosting)
b. Core Application Layers:
• API Gateway (api/index.js)
• Middleware (validate signature)
• Routing Layer (handler selection based on webhook event)
• Command Layer (instantiates and executes Command objects)
• Action Layer (business rules, prompt building, event logging)
• Repository Layer (source and history persistence)
• Service Clients (OpenAI, LINE, SerpAPI, Vercel)
• Utilities & Config (helpers, environment, constants, locales)
Architectural patterns & principles
• Layered (onion) architecture separating transport, application, domain, and infrastructure.
• Command pattern for encapsulating each chat operation.
• Repository pattern for data access.
• Middleware pipeline for request validation.
• Dependency injection via central bootstrap (app/index.js).
• Single Responsibility principle: handlers only route, commands only orchestrate, actions only implement business logic.