What's the best method to have the voice agent wai...
# ask-ai
d
What's the best method to have the voice agent wait for the user input, then use go on to the next function... this kind of works but id like the know a official way
Copy code
@function_tool
    async def verify_code(self, ctx: RunContext[InsuranceSessionInfo], spoken_verification_code: str) -> str:
        """Use this tool verify code, covert user input to alphan-umeric characters.  If it matches call discover_questions function"""
        await self.update_frontend()
        ctx.session.say("OK, can I get the code please?")
        result = await verify_spoken_code(spoken_verification_code)

        if result == True:
            return f"Say: Thanks for providing the verification code! then call discovery_questions function"
        else: 
            return ctx.session.say("Sorry I cant I get that code again please...")
t
Thanks for reaching out, Billy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To handle user input and move on to the next function in a voice agent, here’s a friendly approach you can try out: • Capture User Input: You can use the LiveKit voice agent framework to automatically turn user speech into text. The
generate_reply()
method in
AgentSession
is great for handling what the user says and crafting responses. • Trigger Function Tools: Create tools with the
@function_tool
decorator. These tools can kick in based on what the user says, run specific tasks, and send back results. • Progress to the Next Step: Once you’ve processed the input, you can call another function or switch to a new agent. For example, returning a new agent instance inside a tool function helps keep your workflow modular and smooth. Here’s a simple example of how this flow works:
Copy code
class GreetingAgent(Agent):
    async def on_enter(self):
        self.session.say("Hello! What's your name?")

    @function_tool
    async def collect_name(self, name: str):
        await self.session.say(f"Nice to meet you, {name}! What's your favorite color?")
        return ColorAgent(name=name)  # Proceeds to next agent
This setup helps keep the conversation natural and easy to follow. If you want me to walk you through any part or have questions, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | examples/src/restaurant_agent.ts | Testing and evaluation | LiveKit Docs | flows/simple_flow.py
d
ok i dont want to proceed to next agent just proceed to next function...
t
To move on to the next function after a user input without switching to another agent, you can define and run a function tool right within the current agent. Here’s a simple way to do it: • Define a Function Tool: Use the
@function_tool
decorator to create a tool that handles user input and carries out the logic you want. • Update State or Context: Use
session.state
for straightforward state tracking or
session.userdata
if you need to manage more detailed session-wide data. This helps the agent remember what it needs for the next step. • Generate the Next Step: Once the tool runs, the agent can automatically create the next response with
generate_reply()
or keep the conversation going based on the updated context. • Handle Errors Gracefully: Make sure your tools can deal with invalid inputs or missing info smoothly, so everything runs without a hitch. Here’s an example to illustrate:
Copy code
class DataCollectorAgent(BaseAgent):
    async def on_enter(self):
        await self.session.say("Please provide the code.")

    @function_tool
    async def verify_code(self, code: str):
        if code == "1234":
            await self.session.say("Code verified! Proceeding to the next step.")
        else:
            await self.session.say("Invalid code. Please try again.")
This way, the flow stays inside the same agent, making your multi-step interaction feel smooth and natural. If you want me to walk you through any part or have questions, just let me know—I’m here to help! Sources: complex-agents/drive-thru/test_agent.py | examples/drive-thru/test_agent.py | examples/src/restaurant_agent.ts
d
ok thanks, its nearly the same approach that im using. thanks