Skip to content

Commit

Permalink
Modify /reset command
Browse files Browse the repository at this point in the history
Give a generic response when session reset by user.
  • Loading branch information
justinh-rahb authored Aug 2, 2023
1 parent 1347f4c commit 54420ed
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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:
event = request.get_json()
Expand Down Expand Up @@ -63,6 +64,7 @@ def process_event(request):
print(f"Error processing event: {str(e)}")
return jsonify({'text': 'Sorry, I encountered an error while processing your message.'})


def handle_message(user_id, user_message):
try:
current_time = datetime.datetime.now()
Expand All @@ -77,21 +79,24 @@ def handle_message(user_id, user_message):
ai_chat = AIChat(api_key=openai_api_key, system=system_prompt)
user_sessions[user_id] = ai_chat
turn_count = 0
# If it's not a reset command, handle it normally
elif 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 and the last received time
turn_count += 1
turn_counts[user_id] = turn_count
last_received_times[user_id] = current_time
bot_message = "Your session has been reset. How can I assist you now?"

bot_message = response
# If it's not a reset 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)
user_sessions[user_id] = ai_chat
turn_count = 0

# Generate the response
response = ai_chat(user_message)
bot_message = response

# 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

except Exception as e:
print(f"Error calling OpenAI API: {str(e)}")
Expand Down

0 comments on commit 54420ed

Please sign in to comment.