Offline Speech Recognition with AI Voice Agents

Build an AI Voice Agent for offline speech recognition with VideoSDK. Follow this step-by-step guide with complete code examples.

Introduction to AI Voice Agents in Offline Speech Recognition

What is an AI

Voice Agent

?

An AI

Voice Agent

is a software application designed to interact with users through spoken language. It processes voice commands and provides responses, simulating a conversation with a human. These agents use technologies like Speech-to-Text (STT), Text-to-Speech (TTS), and Language Models (LLM) to understand and generate human-like dialogue.

Why are they important for the offline speech recognition industry?

AI Voice Agents are crucial in offline speech recognition as they enable voice interactions without requiring an internet connection. This is particularly useful in scenarios where privacy is a concern, or internet access is unreliable. Offline agents can perform tasks such as opening applications, setting reminders, and controlling device settings, all while maintaining user privacy.

Core Components of a

Voice Agent

  • Speech-to-Text (STT): Converts spoken language into text.
  • Text-to-Speech (TTS): Converts text back into spoken language.
  • Language Model (LLM): Understands and generates text-based responses.

What You'll Build in This Tutorial

In this tutorial, you will build an AI

Voice Agent

capable of recognizing and processing spoken commands offline. We will guide you through setting up the environment, building the agent, and testing it using the VideoSDK framework.

Architecture and Core Concepts

High-Level Architecture Overview

The AI

Voice Agent

processes user speech through a series of steps: capturing audio, converting it to text, generating a response, and converting the response back to speech. This flow is managed by a

cascading pipeline

that integrates various plugins for STT, LLM, TTS, and more.
Diagram

Understanding Key Concepts in the VideoSDK Framework

  • Agent: Represents your AI bot, handling interactions and managing state.
  • CascadingPipeline: Manages the flow of data through the system, integrating STT, LLM, and TTS plugins.
  • VAD & TurnDetector: Voice

    Activity Detection

    (VAD) identifies when the user is speaking, while the

    Turn Detector for AI voice Agents

    manages conversational turns.

Setting Up the Development Environment

Prerequisites

Before you begin, ensure you have Python 3.11+ installed and a VideoSDK account. You can sign up at app.videosdk.live.

Step 1: Create a Virtual Environment

1python -m venv venv
2source venv/bin/activate  # On Windows use `venv\Scripts\activate`
3

Step 2: Install Required Packages

Install the necessary packages using pip:
1pip install videosdk
2pip install python-dotenv
3

Step 3: Configure API Keys in a .env file

Create a .env file in your project directory and add your VideoSDK API key:
1VIDEOSDK_API_KEY=your_api_key_here
2

Building the AI Voice Agent: A Step-by-Step Guide

First, let's present the complete, runnable code for the AI Voice Agent:
1import asyncio, os
2from videosdk.agents import Agent, AgentSession, CascadingPipeline, JobContext, RoomOptions, WorkerJob, ConversationFlow
3from videosdk.plugins.silero import SileroVAD
4from videosdk.plugins.turn_detector import TurnDetector, pre_download_model
5from videosdk.plugins.deepgram import DeepgramSTT
6from videosdk.plugins.openai import OpenAILLM
7from videosdk.plugins.elevenlabs import ElevenLabsTTS
8from typing import AsyncIterator
9
10# Pre-downloading the Turn Detector model
11pre_download_model()
12
13agent_instructions = "You are an AI Voice Agent specializing in offline speech recognition. Your persona is that of a tech-savvy assistant who helps users interact with their devices without needing an internet connection. Your primary capabilities include recognizing and processing spoken commands to perform tasks such as opening applications, setting reminders, and controlling device settings. You can also provide information about offline speech recognition technology and its benefits. However, you are limited to offline functionalities and cannot access or retrieve information from the internet. You must inform users that for tasks requiring internet access, they should connect to a network. Additionally, you should remind users that while you strive for accuracy, offline speech recognition may have limitations compared to online services."
14
15class MyVoiceAgent(Agent):
16    def __init__(self):
17        super().__init__(instructions=agent_instructions)
18    async def on_enter(self): await self.session.say("Hello! How can I help?")
19    async def on_exit(self): await self.session.say("Goodbye!")
20
21async def start_session(context: JobContext):
22    # Create agent and conversation flow
23    agent = MyVoiceAgent()
24    conversation_flow = ConversationFlow(agent)
25
26    # Create pipeline
27    pipeline = CascadingPipeline(
28        stt=DeepgramSTT(model="nova-2", language="en"),
29        llm=OpenAILLM(model="gpt-4o"),
30        tts=ElevenLabsTTS(model="eleven_flash_v2_5"),
31        vad=SileroVAD(threshold=0.35),
32        turn_detector=TurnDetector(threshold=0.8)
33    )
34
35    session = AgentSession(
36        agent=agent,
37        pipeline=pipeline,
38        conversation_flow=conversation_flow
39    )
40
41    try:
42        await context.connect()
43        await session.start()
44        # Keep the session running until manually terminated
45        await asyncio.Event().wait()
46    finally:
47        # Clean up resources when done
48        await session.close()
49        await context.shutdown()
50
51def make_context() -> JobContext:
52    room_options = RoomOptions(
53    #  room_id="YOUR_MEETING_ID",  # Set to join a pre-created room; omit to auto-create
54        name="VideoSDK Cascaded Agent",
55        playground=True
56    )
57
58    return JobContext(room_options=room_options)
59
60if __name__ == "__main__":
61    job = WorkerJob(entrypoint=start_session, jobctx=make_context)
62    job.start()
63

