Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 21 additions & 47 deletions integrations-examples/livekit-agent/main.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions integrations-examples/livekit-agent/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"livekit-agents[gladia]>=1.2.6",
"python-dotenv>=1.1.1",
]
15 changes: 14 additions & 1 deletion integrations-examples/livekit-agent/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.