Skip to content

Commit

Permalink
fix: add example of truncating the chat context for multimodal agent
Browse files Browse the repository at this point in the history
  • Loading branch information
longcw committed Nov 3, 2024
1 parent ca462a9 commit 348e636
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/multimodal_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ async def get_weather(
)
agent.start(ctx.room, participant)

@agent.on("agent_speech_committed")
@agent.on("agent_speech_interrupted")
def _on_agent_speech_created(msg: llm.ChatMessage):
# example of truncating the chat context
max_ctx_len = 10
if len(agent.chat_ctx.messages) > max_ctx_len:
messages = agent.chat_ctx.messages[-max_ctx_len:]
agent.sync_chat_ctx(llm.ChatContext(messages=messages))


if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, worker_type=WorkerType.ROOM))
1 change: 1 addition & 0 deletions livekit-agents/livekit/agents/llm/chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def copy(self):

copied_msg = ChatMessage(
role=self.role,
id=self.id,
name=self.name,
content=content,
tool_calls=tool_calls,
Expand Down
3 changes: 3 additions & 0 deletions livekit-agents/livekit/agents/multimodal/multimodal_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def chat_ctx(self, value: llm.ChatContext | None) -> None:
def fnc_ctx(self, value: llm.FunctionContext | None) -> None:
self._session.fnc_ctx = self._fnc_ctx = value

def sync_chat_ctx(self, ctx: llm.ChatContext) -> None:
self._session.sync_chat_ctx(ctx)

def start(
self, room: rtc.Room, participant: rtc.RemoteParticipant | str | None = None
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,15 @@ def sync_chat_ctx(self, ctx: llm.ChatContext) -> None:
changes = utils.compute_changes(
self._chat_ctx.messages, ctx.messages, key_fnc=lambda x: x.id
)
logger.debug(
"sync chat context",
extra={
"to_delete": [msg.id for msg in changes.to_delete],
"to_add": [
(prev.id if prev else None, msg.id) for prev, msg in changes.to_add
],
},
)
for msg in changes.to_delete:
self.conversation.item.delete(item_id=msg.id)

Expand Down

0 comments on commit 348e636

Please sign in to comment.