Spaces:
Build error
Build error
Commit
·
2cba419
1
Parent(s):
4e316af
Realtime Flow
Browse files- src/agent_session/main.py +36 -27
src/agent_session/main.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Agent Session for Avurna Flow (Final Corrected Version with HTTP Session Management)
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
import traceback
|
| 8 |
-
import aiohttp
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
from fastapi import FastAPI, BackgroundTasks, Request
|
| 11 |
from pydantic import BaseModel
|
|
@@ -24,14 +24,11 @@ from livekit.plugins.silero import VAD
|
|
| 24 |
from src.agent_session.constants import SYSTEM_PROMPT, GREETING_INSTRUCTIONS
|
| 25 |
from src.utils import validate_env_vars
|
| 26 |
|
| 27 |
-
# 2. Create a lifespan manager for the http session
|
| 28 |
@asynccontextmanager
|
| 29 |
async def lifespan(app: FastAPI):
|
| 30 |
-
# Create a single, shared session for the entire application
|
| 31 |
async with aiohttp.ClientSession() as session:
|
| 32 |
app.state.http_session = session
|
| 33 |
yield
|
| 34 |
-
# The session is automatically closed when the app shuts down
|
| 35 |
|
| 36 |
app = FastAPI(lifespan=lifespan)
|
| 37 |
origins = ["*"]
|
|
@@ -47,16 +44,30 @@ class VoiceAssistant(Agent):
|
|
| 47 |
|
| 48 |
async def send_agent_state(room: Room, state: str):
|
| 49 |
# This function is correct
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
class LLMStateWrapper(LLM):
|
| 54 |
# This class is correct
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
-
# 3. Modify run_agent_session to accept the http_session
|
| 60 |
async def run_agent_session(room_name: str, agent_token: str, http_session: aiohttp.ClientSession):
|
| 61 |
livekit_url = os.getenv("LIVEKIT_URL")
|
| 62 |
room = Room()
|
|
@@ -69,25 +80,23 @@ async def run_agent_session(room_name: str, agent_token: str, http_session: aioh
|
|
| 69 |
await send_agent_state(room, "listening")
|
| 70 |
|
| 71 |
print("DEBUG: 3. Initializing plugins with shared http_session...")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
session=http_session
|
| 86 |
-
)
|
| 87 |
print("DEBUG: 4. Plugins initialized.")
|
| 88 |
|
| 89 |
print("DEBUG: 5. Creating AgentSession...")
|
| 90 |
-
session = AgentSession(vad=vad, stt=stt, llm=llm_wrapper, tts=tts)
|
| 91 |
print("DEBUG: 6. AgentSession created.")
|
| 92 |
|
| 93 |
print("DEBUG: 7. Starting session...")
|
|
@@ -106,7 +115,7 @@ async def run_agent_session(room_name: str, agent_token: str, http_session: aioh
|
|
| 106 |
print(f"DEBUG: Agent session for room {room_name} is ending. Cleaning up.")
|
| 107 |
await room.disconnect()
|
| 108 |
|
| 109 |
-
|
| 110 |
@app.post("/join-room")
|
| 111 |
async def join_room(req: JoinRoomRequest, request: Request, background_tasks: BackgroundTasks):
|
| 112 |
http_session = request.app.state.http_session
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Agent Session for Avurna Flow (Final Corrected Version with TARGETED HTTP Session Management)
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
import traceback
|
| 8 |
+
import aiohttp
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
from fastapi import FastAPI, BackgroundTasks, Request
|
| 11 |
from pydantic import BaseModel
|
|
|
|
| 24 |
from src.agent_session.constants import SYSTEM_PROMPT, GREETING_INSTRUCTIONS
|
| 25 |
from src.utils import validate_env_vars
|
| 26 |
|
|
|
|
| 27 |
@asynccontextmanager
|
| 28 |
async def lifespan(app: FastAPI):
|
|
|
|
| 29 |
async with aiohttp.ClientSession() as session:
|
| 30 |
app.state.http_session = session
|
| 31 |
yield
|
|
|
|
| 32 |
|
| 33 |
app = FastAPI(lifespan=lifespan)
|
| 34 |
origins = ["*"]
|
|
|
|
| 44 |
|
| 45 |
async def send_agent_state(room: Room, state: str):
|
| 46 |
# This function is correct
|
| 47 |
+
try:
|
| 48 |
+
msg = json.dumps({"type": "agent_state", "state": state})
|
| 49 |
+
await room.local_participant.publish_data(msg)
|
| 50 |
+
print(f"DEBUG: Sent agent state: {state}")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"DEBUG: Error publishing agent state: {e}")
|
| 53 |
|
| 54 |
class LLMStateWrapper(LLM):
|
| 55 |
# This class is correct
|
| 56 |
+
def __init__(self, llm: LLM, room: Room):
|
| 57 |
+
super().__init__()
|
| 58 |
+
self._llm = llm
|
| 59 |
+
self._room = room
|
| 60 |
+
|
| 61 |
+
@asynccontextmanager
|
| 62 |
+
async def chat(self, **kwargs):
|
| 63 |
+
await send_agent_state(self._room, "thinking")
|
| 64 |
+
try:
|
| 65 |
+
async with self._llm.chat(**kwargs) as stream:
|
| 66 |
+
yield stream
|
| 67 |
+
finally:
|
| 68 |
+
await send_agent_state(self._room, "listening")
|
| 69 |
|
| 70 |
|
|
|
|
| 71 |
async def run_agent_session(room_name: str, agent_token: str, http_session: aiohttp.ClientSession):
|
| 72 |
livekit_url = os.getenv("LIVEKIT_URL")
|
| 73 |
room = Room()
|
|
|
|
| 80 |
await send_agent_state(room, "listening")
|
| 81 |
|
| 82 |
print("DEBUG: 3. Initializing plugins with shared http_session...")
|
| 83 |
+
|
| 84 |
+
# --- THE DEFINITIVE FIX: Apply the session ONLY where needed ---
|
| 85 |
+
|
| 86 |
+
# Google LLM does NOT take a session argument.
|
| 87 |
+
google_llm = GoogleLLM(model="gemini-1.5-flash")
|
| 88 |
+
|
| 89 |
+
# Hume TTS and Groq STT DO require the session.
|
| 90 |
+
tts = TTS(voice=VoiceByName(name="Tiktok Fashion Influencer"), instant_mode=True, session=http_session)
|
| 91 |
+
stt = StreamAdapter(stt=STT(model="whisper-large-v3-turbo", session=http_session), vad=VAD.load(min_speech_duration=0.1))
|
| 92 |
+
|
| 93 |
+
# Wrap the correctly initialized Google LLM
|
| 94 |
+
llm_wrapper = LLMStateWrapper(llm=google_llm, room=room)
|
| 95 |
+
|
|
|
|
|
|
|
| 96 |
print("DEBUG: 4. Plugins initialized.")
|
| 97 |
|
| 98 |
print("DEBUG: 5. Creating AgentSession...")
|
| 99 |
+
session = AgentSession(vad=stt.vad, stt=stt, llm=llm_wrapper, tts=tts)
|
| 100 |
print("DEBUG: 6. AgentSession created.")
|
| 101 |
|
| 102 |
print("DEBUG: 7. Starting session...")
|
|
|
|
| 115 |
print(f"DEBUG: Agent session for room {room_name} is ending. Cleaning up.")
|
| 116 |
await room.disconnect()
|
| 117 |
|
| 118 |
+
|
| 119 |
@app.post("/join-room")
|
| 120 |
async def join_room(req: JoinRoomRequest, request: Request, background_tasks: BackgroundTasks):
|
| 121 |
http_session = request.app.state.http_session
|