processors(realtime-ai): add support for appending to the LLM context
This commit is contained in:
@@ -158,6 +158,15 @@ class LLMMessagesFrame(DataFrame):
|
|||||||
messages: List[dict]
|
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
|
@dataclass
|
||||||
class LLMMessagesUpdateFrame(DataFrame):
|
class LLMMessagesUpdateFrame(DataFrame):
|
||||||
"""A frame containing a list of new LLM messages. These messages will
|
"""A frame containing a list of new LLM messages. These messages will
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from pipecat.frames.frames import (
|
|||||||
InterimTranscriptionFrame,
|
InterimTranscriptionFrame,
|
||||||
LLMFullResponseEndFrame,
|
LLMFullResponseEndFrame,
|
||||||
LLMFullResponseStartFrame,
|
LLMFullResponseStartFrame,
|
||||||
|
LLMMessagesAppendFrame,
|
||||||
LLMMessagesFrame,
|
LLMMessagesFrame,
|
||||||
LLMMessagesUpdateFrame,
|
LLMMessagesUpdateFrame,
|
||||||
StartInterruptionFrame,
|
StartInterruptionFrame,
|
||||||
@@ -121,6 +122,10 @@ class LLMResponseAggregator(FrameProcessor):
|
|||||||
# Reset anyways
|
# Reset anyways
|
||||||
self._reset()
|
self._reset()
|
||||||
await self.push_frame(frame, direction)
|
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):
|
elif isinstance(frame, LLMMessagesUpdateFrame):
|
||||||
# We push the frame downstream so the assistant aggregator gets
|
# We push the frame downstream so the assistant aggregator gets
|
||||||
# updated as well.
|
# updated as well.
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from pydantic import BaseModel, ValidationError
|
|||||||
|
|
||||||
from pipecat.frames.frames import (
|
from pipecat.frames.frames import (
|
||||||
Frame,
|
Frame,
|
||||||
|
LLMMessagesAppendFrame,
|
||||||
LLMMessagesUpdateFrame,
|
LLMMessagesUpdateFrame,
|
||||||
LLMModelUpdateFrame,
|
LLMModelUpdateFrame,
|
||||||
StartFrame,
|
StartFrame,
|
||||||
@@ -133,6 +134,8 @@ class RealtimeAIProcessor(FrameProcessor):
|
|||||||
await self._handle_config_update(message.data.config)
|
await self._handle_config_update(message.data.config)
|
||||||
case "llm-get-context":
|
case "llm-get-context":
|
||||||
await self._handle_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":
|
case "llm-update-context":
|
||||||
await self._handle_llm_update_context(message.data.config)
|
await self._handle_llm_update_context(message.data.config)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
@@ -203,6 +206,11 @@ class RealtimeAIProcessor(FrameProcessor):
|
|||||||
message = TransportMessageFrame(message=response.model_dump(exclude_none=True))
|
message = TransportMessageFrame(message=response.model_dump(exclude_none=True))
|
||||||
await self.push_frame(message)
|
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):
|
async def _handle_llm_update_context(self, config: RealtimeAIConfig):
|
||||||
if config.llm and config.llm.messages:
|
if config.llm and config.llm.messages:
|
||||||
frame = LLMMessagesUpdateFrame(config.llm.messages)
|
frame = LLMMessagesUpdateFrame(config.llm.messages)
|
||||||
|
|||||||
Reference in New Issue
Block a user