Loan Advisory Agent using Pipecat

Overview

This guide demonstrates how to build a voice-based loan advisory agent that helps customers understand loan products, eligibility, and application processes using Pipecat for real-time communication and Sarvam AI for speech processing. Perfect for banks, NBFCs, fintech companies, and lending platforms serving Indian customers.

What You’ll Build

A loan advisory agent that can:

  • Explain different types of loans (personal, home, vehicle, business, education)
  • Help customers understand eligibility criteria and required documents
  • Provide information about interest rates, EMIs, and loan tenure
  • Guide customers through the application process
  • Answer questions in multiple Indian languages

Quick Overview

  1. Get API keys (Sarvam, OpenAI)
  2. Install packages
  3. Create .env file with your API keys
  4. Write the agent code
  5. Run with appropriate transport

Quick Start

1. Prerequisites

  • Python 3.9 or higher
  • API keys from:

2. Install Dependencies

$pip install "pipecat-ai[daily,openai]" python-dotenv loguru

3. Create Environment File

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

1SARVAM_API_KEY=sk_xxxxxxxxxxxxxxxxxxxxxxxx
2OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxx

Replace the values with your actual API keys.

4. Write Your Agent

Create loan_advisor.py:

1import os
2from dotenv import load_dotenv
3from loguru import logger
4from pipecat.frames.frames import LLMRunFrame
5from pipecat.pipeline.pipeline import Pipeline
6from pipecat.pipeline.runner import PipelineRunner
7from pipecat.pipeline.task import PipelineTask
8from pipecat.processors.aggregators.llm_context import LLMContext
9from pipecat.processors.aggregators.llm_response_universal import (
10 LLMContextAggregatorPair,
11)
12from pipecat.runner.types import RunnerArguments
13from pipecat.runner.utils import create_transport
14from pipecat.services.sarvam.stt import SarvamSTTService
15from pipecat.services.sarvam.tts import SarvamTTSService
16from pipecat.services.openai.llm import OpenAILLMService
17from pipecat.transports.base_transport import TransportParams
18from pipecat.transports.daily.transport import DailyParams
19
20load_dotenv(override=True)
21
22async def bot(runner_args: RunnerArguments):
23 """Main bot entry point."""
24
25 # Create transport (supports both Daily and WebRTC)
26 transport = await create_transport(
27 runner_args,
28 {
29 "daily": lambda: DailyParams(audio_in_enabled=True, audio_out_enabled=True),
30 "webrtc": lambda: TransportParams(
31 audio_in_enabled=True, audio_out_enabled=True
32 ),
33 },
34 )
35
36 # Initialize AI services
37 stt = SarvamSTTService(
38 api_key=os.getenv("SARVAM_API_KEY"),
39 language="unknown", # Auto-detect for diverse customer base
40 model="saaras:v3",
41 mode="transcribe"
42 )
43
44 tts = SarvamTTSService(
45 api_key=os.getenv("SARVAM_API_KEY"),
46 target_language_code="en-IN",
47 model="bulbul:v3",
48 speaker="aditya" # Professional and trustworthy voice
49 )
50
51 llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")
52
53 # Set up conversation context with loan advisor personality
54 messages = [
55 {
56 "role": "system",
57 "content": """You are a professional and helpful loan advisory agent for a leading financial institution in India.
58
59Your expertise covers:
60
61**Loan Products:**
62- Personal Loans: Unsecured loans for various personal needs
63- Home Loans: For purchasing, constructing, or renovating homes
64- Vehicle Loans: Car loans, two-wheeler loans
65- Business Loans: For SMEs, startups, and working capital
66- Education Loans: For domestic and international education
67- Gold Loans: Secured loans against gold jewelry
68- Loan Against Property (LAP): Secured loans using property as collateral
69
70**Key Information to Provide:**
71- Interest rates (mention that actual rates depend on profile and market conditions)
72- Typical loan amounts and tenure options
73- Eligibility criteria (age, income, credit score, employment type)
74- Required documents (ID proof, address proof, income documents, etc.)
75- Processing fees and other charges
76- EMI calculation basics
77- Prepayment and foreclosure options
78
79**Communication Guidelines:**
80- Be professional, trustworthy, and helpful
81- Explain financial terms in simple language
82- Never guarantee loan approval - mention that final approval depends on detailed assessment
83- Always recommend customers to read terms and conditions carefully
84- If asked about specific interest rates, provide indicative ranges and mention they vary
85- Encourage customers to visit the branch or website for exact current rates
86- Be transparent about fees and charges
87- Help customers understand their EMI burden before recommending loan amounts
88- Suggest customers maintain a good credit score
89
90**Compliance Reminders:**
91- Never make false promises about loan approval
92- Always mention that loans are subject to eligibility and documentation
93- Recommend customers to compare offers before deciding
94- Remind about the importance of timely repayments
95
96Start by greeting the customer professionally and asking how you can help them with their loan requirements.""",
97 },
98 ]
99 context = LLMContext(messages)
100 context_aggregator = LLMContextAggregatorPair(context)
101
102 # Build pipeline
103 pipeline = Pipeline(
104 [
105 transport.input(),
106 stt,
107 context_aggregator.user(),
108 llm,
109 tts,
110 transport.output(),
111 context_aggregator.assistant(),
112 ]
113 )
114
115 task = PipelineTask(pipeline)
116
117 @transport.event_handler("on_client_connected")
118 async def on_client_connected(transport, client):
119 logger.info("Customer connected")
120 messages.append(
121 {"role": "system", "content": "Greet the customer professionally and ask how you can help them with their loan requirements."}
122 )
123 await task.queue_frames([LLMRunFrame()])
124
125 @transport.event_handler("on_client_disconnected")
126 async def on_client_disconnected(transport, client):
127 logger.info("Customer disconnected")
128 await task.cancel()
129
130 runner = PipelineRunner(handle_sigint=runner_args.handle_sigint)
131 await runner.run(task)
132
133if __name__ == "__main__":
134 from pipecat.runner.run import main
135 main()

