give me an example on how to get last message of u...
# ask-ai
m
give me an example on how to get last message of user and use it with on_user_turn_completed for LLM node
t
Thanks for reaching out, Extra. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here is an example of how to get the last message from a user and use it with
on_user_turn_completed
for an LLM node:
Copy code
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 Docs
m
how can I pass the ChatContext and ChatMessage
t
To pass
ChatContext
and
ChatMessage
in the
on_user_turn_completed
method, you can follow this structure:
Copy code
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 Docs
m
I am asking where can I get the new message and the ChatContext in entrypoint?
t
In the entrypoint for
on_user_turn_completed
, the
new_message
and
ChatContext
are provided as parameters to the method. Here’s how you can access them:
Copy code
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