Show me a small python code example of how can I u...
# ask-ai
c
Show me a small python code example of how can I use await agent.say() inside a function call
t
Thanks for reaching out, Yousri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Copy code
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.pipeline import VoicePipelineAgent
from livekit.plugins import deepgram, openai, silero

async def entrypoint(ctx: JobContext):
    agent = VoicePipelineAgent(
        vad=silero.VAD.load(),
        stt=deepgram.STT(),
        llm=openai.LLM(),
        tts=openai.TTS(),
    )

    # Connect and start the agent with room context
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
    agent.start(ctx.room)
    
    # Use await agent.say()
    await agent.say("Hey, how can I help you today?", allow_interruptions=True)
Sources: delay-handling/README.mdoc
c
That's not a function call. I want to see how you access the agent inside an Agent class
t
Copy code
class MyAgent(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions="You are a helpful assistant.",
            stt=deepgram.STT(),
            llm=openai.LLM(model="gpt-4o"),
            tts=openai.TTS(),
            vad=silero.VAD.load()
        )

    async def greet_user(self):
        await self.session.say("Hey there! I'm ready to help you out - what can I do for you today?")

# Usage
agent = MyAgent()
await agent.greet_user()
I see you're working on setting up an agent with a greeting! Just a friendly tip: make sure your agent is all set up and connected to a room before you call
say()
. That way, everything will run smoothly. Need any help walking through the setup? Sources: chain-of-thought-tts/agent.py | Agent speech | LiveKit Docs
c
No. I want you to show me an example of function with annotation @function_tool() That calls agent.say() inside the function
t
Copy code
from livekit.agents import Agent, function_tool

class MyAgent(Agent):
    @function_tool
    async def greet_user(self):
        await self.say("Hello, how are you today?")
Hey there! Let me walk you through this cool code snippet. We've got an Agent with a friendly greeting method. By using the
@function_tool
decorator and
await self.say()
, we're setting up our agent to warmly welcome users with a simple "Hello, how are you today?" Pretty neat, right? 😊 Sources: pipeline-tts/tts_comparison.py | Tool definition and use | LiveKit Docs
c
This doesn't work. How do you access a session inside an Agent class?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!