### Running the Speech-to-Text Agent (Bash) Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt Instructions for installing dependencies and running the agent using Python. It details the necessary packages and the command to start the agent, along with an example output. ```bash # Install dependencies pip install -r requirements.txt # Requirements include: # livekit-agents~=1.0 # livekit-plugins-assemblyai~=1.0 # python-dotenv # Start the agent (connects to LiveKit and waits for room assignments) python stt_agent.py # Output example: # INFO:transcriber:starting transcriber (speech to text) example, room: demo-room-abc123 # INFO:transcriber: -> Hello, how are you today? # INFO:transcriber: -> I'm testing the speech to text transcription. # INFO:transcriber: -> This works really well for real-time applications. # The agent will automatically: # 1. Connect to your LiveKit server # 2. Wait for room assignments # 3. Subscribe to audio streams when participants join # 4. Send audio to AssemblyAI for transcription # 5. Log transcripts and optionally send them back to the room ``` -------------------------------- ### Initialize LiveKit Agent and Connect to Room for Transcription (Python) Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt The `entrypoint` function initializes the LiveKit agent session and connects to a room for transcription. It sets up automatic audio subscription and enables transcription output back to the room. ```python import logging from dotenv import load_dotenv from livekit.agents import ( Agent, AgentSession, AutoSubscribe, JobContext, RoomOutputOptions, WorkerOptions, cli, ) from livekit.plugins import assemblyai load_dotenv() logger = logging.getLogger("transcriber") async def entrypoint(ctx: JobContext): """Main entry point for the LiveKit agent""" logger.info(f"starting transcriber (speech to text) example, room: {ctx.room.name}") # Connect to room and subscribe to audio streams only await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) session = AgentSession() # Start the agent session with transcription enabled await session.start( agent=Transcriber(), room=ctx.room, room_output_options=RoomOutputOptions( transcription_enabled=True, # Send transcripts back to room audio_enabled=False, # Don't generate audio responses ), ) if __name__ == "__main__": cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint)) ``` -------------------------------- ### Testing the Agent with LiveKit Agents Playground (Bash) Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt Steps to integrate and test the transcription agent using LiveKit's hosted playground. This involves running the agent locally and then accessing the playground URL. ```bash # 1. Start your agent locally python stt_agent.py # 2. Navigate to https://agents-playground.livekit.io/ # 3. Sign in with your LiveKit account credentials ``` -------------------------------- ### Production Deployment Configuration for Transcription Agent Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt Configures an agent for production deployment using LiveKit's worker options. It sets up structured logging, defines an entrypoint function for agent execution, and specifies worker configurations for scalability and monitoring. Dependencies include `logging`, `os`, and `livekit.agents`. ```python # production_agent.py import logging import os from livekit.agents import WorkerOptions, cli # Configure structured logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler('transcriber.log'), logging.StreamHandler() ] ) async def entrypoint(ctx: JobContext): logger = logging.getLogger("transcriber") logger.info(f"Room: {ctx.room.name}, Participants: {len(ctx.room.participants)}") await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) session = AgentSession() await session.start( agent=Transcriber(), room=ctx.room, room_output_options=RoomOutputOptions( transcription_enabled=True, audio_enabled=False, ), ) if __name__ == "__main__": # Production worker configuration worker_options = WorkerOptions( entrypoint_fnc=entrypoint, num_workers=int(os.getenv("NUM_WORKERS", "4")), worker_type="transcription", ) cli.run_app(worker_options) # Docker deployment: # FROM python:3.11-slim # WORKDIR /app # COPY requirements.txt . # RUN pip install -r requirements.txt # COPY . . # CMD ["python", "production_agent.py"] # Environment variables for production: # NUM_WORKERS=8 # LIVEKIT_URL=wss://production.livekit.cloud # LIVEKIT_API_KEY=prod_key # LIVEKIT_API_SECRET=prod_secret # ASSEMBLYAI_API_KEY=prod_assemblyai_key ``` -------------------------------- ### Environment Configuration for LiveKit and AssemblyAI (.env) Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt This snippet shows the `.env` file configuration for API credentials and connection settings required by LiveKit and AssemblyAI services. These variables are automatically loaded by `python-dotenv`. ```bash # .env file configuration LIVEKIT_URL=wss://your-project.livekit.cloud LIVEKIT_API_KEY=APIxxxxxxxxxxxxxxxxxx LIVEKIT_API_SECRET=secretxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ASSEMBLYAI_API_KEY=your_assemblyai_api_key_here # The agent automatically loads these credentials using python-dotenv # No manual configuration needed in code ``` -------------------------------- ### Custom Transcript Processing with AssemblyAI Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt Extends the `Agent` class to add custom logic to transcript processing. It includes buffering, saving to a database, sending to a webhook, and triggering actions based on keywords. Dependencies include `asyncio`, `json`, `datetime`, and the `livekit.agents` library. ```python import asyncio import json from datetime import datetime class CustomTranscriber(Agent): def __init__(self, webhook_url=None, db_client=None): super().__init__( instructions="not-needed", stt=assemblyai.STT(), ) self.webhook_url = webhook_url self.db_client = db_client self.transcript_buffer = [] async def on_user_turn_completed(self, chat_ctx: llm.ChatContext, new_message: llm.ChatMessage): """Process transcripts with custom logic""" transcript = new_message.text_content timestamp = datetime.utcnow().isoformat() # Log to console logger.info(f"[{timestamp}] -> {transcript}") # Buffer transcripts self.transcript_buffer.append({ "text": transcript, "timestamp": timestamp, "speaker": "user" }) # Save to database if self.db_client: await self.db_client.save_transcript(transcript, timestamp) # Send to webhook if self.webhook_url: await self._send_webhook(transcript, timestamp) # Trigger action based on keywords if "help" in transcript.lower(): logger.info("User requested help - triggering support notification") # await notify_support_team(transcript) raise StopResponse() async def _send_webhook(self, transcript, timestamp): """Send transcript to external webhook""" payload = json.dumps({ "event": "transcript_ready", "text": transcript, "timestamp": timestamp }) # await http_client.post(self.webhook_url, data=payload) # Usage: # agent = CustomTranscriber( # webhook_url="https://your-api.com/webhooks/transcripts", # db_client=your_database_client # ) ``` -------------------------------- ### Custom Transcriber Agent using AssemblyAI for Speech-to-Text (Python) Source: https://context7.com/oconnoob/realtime-stt-livekit-assemblyai/llms.txt The `Transcriber` class extends LiveKit's `Agent` to handle real-time transcription with AssemblyAI. It configures AssemblyAI as the STT provider and defines a callback for processing completed user turns. ```python from livekit.agents import Agent, StopResponse, llm from livekit.plugins import assemblyai class Transcriber(Agent): """Custom agent that transcribes speech using AssemblyAI""" def __init__(self): super().__init__( instructions="not-needed", # No LLM instructions needed for pure STT stt=assemblyai.STT(), # Configure AssemblyAI as STT provider ) async def on_user_turn_completed(self, chat_ctx: llm.ChatContext, new_message: llm.ChatMessage): """Called when user finishes speaking and transcript is ready""" # Extract the transcribed text user_transcript = new_message.text_content logger.info(f" -> {user_transcript}") # Process transcript here (e.g., save to database, trigger actions) # Example: await save_transcript_to_db(user_transcript) # Stop the conversational loop (we only want transcription, not responses) raise StopResponse() # Usage example: agent = Transcriber() # Agent automatically processes audio and calls on_user_turn_completed with transcripts ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.