Files
livekit_primeras_pruebas/prueba_agente_tts.py
T

58 lines
1.6 KiB
Python

# prueba_agente_tts.py
import os
import asyncio
from livekit.agents import WorkerOptions, Worker, JobContext, AgentSession, Agent
from livekit.plugins import openai
LIVEKIT_URL = "ws://192.168.1.131:7880"
LIVEKIT_API_KEY = "devkey"
LIVEKIT_API_SECRET = "secret"
ROOM_NAME = "mi_sala_prueba"
PARTICIPANT_IDENTITY = "agente_tts"
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "sk-...tu_clave...")
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
async def entrypoint(ctx: JobContext):
print("🎙️ Conectando agente al room LiveKit...")
await ctx.connect()
print(f"✅ Conectado a la sala: {ctx.room.name}")
# Crear el modelo TTS
tts_model = openai.realtime.RealtimeModel(
model="gpt-4o-realtime-preview-2024-12-17",
voice="alloy",
)
# Crear agente
agent = Agent(instructions="Eres un agente TTS de prueba con voz de OpenAI.")
session = AgentSession(agent=agent, llm=tts_model)
await session.start(room=ctx.room)
print("🗣️ Agente hablando...")
await session.generate_reply(text="Hola, soy el agente TTS de LiveKit.")
await asyncio.sleep(5)
print("👋 Cerrando sesión del agente.")
await session.close()
async def main():
opts = WorkerOptions(
entrypoint_fnc=entrypoint,
api_key=LIVEKIT_API_KEY,
api_secret=LIVEKIT_API_SECRET,
ws_url=LIVEKIT_URL,
)
worker = Worker(opts)
# 🔥 Esta es la línea correcta para unir al agente al room directamente
await worker.run_in_room(
room_name=ROOM_NAME,
participant_identity=PARTICIPANT_IDENTITY,
)
if __name__ == "__main__":
asyncio.run(main())