lemon-island-66773
02/24/2025, 7:45 PMnew_message_history = []
)
if context_forwarding_type == ContextForwardingType.NO_HISTORY:
# Create a new chat history with only the new system message
new_message_history = []
for message in self.agent.chat_ctx.messages:
if message.role == "system":
# Generate updated prompt message
updated_message = message.copy()
updated_message.content = current_node.prompt
# Append the new message at the end
new_message_history.append(updated_message)
# Remove the original message
new_message_history.remove(message)
self.agent.chat_ctx.messages = new_message_history
gray-monkey-83525
02/24/2025, 7:45 PMnew_message_history
is being manipulated. The code attempts to remove a message from new_message_history
that was never added to it, which might be causing an error. Instead of removing the original message, you should ensure that only the updated message is added to new_message_history
. Here's a revised version of your code:
if context_forwarding_type == ContextForwardingType.NO_HISTORY:
# Create a new chat history with only the new system message
new_message_history = []
for message in self.agent.chat_ctx.messages:
if message.role == "system":
# Generate updated prompt message
updated_message = message.copy()
updated_message.content = current_node.prompt
# Append the new message at the end
new_message_history.append(updated_message)
# Set the new message history
self.agent.chat_ctx.messages = new_message_history
This code ensures that only the updated system messages are added to new_message_history
, and then it sets self.agent.chat_ctx.messages
to this new list. Would you like more details on this approach?
Sources:
- https://docs.livekit.io/python/livekit/agents/llm/chat_context.html
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1732170619.318249
Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.