Skip to content

Commit

Permalink
Added Moderation & Enhanced Output Limitations (#39)
Browse files Browse the repository at this point in the history
Implement max_tokens_output limit, moderation, and documentation updates

- Added max_tokens_output to control output length
- Integrated content moderation for safe interactions
- Updated documentation in index.md, privacy.md, README.md, and usage.md
- Modified gcp-deploy.yml for deployment settings
- General code improvements in main.py
  • Loading branch information
justinh-rahb authored Aug 9, 2023
1 parent e84faf6 commit cb893dd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gcp-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}

- run: gcloud functions deploy ${{ secrets.GCP_FUNCTION }} --runtime python311 --trigger-http --allow-unauthenticated --entry-point process_event --region ${{ secrets.GCP_REGION }} --set-env-vars OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }},MODEL_NAME=${{ secrets.MODEL_NAME }},SYSTEM_PROMPT="${{ secrets.SYSTEM_PROMPT }}",MAX_TURNS="${{ secrets.MAX_TURNS }}",TTL="${{ secrets.TTL }}",MAX_TOKENS_INPUT="${{ secrets.MAX_TOKENS_INPUT }}"
- run: gcloud functions deploy ${{ secrets.GCP_FUNCTION }} --runtime python311 --trigger-http --allow-unauthenticated --entry-point process_event --region ${{ secrets.GCP_REGION }} --set-env-vars OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }},MODEL_NAME=${{ secrets.MODEL_NAME }},SYSTEM_PROMPT="${{ secrets.SYSTEM_PROMPT }}",MAX_TURNS="${{ secrets.MAX_TURNS }}",TTL="${{ secrets.TTL }}",MAX_TOKENS_INPUT="${{ secrets.MAX_TOKENS_INPUT }}",MAX_TOKENS_OUTPUT="${{ secrets.MAX_TOKENS_OUTPUT }}"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
</p>
<hr/>

