many-machine-74335
05/13/2025, 6:12 AMtall-airline-16756
05/13/2025, 6:12 AMtall-airline-16756
05/13/2025, 6:12 AMon_user_turn_completed
for an LLM node:
async def on_user_turn_completed(
self, turn_ctx: ChatContext, new_message: ChatMessage,
) -> None:
# Get the last user message directly from the new_message parameter
user_query = new_message.text_content
# RAG function definition omitted for brevity
rag_content = await my_rag_lookup(user_query)
# Add the RAG content as context for the LLM
turn_ctx.add_message(
role="assistant",
content=f"Additional information relevant to the user's next message: {rag_content}"
)
This example demonstrates how to:
• Access the user's last message via the new_message
parameter
• Extract the text content from that message
• Use it to perform a RAG (Retrieval-Augmented Generation) lookup
• Add the retrieved information to the chat context for the next LLM generation Sources: External data and RAG | LiveKit Docsmany-machine-74335
05/13/2025, 6:13 AMtall-airline-16756
05/13/2025, 6:13 AMChatContext
and ChatMessage
in the on_user_turn_completed
method, you can follow this structure:
from livekit.agents import ChatContext, ChatMessage
async def on_user_turn_completed(
self, turn_ctx: ChatContext, new_message: ChatMessage,
) -> None:
# Your code here
pass
Here’s how the parameters are used:
• `turn_ctx`: This is the full ChatContext
, containing the conversation history up to but not including the user’s latest message.
• `new_message`: This is the user’s latest message as a ChatMessage
object, representing their current turn.
You can use these parameters to:
• Retrieve context relevant to the newest message
• Add temporary messages to the chat context
• Modify the user’s message before it’s added to the chat context
• Abort generation entirely if needed Sources: Voice pipeline nodes | LiveKit Docsmany-machine-74335
05/13/2025, 6:14 AMtall-airline-16756
05/13/2025, 6:14 AMon_user_turn_completed
, the new_message
and ChatContext
are provided as parameters to the method. Here’s how you can access them:
from livekit.agents import ChatContext, ChatMessage
async def on_user_turn_completed(
self, turn_ctx: ChatContext, new_message: ChatMessage,
) -> None:
# Access the ChatContext and new_message here
rag_content = await my_rag_lookup(new_message.text_content())
turn_ctx.add_message(
role="assistant",
content=f"Additional information relevant to the user's next message: {rag_content}"
)
The turn_ctx
parameter contains the chat history up to but not including the user’s latest message, while new_message
represents the user’s latest message. After the method completes, the new_message
is automatically added to the chat context.
Sources: Voice pipeline nodes | LiveKit Docs