Hey, how to manually or explicitly make a tool cal...
# ask-ai
p
Hey, how to manually or explicitly make a tool call from llm_node?
t
Thanks for reaching out, harish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Just a quick heads-up: you can't manually or explicitly trigger a tool call from within the
llm_node
function in LiveKit. The LLM decides on its own when to call a tool, based on the input, chat context, and the tools available. That said, you can still guide those tool calls by: • Choosing which tools you pass into the
llm_node
function. • Using the
tool_choice
parameter in
ModelSettings
to require or specify a particular tool. • Setting up the agent's instructions or chat context in a way that encourages the LLM to pick the tool you want. If you have a specific scenario in mind or want a hand figuring this out, just let me know—I’m here to help! Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
p
Here is my usecase, I need to make a tool call to end the session with a good bye note.
t
When it's time to wrap up a session with a friendly goodbye, you can create a function tool that sends that farewell message and then closes the session. Here are a couple of easy ways to do it: 1. Using `session.aclose()`:
Copy code
@function_tool
async def end_session(self):
    """Ends the session with a goodbye note."""
    await self.session.say("Goodbye!")
    await self.session.aclose()
2. Generating a Goodbye Message:
Copy code
@function_tool
async def close_session(self):
    """Ends the session with an LLM-generated goodbye."""
    await self.session.generate_reply(instructions="say goodbye to the user")
    await self.session.aclose()
Both ways make sure your session closes smoothly after sharing that warm farewell. If you want a hand setting this up or have any questions, just let me know—I’m here to help! Sources: flows/multi_stage_flow.py | tests/test_agent_session.py | Agents telephony integration | LiveKit Docs
p
but what is the trigger to make the function call? I need to manually decide to end the session. How to instruct the agent to end the session. I have my own llm_node impl
t
If you want to manually trigger a function call to end the session in your custom
llm_node
implementation, you can simply yield a
ChatChunk
that contains the tool call. Here’s a quick example:
Copy code
async def llm_node(self, chat_ctx: ChatContext, tools: list[FunctionTool], model_settings: ModelSettings):\n    # Check for a specific condition to end the session\n    if some_condition_to_end_session():\n        yield ChatChunk(\n            delta=llm.ChoiceDelta(\n                tool_calls=[\n                    llm.ToolCall(\n                        call_id=\"end_session_call\",\n                        type=\"function\",\n                        name=\"end_session\",\n                        arguments={}\n                    )\n                ]\n            )\n        )\n        return  # Skip further processing\n\n    # Default behavior\n    async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):\n        yield chunk
Just swap out
some_condition_to_end_session()
with whatever logic makes sense for when you want to end the session. This way, you’re making sure the tool call happens exactly when you want it to. If you have any questions or want me to walk you through it, just let me know—I’m here to help!
p
what is call id here? can a random value be added here?
t
Think of the
call_id
in a tool call as a special name tag that helps match your request with its result. It can be randomly created, but it does need to fit certain format rules depending on the AI model you’re working with. For example: • Some models use alphanumeric strings of a set length (like 9 characters). • Others might expect just integer strings. LiveKit usually relies on handy generators like
shortuuid
to whip up random values that fit the bill. Just make sure your
call_id
is unique and follows the format your setup requires—this way, everything runs smoothly! Sources: models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...