Collection Agent using LiveKit

View as Markdown

Overview

This guide demonstrates how to build a voice-based collection agent that can handle payment reminders, follow-ups, and payment assistance using LiveKit for real-time communication and Sarvam AI for speech processing. Perfect for fintech companies, banks, and lending institutions serving Indian customers.

What You’ll Build

A collection agent that can:

  • Make professional payment reminder calls in multiple Indian languages
  • Handle customer queries about payments, due dates, and payment options
  • Guide customers through payment processes
  • Maintain a professional and empathetic tone

Quick Overview

  1. Get API keys (LiveKit, Sarvam)
  2. Install packages
  3. Create .env file with your API keys
  4. Write the agent code
  5. Run: python agent.py dev
  6. Test: python agent.py console

Quick Start

1. Prerequisites

2. Install Dependencies

$pip install "livekit-agents[sarvam,silero]" python-dotenv

3. Create Environment File

Create a file named .env in your project folder and add your API keys:

LIVEKIT_URL=wss://your-project-xxxxx.livekit.cloud
LIVEKIT_API_KEY=APIxxxxxxxxxxxxx
LIVEKIT_API_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SARVAM_API_KEY=sk_xxxxxxxxxxxxxxxxxxxxxxxx

Replace the values with your actual API keys.

4. Write Your Agent

Create collection_agent.py:

1import logging
2from dotenv import load_dotenv
3from livekit.agents import JobContext, WorkerOptions, cli
4from livekit.agents.voice import Agent, AgentSession
5from livekit.plugins import sarvam
6
7# Load environment variables
8load_dotenv()
9
10# Set up logging
11logger = logging.getLogger("collection-agent")
12logger.setLevel(logging.INFO)
13
14
15class CollectionAgent(Agent):
16 def __init__(self) -> None:
17 super().__init__(
18 # Collection agent personality and instructions
19 instructions="""
20 You are a professional and empathetic collection agent working for ABC Bank.
21
22 Customer Account Details:
23 - Bank Name: ABC Bank
24 - EMI Amount: ₹5,000
25 - Due Date: 15th January 2025
26 - Loan Type: Personal Loan
27 - Account Status: Payment Overdue
28
29 Your responsibilities:
30 - Remind customers about their pending EMI payment of ₹5,000 which was due on 15th January
31 - Provide information about payment due dates, amounts, and available payment methods
32 - Help customers understand their payment options and any applicable late fees
33 - Guide customers through the payment process if they want to pay immediately
34 - Address customer concerns about their account with empathy
35 - Offer payment plans or extensions when appropriate (mention that you can connect them with a specialist)
36
37 Payment Methods to mention:
38 - UPI payment to ABC Bank
39 - Net Banking
40 - ABC Bank mobile app
41 - Visit nearest ABC Bank branch
42
43 Communication guidelines:
44 - Always maintain a professional yet friendly tone
45 - Be empathetic to customer's financial situations
46 - Never be aggressive, threatening, or use inappropriate language
47 - If a customer is upset, remain calm and understanding
48 - Speak clearly and concisely
49 - Confirm important details like EMI amount (₹5,000) and due date (15th January)
50 - If customer requests to speak to a human, acknowledge and offer to transfer
51
52 Start by greeting the customer, introducing yourself as calling from ABC Bank,
53 and politely remind them about their pending EMI of ₹5,000.
54 """,
55
56 # Saaras v3 STT - Converts speech to text
57 stt=sarvam.STT(
58 language="unknown", # Auto-detect language
59 model="saaras:v3",
60 mode="transcribe"
61 ),
62
63 # Sarvam LLM - The "brain" that processes and generates responses
64 llm=sarvam.LLM(model="sarvam-105b"),
65
66 # Bulbul TTS - Converts text to speech
67 tts=sarvam.TTS(
68 target_language_code="en-IN",
69 model="bulbul:v3",
70 speaker="aditya" # Professional male voice
71 ),
72 )
73
74 async def on_enter(self):
75 """Called when user joins - agent starts the conversation"""
76 self.session.generate_reply()
77
78
79async def entrypoint(ctx: JobContext):
80 """Main entry point - LiveKit calls this when a user connects"""
81 logger.info(f"User connected to room: {ctx.room.name}")
82
83 # Create and start the agent session
84 session = AgentSession()
85 await session.start(
86 agent=CollectionAgent(),
87 room=ctx.room
88 )
89
90
91if __name__ == "__main__":
92 # Run the agent
93 cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))

5. Run Your Agent

$python collection_agent.py dev

6. Test Your Agent

In a new terminal, run:

$python collection_agent.py console

Customization Examples

Example 1: Hindi Collection Agent

For customers who prefer Hindi:

1stt=sarvam.STT(
2 language="hi-IN", # Hindi
3 model="saaras:v3",
4 mode="transcribe"
5),
6tts=sarvam.TTS(
7 target_language_code="hi-IN",
8 model="bulbul:v3",
9 speaker="anand" # Professional Hindi male voice
10)

Example 2: Tamil Collection Agent

1stt=sarvam.STT(language="ta-IN", model="saaras:v3", mode="transcribe"),
2tts=sarvam.TTS(
3 target_language_code="ta-IN",
4 model="bulbul:v3",
5 speaker="priya"
6)

Example 3: Multilingual Agent (Auto-detect)

1stt=sarvam.STT(language="unknown", model="saaras:v3", mode="transcribe"), # Auto-detects language
2tts=sarvam.TTS(target_language_code="en-IN", model="bulbul:v3", speaker="aditya")

Available Options

Language Codes

LanguageCode
English (India)en-IN
Hindihi-IN
Bengalibn-IN
Tamilta-IN
Telugute-IN
Gujaratigu-IN
Kannadakn-IN
Malayalamml-IN
Marathimr-IN
Punjabipa-IN
Odiaod-IN
Auto-detectunknown

Speaker Voices (Bulbul v3)

Male (23): Shubh (default), Aditya, Rahul, Rohan, Amit, Dev, Ratan, Varun, Manan, Sumit, Kabir, Aayan, Ashutosh, Advait, Anand, Tarun, Sunny, Mani, Gokul, Vijay, Mohit, Rehan, Soham

Female (14): Ritu, Priya, Neha, Pooja, Simran, Kavya, Ishita, Shreya, Roopa, Tanya, Shruti, Suhani, Kavitha, Rupali


Pro Tips

  • Use language="unknown" to automatically detect the language - great for diverse customer bases
  • Use a professional male voice like aditya or anand for collection calls
  • Sarvam’s models handle code-mixing naturally - customers can switch between languages mid-conversation
  • Always maintain compliance with collection regulations in your jurisdiction

Troubleshooting

API key errors: Check that all keys are in your .env file and the file is in the same directory as your script.

Module not found: Run the installation command again based on your operating system.

Poor transcription: Try language="unknown" for auto-detection, or specify the correct language code.


Additional Resources


Need Help?


Happy Building!