can we have pre warm in inbound calls in Livekit?
# ask-ai
c
can we have pre warm in inbound calls in Livekit?
t
Thanks for reaching out, NAJAM. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please help me understand a bit more about what you mean by 'pre-warm' for inbound calls? Are you talking about pre-connecting audio, video, or maybe something else before the call is fully set up? I'm here to help clarify everything for you!
a
yes you can @clever-doctor-15083
t
Could you please share a bit more about what you mean by 'pre-warm' for inbound calls? For instance, are you referring to pre-connecting audio, video, or maybe another specific feature? I'm here to help and want to make sure I understand your needs perfectly!
c
@adorable-petabyte-73906 how can I? Could you please share any example for inbound with Python code for this.
t
To help you handle inbound calls in LiveKit using Python, here’s a friendly step-by-step guide you can follow: 1. Set Up the Agent: Start by creating a Python script to define your AI voice agent. Here’s a simple example to get you going:
Copy code
import logging
from pathlib import Path
from dotenv import load_dotenv
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.voice import Agent, AgentSession
from livekit.plugins import openai, deepgram, silero

# Load environment variables from .env file
load_dotenv(dotenv_path=Path(__file__).parent.parent / '.env')

class SimpleAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="You are a helpful agent.",
            stt=deepgram.STT(),
            llm=openai.LLM(model="gpt-4o"),
            tts=openai.TTS(),
            vad=silero.VAD.load()
        )
        
    async def on_enter(self):
        # Automatically greet the caller when the call starts
        self.session.generate_reply()

async def entrypoint(ctx: JobContext):
    session = AgentSession()
    agent = SimpleAgent()

    await session.start(
        agent=agent,
        room=ctx.room
    )

    # Optionally greet the caller
    await session.generate_reply(
        instructions="Greet the user and offer your assistance."
    )

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
1. Configure Inbound SIP Trunk: Next, you’ll want to create a JSON file for your trunk configuration and then use the LiveKit CLI to set it up like this:
Copy code
{
  "trunk": {
    "name": "My inbound trunk",
    "numbers": [
      "+15105550100"
    ]
  }
}
Copy code
lk sip inbound create inbound-trunk.json
1. Create a Dispatch Rule: This step is about defining a dispatch rule to route calls smoothly to your agent. Here’s how you can do it:
Copy code
{
  "dispatch_rule": {
    "rule": {
      "dispatchRuleIndividual": {
        "roomPrefix": "call-"
      }
    },
    "roomConfig": {
      "agents": [
        {
          "agentName": "inbound-agent"
        }
      ]
    }
  }
}
Copy code
lk sip dispatch create dispatch-rule.json
1. Start the Agent: Finally, run your agent in development mode with this command:
Copy code
python3 agent.py dev
Once your agent is up and running, you can give your SIP number a call and interact with the AI agent. If you hit any bumps or have questions along the way, please don’t hesitate to reach out—I’m here to help! Sources: telephony/sip_lifecycle.py | Agents telephony integration | LiveKit Docs | Accepting incoming calls | LiveKit Docs | Docs::Telephony | Creating SIP Inbound Trunks and Dispatch Rules with Python SDK
a
I Can not share the code , but you can prewarm the LLM and other models directly by AgetnSession.
c
Is this possible in Livekit 0.x too? I am on 0.x as of now
a
you can check the docs for this
c
sure.
✅ 1