DEV Community

Cover image for 🤖 Building a Financial AI Chatbot with Rasa: A Hands-On Guide 🌱
Santhosh Vijayabaskar
Santhosh Vijayabaskar Subscriber

Posted on

🤖 Building a Financial AI Chatbot with Rasa: A Hands-On Guide 🌱

The Chatbots and intelligent virtual assistants market is projected to grow at a CAGR of 22% between 2020 and 2025, potentially reaching nearly $14 billion by 2025.📈

When it comes to the world of chatbots 🤖, not all the bots are the same. Think of the simplest chatbots, those that stick to set scripts and respond to exact phrases—like an FAQ section come to life. These are rule-based bots and can be helpful but often feel a bit, well, robotic.

Then there are retrieval-based bots that look for the best match from pre-existing responses, leveraging machine learning to be more flexible with language. These are good but still limited, especially if you’re looking for real interaction and multi-turn conversations.

Finally, we have conversational AI bots—the real stars of the chatbot world 🌟. They go beyond just answering questions; they’re context-aware, meaning they can remember details, adapt based on the conversation, and feel more like a human chat. And this is where Rasa comes in. With Rasa, you can build a bot that’s genuinely smart, customizable, and responsive to individual users.

🤖 What is Rasa?

Rasa is an open-source framework for building chat and voice-based AI assistants. Rasa lets us control every step of the chatbot's workflow.

Behind the Scenes: How Rasa’s Conversational AI Works

Here’s a glimpse of what’s happening under the hood with Rasa:

  • Intent Recognition: The bot uses machine learning to identify user intent (e.g., “check balance”) based on trained examples, allowing it to interpret even varied phrasings.

  • Entity Extraction: Rasa recognizes specific details, or entities (like “account balance” or “loan eligibility”), from the user’s input to respond accurately.

  • Dialogue Management: Rasa’s dialogue policies and stories control the conversation flow, remembering previous interactions and guiding the bot to respond contextually.

What You’ll Build in This Tutorial

In this tutorial, we’ll bring these elements together to create a financial AI chatbot capable of assisting with common banking inquiries. By the end, you’ll have the skills to expand this bot further, adapt it to specific use cases, and understand the core of conversational AI.

✨Let’s get started!🚀

Step 1: Set Up Your Project

Let’s start by creating a new Rasa project. Open your terminal and from your project directory, type:

rasa init --no-prompt
Enter fullscreen mode Exit fullscreen mode

This command sets up the files we’ll be working with, like nlu.yml for defining intents and stories.yml for conversation flows.

Step 2: Defining Intents

Think of an “intent” as what the user wants to do. For example, when someone says “Hey!” they want to greet the bot. Let’s add a few intents to teach Rasa how to recognize them:

Open data/nlu.yml and add some examples:

- intent: greet
  examples: |
    - hello
    - hi there
    - hey
Enter fullscreen mode Exit fullscreen mode

Great job! Now, let’s train the model so Rasa can understand these intents. In your terminal, run:

rasa train
Enter fullscreen mode Exit fullscreen mode

Step 3: Creating Conversation Flows

Next, we’ll map out conversation flows. Think of it as scripting a conversation—what should the bot say when someone greets it?

a. Open data/stories.yml and add this story

- story: greet path
  steps:
    - intent: greet
    - action: utter_greet
Enter fullscreen mode Exit fullscreen mode

Now, the bot will respond with “utter_greet” whenever someone says hello!

Step 4: Defining Responses

Let’s make the bot sound friendly! In domain.yml, define what “utter_greet” should say:

responses:
  utter_greet:
    - text: "Hello! How can I assist you today?"
Enter fullscreen mode Exit fullscreen mode

Feel free to get creative with responses—this is your chatbot, after all!

Real-World Case Study: Finance Chatbot

Let’s take this up a notch. Imagine a bank chatbot that can answer questions about account balances and loan eligibility.

a. Add a check_balance intent in nlu.yml:

- intent: check_balance
  examples: |
    - What is my account balance?
    - Show my balance

- intent: loan_eligibility
  examples: |
    - Am I eligible for a loan?
    - Can I get a loan?
    - What’s my loan eligibility?
Enter fullscreen mode Exit fullscreen mode

In nlu.yml, we’re teaching the bot to recognize user intents: if they ask about balance, it’s check_balance; for loans, it’s loan_eligibility. This setup helps the bot understand and respond accurately

b. Define a Custom Action in actions/actions.py to handle balance requests:

from rasa_sdk import Action

class ActionCheckBalance(Action):
    def name(self):
        return "action_check_balance"

    def run(self, dispatcher, tracker, domain):
        # Example logic for retrieving balance
        balance = "$10,000"
        dispatcher.utter_message(text=f"Your balance is {balance}.")
        return []

class ActionCheckLoanEligibility(Action):
    def name(self):
        return "action_check_loan_eligibility"

    def run(self, dispatcher, tracker, domain):
        # Placeholder logic for loan eligibility
        eligibility_status = "You're eligible for a loan up to $50,000!"
        dispatcher.utter_message(text=eligibility_status)
        return []
Enter fullscreen mode Exit fullscreen mode

In the above code, we set up a custom action called ActionCheckBalance for the bot. When a user asks about their balance, this action responds with a set balance of "$10,000." Think of it as a placeholder—later, you can link it to actual account data for live results!

For eligibility status, I have set to respond with "You're eligible for a loan up to $50,000!" as a placeholder. Similar to account balance action, you can also replace it with actual calculations or criteria.

c. In stories.yml, add this flow for the balance check:

- story: check balance path
  steps:
    - intent: check_balance
    - action: action_check_balance

- story: check loan eligibility path
  steps:
    - intent: loan_eligibility
    - action: action_check_loan_eligibility
Enter fullscreen mode Exit fullscreen mode

In the above stories.yml, we're mapping user paths: if they ask for a balance, the bot triggers action_check_balance; for loans, it triggers action_check_loan_eligibility. This guides the bot’s responses based on user intent!

Step 5: Testing and Fine-Tuning

Time to test it! In your terminal, run:

rasa interactive
Enter fullscreen mode Exit fullscreen mode

This lets you simulate real conversations and adjust responses on the go.

🎉 Congratulations! Your chatbot can now handle basic finance queries.

Remember, the world of conversational AI is constantly evolving, and every tweak you make to your bot brings it closer to a more natural, human-like interaction. So, keep experimenting, stay curious, and don’t be afraid to explore new possibilities with Rasa and conversational AI. Who knows? This might just be the start of your very own AI-driven customer experience journey 🌱.

Scaling Up: Going Beyond Basics

For those ready to dive deeper:

  1. Deploy to Multiple Platforms: Consider integrating with Slack/MS Teams or a custom API for a broader reach.
  2. Advanced Response Customization: Add Rasa custom pipelines or integrate with external databases.

More Recommended Reading on this topic

Happy building!🏆 Don't forget to check out my other articles on AIHandsOn Series for more interesting projects on AI! 🚀

🌐 You can also learn more about my work and projects at https://santhoshvijayabaskar.com

Top comments (1)

Collapse
 
hari_krishnan_bc5a45362e2 profile image
Hari Krishnan

Nice work Santosh. Keep it going