import asyncio
import logging
from uuid import uuid4
from typing import Union
from dotenv import load_dotenv
from vision_agents.core.edge.types import User
from vision_agents.plugins import elevenlabs, deepgram, openai, getstream
from vision_agents.core import agents
from vision_agents.core.events import (
CallSessionParticipantJoinedEvent,
CallSessionParticipantLeftEvent,
CallSessionStartedEvent,
CallSessionEndedEvent,
STTTranscriptEvent,
LLMResponseEvent,
PluginErrorEvent
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
load_dotenv()
async def start_agent():
call_id = str(uuid4())
# Create the agent
agent = agents.Agent(
edge=getstream.Edge(),
agent_user=User(name="My AI Assistant", id="agent"),
instructions="You're a helpful voice AI assistant. Keep responses short and conversational.",
llm=openai.LLM(model="gpt-4o-mini"),
tts=elevenlabs.TTS(),
stt=deepgram.STT(),
)
await agent.create_user()
# Event handlers
@agent.events.subscribe
async def handle_participant_joined(event: CallSessionParticipantJoinedEvent):
if event.participant.user.id == "agent":
return
logger.info(f"New participant: {event.participant.user.name}")
await agent.simple_response(f"Hello {event.participant.user.name}! Welcome!")
@agent.events.subscribe
async def handle_participant_left(event: CallSessionParticipantLeftEvent):
if event.participant.user.id == "agent":
return
logger.info(f"Participant left: {event.participant.user.name}")
await agent.simple_response(f"Goodbye {event.participant.user.name}!")
@agent.events.subscribe
async def handle_session_started(event: CallSessionStartedEvent):
logger.info("Call session started")
await agent.simple_response("The call has started. I'm here to help!")
@agent.events.subscribe
async def handle_transcript(event: STTTranscriptEvent):
if event.is_final and event.confidence > 0.8:
logger.info(f"User said: {event.text}")
@agent.events.subscribe
async def handle_llm_response(event: LLMResponseEvent):
logger.info(f"Agent responding: {event.text}")
@agent.events.subscribe
async def handle_errors(event: PluginErrorEvent):
logger.error(f"Plugin error: {event.error_message}")
if event.is_fatal:
await agent.simple_response("I'm experiencing technical difficulties.")
# Create and join the call
call = agent.edge.client.video.call("default", call_id)
await agent.edge.open_demo(call)
with await agent.join(call):
await agent.finish()
if __name__ == "__main__":
asyncio.run(start_agent())