From 9bbfc8ad053bbe3ed9cffadd41f95dfa8b771c3e Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Fri, 12 Jan 2024 11:40:49 -0500 Subject: [PATCH] Updating 06- sample --- .../services/daily_transport_service.py | 38 ++++++++++--------- .../06-listen-and-respond.py | 36 +++++++++--------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/dailyai/services/daily_transport_service.py b/src/dailyai/services/daily_transport_service.py index e26bd8195..1f1fe483b 100644 --- a/src/dailyai/services/daily_transport_service.py +++ b/src/dailyai/services/daily_transport_service.py @@ -50,6 +50,18 @@ class DailyTransportService(EventHandler): self.camera_thread = None self.frame_consumer_thread = None + self.transcription_settings = { + "language": "en", + "tier": "nova", + "model": "2-conversationalai", + "profanity_filter": True, + "redact": False, + "extra": { + "endpointing": True, + "punctuate": False, + }, + } + # This queue is used to marshal frames from the async output queue to the sync output queue # We need this to maintain the asynchronous behavior of asyncio queues -- to give async functions # a chance to run while waiting for queue items -- but also to maintain thread safety for the @@ -170,26 +182,14 @@ class DailyTransportService(EventHandler): ) if self.token: - self.transcription_queue = Queue() - self.client.start_transcription( - { - "language": "en", - "tier": "nova", - "model": "2-conversationalai", - "profanity_filter": True, - "redact": False, - "extra": { - "endpointing": True, - "punctuate": False, - }, - } - ) + self.transcription_queue = asyncio.Queue() + self.client.start_transcription(self.transcription_settings) self.my_participant_id = self.client.participants()["local"]["id"] - def get_transcriptions(self): + async def get_transcriptions(self): while True: - transcript = self.transcription_queue.get() + transcript = await self.transcription_queue.get() yield transcript def get_async_output_queue(self): @@ -259,8 +259,10 @@ class DailyTransportService(EventHandler): pass def on_transcription_message(self, message): - self.transcription_queue.put(message["text"]) - pass + print("got transcription", message) + if self.loop: + asyncio.run_coroutine_threadsafe(self.transcription_queue.put(message["text"]), self.loop) + print("put transcription in queue", message) def on_transcription_stopped(self, stopped_by, stopped_by_error): pass diff --git a/src/samples/theoretical-to-real/06-listen-and-respond.py b/src/samples/theoretical-to-real/06-listen-and-respond.py index c36d70107..034e608f0 100644 --- a/src/samples/theoretical-to-real/06-listen-and-respond.py +++ b/src/samples/theoretical-to-real/06-listen-and-respond.py @@ -26,28 +26,28 @@ async def main(room_url:str, token): llm = AzureLLMService() tts = AzureTTSService() - transcribed_message = "" - transcription_timeout = None + async def handle_transcriptions(): + messages = [ + {"role": "system", "content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Your output will be converted to audio. Respond to what the user said in a creative and helpful way."}, + ] - @transport.event_handler("on_participant_joined") - async def on_joined(transport, participant): - if participant["id"] == transport.my_participant_id: - return + sentence = "" + async for message in transport.get_transcriptions(): + sentence += message + if sentence.endswith((".", "?", "!")): + messages.append({"role": "user", "content": sentence}) + sentence = '' - async for audio_chunk in tts.run_tts("If you say something, I will respond."): - transport.output_queue.put(QueueFrame(FrameType.AUDIO_FRAME, audio_chunk)) + full_response = "" + async for response in llm.run_llm_async_sentences(messages): + full_response += response + async for audio in tts.run_tts(response): + transport.output_queue.put(QueueFrame(FrameType.AUDIO_FRAME, audio)) - @transport.event_handler("on_transcription_message") - async def on_transcription_message(transport, message) -> None: - nonlocal transcribed_message - nonlocal transcription_timeout - print(message) - if message["session_id"] != transport.my_participant_id: - transcribed_message += message['text'] + messages.append({"role": "assistant", "content": full_response}) - print("message received", transcribed_message) - - await transport.run() + transport.transcription_settings["extra"]["punctuate"] = True + await asyncio.gather(transport.run(), handle_transcriptions()) if __name__ == "__main__":