DEV Community

Cover image for šŸ¤– Building a Financial AI Chatbot with Rasa: A Hands-On Guide šŸŒ±
Santhosh Vijayabaskar
Santhosh Vijayabaskar

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