Skip to content

Commit

Permalink
Moderation toggle, Dockerfile improvements, version bumps (#132)
Browse files Browse the repository at this point in the history
* Add OpenAI Moderation API toggle

* Update versions

* Update content moderation setting because bools are hard

* Refactor Dockerfile to improve build process
  • Loading branch information
justinh-rahb authored Mar 12, 2024
1 parent 1315072 commit db07daa
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gcp-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-3.11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 12 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🚀**

Expand Down
1 change: 1 addition & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🚀**
Expand Down
15 changes: 9 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -246,9 +248,10 @@ def handle_message(user_id, user_message):
response = response[:4070] + "<MESSAGE TRUNCATED>" # 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

Expand Down
2 changes: 1 addition & 1 deletion runtime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-3.11.5
python-3.11.7
6 changes: 6 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,11 @@
"required": False,
"type": int,
"description": "Server port."
},
"MODERATION": {
"default": "True",
"required": False,
"type": str,
"description": "Enable content moderation."
}
}

0 comments on commit db07daa

Please sign in to comment.