Conversational AI in Banking: Build a Voice Agent

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

Introduction to AI Voice Agents in Conversational AI in Banking

What is an AI

Voice Agent

?

AI Voice Agents are sophisticated software applications designed to interact with users through voice commands. These agents leverage advanced technologies such as Speech-to-Text (STT), Text-to-Speech (TTS), and Natural Language Processing (NLP) to understand and respond to user queries. They are capable of performing tasks, providing information, and enhancing user experiences through seamless voice interactions.

Why are they important for the Conversational AI in Banking Industry?

In the banking industry, AI Voice Agents play a crucial role in transforming customer service and operational efficiency. They can handle a wide range of tasks such as answering customer queries, providing account information, assisting with transactions, and offering financial guidance. By automating routine interactions, banks can improve customer satisfaction, reduce operational costs, and provide 24/7 support.

Core Components of a

Voice Agent

What You'll Build in This Tutorial

In this tutorial, we will guide you through building a conversational AI

voice agent

tailored for the banking sector using the VideoSDK framework. You will learn how to set up the environment, create a custom agent, and deploy it for testing.

Architecture and Core Concepts

High-Level Architecture Overview

The architecture of our AI

Voice Agent

involves several key components working together to process user input and generate responses. The flow begins with capturing user speech, converting it to text, processing it using an LLM, and finally converting the response back to speech.
Diagram

Understanding Key Concepts in the VideoSDK Framework

  • Agent: The core class representing your AI bot, responsible for managing interactions.
  • CascadingPipeline: Manages the flow of audio processing from STT to LLM to TTS, as detailed in the

    Cascading pipeline in AI voice Agents

    .
  • VAD & TurnDetector: These components help the agent determine when to listen and when to speak, with the

    Turn detector for AI voice Agents

    playing a crucial role.

Setting Up the Development Environment

Prerequisites

Before you begin, ensure you have Python 3.11+ installed and a VideoSDK account. Sign up at app.videosdk.live to access the necessary API keys.

Step 1: Create a Virtual Environment

Create a virtual environment to manage dependencies:
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-agents videosdk-plugins
2

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

To build our AI Voice Agent, we will use the following complete code block as a starting point:
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 a knowledgeable and friendly banking assistant AI, designed to enhance customer experience in the banking sector. Your primary role is to assist customers with their banking inquiries, provide information on banking products, and guide them through various banking processes. You can answer questions about account balances, recent transactions, loan options, interest rates, and branch locations. Additionally, you can help users navigate online banking platforms and provide tips on financial management.\n\nConstraints and Limitations:\n1. You are not a financial advisor and cannot provide personalized financial advice or investment recommendations.\n2. Always include a disclaimer advising users to consult with a financial advisor for personalized advice.\n3. You cannot perform any transactions or access personal banking information.\n4. Ensure user privacy and data security by not storing any personal information.\n5. You must adhere to banking regulations and compliance standards, avoiding any actions that could breach these rules."
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 the agent, you need a meeting ID. Use the following curl command to generate one:
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 is the heart of our application. It extends the Agent class and defines the behavior of our voice agent. The on_enter and on_exit methods handle the initial and final interactions with users.
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 is responsible for managing the flow of audio data through various processing stages. It integrates STT, LLM, TTS, VAD, and a Turn Detector.
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 session and begins the interaction loop. The make_context function sets up the room options for the session.
1async def start_session(context: JobContext):
2    agent = MyVoiceAgent()
3    conversation_flow = ConversationFlow(agent)
4
5    pipeline = CascadingPipeline(
6        stt=DeepgramSTT(model="nova-2", language="en"),
7        llm=OpenAILLM(model="gpt-4o"),
8        tts=ElevenLabsTTS(model="eleven_flash_v2_5"),
9        vad=SileroVAD(threshold=0.35),
10        turn_detector=TurnDetector(threshold=0.8)
11    )
12
13    session = AgentSession(
14        agent=agent,
15        pipeline=pipeline,
16        conversation_flow=conversation_flow
17    )
18
19    try:
20        await context.connect()
21        await session.start()
22        await asyncio.Event().wait()
23    finally:
24        await session.close()
25        await context.shutdown()
26
27def make_context() -> JobContext:
28    room_options = RoomOptions(
29        name="VideoSDK Cascaded Agent",
30        playground=True
31    )
32
33    return JobContext(room_options=room_options)
34
35if __name__ == "__main__":
36    job = WorkerJob(entrypoint=start_session, jobctx=make_context)
37    job.start()
38

Running and Testing the Agent

Step 5.1: Running the Python Script

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

Step 5.2: Interacting with the Agent in the Playground

Once the script is running, you will see a playground link in the console. Use this link to join the session and interact with your AI Voice Agent.

Advanced Features and Customizations

Extending Functionality with Custom Tools

The VideoSDK framework allows you to extend your agent's capabilities by integrating custom tools. These tools can perform specific tasks or provide additional data processing.

Exploring Other Plugins

While this tutorial uses specific plugins, you can explore other STT, LLM, and TTS options available in the VideoSDK framework to customize your agent further.

Troubleshooting Common Issues

API Key and Authentication Errors

Ensure your API keys are correctly configured in the .env file and that you have the necessary permissions.

Audio Input/Output Problems

Check your microphone and speaker settings to ensure they are correctly configured and functioning.

Dependency and Version Conflicts

Ensure all dependencies are installed and compatible with your Python version. Use a virtual environment to manage package versions.

Conclusion

Summary of What You've Built

In this tutorial, you have built a conversational AI voice agent for the banking sector using the VideoSDK framework. You learned how to set up the environment, create a custom agent, and deploy it for testing.

Next Steps and Further Learning

Explore additional features and plugins in the VideoSDK framework to enhance your agent's capabilities. Consider integrating more complex functionalities and exploring advanced AI models. For a comprehensive understanding, refer to the

AI voice Agent core components overview

and learn about

AI voice Agent Sessions

to further refine your implementations.

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