Build a Conversational AI for Consulting

Step-by-step guide to build a conversational AI voice agent for consulting using VideoSDK.

Introduction to AI Voice Agents in Conversational AI for Consulting

AI Voice Agents are transforming the way businesses engage with clients by providing seamless, interactive, and automated customer service experiences. In the consulting industry, these agents can assist in delivering insights, answering client queries, and providing strategic guidance without human intervention.

What is an AI

Voice Agent

?

An AI

Voice Agent

is a software application that uses artificial intelligence to understand and respond to human speech. It leverages technologies like Speech-to-Text (STT), Natural Language Processing (NLP), and Text-to-Speech (TTS) to interact with users in a conversational manner.

Why are they important for the Conversational AI for Consulting industry?

In consulting, AI Voice Agents can handle initial client interactions, provide data-driven insights, and assist consultants by automating routine tasks. This not only saves time but also enhances the client experience by ensuring quick and accurate responses.

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.

What You'll Build in This Tutorial

In this tutorial, you will build an AI

Voice Agent

using the VideoSDK framework. This agent will be capable of understanding and responding to consulting-related queries, providing a practical demonstration of conversational AI in action.

Architecture and Core Concepts

High-Level Architecture Overview

The architecture of an AI Voice Agent involves multiple components working in tandem. The user speaks into a microphone, and the audio is processed through a series of stages:
  1. Voice

    Activity Detection

    (VAD):
    Identifies when the user is speaking.
  2. STT: Converts speech to text.
  3. LLM: Analyzes the text and generates a response.
  4. TTS: Converts the response back to speech.
  5. Turn Detection: Ensures smooth conversation flow by managing when the agent listens and speaks.
Diagram

Understanding Key Concepts in the VideoSDK Framework

Setting Up the Development Environment

Prerequisites

To get started, 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

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

Step 2: Install Required Packages

Install the necessary Python packages: bash pip install videosdk pip install python-dotenv

Step 3: Configure API Keys in a .env file

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

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

Here is the complete code for the 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 consulting assistant AI specializing in providing insights and guidance for business consulting. Your primary role is to assist users by answering questions related to business strategies, market analysis, and operational improvements. You can provide information on best practices, industry trends, and case studies relevant to consulting. However, you are not a certified consultant and must advise users to seek professional consulting services for personalized advice. You should maintain a professional and informative tone, ensuring that all information is accurate and up-to-date. Your responses should be concise and focused on delivering value to the user's consulting needs."
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 interact with your agent, you need a meeting ID. You can generate one using the VideoSDK API:
1curl -X POST \
2  https://api.videosdk.live/v1/meetings \
3  -H "Authorization: Bearer YOUR_API_KEY" \
4  -H "Content-Type: application/json" \
5  -d '{}'
6

Step 4.2: Creating the Custom Agent Class

The MyVoiceAgent class is where you define the behavior of your agent. It inherits from the Agent class and uses the agent_instructions to guide its responses. The on_enter and on_exit methods handle greetings and farewells.

Step 4.3: Defining the Core Pipeline

The CascadingPipeline is crucial as it defines how the agent processes audio and generates responses. It integrates several plugins:
  • DeepgramSTT: Converts speech to text.
  • OpenAILLM: Processes text and generates responses.
  • ElevenLabsTTS: Converts text responses back to speech.
  • SileroVAD: Detects when the user is speaking.
  • TurnDetector: Manages conversation flow.

Step 4.4: Managing the Session and Startup Logic

The start_session function initializes the agent and starts the conversation flow. It connects the session to the VideoSDK framework and keeps it running until manually stopped.
The make_context function sets up the job context with room options, allowing the agent to operate in a test environment.
The if __name__ == "__main__": block ensures that the agent starts when the script is executed.

Running and Testing the Agent

Step 5.1: Running the Python Script

To start your agent, run the following command: bash python main.py

Step 5.2: Interacting with the Agent in the

AI Agent playground

Once the agent is running, you will see a link to the VideoSDK playground in your console. Open this link in a browser to interact with your agent. You can speak into your microphone, and the agent will respond based on the instructions provided.

Advanced Features and Customizations

Extending Functionality with Custom Tools

You can extend your agent's capabilities by integrating custom tools. This involves creating additional plugins or modifying the existing pipeline to include new functionalities.

Exploring Other Plugins

The VideoSDK framework supports various STT, LLM, and TTS plugins. You can experiment with different combinations to find the best fit for your use case.

Troubleshooting Common Issues

API Key and Authentication Errors

Ensure your API keys are correctly set in the .env file. Double-check the permissions and validity of your keys.

Audio Input/Output Problems

Verify that your microphone and speakers are working correctly. Check the audio settings in your operating system.

Dependency and Version Conflicts

Ensure all dependencies are installed in the correct versions. Use a virtual environment to manage dependencies effectively.

Conclusion

Summary of What You've Built

In this tutorial, you have built a fully functional AI Voice Agent capable of handling consulting-related queries. You learned about the architecture, setup, and operation of the agent using the VideoSDK framework.

Next Steps and Further Learning

Consider exploring more advanced features, such as integrating additional data sources or customizing the agent's behavior further. Continue learning about AI and voice technologies to enhance your skills.

Get 10,000 Free Minutes Every Months

No credit card required to start.

Want to level-up your learning? Subscribe now

Subscribe to our newsletter for more tech based insights

FAQ