"Tell, don't ask" Principle

This commit is contained in:
Lunjie Li 2023-06-13 14:58:46 -04:00
parent cee3e7e2df
commit 62bb4bb1af
No known key found for this signature in database
GPG Key ID: 2EBD5B8D3200BB8F
2 changed files with 37 additions and 10 deletions

View File

@ -24,11 +24,23 @@ def generate_response_from_transcript(transcript):
except:
return ''
def generate_response_from_transcript_revChatGPT(transcript):
try:
response = ''
for data in chatbot.ask(
create_prompt(transcript)
):
response = data["message"]
return response
except Exception as e:
print(e)
return ''
class GPTResponder:
def __init__(self, revChatGPT):
def __init__(self, revChatGPT=False):
self.response = INITIAL_RESPONSE
self.response_interval = 2
self.revChatGPT = revChatGPT
self.respond_to_transcriber = self.respond_to_transcriber_revChatGPT if revChatGPT else self.respond_to_transcriber
def respond_to_transcriber(self, transcriber):
while True:
@ -37,12 +49,6 @@ class GPTResponder:
transcriber.transcript_changed_event.clear()
transcript_string = transcriber.get_transcript()
if self.revChatGPT:
for data in chatbot.ask(
create_prompt(transcript_string)
):
response = data["message"]
else:
response = generate_response_from_transcript(transcript_string)
end_time = time.time() # Measure end time
@ -57,5 +63,26 @@ class GPTResponder:
else:
time.sleep(0.3)
def respond_to_transcriber_revChatGPT(self, transcriber):
while True:
if transcriber.transcript_changed_event.is_set():
start_time = time.time()
transcriber.transcript_changed_event.clear()
transcript_string = transcriber.get_transcript()
response = generate_response_from_transcript_revChatGPT(transcript_string)
end_time = time.time() # Measure end time
execution_time = end_time - start_time # Calculate the time it took to execute the function
if response != '':
self.response = response
remaining_time = self.response_interval - execution_time
if remaining_time > 0:
time.sleep(remaining_time)
else:
time.sleep(0.3)
def update_response_interval(self, interval):
self.response_interval = interval

View File

@ -92,7 +92,7 @@ def main():
transcribe.daemon = True
transcribe.start()
responder = GPTResponder(revChatGPT=True) if '--revChatGPT' in sys.argv else GPTResponder(revChatGPT=False)
responder = GPTResponder(revChatGPT=True) if '--revChatGPT' in sys.argv else GPTResponder()
respond = threading.Thread(target=responder.respond_to_transcriber, args=(transcriber,))
respond.daemon = True
respond.start()