I want to automatically generate and retrieve a se...
# ask-ai
m
I want to automatically generate and retrieve a session summary whenever an agent session ends. I’m using Python to manage the agent lifecycle. What is the best way to implement this, ensuring that the summary includes key details like: • Conversation transcript or highlights • User intent summary • Actions taken or tools used • Sentiment or performance analysis (if available) I’d like the approach to be modular, easily integrated with existing agent code, and triggered automatically at the end of every session. Please suggest the best architecture or code pattern to achieve this.
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you automatically generate and retrieve a session summary in Python when an agent session ends! You can use the
close
event on
AgentSession
, which is triggered at the end of a session—this is a great opportunity to create your summary. Here’s a friendly guide on how to include the details you’re looking for: • Conversation Transcript: Simply use
session.history.items
to gather all the messages. • Actions Taken: You can extract
function_call
and
function_call_output
from the session history to see what actions were performed. • User Intent Summary and Sentiment Analysis: For these insights, integrating external NLP services like OpenAI or Hugging Face works really well to analyze the transcript. Here’s a high-level approach you might find useful: 1. Set up a
close
event handler that triggers when the session ends. 2. Go through
session.history.items
to collect the transcript and actions. 3. Use an external LLM or NLP library to analyze the transcript for intent and sentiment. 4. Then, store or display the summary however you prefer. If you’d like, I’d be more than happy to provide a detailed code example or help you with integrating a specific NLP service. Just let me know! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Events and error handling | LiveKit Docs | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki
m
’ve also created a
function_tool
that handles file operations and used the
generate_reply()
method to invoke it. It works correctly sometimes, but occasionally the
function_tool
doesn’t trigger at all. How can I ensure that the
generate_reply()
method consistently calls the
function_tool
every time
when the conditions are met? I’d appreciate a reliable pattern or debugging approach to guarantee consistent invocation.
t
To help make sure the
function_tool
is always triggered by
generate_reply()
, here are some friendly best practices to guide you: 1. Proper Tool Definition: Please double-check that your tool is decorated with
@function_tool
and that it accepts a
RunContext
parameter. This setup is key! 2. Control Tool Call Behavior: Try using
tool_choice="auto"
in
generate_reply()
. This lets the LLM decide when to invoke tools based on the situation, making things smoother. 3. Return Values: Make sure your tool returns a string or a tuple to prompt an LLM reply. If it returns
None
, the tool will finish quietly without a response. 4. Post-Execution Logic: You can use the
function_tools_executed
event to check what tools were called and their outputs, helping you manage any follow-up steps easily. 5. Debugging: It’s a good idea to confirm your tool is part of the agent's configuration. Also, watch out for any logic that might prevent the tool from being called, like incorrect
tool_choice
settings. If you'd like, I’d be happy to provide a detailed code example or walk you through any of these steps in more detail. Just let me know! Sources: Docs::Agents | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Tool definition and use | LiveKit Docs | Events and error handling | LiveKit Docs
m
Any other easy alternatives?
t
I completely understand the need for simpler ways to ensure the
function_tool
is called consistently. Here are a few straightforward approaches that might help: 1. Direct Invocation: Instead of waiting for
generate_reply()
to trigger the tool, you can directly call the
function_tool
in your code whenever the right conditions are met. This way, you can skip the LLM's decision-making step altogether. 2. Predefined Tool Logic: Another approach is to use a fixed instruction or context that clearly tells the LLM to call the tool. For example, you could include a prompt like "Always use the file operation tool when handling files." 3. Default Tool Execution: You might also consider setting up a fallback where the tool automatically runs if no other action happens during
generate_reply()
. This can be managed by checking the session state after generating the reply. If you'd like, I’d be happy to walk you through how to implement any of these options. Just let me know!