For running the agent, we always needs to run the ...
# server-api
a
For running the agent, we always needs to run the script, is there anyway that we can start the agent on calling the fastapi endpoint only?
p
Copy code
@asynccontextmanager
async def lifespan(app: FastAPI):
    # Get the virtual environment Python path
    python_path = sys.executable
    agent_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'agents.py')
    
    try:
        # Download required LiveKit models first
        <http://logger.info|logger.info>("Downloading required LiveKit models...")
        subprocess.run([python_path, agent_path, 'download-files'], check=True)
        
        # Start the agent process in dev mode
        <http://logger.info|logger.info>("Starting agent process...")
        agent_process = subprocess.Popen([python_path, agent_path, 'start'])
        
        yield
        
        # Cleanup when the application shuts down
        <http://logger.info|logger.info>("Shutting down agent process...")
        agent_process.terminate()
        agent_process.wait()
        
    except subprocess.CalledProcessError as e:
        logger.error(f"Failed to download LiveKit models: {e}")
        raise
    except Exception as e:
        logger.error(f"Error in lifespan manager: {e}")
        raise

app = FastAPI(lifespan=lifespan)
Add this to your main.py, replace agents.py with your livekit agent
a
Okay, thanks for providing the snippet. Is there anyway that we can directly start the agent using endpoint?
p
That is not an endpoint, that snippet automates manually starting your agent from the console when you start your fastapi server
a
Sure, thanks
p
Oh! i misread your response, apologies!
🙌 1
you can expose an endpoint for it, when called, it starts your agent server, just add the route @app.get("/") wherever you want to use it
a
Yes, but what if there are multiple agents, so I'll need to create all new scripts for running the agents
so, I just want to create a base agent and then it should be run independently on calling the endpoint
p
a
Okay, thanks
🙌 1
p
let me know if it helps
a
Sure, checking it
Thanks for sharing
r
Not sure why you do it this way. Why not just have all your agents running and dispatch the ones you want in the room.
p
Gives the flexibility of our users to use their own models or ours
r
You can provide the model in the dispatch
p
I modified my code to test if the issue was coming from the format of my metadata, I used the sample user_id as provided in the documentation but I still didn’t get anything from the job context
r
can I see your agent code and full dispatch code?
p
Alright, I will provide it as soon as I get on my pc
r
um, not sure how we got on the topic of metadata here 🙂
p
🫣
I will start a new thread, apologies @aloof-ghost-16048
👍 2