From db07daad63e11c60f02b42eb26a0c9455154a3e5 Mon Sep 17 00:00:00 2001 From: Justin Hayes Date: Tue, 12 Mar 2024 10:32:47 -0400 Subject: [PATCH] Moderation toggle, Dockerfile improvements, version bumps (#132) * Add OpenAI Moderation API toggle * Update versions * Update content moderation setting because bools are hard * Refactor Dockerfile to improve build process --- .github/workflows/codeql.yml | 2 +- .github/workflows/dependabot-release.yml | 2 +- .github/workflows/gcp-deploy.yml | 2 +- .github/workflows/pages.yml | 2 +- .github/workflows/python-3.11.yml | 2 +- Dockerfile | 22 ++++++++++++---------- README.md | 1 + docs/setup.md | 1 + main.py | 15 +++++++++------ runtime.txt | 2 +- settings.py | 6 ++++++ 11 files changed, 35 insertions(+), 22 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index a863385..efcbdea 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -34,7 +34,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/dependabot-release.yml b/.github/workflows/dependabot-release.yml index 18cebf6..7601dc0 100644 --- a/.github/workflows/dependabot-release.yml +++ b/.github/workflows/dependabot-release.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Bump version and push tag id: create_tag diff --git a/.github/workflows/gcp-deploy.yml b/.github/workflows/gcp-deploy.yml index efa7fef..f0c5edf 100644 --- a/.github/workflows/gcp-deploy.yml +++ b/.github/workflows/gcp-deploy.yml @@ -13,7 +13,7 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - id: 'auth' name: 'Authenticate to Google Cloud' diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 4ab055c..5ddad8c 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -27,7 +27,7 @@ jobs: working-directory: docs # Change this to your Jekyll site folder steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup Ruby uses: ruby/setup-ruby@v1 with: diff --git a/.github/workflows/python-3.11.yml b/.github/workflows/python-3.11.yml index e2fcdc3..38f2421 100644 --- a/.github/workflows/python-3.11.yml +++ b/.github/workflows/python-3.11.yml @@ -17,7 +17,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python 3.11 uses: actions/setup-python@v4 with: diff --git a/Dockerfile b/Dockerfile index 7092e84..b51896d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,19 @@ -# Use an official Python runtime as a parent image -FROM python:3.11-slim-buster - -# Set the working directory in the container to /app +# Build stage +FROM python:3.11-slim-bookworm as builder WORKDIR /app - -# Add the current directory files (i.e., the app) to the Docker container at /app -ADD . /app - -# Install any needed packages specified in requirements.txt +# Copy requirements first to leverage Docker cache +COPY requirements.txt /app/requirements.txt +# Install dependencies RUN pip install --no-cache-dir -r requirements.txt +# Final stage +FROM python:3.11-slim-bookworm +WORKDIR /app +COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages +COPY . /app + # Define environment variable -ENV NAME chat2gpt +ENV NAME=chat2gpt # Make port 5000 available to the world outside this container EXPOSE 5000 diff --git a/README.md b/README.md index b35e611..2f92d47 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ In your GitHub repository: - `ELEVENLABS_API_KEY`: Your ElevenLabs API key. Can be disabled by omitting this secret. - `ELEVENLABS_MODEL_NAME`: ElevenLabs model you're using. Default: "eleven_multilingual_v2". - `GCS_BUCKET_NAME`: Your chosen name for the GCS bucket meant for TTS audio file storage. + - `MODERATION`: Set to "False" to disable OpenAI's Moderation API. Default: "True". **5. GitHub Actions 🚀** diff --git a/docs/setup.md b/docs/setup.md index 8f05c5d..307d43a 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -65,6 +65,7 @@ This bot is intended to be deployed on Google Cloud Functions, with audio data t - `ELEVENLABS_API_KEY`: Your ElevenLabs API key. Can be disabled by omitting this secret. - `ELEVENLABS_MODEL_NAME`: ElevenLabs model you're using. Default: "eleven_multilingual_v2". - `GCS_BUCKET_NAME`: Your chosen name for the GCS bucket meant for TTS audio file storage. + - `MODERATION`: Set to "False" to disable OpenAI's Moderation API. Default: "True". 5. **GitHub Actions 🚀** diff --git a/main.py b/main.py index aa8f694..0f9186e 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,7 @@ # Initialize OpenAI parameters params = initialize_openai(OPENAI_API_KEY, TEMPERATURE, MAX_TOKENS_OUTPUT) +enable_moderation = get_env("MODERATION") # Define globals user_sessions = {} # A dictionary to track the AIChat instances for each user @@ -64,9 +65,10 @@ def process_event(request): def handle_message(user_id, user_message): try: # Check the user input for any policy violations - moderation_result = moderate_content(user_message) - if moderation_result["flagged"]: - return jsonify({'text': 'Sorry, your message does not comply with our content policy. Please refrain from inappropriate content.'}) + if enable_moderation == "True": + moderation_result = moderate_content(user_message) + if moderation_result["flagged"]: + return jsonify({'text': 'Sorry, your message does not comply with our content policy. Please refrain from inappropriate content.'}) current_time = datetime.datetime.now() @@ -246,9 +248,10 @@ def handle_message(user_id, user_message): response = response[:4070] + "" # truncated to leave space for the appended message # Check the API output for any policy violations - moderation_result = moderate_content(response) - if moderation_result["flagged"]: - return jsonify({'text': 'Sorry, your message does not comply with our content policy. Please refrain from inappropriate content.'}) + if enable_moderation == "True": + moderation_result = moderate_content(user_message) + if moderation_result["flagged"]: + return jsonify({'text': 'Sorry, your message does not comply with our content policy. Please refrain from inappropriate content.'}) bot_message = response diff --git a/runtime.txt b/runtime.txt index d5831c5..d45f665 100644 --- a/runtime.txt +++ b/runtime.txt @@ -1 +1 @@ -python-3.11.5 \ No newline at end of file +python-3.11.7 \ No newline at end of file diff --git a/settings.py b/settings.py index 8ffedf2..e291885 100644 --- a/settings.py +++ b/settings.py @@ -130,5 +130,11 @@ "required": False, "type": int, "description": "Server port." + }, + "MODERATION": { + "default": "True", + "required": False, + "type": str, + "description": "Enable content moderation." } }