**Chat²GPT** is a ChatGPT chat bot for Google Chat 🤖💬. It's designed to amplify the experience in your Google Chat rooms by offering personalized user sessions for coherent dialogues, a manual reset capability, the power to generate images via OpenAI's DALL·E 2 API, and dynamic interactions through mentions or direct messaging.
**Chat²GPT** is a [ChatGPT](https://openai.com/chatgpt) chat bot for Google Chat 🤖💬. It's designed to amplify the experience in your Google Chat rooms by offering personalized user sessions for coherent dialogues, a manual reset capability, the power to generate images via OpenAI's [DALL·E 2 API](https://openai.com/dall-e-2), and dynamic interactions through mentions or direct messaging. User input and text output is moderated with OpenAI's [Moderation API](https://platform.openai.com/docs/guides/moderation).

## 📖 Table of Contents
- [🛠️ Setup](#%EF%B8%8F-setup)
- [🧑‍💻 Usage](#-usage)
- [🛡️ Privacy](#%EF%B8%8F-privacy)
- [Data Practices: 📝](#data-practices-)
- [OpenAI and User Awareness: ℹ️](#openai-and-user-awareness-%E2%84%B9%EF%B8%8F)
- [Data Practices 📝](#data-practices-)
- [OpenAI and User Awareness ℹ️](#openai-and-user-awareness-%E2%84%B9%EF%B8%8F)
- [🌐 Community](#-community)
- [Contributing 👥🤝](#contributing-)
- [Reporting Bugs 🐛📝](#reporting-bugs-)
Expand Down Expand Up @@ -100,15 +100,15 @@ Remember, Chat²GPT is flexible, suitable for deployment on Google Cloud, FaaS (

## 🛡️ Privacy

### Data Practices: 📝
### Data Practices 📝

- **Ephemeral Conversations:** Chat²GPT doesn't store or retain conversation history. Every session is temporary, ending when a conversation concludes or times out.

- **Reactive Responses:** The bot only reacts to direct prompts, such as @mentions or direct messages, and doesn't "read the room".

- **Anonymous Sessions:** Users are tracked using anonymous ID numbers solely for session consistency. These IDs are cleared with each app redeployment.

### OpenAI and User Awareness: ℹ️
### OpenAI and User Awareness ℹ️

- **OpenAI's Commitment:** We use OpenAI's APIs, which, as per OpenAI's policy, don't use user inputs for model training. More details are on [OpenAI's official site](https://openai.com/policies/api-data-usage-policies).

Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ nav_order: 0
</p>
<hr/>

**Chat²GPT** is a ChatGPT chat bot for Google Chat 🤖💬. It's designed to amplify the experience in your Google Chat rooms by offering personalized user sessions for coherent dialogues, a manual reset capability, the power to generate images via OpenAI's DALL·E 2 API, and dynamic interactions through mentions or direct messaging.
**Chat²GPT** is a [ChatGPT](https://openai.com/chatgpt) chat bot for Google Chat 🤖💬. It's designed to amplify the experience in your Google Chat rooms by offering personalized user sessions for coherent dialogues, a manual reset capability, the power to generate images via OpenAI's [DALL·E 2 API](https://openai.com/dall-e-2), and dynamic interactions through mentions or direct messaging. User input and text output is moderated with OpenAI's [Moderation API](https://platform.openai.com/docs/guides/moderation).

## 📖 Table of Contents
- [🛠️ Setup](setup.html)
- [🧑‍💻 Usage](usage.html)
- [🛡️ Privacy](privacy.html)
- [Data Practices: 📝](privacy.html#data-practices-)
- [OpenAI and User Awareness: ℹ️](privacy.html#openai-and-user-awareness-%E2%84%B9%EF%B8%8F)
- [Data Practices 📝](privacy.html#data-practices-)
- [OpenAI and User Awareness ℹ️](privacy.html#openai-and-user-awareness-%E2%84%B9%EF%B8%8F)
- [🌐 Community](community.html)
- [Contributing 👥🤝](community.html#Contributing-)
- [Reporting Bugs 🐛📝](community.html#reporting-bugs-)
Expand Down
6 changes: 3 additions & 3 deletions docs/privacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ nav_order: 3

# 🛡️ Privacy

## Data Practices: 📝
## Data Practices 📝

- **Ephemeral Conversations:** Chat²GPT doesn't store or retain conversation history. Every session is temporary, ending when a conversation concludes or times out.

- **Reactive Responses:** The bot only reacts to direct prompts, such as @mentions or direct messages, and doesn't "read the room".

- **Anonymous Sessions:** Users are tracked using anonymous ID numbers solely for session consistency. These IDs are cleared with each app redeployment.

## OpenAI and User Awareness: ℹ️
## OpenAI and User Awareness ℹ️

- **OpenAI's Commitment:** We use OpenAI's APIs, which, as per OpenAI's policy, don't use user inputs for model training. More details are on [OpenAI's official site](https://openai.com/policies/api-data-usage-policies).

- **User Awareness:** Discussing sensitive topics? Exercise caution, especially in group settings. Chat²GPT doesn't log conversations, but your organization or platform might.
- **User Awareness:** Discussing sensitive topics? Exercise caution, especially in group settings. Chat²GPT doesn't log conversations, but your organization or platform might.
14 changes: 8 additions & 6 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ nav_order: 2

# 🧑‍💻 Usage

- **Dynamic Interactions:** Chat²GPT is attentive to its surroundings. You can invoke it in chat rooms by directly mentioning it using `@botname`. Alternatively, for more private interactions or queries, you can send a direct message to the bot.
- **Dynamic Interactions:** Chat²GPT is deeply integrated with your chat environment. Invoke it directly in chat rooms by tagging `@botname`. For confidential interactions or specific queries, slide into its direct messages.

- **Interactive Sessions:** This bot remembers multiple rounds of a conversation per user, creating an illusion of continuous dialogue. It can even reference past questions or answers, mimicking a natural conversation flow.
- **Interactive Sessions:** Chat²GPT recalls multiple interaction rounds for each user, creating a seamless dialogue illusion. It can refer back to earlier parts of the conversation, providing a more human-like chat experience.

- **Session Management:** To maintain efficient performance, each conversation is limited by a configurable setting, recommended at 5-10 turns. Moreover, the bot keeps an eye on the time since the last message, auto-resetting the session if a set time limit is surpassed. And if needed, users can manually reset their own session anytime with the `/reset` command.
- **Session Management:** Efficiency is key. Conversations are confined by a tunable setting, ideally between 5-10 turns. The bot also watches for inactive periods, resetting sessions automatically after a certain duration. Users wishing for a fresh start can use the `/reset` command to reinitialize their session.

- **Image Generation:** Want to visualize an idea? Use the `/image <prompt>` command. Based on the given prompt, which can range from a word to a paragraph, the bot leverages OpenAI's DALL·E 2 API to generate a relevant image.
- **Content Moderation:** Keeping our interactions safe, Chat²GPT screens both incoming messages and its own responses to make sure they're in line with content standards. If any content is flagged, a polite reminder is returned, ensuring all interactions uphold a high quality standard.

- **Optimized Performance:** We prioritize a smooth experience. Before processing any message, the bot checks its size by counting its tokens. If found too lengthy, an error message suggests the user to condense their message. This ensures uninterrupted bot interactions without straining the system.
- **Image Generation:** Visualize thoughts effortlessly with the `/image <prompt>` command. Feed in your prompt, and watch as Chat²GPT, utilizing OpenAI's DALL·E 2 API, crafts a fitting image.

Remember, Chat²GPT is flexible, suitable for deployment on Google Cloud, FaaS (Function as a Service), or PaaS (Platform as a Service) environments, ensuring it's a perfect fit for all your Google Chat endeavors.
- **Optimized Performance:** A fluid experience is paramount. The bot reviews message length by analyzing token count. If a message is overly verbose, a friendly error message nudges the user to keep it more concise. This ensures consistent, smooth interactions without overburdening the system.

Chat²GPT's versatility shines, making it apt for deployment across Google Cloud, FaaS (Function as a Service), and PaaS (Platform as a Service) platforms, cementing its place in all your Google Chat activities.
33 changes: 28 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
except Exception as e:
print(f"Error getting TTL: {str(e)}")

# Try to get the max tokens from an environment variable
# Try to get the max tokens (input) from an environment variable
try:
MAX_TOKENS_INPUT = int(os.getenv('MAX_TOKENS_INPUT', 2000)) # Default to 2000 tokens
MAX_TOKENS_INPUT = int(os.getenv('MAX_TOKENS_INPUT', 1000)) # Default to 1000 tokens
except Exception as e:
print(f"Error getting MAX_TOKENS_INPUT: {str(e)}")

# Try to get the max tokens (output) from an environment variable
try:
MAX_TOKENS_OUTPUT = int(os.getenv('MAX_TOKENS_OUTPUT', 1000)) # Default to 1000 tokens
except Exception as e:
print(f"Error getting MAX_TOKENS_OUTPUT: {str(e)}")

# Define globals
user_sessions = {} # A dictionary to track the AIChat instances for each user
turn_counts = {} # A dictionary to track the turn count for each user
Expand All @@ -48,6 +54,14 @@
# Set the OpenAI API key
openai.api_key = openai_api_key

# Set the max_tokens for output
max_tokens_output = MAX_TOKENS_OUTPUT


# define the function for moderation
def moderate_content(text: str) -> dict:
response = openai.Moderation.create(input=text)
return response["results"][0]

# Function to generate a unique cardId
def generate_unique_card_id():
Expand Down Expand Up @@ -91,6 +105,11 @@ 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.'})

current_time = datetime.datetime.now()

# Get the AIChat instance for the user, or create a new one
Expand All @@ -103,7 +122,7 @@ def handle_message(user_id, user_message):

# If the user types '/reset', reset the session
if user_message.strip().lower() == '/reset':
ai_chat = AIChat(api_key=openai_api_key, system=system_prompt)
ai_chat = AIChat(api_key=openai_api_key, system=system_prompt, max_tokens=max_tokens_output)
user_sessions[user_id] = ai_chat
turn_count = 0
bot_message = "Your session has been reset. How can I assist you now?"
Expand Down Expand Up @@ -151,12 +170,16 @@ def handle_message(user_id, user_message):
# If it's not a slash command, handle it normally
else:
if ai_chat is None or turn_count >= MAX_TURNS or (last_received_time is not None and (current_time - last_received_time).total_seconds() > TTL):
ai_chat = AIChat(api_key=openai_api_key, system=system_prompt)
ai_chat = AIChat(api_key=openai_api_key, system=system_prompt, max_tokens=max_tokens_output)
user_sessions[user_id] = ai_chat
turn_count = 0

# Generate the response
response = ai_chat(user_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.'})
bot_message = response

# Update the turn count and the last received time
Expand All @@ -167,4 +190,4 @@ def handle_message(user_id, user_message):
except Exception as e:
print(f"Error calling OpenAI API: {str(e)}")
bot_message = "Sorry, I'm currently unable to generate a response."
return jsonify({'text': bot_message})
return jsonify({'text': bot_message})

0 comments on commit cb893dd

Please sign in to comment.