Skip to content

Commit 7271cda

Browse files
author
self
committed
Made it so that if ChatGPT replies with the time (which is almost always wrong because it can't understand time well), the bot will replace the time provided for the actual current time. Time will still confuse it tho
1 parent e3978c8 commit 7271cda

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

chatbot.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def gpt_response(self, prompt: str) -> str:
182182

183183
return response
184184

185-
def say_to_chatbot(self, text: str, outloud: bool = True, show_text:bool = True) -> str:
185+
def say_to_chatbot(self, text: str, outloud: bool = True,
186+
show_text:bool = True, correct_time=True) -> str:
186187
"""
187188
This sends a message to GPT-3 if it passes tests, then returns a
188189
response. Manages advancing the conversation.
@@ -232,12 +233,19 @@ def say_to_chatbot(self, text: str, outloud: bool = True, show_text:bool = True)
232233
response = json.loads(str(response))
233234
self.tokens = response['usage']['total_tokens']
234235

235-
# Cut response and play it
236+
# Cut response
236237
if not self.gpt_model == 'gpt-3.5-turbo':
237238
reply = response['choices'][0]['text']
238239
else:
239240
reply = response['choices'][0]['message']['content']
240241

242+
# If chatbot says time, replace with current time (it does not understand time and will give the wrong answer otherwise)
243+
if correct_time:
244+
now = datetime.now()
245+
time = convert_to_12hr(now.hour, now.minute)
246+
reply = replace_time(reply, time)
247+
248+
# Show / play text
241249
if show_text:
242250
info(f'{self.name}\'s Response', 'topic')
243251
info(self.get_AI_response(reply), 'plain')

general_functions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from rich import print as color
22
import os
3-
3+
import re
44

5+
def replace_time(original, time):
6+
result = re.sub(r'\d+:\d+ [A, P]M', time, original)
7+
return result
8+
59
def convert_to_12hr(hour_24, minutes):
610
'''
711
Converts 24 hour time into a nicely formatted 12 hour timestamp

0 commit comments

Comments
 (0)