Build AI Voice Agents for LLM Tool Use

Step-by-step guide to build AI Voice Agents for LLM tool use using VideoSDK.

Introduction to AI Voice Agents in Tool Use for LLMs

AI Voice Agents are sophisticated systems that interact with users through voice commands, leveraging technologies like Speech-to-Text (STT), Text-to-Speech (TTS), and Large Language Models (LLMs) to understand and respond to queries. These agents are crucial in the tool use for LLMs industry, providing seamless interfaces for users to interact with complex machine learning models.

What is an AI

Voice Agent

?

An AI

Voice Agent

is a software application designed to understand and respond to human speech. By converting spoken language into text, processing it with LLMs, and converting responses back into speech, these agents facilitate natural and intuitive user interactions.

Why are they important for the tool use for LLMs industry?

In the context of LLMs, AI Voice Agents simplify the interaction with complex models, allowing users to query, manipulate, and receive insights from these models without needing deep technical expertise. They are used in customer service, virtual assistants, and educational tools to enhance user experience and accessibility.

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 text responses back into spoken language.
For a comprehensive understanding, refer to the

AI voice Agent core components overview

.

What You'll Build in This Tutorial

In this tutorial, you'll build an AI

Voice Agent

using the VideoSDK framework. This agent will guide users in understanding and utilizing various tools related to LLMs, leveraging plugins for STT, LLM, TTS, and more.

Architecture and Core Concepts

High-Level Architecture Overview

The AI

Voice Agent

processes user speech through a series of steps: capturing audio input, converting it to text, processing it with an LLM, and then generating a spoken response. This flow ensures a seamless interaction between the user and the agent.
Diagram

Understanding Key Concepts in the VideoSDK Framework

  • Agent: The core class representing your bot, responsible for managing interactions.
  • Cascading pipeline in AI voice Agents

    :
    The flow of audio processing from STT to LLM to TTS, ensuring smooth transitions between each stage.
  • VAD &

    Turn detector for AI voice Agents

    :
    These components help the agent determine when to listen and when to speak, enhancing the natural flow of conversation.

Setting Up the Development Environment

Prerequisites

To get started, ensure you have Python 3.11 or higher and a VideoSDK account. Sign up at the VideoSDK dashboard to obtain necessary API keys.

Step 1: Create a Virtual Environment

Create a virtual environment to manage dependencies:
1python -m venv venv
2source venv/bin/activate  # On Windows use `venv\Scripts\activate`
3

Step 2: Install Required Packages

Install the VideoSDK and other required packages:
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 keys:
1VIDEOSDK_API_KEY=your_api_key_here
2

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

Here is the complete code for building your AI Voice Agent:
1import asyncio, os
2from videosdk.agents import Agent, [AgentSession](https://docs.videosdk.live/ai_agents/core-components/agent-session), 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 a knowledgeable AI Voice Agent specializing in 'tool use for LLMs' (Large Language Models). Your primary role is to assist users in understanding and utilizing various tools and techniques related to LLMs effectively. \n\n**Persona:**\n- You are a friendly and approachable AI expert with a focus on educational support.\n\n**Capabilities:**\n- Provide detailed explanations of different tools used in conjunction with LLMs, such as tokenizers, embeddings, and transformers.\n- Guide users through the process of integrating these tools into their projects.\n- Offer best practices for optimizing LLM performance and efficiency.\n- Answer questions related to the latest advancements and updates in LLM technology.\n\n**Constraints and Limitations:**\n- You are not a substitute for professional software engineering advice and should always recommend consulting with a qualified engineer for complex implementations.\n- You must include a disclaimer that the information provided is for educational purposes only and may not cover all aspects of tool use for LLMs.\n- You should not provide any proprietary or confidential information.\n- You must refrain from making any guarantees about the performance or outcomes of using specific tools or techniques."
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/rooms" \
2-H "Authorization: Bearer YOUR_API_KEY" \
3-H "Content-Type: application/json" \
4-d '{"name":"My Meeting"}'
5

Step 4.2: Creating the Custom Agent Class

The MyVoiceAgent class inherits from the Agent class, defining the agent's behavior. It uses the agent_instructions to set the agent's persona and capabilities. The on_enter and on_exit methods manage greetings and farewells, enhancing user interaction.
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 various plugins to process audio input and output. Each plugin plays a specific role:
  • DeepgramSTT: Converts speech to text.
  • OpenAILLM: Processes the text and generates responses.
  • ElevenLabsTTS: Converts text responses back into speech.
  • SileroVAD & TurnDetector: Manage when the agent listens and responds.
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 start_session function initializes the agent, pipeline, and session. It connects to the VideoSDK context and starts the session, keeping it active until manually terminated.
1async def start_session(context: JobContext):
2    # Create agent and conversation flow
3    agent = MyVoiceAgent()
4    conversation_flow = ConversationFlow(agent)
5
6    # Create pipeline
7    pipeline = CascadingPipeline(
8        stt=DeepgramSTT(model="nova-2", language="en"),
9        llm=OpenAILLM(model="gpt-4o"),
10        tts=ElevenLabsTTS(model="eleven_flash_v2_5"),
11        vad=SileroVAD(threshold=0.35),
12        turn_detector=TurnDetector(threshold=0.8)
13    )
14
15    session = AgentSession(
16        agent=agent,
17        pipeline=pipeline,
18        conversation_flow=conversation_flow
19    )
20
21    try:
22        await context.connect()
23        await session.start()
24        # Keep the session running until manually terminated
25        await asyncio.Event().wait()
26    finally:
27        # Clean up resources when done
28        await session.close()
29        await context.shutdown()
30
The make_context function sets up the environment for the agent, including room options and playground mode.
1def make_context() -> JobContext:
2    room_options = RoomOptions(
3    #  room_id="YOUR_MEETING_ID",  # Set to join a pre-created room; omit to auto-create
4        name="VideoSDK Cascaded Agent",
5        playground=True
6    )
7
8    return JobContext(room_options=room_options)
9
The main block starts the job, initializing the session and running the agent.
1if __name__ == "__main__":
2    job = WorkerJob(entrypoint=start_session, jobctx=make_context)
3    job.start()
4

Running and Testing the Agent

Step 5.1: Running the Python Script

To run your AI Voice Agent, execute the following command in your terminal:
1python main.py
2

Step 5.2: Interacting with the Agent in the Playground

Once the script is running, you'll receive a playground link in the console. Open this link in your browser to interact with your AI Voice Agent. Speak into your microphone, and the agent will respond based on your input.

Advanced Features and Customizations

Extending Functionality with Custom Tools

The VideoSDK framework allows you to extend your agent's functionality with custom tools, known as function_tool. These tools can be integrated into the pipeline to enhance the agent's capabilities.

Exploring Other Plugins

While this tutorial uses specific plugins for STT, LLM, and TTS, VideoSDK supports a variety of options. Explore other plugins to tailor the agent to your specific needs.

Troubleshooting Common Issues

API Key and Authentication Errors

Ensure your API keys are correctly configured in the .env file. Double-check for typos or missing keys.

Audio Input/Output Problems

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

Dependency and Version Conflicts

Use a virtual environment to manage dependencies and avoid version conflicts. Ensure all packages are up-to-date.

Conclusion

Summary of What You've Built

In this tutorial, you've built an AI Voice Agent capable of interacting with users to assist in tool use for LLMs. You've learned to set up the environment, create a custom agent, and test it using the VideoSDK framework.

Next Steps and Further Learning

Explore additional plugins and features offered by VideoSDK to enhance your agent. Consider integrating more complex workflows and expanding the agent's capabilities to suit your specific needs.

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