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="saarika:v2.5"
41 )
42
43 tts = SarvamTTSService(
44 api_key=os.getenv("SARVAM_API_KEY"),
45 target_language_code="en-IN",
46 model="bulbul:v2",
47 speaker="abhilash" # Professional and trustworthy voice
48 )
49
50 llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")
51
52 # Set up conversation context with loan advisor personality
53 messages = [
54 {
55 "role": "system",
56 "content": """You are a professional and helpful loan advisory agent for a leading financial institution in India.
57
58Your expertise covers:
59
60**Loan Products:**
61- Personal Loans: Unsecured loans for various personal needs
62- Home Loans: For purchasing, constructing, or renovating homes
63- Vehicle Loans: Car loans, two-wheeler loans
64- Business Loans: For SMEs, startups, and working capital
65- Education Loans: For domestic and international education
66- Gold Loans: Secured loans against gold jewelry
67- Loan Against Property (LAP): Secured loans using property as collateral
68
69**Key Information to Provide:**
70- Interest rates (mention that actual rates depend on profile and market conditions)
71- Typical loan amounts and tenure options
72- Eligibility criteria (age, income, credit score, employment type)
73- Required documents (ID proof, address proof, income documents, etc.)
74- Processing fees and other charges
75- EMI calculation basics
76- Prepayment and foreclosure options
77
78**Communication Guidelines:**
79- Be professional, trustworthy, and helpful
80- Explain financial terms in simple language
81- Never guarantee loan approval - mention that final approval depends on detailed assessment
82- Always recommend customers to read terms and conditions carefully
83- If asked about specific interest rates, provide indicative ranges and mention they vary
84- Encourage customers to visit the branch or website for exact current rates
85- Be transparent about fees and charges
86- Help customers understand their EMI burden before recommending loan amounts
87- Suggest customers maintain a good credit score
88
89**Compliance Reminders:**
90- Never make false promises about loan approval
91- Always mention that loans are subject to eligibility and documentation
92- Recommend customers to compare offers before deciding
93- Remind about the importance of timely repayments
94
95Start by greeting the customer professionally and asking how you can help them with their loan requirements.""",
96 },
97 ]
98 context = LLMContext(messages)
99 context_aggregator = LLMContextAggregatorPair(context)
100
101 # Build pipeline
102 pipeline = Pipeline(
103 [
104 transport.input(),
105 stt,
106 context_aggregator.user(),
107 llm,
108 tts,
109 transport.output(),
110 context_aggregator.assistant(),
111 ]
112 )
113
114 task = PipelineTask(pipeline)
115
116 @transport.event_handler("on_client_connected")
117 async def on_client_connected(transport, client):
118 logger.info("Customer connected")
119 messages.append(
120 {"role": "system", "content": "Greet the customer professionally and ask how you can help them with their loan requirements."}
121 )
122 await task.queue_frames([LLMRunFrame()])
123
124 @transport.event_handler("on_client_disconnected")
125 async def on_client_disconnected(transport, client):
126 logger.info("Customer disconnected")
127 await task.cancel()
128
129 runner = PipelineRunner(handle_sigint=runner_args.handle_sigint)
130 await runner.run(task)
131
132if __name__ == "__main__":
133 from pipecat.runner.run import main
134 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="saarika:v2.5"
5)
6
7tts = SarvamTTSService(
8 api_key=os.getenv("SARVAM_API_KEY"),
9 target_language_code="hi-IN",
10 model="bulbul:v2",
11 speaker="karun" # Professional male voice
12)
13
14llm = 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="saarika:v2.5"
5)
6
7tts = SarvamTTSService(
8 api_key=os.getenv("SARVAM_API_KEY"),
9 target_language_code="ta-IN",
10 model="bulbul:v2",
11 speaker="anushka"
12)
13
14llm = 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="saarika:v2.5"
5)
6
7tts = SarvamTTSService(
8 api_key=os.getenv("SARVAM_API_KEY"),
9 target_language_code="en-IN",
10 model="bulbul:v2",
11 speaker="abhilash"
12)
13
14llm = 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:v2.5" # Speech-to-English translation
6)
7
8tts = SarvamTTSService(
9 api_key=os.getenv("SARVAM_API_KEY"),
10 target_language_code="en-IN",
11 model="bulbul:v2",
12 speaker="abhilash"
13)
14
15llm = 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 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 finance)
  • karun - Natural and conversational
  • hitesh - Professional and engaging

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:v2",
5 speaker="abhilash",
6 pitch=0.0, # Range: -1.0 to 1.0
7 pace=1.0, # Normal pace for clear communication
8 loudness=1.5, # Range: 0.5 to 2.0
9 speech_sample_rate=16000 # 8000, 16000, or 24000 Hz
10)

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 Saarika
  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 abhilash 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!