Loan Advisory Agent using Pipecat

View as Markdown

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)
  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,sarvam]" python-dotenv loguru

3. Create Environment File

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

SARVAM_API_KEY=sk_xxxxxxxxxxxxxxxxxxxxxxxx

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

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 = SarvamLLMService(
16 api_key=os.getenv("SARVAM_API_KEY"),
17 settings=SarvamLLMService.Settings(model="sarvam-105b"),
18)

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 = SarvamLLMService(
16 api_key=os.getenv("SARVAM_API_KEY"),
17 settings=SarvamLLMService.Settings(model="sarvam-105b"),
18)

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 = SarvamLLMService(
17 api_key=os.getenv("SARVAM_API_KEY"),
18 settings=SarvamLLMService.Settings(model="sarvam-105b"),
19)

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

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 Sarvam
  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!