Skip to content

Commit

Permalink
Enhance User Session Management with TTL Functionality (#18)
Browse files Browse the repository at this point in the history
* Update main.py

* Update README.md

* Update gcp-deploy.yml
  • Loading branch information
justinh-rahb authored Aug 2, 2023
1 parent 9cb8527 commit ffc1ced
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gcp-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,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 }}"
- 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 }}"
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Chat²GPT is a ChatGPT chat bot for Google Chat. 💬🤖 It enhances communicat

## 📖 Table of Contents
- [🛠️ Setup](#%EF%B8%8F-setup)
- [🧑‍💻 Usage](#-usage)
- [🌐 Community](#-community)
- [Contributing 👥🤝](#contributing-)
- [Reporting Bugs 🐛📝](#reporting-bugs-)
Expand Down Expand Up @@ -58,6 +59,8 @@ In your GitHub repository:
- `OPENAI_API_KEY`: Your OpenAI API key.
- `MODEL_NAME`: The name of the OpenAI model you're using. For this project, we recommend "gpt-3.5-turbo".
- `SYSTEM_PROMPT`: The system prompt to use for the OpenAI API.
- `MAX_TURNS`: This sets the maximum number of exchanges the bot remembers in a user session before resetting. Default: 10 exchanges.
- `TTL`: This sets the duration (in seconds) a user session stays active from the last received message before it resets. Default: 600 seconds (10 minutes).

**5. GitHub Actions 🚀**

Expand All @@ -73,7 +76,9 @@ The GitHub Actions workflow is configured to automatically deploy the bot to Goo

Now, your bot can be added to any room within your Google Workspace.

Your bot is now ready! It can interact in any chat room when it's explicitly mentioned (@botname) or directly messaged, depending on the functionality you've programmed it with.
## 🧑‍💻 Usage

Your bot is all set and ready for action! It's capable of interacting in any chat room, responding when directly mentioned (@botname), or when it receives a direct message, based on the functionality you've programmed. Our bot is designed to remember several rounds of a conversation per user session, providing a coherent and continuous interaction. This means you can ask a question, receive a response, and continue the conversation by referencing the initial query or its response. However, for performance optimization, the length of the conversation is limited by a configurable setting; we recommend setting it to 5-10 turns. Importantly, each session also tracks the time since the last received message, automatically resetting if it exceeds the specified 'Time to Live' (TTL) value. This ensures a seamless and efficient conversation experience with the bot.

## 🌐 Community

Expand Down
19 changes: 16 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import datetime
from flask import jsonify
from simpleaichat import AIChat

Expand All @@ -24,9 +25,16 @@
except Exception as e:
print(f"Error getting max turns: {str(e)}")

# Try to get the TTL from an environment variable
try:
TTL = int(os.getenv('TTL', 600)) # Default to 600 seconds (10 minutes)
except Exception as e:
print(f"Error getting TTL: {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
last_received_times = {} # A dictionary to track the last received time for each user

def process_event(request):
try:
Expand Down Expand Up @@ -57,24 +65,29 @@ def process_event(request):

def handle_message(user_id, user_message):
try:
current_time = datetime.datetime.now()

# Get the AIChat instance for the user, or create a new one
ai_chat = user_sessions.get(user_id)
turn_count = turn_counts.get(user_id, 0)
if ai_chat is None or turn_count >= MAX_TURNS:
last_received_time = last_received_times.get(user_id)

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)
user_sessions[user_id] = ai_chat
turn_count = 0

# Generate the response
response = ai_chat(user_message)

# Update the turn count
# Update the turn count and the last received time
turn_count += 1
turn_counts[user_id] = turn_count
last_received_times[user_id] = current_time

bot_message = response

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 ffc1ced

Please sign in to comment.