Collection Agent using LiveKit

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, OpenAI)
  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,openai,silero]" python-dotenv

3. Create Environment File

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

1LIVEKIT_URL=wss://your-project-xxxxx.livekit.cloud
2LIVEKIT_API_KEY=APIxxxxxxxxxxxxx
3LIVEKIT_API_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4SARVAM_API_KEY=sk_xxxxxxxxxxxxxxxxxxxxxxxx
5OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxx

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 openai, 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 # Saarika STT - Converts speech to text
57 stt=sarvam.STT(
58 language="unknown", # Auto-detect language
59 model="saarika:v2.5"
60 ),
61
62 # OpenAI LLM - The "brain" that processes and generates responses
63 llm=openai.LLM(model="gpt-4o"),
64
65 # Bulbul TTS - Converts text to speech
66 tts=sarvam.TTS(
67 target_language_code="en-IN",
68 model="bulbul:v2",
69 speaker="abhilash" # Professional male voice
70 ),
71 )
72
73 async def on_enter(self):
74 """Called when user joins - agent starts the conversation"""
75 self.session.generate_reply()
76
77
78async def entrypoint(ctx: JobContext):
79 """Main entry point - LiveKit calls this when a user connects"""
80 logger.info(f"User connected to room: {ctx.room.name}")
81
82 # Create and start the agent session
83 session = AgentSession()
84 await session.start(
85 agent=CollectionAgent(),
86 room=ctx.room
87 )
88
89
90if __name__ == "__main__":
91 # Run the agent
92 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="saarika:v2.5"
4),
5tts=sarvam.TTS(
6 target_language_code="hi-IN",
7 model="bulbul:v2",
8 speaker="karun" # Professional Hindi male voice
9)

Example 2: Tamil Collection Agent

1stt=sarvam.STT(language="ta-IN", model="saarika:v2.5"),
2tts=sarvam.TTS(
3 target_language_code="ta-IN",
4 model="bulbul:v2",
5 speaker="anushka"
6)

Example 3: Multilingual Agent (Auto-detect)

1stt=sarvam.STT(language="unknown", model="saarika:v2.5"), # Auto-detects language
2tts=sarvam.TTS(target_language_code="en-IN", model="bulbul:v2", speaker="abhilash")

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 v2)

Female Voices:

  • anushka - Clear and professional (default)
  • manisha - Warm and friendly
  • vidya - Articulate and precise
  • arya - Young and energetic

Male Voices:

  • abhilash - Deep and authoritative (recommended for collection)
  • karun - Natural and conversational
  • hitesh - Professional and engaging

Pro Tips

  • Use language="unknown" to automatically detect the language - great for diverse customer bases
  • Use a professional male voice like abhilash or karun 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!