im getting objects in session.history , how can i ...
# ask-ai
s
im getting objects in session.history , how can i get them , and i need seperate user transcript not llm context
t
Thanks for reaching out, ABHIRAM. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking to retrieve objects from
session.history
and separate user transcripts from LLM-generated content. Here are some friendly ways to approach this: • Accessing Full Conversation History: You can use
session.history
to see all the messages exchanged during your session. Each message has a
role
field, which helps you identify where it came from. • Filtering for User Transcripts: To get only the user messages, just filter where
role == "user"
. Here’s a simple example:
Copy code
# Extract user messages
user_transcripts = [item for item in session.history if item.role == "user"]
for message in user_transcripts:
    print(f"User said: {message.content}")
• Real-Time Access: If you want to grab user input as it’s being transcribed, you can listen for the
user_input_transcribed
event. Here’s how:
Copy code
def on_user_input_transcribed(event):
    if event.is_final:
        print(f"Final user transcript: {event.transcript}")
session.on("user_input_transcribed", on_user_input_transcribed)
• Post-Session Persistence: To save user transcripts after the session ends, you can use
add_shutdown_callback
. Here’s a handy example:
Copy code
def save_user_transcripts():
    user_messages = [item for item in session.history if item.role == "user"]
    with open("user_transcripts.json", "w") as f:
        import json
        json.dump([{"content": m.content, "timestamp": m.timestamp} for m in user_messages], f)
session.add_shutdown_callback(save_user_transcripts)
I hope these methods help you separate user transcripts from the LLM-generated content smoothly. If you have any questions or want to explore this further, just let me know—I’m here to help! Sources: Session recording and transcripts | LiveKit Docs | Workflows | LiveKit Docs | examples/voice_agents/realtime_load_chat_history.py
s
i want user and agent transcript and i need llm context seperately and i need to print llm context everytime and i dont need it in object
t
I'm happy to guide you through retrieving user and agent transcripts separately and printing the LLM context without using objects. Here's how you can do it step-by-step: • Separate User and Agent Transcripts: You can use the
conversation_item_added
event to filter messages by role (
user
or
assistant
) and print them directly. Here's an example for you:
Copy code
def handle_conversation_item(event):
    item = event.item
    if item.role == "user":
        print(f"User: {item.text_content}")
    elif item.role == "assistant":
        print(f"Agent: {item.text_content}")
• Print LLM Context: To access and print the LLM context before processing, you can override the
llm_node
method. Here's a simple example:
Copy code
async def llm_node(self, chat_ctx, tools, model_settings):
    for item in chat_ctx.items:
        print(f"[{item.role.upper()}]: {item.text_content}")
    return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
• Real-Time User Input: If you'd like to print user input as it's being transcribed, use the
user_input_transcribed
event like this:
Copy code
def handle_user_transcript(event):
    if event.is_final:
        print(f"User (Final): {event.transcript}")
These approaches should help you separate and print user, agent, and LLM context either in real-time or after the session. If anything feels unclear or you want to dive deeper into any part, please don't hesitate to ask—I'm here to help! Sources: Session recording and transcripts | LiveKit Docs | Events and error handling | LiveKit docs | Understanding Transcript Timing in Voice Conversations | complex-agents/note-taking-assistant/agent.py
s
i need code sampel to place it in entry function : • Separate User and Agent Transcripts: You can use the
conversation_item_added
event to filter messages by role (
user
or
assistant
) and print them directly. Here's an example for you:
Copy code
def handle_conversation_item(event):
    item = event.item
    if item.role == "user":
        print(f"User: {item.text_content}")
    elif item.role == "assistant":
        print(f"Agent: {item.text_content}")
t
I'm happy to share a complete code sample that you can add to your entry function to help separate user and agent transcripts using the
conversation_item_added
event:
Copy code
from livekit.agents import Agent, AgentSession, JobContext, AutoSubscribe
from livekit.agents.voice import ConversationItemAddedEvent

class TranscriptAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="You are a helpful assistant."
        )

async def entrypoint(ctx: JobContext):
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

    agent = TranscriptAgent()
    session = AgentSession()

    # Event handler for separating user and agent transcripts
    @session.on("conversation_item_added")
    def handle_conversation_item(event: ConversationItemAddedEvent):
        item = event.item
        if item.role == "user":
            print(f"User: {item.text_content}")
        elif item.role == "assistant":
            print(f"Agent: {item.text_content}")

    await session.start(agent=agent, room=ctx.room)
Just place this code in your entry function, and it will print user and agent transcripts separately in real-time. If you have any questions or need more help, please feel free to ask—I’m here to support you!