Building AI Voice Agents with LLMs

Step-by-step guide to building AI Voice Agents using LLMs for conversational AI with VideoSDK.

Introduction to AI Voice Agents in LLMs for Conversational AI

AI Voice Agents are transforming the way we interact with technology, enabling seamless communication through natural language processing. These agents are particularly significant in the realm of Large Language Models (LLMs) for conversational AI, where they facilitate dynamic interactions across various applications.

What is an AI Voice Agent?

An AI Voice Agent is a software entity capable of understanding and responding to human speech. It leverages advanced technologies like speech-to-text (STT), language models (LLM), and text-to-speech (TTS) to process and generate human-like responses. For a comprehensive understanding of the

AI voice Agent core components overview

, you can explore detailed documentation.

Why are they important for the LLMs for conversational AI industry?

AI Voice Agents play a crucial role in industries such as customer service, healthcare, and education by providing efficient and scalable solutions for handling inquiries, offering assistance, and enhancing user experiences.

Core Components of a Voice Agent

  • STT (Speech-to-Text): Converts spoken language into text.
  • LLM (Large Language Model): Processes the text to understand and generate responses.
  • TTS (Text-to-Speech): Converts the generated text back into spoken language.

What You'll Build in This Tutorial

In this tutorial, we will guide you through building an AI Voice Agent using the VideoSDK framework, integrating components such as

Deepgram STT Plugin for voice agent

,

OpenAI LLM Plugin for voice agent

, and

ElevenLabs TTS Plugin for voice agent

.

Architecture and Core Concepts

High-Level Architecture Overview

The architecture of an AI Voice Agent involves a seamless flow of data from user speech to agent response. The process begins with capturing the user's voice, converting it to text, processing it through an LLM, and finally generating a spoken response.
1sequenceDiagram
2    participant User
3    participant Agent
4    participant STT
5    participant LLM
6    participant TTS
7    User->>Agent: Speak
8    Agent->>STT: Convert Speech to Text
9    STT-->>Agent: Text
10    Agent->>LLM: Process Text
11    LLM-->>Agent: Response
12    Agent->>TTS: Convert Text to Speech
13    TTS-->>Agent: Audio
14    Agent->>User: Speak Response
15

Understanding Key Concepts in the VideoSDK Framework

  • Agent: The core class representing your bot, responsible for managing interactions.
  • CascadingPipeline: Manages the flow of audio processing, integrating STT, LLM, and TTS. Learn more about the

    Cascading pipeline in AI voice Agents

    .
  • VAD & TurnDetector: These components help the agent determine when to listen and when to speak, ensuring smooth interactions. The

    Turn detector for AI voice Agents

    is crucial for this functionality.

Setting Up the Development Environment

Prerequisites

  • Python 3.11+
  • VideoSDK Account: Sign up at app.videosdk.live to access necessary API keys.

Step 1: Create a Virtual Environment

Create a virtual environment to manage dependencies separately.
1python3 -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 to securely store your API keys.
1VIDEOSDK_API_KEY=your_videosdk_api_key
2DEEPGRAM_API_KEY=your_deepgram_api_key
3OPENAI_API_KEY=your_openai_api_key
4ELEVENLABS_API_KEY=your_elevenlabs_api_key
5

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

Here is the complete, runnable code for building your 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 = "{
14  \"persona\": \"Conversational AI Expert\",
15  \"capabilities\": [
16    \"Provide detailed explanations about Large Language Models (LLMs) and their applications in conversational AI.\",
17    \"Assist users in understanding how LLMs can be integrated into various conversational AI systems.\",
18    \"Offer guidance on best practices for deploying LLMs in customer service, healthcare, and other industries.\",
19    \"Answer questions related to the technical aspects of LLMs, such as model training, fine-tuning, and deployment.\"
20  ],
21  \"constraints\": [
22    \"You are not a certified AI researcher and should not provide in-depth technical analysis beyond general guidance.\",
23    \"Always include a disclaimer that users should consult with AI specialists for specific implementation advice.\",
24    \"Avoid making definitive claims about the future capabilities of LLMs, as the field is rapidly evolving.\"
25  ]
26}"
27
28class MyVoiceAgent(Agent):
29    def __init__(self):
30        super().__init__(instructions=agent_instructions)
31    async def on_enter(self): await self.session.say("Hello! How can I help?")
32    async def on_exit(self): await self.session.say("Goodbye!")
33
34async def start_session(context: JobContext):
35    # Create agent and conversation flow
36    agent = MyVoiceAgent()
37    conversation_flow = ConversationFlow(agent)
38
39    # Create pipeline
40    pipeline = CascadingPipeline(
41        stt=DeepgramSTT(model="nova-2", language="en"),
42        llm=OpenAILLM(model="gpt-4o"),
43        tts=ElevenLabsTTS(model="eleven_flash_v2_5"),
44        vad=SileroVAD(threshold=0.35),
45        turn_detector=TurnDetector(threshold=0.8)
46    )
47
48    session = AgentSession(
49        agent=agent,
50        pipeline=pipeline,
51        conversation_flow=conversation_flow
52    )
53
54    try:
55        await context.connect()
56        await session.start()
57        # Keep the session running until manually terminated
58        await asyncio.Event().wait()
59    finally:
60        # Clean up resources when done
61        await session.close()
62        await context.shutdown()
63
64def make_context() -> JobContext:
65    room_options = RoomOptions(
66    #  room_id="YOUR_MEETING_ID",  # Set to join a pre-created room; omit to auto-create
67        name="VideoSDK Cascaded Agent",
68        playground=True
69    )
70
71    return JobContext(room_options=room_options)
72
73if __name__ == "__main__":
74    job = WorkerJob(entrypoint=start_session, jobctx=make_context)
75    job.start()
76

