How to extract data from mongodb database in a rasa chatbot?

Hello, I have simply installed Rasa chatbot. Now I want to attach my Rasa chatbot with Mongodb Database(.csv file) . The .csv file contains data of (student names, roll no., marks). Now I want that if user input the roll no. in chatbot then it should return the whole information of atudent, Or if user input marks of student then it should ask the name or roll no. of student and if there are two students of same name then chatbot should ask the roll no.

I am attaching the .csv below.Attendence.csv (2.3 KB) I have uploaded this .csv file into mongodb database.

Could you please provide me the code which I do need to write to achieve the above scenario? Please tell me the procedure to implement this in detail. I shall be highly thankful to you.

@nik202 please help

@Akshat_Vashisht Right, No worries. Please can you see this thread and try to implement the process for MongoDB. I’d recommend going step by step: Create MongoDB schema, upload data, try to connect database to rasa, and then write the simple query using custom actions as shown in this thread, I never worked with MongoDB database, I guess SQL commands will be the same for every SQL.

Ref: Display fetch data from MySQL Database as an output

Mongodb is not mandatory. You can also prefer .csv file directly (using pandas) or anything which suits you.

You can prefer anything, I just need output. @nik202

@Akshat_Vashisht Please see the link I have share then and try implement the process. I am here for providing the assistant on Rasa, Code you stuck while running and I’m not here exclusive to write the code for anyone. I hope you getting my point Akshat?

Ref: Rasa Livecoding: Connecting your Rasa Assistant to a Database - YouTube | Rasa Livecoding: Querying a database with a chatbot - YouTube

Yes I know

code I have written in action.py file

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker

from rasa_sdk.executor import CollectingDispatcher

import csv

class actionfindengr(Action):

def name(self) -> Text:

    return "action_findengr"

def run(self,

        dispatcher: CollectingDispatcher,

        tracker: Tracker,

        domain: Dict[Text, Any]

    ) -> List[Dict[Text, Any]]:

    # get the location slot

    location = tracker.get_slot('location')

    # read the CSV file

    with open('C:\\Users\\om sai infotech\\OneDrive\\Desktop\\bot\\actions\\financial.csv','r',encoding = "utf-8") as file:

        reader = csv.DictReader(file)

        # get a list of universities in the desired location

        output = [row for row in reader if row['Location'] == location]

    if output:

        reply  = f"Output: {location}:"

        reply += "\n- " + "\n- ".join([item['S.No.','date','firm','Ticker','Research Development','Income Before Tax','Net Income','Selling General Administrative','Gross Profit','Ebit','Operating Income','Interest Expense','Income Tax Expense','Total Revenue','Total Operating Expenses','Cost Of Revenue','Total Other Income Expense Net','Net Income From Continuing Ops','Net Income Applicable To Common Shares'] for item in output])

        # utter the message

        dispatcher.utter_message(reply)

    else: # the list is empty

        dispatcher.utter_message(f"I could not this in {location}")

@nik202 Could you please tell whether it is right or wrong?