ecoute/GPTResponder.py
2023-05-09 19:33:15 -04:00

38 lines
1.3 KiB
Python

import openai
from keys import OPENAI_API_KEY
from prompts import create_prompt, INITIAL_RESPONSE
import time
openai.api_key = OPENAI_API_KEY
def generate_response_from_transcript(transcript):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
messages=[{"role": "system", "content": create_prompt(transcript)}],
temperature = 0.0
)
full_response = response.choices[0].message.content
try:
return full_response.split('[')[1].split(']')[0]
except:
return ''
class GPTResponder:
def __init__(self):
self.response = INITIAL_RESPONSE
self.response_interval = 2
def respond_to_transcriber(self, transcriber):
while True:
if transcriber.transcript_changed_event.is_set():
transcriber.transcript_changed_event.clear()
transcript_string = transcriber.get_transcript()
response = generate_response_from_transcript(transcript_string)
if response != '':
self.response = response
time.sleep(self.response_interval)
else:
time.sleep(0.3)
def update_response_interval(self, interval):
self.response_interval = interval