WebEssentz commited on
Commit
51a1cba
·
1 Parent(s): c61d9e2

Realtime Flow

Browse files
Files changed (1) hide show
  1. src/agent_session/main.py +37 -39
src/agent_session/main.py CHANGED
@@ -81,57 +81,55 @@ class VoiceAssistant(Agent):
81
  async def run_agent_session(room_name: str, agent_token: str):
82
  livekit_url = os.getenv("LIVEKIT_URL")
83
  room = Room()
 
84
 
85
  try:
86
  print(f"DEBUG: 1. Connecting to LiveKit room '{room_name}'...")
87
  await room.connect(livekit_url, agent_token)
88
  print("DEBUG: 2. Connection successful.")
89
 
90
- # Create HTTP session for TTS
91
- async with aiohttp.ClientSession() as session:
92
- # 1. Instantiate all plugins
93
- vad = VAD.load(min_speech_duration=0.1, min_silence_duration=0.5)
94
- tts = TTS(
95
- voice=VoiceByName(
96
- name="Tiktok Fashion Influencer",
97
- provider=VoiceProvider.hume
98
- ),
99
- instant_mode=True,
100
- http_session=session, # Pass the session here
101
- )
102
- stt = StreamAdapter(stt=STT(model="whisper-large-v3-turbo"), vad=vad)
103
- google_llm = GoogleLLM(model="gemini-2.5-flash")
104
- llm_wrapper = LLMStateWrapper(llm=google_llm, room=room)
105
-
106
- # 2. Create the AgentSession
107
- session_agent = AgentSession(
108
- stt=stt,
109
- llm=llm_wrapper,
110
- tts=tts,
111
- vad=vad,
112
- )
113
-
114
- # 3. Create an instance of your agent
115
- agent = VoiceAssistant()
116
-
117
- # 4. Start the session
118
- print("DEBUG: Starting AgentSession...")
119
- task = asyncio.create_task(session_agent.start(agent=agent, room=room))
120
-
121
- # 5. Generate the initial greeting
122
- print("DEBUG: Generating initial greeting...")
123
- await send_agent_state(room, "speaking")
124
- await session_agent.generate_reply(instructions="hello")
125
- print("DEBUG: Initial greeting complete. Agent is now fully operational.")
126
-
127
- # Await the main session task
128
- await task
129
 
130
  except Exception as e:
131
  print(f"FATAL ERROR in agent session: {e}")
132
  print(traceback.format_exc())
133
  finally:
134
  print(f"DEBUG: Agent session for room {room_name} is ending.")
 
 
135
  await room.disconnect()
136
 
137
 
 
81
  async def run_agent_session(room_name: str, agent_token: str):
82
  livekit_url = os.getenv("LIVEKIT_URL")
83
  room = Room()
84
+ session = None
85
 
86
  try:
87
  print(f"DEBUG: 1. Connecting to LiveKit room '{room_name}'...")
88
  await room.connect(livekit_url, agent_token)
89
  print("DEBUG: 2. Connection successful.")
90
 
91
+ # Create HTTP session that will persist for the entire session
92
+ session = aiohttp.ClientSession()
93
+
94
+ # 1. Instantiate all plugins
95
+ vad = VAD.load(min_speech_duration=0.1, min_silence_duration=0.5)
96
+ tts = TTS(
97
+ voice=VoiceByName(
98
+ name="Male English Actor",
99
+ provider=VoiceProvider.hume,
100
+ ),
101
+ instant_mode=True,
102
+ http_session=session,
103
+ )
104
+ stt = StreamAdapter(stt=STT(model="whisper-large-v3-turbo"), vad=vad)
105
+ google_llm = GoogleLLM(model="gemini-2.5-flash")
106
+ llm_wrapper = LLMStateWrapper(llm=google_llm, room=room)
107
+
108
+ # 2. Create the AgentSession
109
+ agent_session = AgentSession(
110
+ stt=stt,
111
+ llm=llm_wrapper,
112
+ tts=tts,
113
+ vad=vad,
114
+ )
115
+
116
+ # 3. Create an instance of your agent
117
+ agent = VoiceAssistant()
118
+
119
+ # 4. Start the session
120
+ print("DEBUG: Starting AgentSession...")
121
+ await send_agent_state(room, "speaking")
122
+ await agent_session.start(agent=agent, room=room)
123
+ await agent_session.generate_reply(instructions=GREETING_INSTRUCTIONS)
124
+ print("DEBUG: Initial greeting complete. Agent is now fully operational.")
 
 
 
 
 
125
 
126
  except Exception as e:
127
  print(f"FATAL ERROR in agent session: {e}")
128
  print(traceback.format_exc())
129
  finally:
130
  print(f"DEBUG: Agent session for room {room_name} is ending.")
131
+ if session:
132
+ await session.close()
133
  await room.disconnect()
134
 
135