Step 4.1: Generating a VideoSDK Meeting ID

To generate a meeting ID, use the following curl command:
1curl -X POST "https://api.videosdk.live/v1/meetings" \
2-H "Authorization: Bearer YOUR_API_KEY" \
3-H "Content-Type: application/json"
4

Step 4.2: Creating the Custom Agent Class

The MyVoiceAgent class extends the Agent class from the VideoSDK framework. It defines the agent's behavior on entering and exiting a conversation. The on_enter and on_exit methods use the agent's session to communicate with users.

Step 4.3: Defining the Core Pipeline

The CascadingPipeline integrates various plugins for processing audio and generating responses. It includes:
  • DeepgramSTT: Converts speech to text using the "nova-2" model.
  • OpenAILLM: Generates text responses using the "gpt-4o" model.
  • ElevenLabsTTS: Converts text back to speech using the "elevenflashv2_5" model.
  • SileroVAD: Detects voice activity with a threshold of 0.35.
  • TurnDetector: Manages conversational turns with a threshold of 0.8.

Step 4.4: Managing the Session and Startup Logic

The start_session function initializes the agent, pipeline, and conversation flow. It connects to the VideoSDK service and starts the session. The make_context function sets up the room options, enabling the

AI Agent playground

mode for testing. The main block starts the job, running the agent in an event loop.

Running and Testing the Agent

Step 5.1: Running the Python Script

Execute the script using Python:
1python main.py
2

Step 5.2: Interacting with the Agent in the Playground

Once the script is running, you will receive a playground link in the console. Open this link in a browser to interact with your AI Voice Agent. You can test various commands and see how the agent responds.

Advanced Features and Customizations

Extending Functionality with Custom Tools

You can extend the agent's functionality by adding custom tools. This involves creating new plugins or integrating additional APIs to enhance the agent's capabilities.

Exploring Other Plugins

The VideoSDK framework supports various plugins for STT, LLM, and TTS. Explore options like Cartesia for STT, Google Gemini for LLM, and others to customize your agent further.

Troubleshooting Common Issues

API Key and Authentication Errors

Ensure your API key is correctly set in the .env file and that it has the necessary permissions.

Audio Input/Output Problems

Check your microphone and speaker settings. Ensure that the correct devices are selected and functioning properly.

Dependency and Version Conflicts

Ensure all dependencies are installed with compatible versions. Use a virtual environment to manage package versions effectively.

Conclusion

Summary of What You've Built

You have successfully built an AI Voice Agent capable of offline speech recognition using the VideoSDK framework. This agent can process spoken commands and interact with users without needing an internet connection.

Next Steps and Further Learning

Explore more advanced features of the VideoSDK framework, such as integrating additional plugins or creating more complex conversation flows. Consider learning about other AI technologies to enhance your agent's capabilities.

Start Building With Free $20 Balance

No credit card required to start.

Want to level-up your learning? Subscribe now

Subscribe to our newsletter for more tech based insights

FAQ