give me a simple pseudo code where an agent makes ...
# ask-ai
a
give me a simple pseudo code where an agent makes a tool call; if the tool call returns "g; a" then the agent just session.say "a"; can the tool call command session.say if result = a?
t
Thanks for reaching out, Ali. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thank you for sharing your scenario! Here’s a simple pseudo code example that might help you:
Copy code
class MyAgent(Agent):
    @function_tool
    async def my_tool(self) -> str:
        # Perform the tool call
        result = await some_tool_call()

        # Check the result and respond accordingly
        if result == "g; a":
            await self.session.say("a")
        return result
In this example, the tool call checks if the result is "g; a" and then uses
session.say
to say "a". If you’d like, I’m here to walk you through it further or provide more examples—just let me know! Sources: Tool definition and use | LiveKit docs | Agent speech and audio | LiveKit docs | examples/src/basic_tool_call_agent.ts | agent.py | tests/test_agent_session.py
a
do it for this example here: where if the temperature is "5 degrees"; session.say "it's 5"; otherwise tool result returns back to the agent that speaks from livekit.agents import function_tool, Agent, RunContext class MyAgent(Agent): @function_tool() async def lookup_weather( self, context: RunContext, location: str, ) -> dict[str, Any]: """Look up weather information for a given location. Args: location: The location to look up weather information for. """ return {"weather": "sunny", "temperature_f": 70}
t
Here is the updated example based on your scenario:
Copy code
from livekit.agents import function_tool, Agent, RunContext

class MyAgent(Agent):
    @function_tool()
    async def lookup_weather(
        self,
        context: RunContext,
        location: str,
    ) -> dict[str, Any]:
        """Look up weather information for a given location.

        Args:
            location: The location to look up weather information for.
        """
        # Simulate weather lookup
        result = {"weather": "sunny", "temperature_f": 5}

        # Check the temperature and respond accordingly
        if result["temperature_f"] == 5:
            await context.session.say("It's 5 degrees")
        else:
            return result

        return result
In this example, if the temperature is exactly 5 degrees, the agent will say "It's 5 degrees" using
session.say
. Otherwise, the tool result will be returned to the agent for further processing. Please feel free to reach out if you'd like any more tweaks or have questions—I'm here to help! Sources: Agent speech and audio | LiveKit docs | examples/src/realtime_agent.ts | Tool definition and use | LiveKit docs
a
continue to where the agent is initialized and all
t
Thank you for sharing your example! Here's the continuation, including how to initialize the agent:
Copy code
from livekit.agents import function_tool, Agent, RunContext

class MyAgent(Agent):
    @function_tool()
    async def lookup_weather(
        self,
        context: RunContext,
        location: str,
    ) -> dict[str, Any]:
        """Look up weather information for a given location.

        Args:
            location: The location to look up weather information for.
        """
        # Simulate weather lookup
        result = {"weather": "sunny", "temperature_f": 5}

        # Check the temperature and respond accordingly
        if result["temperature_f"] == 5:
            await context.session.say("It's 5 degrees")
        else:
            return result

        return result

# Initialize and run the agent
def main():
    agent = MyAgent()
    agent.run()

if __name__ == "__main__":
    main()
This snippet includes the
MyAgent
class initialization along with the
main
function to launch the agent. If you have any questions or need help tweaking it, please feel free to ask—I’m here to help!
a
where's the agent prompt here...
t
You're absolutely right, the agent prompt was missing in the example provided. No worries—here’s the updated version with the agent prompt included:
Copy code
from livekit.agents import function_tool, Agent, RunContext

class MyAgent(Agent):
    def __init__(self):
        super().__init__(instructions="You are a helpful assistant that provides weather information.")

    @function_tool()
    async def lookup_weather(
        self,
        context: RunContext,
        location: str,
    ) -> dict[str, Any]:
        """Look up weather information for a given location.

        Args:
            location: The location to look up weather information for.
        """
        # Simulate weather lookup
        result = {"weather": "sunny", "temperature_f": 5}

        # Check the temperature and respond accordingly
        if result["temperature_f"] == 5:
            await context.session.say("It's 5 degrees")
        else:
            return result

        return result

# Initialize and run the agent
def main():
    agent = MyAgent()
    agent.run()

if __name__ == "__main__":
    main()
The
instructions
parameter inside the
MyAgent
class constructor acts as the agent's prompt, gently guiding how it behaves and responds. If you’d like me to walk you through any part of this or help with further tweaks, just let me know—I’m here to help!