diff --git a/src/dailyai/queue_aggregators.py b/src/dailyai/queue_aggregators.py index 07d6da7c0..80c99bc19 100644 --- a/src/dailyai/queue_aggregators.py +++ b/src/dailyai/queue_aggregators.py @@ -70,6 +70,13 @@ class LLMContextAggregator(AIService): self.messages.append({"role": self.role, "content": frame.text}) # type: ignore -- the linter thinks this isn't a TextQueueFrame, even though we check it above yield LLMMessagesQueueFrame(self.messages) + async def finalize(self) -> AsyncGenerator[QueueFrame, None]: + # Send any dangling words that weren't finished with punctuation. + if self.complete_sentences and self.sentence: + self.messages.append({"role": self.role, "content": self.sentence}) + yield LLMMessagesQueueFrame(self.messages) + + class LLMUserContextAggregator(LLMContextAggregator): def __init__(self, messages: list[dict], diff --git a/src/samples/foundational/06-listen-and-respond.py b/src/samples/foundational/06-listen-and-respond.py index 8dbaea1e2..438c6e0ca 100644 --- a/src/samples/foundational/06-listen-and-respond.py +++ b/src/samples/foundational/06-listen-and-respond.py @@ -6,7 +6,7 @@ import urllib.parse from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.azure_ai_services import AzureLLMService, AzureTTSService -from dailyai.queue_aggregators import LLMContextAggregator +from dailyai.queue_aggregators import LLMAssistantContextAggregator, LLMContextAggregator, LLMUserContextAggregator async def main(room_url: str, token): @@ -36,12 +36,8 @@ async def main(room_url: str, token): {"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."}, ] - tma_in = LLMContextAggregator( - messages, "user", transport.my_participant_id - ) - tma_out = LLMContextAggregator( - messages, "assistant", transport.my_participant_id - ) + tma_in = LLMUserContextAggregator(messages, transport.my_participant_id) + tma_out = LLMAssistantContextAggregator(messages, transport.my_participant_id) await tts.run_to_queue( transport.send_queue, tma_out.run(