Step 4.1: Generating a VideoSDK Meeting ID

To generate a meeting ID, use the following curl command or equivalent API call.
1curl -X POST "https://api.videosdk.live/v1/rooms" \\
2-H "Authorization: Bearer YOUR_VIDEOSDK_API_KEY"
3

Step 4.2: Creating the Custom Agent Class

The MyVoiceAgent class extends the base Agent class, defining custom behavior for entering and exiting a session.
1class MyVoiceAgent(Agent):
2    def __init__(self):
3        super().__init__(instructions=agent_instructions)
4    async def on_enter(self): await self.session.say("Hello! How can I help?")
5    async def on_exit(self): await self.session.say("Goodbye!")
6

Step 4.3: Defining the Core Pipeline

The CascadingPipeline integrates the STT, LLM, and TTS components, ensuring a smooth flow of data.
1pipeline = CascadingPipeline(
2    stt=DeepgramSTT(model="nova-2", language="en"),
3    llm=OpenAILLM(model="gpt-4o"),
4    tts=ElevenLabsTTS(model="eleven_flash_v2_5"),
5    vad=SileroVAD(threshold=0.35),
6    turn_detector=TurnDetector(threshold=0.8)
7)
8

Step 4.4: Managing the Session and Startup Logic

The session management involves setting up the context and starting the agent.
1async def start_session(context: JobContext):
2    agent = MyVoiceAgent()
3    conversation_flow = ConversationFlow(agent)
4    pipeline = CascadingPipeline(...)
5    session = AgentSession(agent=agent, pipeline=pipeline, conversation_flow=conversation_flow)
6    try:
7        await context.connect()
8        await session.start()
9        await asyncio.Event().wait()
10    finally:
11        await session.close()
12        await context.shutdown()
13
14def make_context() -> JobContext:
15    room_options = RoomOptions(name="VideoSDK Cascaded Agent", playground=True)
16    return JobContext(room_options=room_options)
17
18if __name__ == "__main__":
19    job = WorkerJob(entrypoint=start_session, jobctx=make_context)
20    job.start()
21

Running and Testing the Agent

Step 5.1: Running the Python Script

To run your agent, execute the script using Python.
1python main.py
2

Step 5.2: Interacting with the Agent in the Playground

Once the script is running, use the

AI Agent playground

URL provided in the console to interact with your agent.

Advanced Features and Customizations

Extending Functionality with Custom Tools

The VideoSDK framework allows for the addition of custom tools to extend your agent's capabilities.

Exploring Other Plugins

Consider exploring other plugins for STT, LLM, and TTS to tailor your agent's performance to specific needs.

Troubleshooting Common Issues

API Key and Authentication Errors

Ensure your API keys are correctly configured in the .env file to avoid authentication issues.

Audio Input/Output Problems

Verify that your microphone and speaker settings are correctly configured and that the necessary permissions are granted.

Dependency and Version Conflicts

Use a virtual environment to manage dependencies and avoid conflicts with other Python packages.

Conclusion

Summary of What You've Built

You've successfully built an AI Voice Agent using LLMs, capable of understanding and responding to user queries. For a quick setup, refer to the

Voice Agent Quick Start Guide

.

Next Steps and Further Learning

Explore further by integrating additional features or experimenting with different plugins 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