From 363a722370e374531f816aa995f0d97ba3bd79f5 Mon Sep 17 00:00:00 2001 From: Kwindla Hultman Kramer Date: Sun, 3 Mar 2024 17:16:46 -0800 Subject: [PATCH] example 02 --- src/examples/foundational/01-say-one-thing.py | 19 +++++++----- .../foundational/02-llm-say-one-thing.py | 29 ++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/examples/foundational/01-say-one-thing.py b/src/examples/foundational/01-say-one-thing.py index 51e401cd0..6f05b4d22 100644 --- a/src/examples/foundational/01-say-one-thing.py +++ b/src/examples/foundational/01-say-one-thing.py @@ -1,7 +1,7 @@ import asyncio import aiohttp import os -from dailyai.queue_frame import EndStreamQueueFrame +from dailyai.queue_frame import EndStreamQueueFrame, TextQueueFrame from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService @@ -30,6 +30,7 @@ async def main(room_url): ) tts = ElevenLabsTTSService( + source_queue=asyncio.Queue(), aiohttp_session=session, api_key=os.getenv("ELEVENLABS_API_KEY"), voice_id=os.getenv("ELEVENLABS_VOICE_ID")) @@ -42,22 +43,26 @@ async def main(room_url): ) """ - tts.sink = transport.send_queue + tts.sink_queue = transport.send_queue # Register an event handler so we can play the audio when the participant joins. @transport.event_handler("on_first_other_participant_joined") async def on_first_other_participant_joined(transport, participant): nonlocal tts - await tts.say( - "Hello there, " + participant["info"]["userName"] + "!" + # todo: update the tts.say() convenience method to use the new queue architecture + await tts.source_queue.put( + TextQueueFrame("Hello there, " + + participant["info"]["userName"] + "!") ) + await tts.source_queue.put(EndStreamQueueFrame()) # wait for the output queue to be empty, then leave the meeting - await transport.stop_when_done() - # question: how do we exit the script? + # todo: commented out because it seems to exit a little early, before + # the audio is finished playing + # await transport.stop_when_done() - await transport.run() + await asyncio.gather(transport.run(), tts.process_queue()) if __name__ == "__main__": (url, token) = configure() diff --git a/src/examples/foundational/02-llm-say-one-thing.py b/src/examples/foundational/02-llm-say-one-thing.py index 8d1ac1edd..90df0b258 100644 --- a/src/examples/foundational/02-llm-say-one-thing.py +++ b/src/examples/foundational/02-llm-say-one-thing.py @@ -24,33 +24,34 @@ async def main(room_url): ) llm = AzureLLMService( + source_queue=asyncio.Queue(), api_key=os.getenv("AZURE_CHATGPT_API_KEY"), endpoint=os.getenv("AZURE_CHATGPT_ENDPOINT"), model=os.getenv("AZURE_CHATGPT_MODEL"), ) tts = ElevenLabsTTSService( - sink=transport.send_queue, + + source_queue=llm.sink_queue, # this should really be a sentence aggregator? aiohttp_session=session, api_key=os.getenv("ELEVENLABS_API_KEY"), voice_id=os.getenv("ELEVENLABS_VOICE_ID"), - ).chain(llm) + ) - messages = [{ - "role": "system", - "content": "You are an LLM in a WebRTC session, and this is a 'hello world' demo. Say hello to the world." - }] - await llm.source.put(LLMMessagesQueueFrame(messages)) - await llm.source.put(EndStreamQueueFrame()) - - tts_task = asyncio.gather(llm.process_queue(), tts.process_queue()) + tts.sink_queue = transport.send_queue @transport.event_handler("on_first_other_participant_joined") - async def on_first_other_participant_joined(transport): - await tts_task - await transport.stop_when_done() + async def on_first_other_participant_joined(transport, participant): + messages = [{ + "role": "system", + "content": "You are an LLM in a WebRTC session, and this is a 'hello world' demo. Say hello to the world." + }] + await llm.source_queue.put(LLMMessagesQueueFrame(messages)) + await llm.source_queue.put(EndStreamQueueFrame()) + # todo: commented out because it exits before audio plays + # await transport.stop_when_done() - await transport.run() + await asyncio.gather(transport.run(), llm.process_queue(), tts.process_queue()) if __name__ == "__main__":