5. Run Your Agent

$python loan_advisor.py

The agent will create a Daily room and provide you with a URL to join.

6. Test Your Agent

Open the provided Daily room URL in your browser and start speaking. Your loan advisor will listen and respond!


Customization Examples

Example 1: Hindi Loan Advisor

For Hindi-speaking customers:

1stt = SarvamSTTService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 language="hi-IN", # Hindi
4 model="saaras:v3",
5 mode="transcribe"
6)
7
8tts = SarvamTTSService(
9 api_key=os.getenv("SARVAM_API_KEY"),
10 target_language_code="hi-IN",
11 model="bulbul:v3",
12 speaker="anand" # Professional male voice
13)
14
15llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")

Example 2: Tamil Loan Advisor

1stt = SarvamSTTService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 language="ta-IN",
4 model="saaras:v3",
5 mode="transcribe"
6)
7
8tts = SarvamTTSService(
9 api_key=os.getenv("SARVAM_API_KEY"),
10 target_language_code="ta-IN",
11 model="bulbul:v3",
12 speaker="priya"
13)
14
15llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")

Example 3: Multilingual Advisor (Auto-detect)

For diverse customer bases:

1stt = SarvamSTTService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 language="unknown", # Auto-detects language
4 model="saaras:v3",
5 mode="transcribe"
6)
7
8tts = SarvamTTSService(
9 api_key=os.getenv("SARVAM_API_KEY"),
10 target_language_code="en-IN",
11 model="bulbul:v3",
12 speaker="aditya"
13)
14
15llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")

Example 4: Speech-to-English Advisor (Saaras)

When customers speak regional languages but you need English processing:

1# Customer speaks Hindi/Tamil/etc. → Saaras converts to English → LLM processes
2
3stt = SarvamSTTService(
4 api_key=os.getenv("SARVAM_API_KEY"),
5 model="saaras:v3", # Speech-to-English translation
6 mode="translate"
7)
8
9tts = SarvamTTSService(
10 api_key=os.getenv("SARVAM_API_KEY"),
11 target_language_code="en-IN",
12 model="bulbul:v3",
13 speaker="aditya"
14)
15
16llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")

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 (16): Ritu, Priya, Neha, Pooja, Simran, Kavya, Ishita, Shreya, Roopa, Amelia, Sophia, Tanya, Shruti, Suhani, Kavitha, Rupali

TTS Additional Parameters

Customize the voice for professional financial advisory:

1tts = SarvamTTSService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 target_language_code="en-IN",
4 model="bulbul:v3",
5 speaker="aditya",
6 pace=1.0, # Normal pace for clear communication
7 speech_sample_rate=24000 # 8000, 16000, 22050, 24000 Hz (default). v3 REST API also supports 32000, 44100, 48000 Hz
8)

Understanding the Pipeline

Pipecat uses a pipeline architecture where data flows through a series of processors:

Customer Audio → STT → Context Aggregator → LLM → TTS → Audio Output
  1. Transport Input: Receives audio from the customer
  2. STT (Speech-to-Text): Converts audio to text using Sarvam’s Saaras v3 (transcription via mode="transcribe", or translation to English via mode="translate")
  3. Context Aggregator (User): Adds customer’s query to conversation context
  4. LLM: Generates advisory response using OpenAI
  5. TTS (Text-to-Speech): Converts response to audio using Sarvam’s Bulbul
  6. Transport Output: Sends audio back to the customer
  7. Context Aggregator (Assistant): Saves advisor’s response to context

Pro Tips

  • Use language="unknown" to support customers who code-mix (Hinglish, etc.)
  • Use professional voices like aditya for financial services
  • Sarvam’s models handle code-mixing naturally
  • Always maintain compliance - never guarantee loan approvals
  • Consider integrating with your loan management system for real-time information

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.

Connection issues: Ensure you have a stable internet connection and the transport is properly configured.


Additional Resources


Need Help?


Happy Building!