Spaces:
Build error
Build error
Commit
·
51a1cba
1
Parent(s):
c61d9e2
Realtime Flow
Browse files- 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
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 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 |
|