processors(realtime-ai): add support for appending to the LLM context

This commit is contained in:
Aleix Conchillo Flaqué
2024-07-19 13:44:16 -07:00
parent 846ae765e5
commit b85dd7283a
3 changed files with 22 additions and 0 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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)