RAHB-REALTORS-Association / chat2gpt

Chat²GPT is a ChatGPT (and DALL·E 2/3, and ElevenLabs) chat bot for Google Chat. 🤖💬
https://chat2gpt.oncornerstone.app
MIT License
11 stars 1 forks source link

[Refactor] Modularize main.py #47

Open justinh-rahb opened 1 year ago

justinh-rahb commented 1 year ago

Refactor

Description:
Refactor the existing main.py to adopt a more modular structure by dividing its functionalities into separate modules under different directories. This ensures clearer code organization and ease of maintainability.

Problem Statement:
The current structure of main.py is dense and combines multiple functionalities. This dense structure can hinder navigation, make debugging challenging, and complicate future extensions. Combining numerous responsibilities in one file can lead to decreased developer productivity and a heightened potential for errors.

Proposed Solution:

  1. Retain a stub for process_events() in main.py but move its core logic into the following directory and file structure:
    chat2gpt/
    │
    ├── main.py (contains a stub for process_event())
    │
    ├── handlers/
    │   ├── process_event.py (contains the actual logic for process_event())
    │   ├── chat_response.py
    │   ├── image_response.py
    │   ├── tts_response.py
    │   └── slash_commands.py
    │
    ├── settings/
    │   ├── env_loader.py
    │   └── sessions.py
    │
    └── utils/
        ├── moderation.py
        ├── voices.py
        ├── tokenizer.py
        ├── text_to_speech.py
        ├── google_cloud.py
        └── ...
  2. Segregate functionalities and transfer them into their respective files:
    • Move event processing logic to handlers/process_event.py.
    • Shift message handling to handlers/chat_response.py.
    • Transfer image response code to handlers/image_response.py.
    • Relocate TTS and voice list response functionality to handlers/tts_response.py.
    • Move any Google Cloud related functions to utils/google_cloud.py.
    • Move any voices related utility functions to utils/voices.py.
    • Move the text-to-speech function to utils/text_to_speech.py.
    • Move any slash command handling logic to handlers/slash_commands.py.
    • Transfer environment variable and session management to the settings/ directory, adopting a streamlined method using a loop or function to load environment variables.
    • Migrate any other auxiliary utility functions to the utils/ directory, including creating a function to centralize API call headers for the ElevenLabs API.
  3. Ensure all module imports remain absolute to prevent potential path issues and maintain clarity.
  4. Ensure that existing user input and output moderation rules are applied.

Benefits:

Additional Context:
This architectural change is pivotal for the sustainable evolution of the project. As we incorporate more features, having a well-defined and modular structure will be indispensable. Furthermore, breaking down functions like handle_message into more specific sub-functions will aid in readability and error management.

justinh-rahb commented 1 year ago
  1. Utility Functions First:

  2. Break Down handle_message():

    • Start by segregating the slash command logic (/image, /voices, /tts) into their respective handler functions.
    • After breaking these out, focus on the core chat logic, ensuring the modularization retains the existing flow and behavior.