diff --git a/integrations-examples/livekit-agent/main.py b/integrations-examples/livekit-agent/main.py index 4487962..a9fad25 100644 --- a/integrations-examples/livekit-agent/main.py +++ b/integrations-examples/livekit-agent/main.py @@ -1,62 +1,36 @@ -import logging +from dotenv import load_dotenv import os -from livekit.agents import ( - Agent, - AgentSession, - AutoSubscribe, - JobContext, - MetricsCollectedEvent, - RoomOutputOptions, - StopResponse, - WorkerOptions, - cli, - llm, - metrics, +from livekit import agents +from livekit.agents import AgentSession, Agent, RoomOutputOptions +from livekit.plugins import ( + gladia, ) -from livekit.plugins.gladia.stt import STT as GladiaSTT - -logger = logging.getLogger("transcriber") - -class Transcriber(Agent): - def __init__(self): - api_key = os.getenv("GLADIA_API_KEY") - if not api_key: - raise RuntimeError( - "GLADIA_API_KEY is not set. Please create a .env from .env.example and set it, or export it in your shell." - ) - super().__init__( - stt=GladiaSTT( - api_key=api_key, - ), - ) - - async def on_user_turn_completed(self, chat_ctx: llm.ChatContext, new_message: llm.ChatMessage): - user_transcript = new_message.text_content - logger.info(f" -> {user_transcript}") +load_dotenv(".env") - raise StopResponse() - -async def entrypoint(ctx: JobContext): - logger.info(f"Starting transcriber (speech to text) in room: {ctx.room.name}") - await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) +class Assistant(Agent): + def __init__(self) -> None: + super().__init__(instructions="") # Agent instructions of your choice - session = AgentSession() - @session.on("metrics_collected") - def on_metrics_collected(ev: MetricsCollectedEvent): - metrics.log_metrics(ev.metrics) +async def entrypoint(ctx: agents.JobContext): + session = AgentSession( + stt=gladia.STT( + api_key=os.getenv('GLADIA_API_KEY'), + interim_results=True, + ), + ) await session.start( - agent=Transcriber(), room=ctx.room, + agent=Assistant(), room_output_options=RoomOutputOptions( transcription_enabled=True, - # disable audio output if it's not needed - audio_enabled=False, - ), + audio_enabled=False + ) ) + if __name__ == "__main__": - cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint)) + agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint)) \ No newline at end of file diff --git a/integrations-examples/livekit-agent/pyproject.toml b/integrations-examples/livekit-agent/pyproject.toml index 6ba66b0..feb1e14 100644 --- a/integrations-examples/livekit-agent/pyproject.toml +++ b/integrations-examples/livekit-agent/pyproject.toml @@ -6,4 +6,5 @@ readme = "README.md" requires-python = ">=3.13" dependencies = [ "livekit-agents[gladia]>=1.2.6", + "python-dotenv>=1.1.1", ] diff --git a/integrations-examples/livekit-agent/uv.lock b/integrations-examples/livekit-agent/uv.lock index 4aedb4c..5fe4191 100644 --- a/integrations-examples/livekit-agent/uv.lock +++ b/integrations-examples/livekit-agent/uv.lock @@ -406,10 +406,14 @@ version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "livekit-agents", extra = ["gladia"] }, + { name = "python-dotenv" }, ] [package.metadata] -requires-dist = [{ name = "livekit-agents", extras = ["gladia"], specifier = ">=1.2.6" }] +requires-dist = [ + { name = "livekit-agents", extras = ["gladia"], specifier = ">=1.2.6" }, + { name = "python-dotenv", specifier = ">=1.1.1" }, +] [[package]] name = "livekit-plugins-gladia" @@ -797,6 +801,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/61/ad/689f02752eeec26aed679477e80e632ef1b682313be70793d798c1d5fc8f/PyJWT-2.10.1-py3-none-any.whl", hash = "sha256:dcdd193e30abefd5debf142f9adfcdd2b58004e644f25406ffaebd50bd98dacb", size = 22997, upload-time = "2024-11-28T03:43:27.893Z" }, ] +[[package]] +name = "python-dotenv" +version = "1.1.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f6/b0/4bc07ccd3572a2f9df7e6782f52b0c6c90dcbb803ac4a167702d7d0dfe1e/python_dotenv-1.1.1.tar.gz", hash = "sha256:a8a6399716257f45be6a007360200409fce5cda2661e3dec71d23dc15f6189ab", size = 41978, upload-time = "2025-06-24T04:21:07.341Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5f/ed/539768cf28c661b5b068d66d96a2f155c4971a5d55684a514c1a0e0dec2f/python_dotenv-1.1.1-py3-none-any.whl", hash = "sha256:31f23644fe2602f88ff55e1f5c79ba497e01224ee7737937930c448e4d0e24dc", size = 20556, upload-time = "2025-06-24T04:21:06.073Z" }, +] + [[package]] name = "requests" version = "2.32.5"