From fc3eb18780595e077ad3c16e1100fb7ff1d82d54 Mon Sep 17 00:00:00 2001 From: vivek Date: Wed, 26 Jul 2023 21:12:12 -0400 Subject: [PATCH] Bug fixes for exceptions observed during usage. Add further plumbing for upcoming features. --- AudioRecorder.py | 2 +- GPTResponder.py | 26 +++++++++++++++++--------- TranscriberModels.py | 2 +- conversation.py | 30 ++++++++++++++++++++++++++++++ main.py | 2 +- prompts.py | 15 +++++++++++++++ 6 files changed, 65 insertions(+), 12 deletions(-) diff --git a/AudioRecorder.py b/AudioRecorder.py index 6ffbd8f..dac3251 100644 --- a/AudioRecorder.py +++ b/AudioRecorder.py @@ -55,7 +55,7 @@ def __init__(self): print("[ERROR] No loopback device found.") source = sr.Microphone(speaker=True, - device_index= default_speakers["index"], + device_index=default_speakers["index"], sample_rate=int(default_speakers["defaultSampleRate"]), chunk_size=pyaudio.get_sample_size(pyaudio.paInt16), channels=default_speakers["maxInputChannels"]) diff --git a/GPTResponder.py b/GPTResponder.py index 29f2579..31ec8bd 100644 --- a/GPTResponder.py +++ b/GPTResponder.py @@ -1,6 +1,7 @@ import openai +import datetime import GlobalVars -from prompts import create_prompt, INITIAL_RESPONSE +import prompts import time import conversation import constants @@ -12,7 +13,7 @@ class GPTResponder: def __init__(self, convo: conversation.Conversation): - self.response = INITIAL_RESPONSE + self.response = prompts.INITIAL_RESPONSE self.response_interval = 2 self.gl_vars = GlobalVars.TranscriptionGlobals() openai.api_key = self.gl_vars.api_key @@ -22,18 +23,25 @@ def __init__(self, convo: conversation.Conversation): def generate_response_from_transcript_no_check(self, transcript): try: - prompt_content = create_prompt(transcript) - response = openai.ChatCompletion.create( + # prompt_content = create_prompt(transcript) + # prompt_api_message = [{"role": "system", "content": prompt_content}] + prompt_api_message = prompts.create_single_turn_prompt_message(transcript) + multiturn_prompt_content = self.conversation.get_merged_conversation(length=MAX_PHRASES) + multiturn_prompt_api_message = prompts.create_multiturn_prompt(multiturn_prompt_content) + # print(f'Usual prompt api message: {prompt_api_message}') + # print(f'Multiturn prompt: {multiturn_prompt_api_message}') + usual_response = openai.ChatCompletion.create( model=self.model, - messages=[{"role": "system", "content": prompt_content}], + messages=prompt_api_message, temperature=0.0 ) + except Exception as exception: print(exception) return '' - full_response = response.choices[0].message.content + usual_full_response = usual_response.choices[0].message.content try: - return full_response.split('[')[1].split(']')[0] + return usual_full_response.split('[')[1].split(']')[0] except: return '' @@ -44,7 +52,7 @@ def generate_response_from_transcript(self, transcript): if self.gl_vars.freeze_state[0]: return '' - return generate_response_from_transcript_no_check(self, transcript) + return self.generate_response_from_transcript_no_check(transcript) def respond_to_transcriber(self, transcriber): while True: @@ -63,7 +71,7 @@ def respond_to_transcriber(self, transcriber): self.response = response self.conversation.update_conversation(persona=constants.PERSONA_ASSISTANT, text=response, - time_spoken=end_time) + time_spoken=datetime.datetime.now()) remaining_time = self.response_interval - execution_time if remaining_time > 0: diff --git a/TranscriberModels.py b/TranscriberModels.py index 0c80ebe..35cacc5 100644 --- a/TranscriberModels.py +++ b/TranscriberModels.py @@ -10,7 +10,7 @@ def get_model(use_api: bool, model: str = None): return APIWhisperTranscriber() model_cleaned = model if model else 'tiny' - print(f'Using model: {model_cleaned}') + print(f'[INFO] Using local model: {model_cleaned}') return WhisperTranscriber(model=model_cleaned) diff --git a/conversation.py b/conversation.py index ca155f5..435888a 100644 --- a/conversation.py +++ b/conversation.py @@ -1,6 +1,14 @@ from heapq import merge import constants import configuration +import datetime + +DEFAULT_PREAMBLE = """You are a casual pal, genuinely interested in the conversation at hand.""" \ + """Please respond, in detail, to the conversation. Confidently give a """\ + """straightforward response to the speaker, even if you don't understand """\ + """them. Give your response in square brackets. DO NOT ask to repeat, """\ + """and DO NOT ask for clarification. Just answer the speaker directly."""\ + """A poor transcription of conversation is given below.""" class Conversation: @@ -13,6 +21,8 @@ def __init__(self): constants.PERSONA_YOU: [], constants.PERSONA_SPEAKER: [], constants.PERSONA_ASSISTANT: []} + transcript = self.transcript_data[constants.PERSONA_SYSTEM] + transcript.append((f"{constants.PERSONA_SYSTEM}: [{DEFAULT_PREAMBLE}]\n\n", datetime.datetime.now())) config = configuration.Config().get_data() def clear_conversation_data(self): @@ -58,3 +68,23 @@ def get_conversation(self, key=lambda x: x[1])) combined_transcript = combined_transcript[-length:] return "".join([t[0] for t in combined_transcript]) + + def get_merged_conversation(self, length: int = 0) -> list: + """Creates a prompt to be sent to LLM (OpenAI by default) + length: Get the last length elements from the audio transcript. + Default value = 0, gives the complete transcript + """ + # print(f'You: Length: {len(self.transcript_data[constants.PERSONA_YOU])}') + # print(f'Speaker: Length: {len(self.transcript_data[constants.PERSONA_SPEAKER])}') + # print(f'Assistant: Length: {len(self.transcript_data[constants.PERSONA_ASSISTANT])}') + # print(f'System: Length: {len(self.transcript_data[constants.PERSONA_SYSTEM])}') + + combined_transcript = list(merge( + self.transcript_data[constants.PERSONA_YOU][-length:], + self.transcript_data[constants.PERSONA_SPEAKER][-length:], + self.transcript_data[constants.PERSONA_ASSISTANT][-length:], + key=lambda x: x[1])) + combined_transcript = combined_transcript[-length:] + + combined_transcript.insert(0, (f"{constants.PERSONA_SYSTEM}: [{self.transcript_data[constants.PERSONA_SYSTEM][0]}]\n\n", datetime.datetime.now())) + return combined_transcript diff --git a/main.py b/main.py index 57b57de..1cb227b 100644 --- a/main.py +++ b/main.py @@ -62,7 +62,7 @@ def main(): if response.status_code != 200: print(f'Error received: {response}') except ConnectionError: - print('Operating as a standalone client') + print('[INFO] Operating in Desktop mode') config = configuration.Config().get_data() diff --git a/prompts.py b/prompts.py index c0d85d2..5d08c88 100644 --- a/prompts.py +++ b/prompts.py @@ -8,3 +8,18 @@ def create_prompt(transcript): \ {transcript}.\ {EPILOGUE}' + + +def create_single_turn_prompt_message(transcript: str): + message = f'{PREAMBLE} \ + \ +{transcript}.\ +{EPILOGUE}' + + prompt_api_message = [{"role": "system", "content": message}] + return prompt_api_message + + +def create_multiturn_prompt(convo: list): + # print(convo) + return ''