Hi,
I am developing a chatbot where upon the users question ,i need to go to mongodb database and retrieve the answer for his question.
what i have done till now:
i tried reading the collection into csv file and then answer based on users question in chatbot.It is working partially
example:when i ask the employees with skill as python,it is returning only the first match.Also when i ask about the count of employees with skill as python it is giving not giving count
attached is my code and csv data:
import csv
from pandas import read_csv
from pymongo import MongoClient
from transformers import pipeline
# Function to read data from MongoDB and store it in a CSV file in a table format
def mongo_to_csv(mongo_uri, database_name, collection_name, csv_file_path):
# Connect to MongoDB
client = MongoClient(mongo_uri)
db = client[database_name]
collection = db[collection_name]
# Query the collection (you can modify the query as needed)
data = collection.find()
# Extract all field names from the documents to handle varying fields
all_keys = set()
for document in data:
all_keys.update(document.keys())
# Re-query the collection since the previous iteration exhausted the cursor
data = collection.find()
# Open a CSV file to write
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as csv_file:
# Create a CSV DictWriter
csv_writer = csv.DictWriter(csv_file, fieldnames=all_keys)
# Write the header (field names)
csv_writer.writeheader()
# Write the data rows
for document in data:
csv_writer.writerow(document)
print(f"Data from {collection_name} collection in {database_name} database has been written to {csv_file_path}")
import pandas as pd
tqa=pipeline(task="table-question-answering",model="google/tapas-base-finetuned-wtq")
table=read_csv("output.csv")
table=table.astype(str)
print(table)
query="what is name of all employees with skills as Python"
print(tqa(table=table,query=query)['answer'])
{
"_id": {
"$oid": "66882351d969e86b0b22d603"
},
"id": "1",
"name": "abc",
"skills": "Python",
},
{
"_id": {
"$oid": "66882352d969e86b0b22d603"
},
"id": "2",
"name": "ert",
"skills": "python",
}
is there any better way of solving this or can i fix the existing code