diff --git a/src/pipecat/frames/frames.py b/src/pipecat/frames/frames.py index f28f2b85e..79fd24322 100644 --- a/src/pipecat/frames/frames.py +++ b/src/pipecat/frames/frames.py @@ -158,6 +158,15 @@ class LLMMessagesFrame(DataFrame): messages: List[dict] +@dataclass +class LLMMessagesAppendFrame(DataFrame): + """A frame containing a list of LLM messages that neeed to be added to the + current context. + + """ + messages: List[dict] + + @dataclass class LLMMessagesUpdateFrame(DataFrame): """A frame containing a list of new LLM messages. These messages will diff --git a/src/pipecat/processors/aggregators/llm_response.py b/src/pipecat/processors/aggregators/llm_response.py index efc8af179..6939a70c4 100644 --- a/src/pipecat/processors/aggregators/llm_response.py +++ b/src/pipecat/processors/aggregators/llm_response.py @@ -14,6 +14,7 @@ from pipecat.frames.frames import ( InterimTranscriptionFrame, LLMFullResponseEndFrame, LLMFullResponseStartFrame, + LLMMessagesAppendFrame, LLMMessagesFrame, LLMMessagesUpdateFrame, StartInterruptionFrame, @@ -121,6 +122,10 @@ class LLMResponseAggregator(FrameProcessor): # Reset anyways self._reset() await self.push_frame(frame, direction) + elif isinstance(frame, LLMMessagesAppendFrame): + self._messages.extend(frame.messages) + messages_frame = LLMMessagesFrame(self._messages) + await self.push_frame(messages_frame) elif isinstance(frame, LLMMessagesUpdateFrame): # We push the frame downstream so the assistant aggregator gets # updated as well. diff --git a/src/pipecat/processors/frameworks/realtimeai.py b/src/pipecat/processors/frameworks/realtimeai.py index df2e7650a..742e85198 100644 --- a/src/pipecat/processors/frameworks/realtimeai.py +++ b/src/pipecat/processors/frameworks/realtimeai.py @@ -12,6 +12,7 @@ from pydantic import BaseModel, ValidationError from pipecat.frames.frames import ( Frame, + LLMMessagesAppendFrame, LLMMessagesUpdateFrame, LLMModelUpdateFrame, StartFrame, @@ -133,6 +134,8 @@ class RealtimeAIProcessor(FrameProcessor): await self._handle_config_update(message.data.config) case "llm-get-context": await self._handle_llm_get_context() + case "llm-append-context": + await self._handle_llm_append_context(message.data.config) case "llm-update-context": await self._handle_llm_update_context(message.data.config) except ValidationError as e: @@ -203,6 +206,11 @@ class RealtimeAIProcessor(FrameProcessor): message = TransportMessageFrame(message=response.model_dump(exclude_none=True)) await self.push_frame(message) + async def _handle_llm_append_context(self, config: RealtimeAIConfig): + if config.llm and config.llm.messages: + frame = LLMMessagesAppendFrame(config.llm.messages) + await self.push_frame(frame) + async def _handle_llm_update_context(self, config: RealtimeAIConfig): if config.llm and config.llm.messages: frame = LLMMessagesUpdateFrame(config.llm.messages)