Building Our Own RAG Model: A Journey with Udhyam

This blog was collaboratively written by Kunal and my colleague and friend, Suhail. Together, we explored the topic of RAG to provide a detailed and comprehensive understanding of its key concepts and implications.

At Udhyam, we embarked on an exciting journey to build our own Retrieval-Augmented Generation (RAG) model to enhance our WhatsApp-based chatbot. This blog outlines our experience, the decisions we made, and the challenges we faced. Our goal was to create a system that could provide contextual and accurate responses to students and teachers using the program-related information stored in our documents.

What is RAG? đꤔ

Retrieval-Augmented Generation (RAG) is a method that enhances the capabilities of Large Language Models (LLMs) by integrating them with an information retrieval system. Unlike traditional LLMs, which rely solely on their training data, a RAG model can access external sources of information, such as databases or documents, to generate more accurate and contextually relevant responses.

When a user asks a question, the system retrieves relevant information from external sources (Documents with extra knowledge) and combines it with the query. This augmented input enables the LLM to provide more precise and informed answers, making RAG a powerful tool for improving response quality.

Why did we need our own RAG system ?

We initially relied on Jugalbandi, a RAG-based solution, to provide contextual answers to program-related questions through our WhatsApp chatbot. However, when Jugalbandi stopped supporting the version we were using, we needed to find an alternative.

The lack of in-house AI skills made us hesitant to build our own solution, but after researching RAG, we realized that it was within our reach. The simplicity of the framework and our desire for greater control over the chatbot’s performance encouraged us to take the plunge.

The Development Process: Building the RAG System

We began by focusing on optimizing the process for cost and efficiency. Our system works by converting the information from program-related PDF files into numerical representations called embeddings, which are stored in MongoDB.

Key Concepts: Chunking vs. Embeddings

Chunking and embeddings are essential concepts in a RAG system, and while they work together, they serve different purposes:

Chunking: This is the process of dividing a document into smaller, manageable parts. In our case, we divided the content based on paragraph separations or by question-answer format. Each chunk represents a coherent piece of information that can be independently searched and retrieved.

Example: If a PDF document has 10 paragraphs, each paragraph is treated as a separate chunk. These chunks are stored individually in MongoDB.

Embeddings: An embedding is a numerical representation of text, capturing its meaning in a vector format that the computer can understand. We use the text-embedding-ada-002 model, known for its efficiency and accuracy, to create embeddings for each chunk of content. These embeddings allow us to perform similarity searches in the vector database.

Example: When a user asks a question, the system converts the query into an embedding and searches for closest embedding in the vector database using a formula, in this case Euclidean formula, retrieving the most relevant content chunks.

Why we chose MongoDB 

We selected MongoDB because of its unique combination of flexibility, performance, and native support for advanced use cases like vector search. Our primary need was to efficiently store chunked data alongside their respective embeddings and to quickly search through these embeddings. MongoDB stood out as the best choice for the following reasons:

Schema Flexibility: 

  • MongoDB is document-based and allows for storing data in a flexible, JSON-like format. 
  • This is critical for storing embeddings, which may vary in structure and length, along with metadata or additional fields. 

High Performance: 

  • MongoDB is optimized for fast read and write operations, which is essential when dealing with large volumes of embeddings and associated data chunks.

Native Vector Search:

  • MongoDB Atlas now supports vector search, which is crucial for our use case. We can store embeddings as vectors and quickly retrieve the most relevant data chunks based on similarity searches using cosine similarity or Euclidean distance.
  • The vector search functionality is particularly well-suited for AI-driven applications like semantic search, recommendation engines, and natural language processing tasks.

Implementation Steps:

Content Chunking: We used Python’s .split function to break down the PDF content into chunks, typically based on paragraph separation.

Embedding Generation: The text-embedding-ada-002 model was used to convert these chunks into embeddings, which are then stored in MongoDB using MongoDB Atlas Vector Search with their respective chunks..

Query Handling: When a user asks a question, the system generates an embedding of the query and uses MongoDB’s vector search to find the top three relevant content chunks.

Answer Generation: The relevant chunks, combined with the user’s question, are sent to ChatGPT to generate an accurate and contextual response.

Each chunk is stored in MongoDB along with its corresponding embedding. When a question  embedding is generated, MongoDB’s vector search compares it to the stored embeddings and retrieves the closest matching chunks from the database.

Testing and Comparisons: Exploring Alternatives

During development, we compared our system with other solutions, Including OpenFile search  and Jugalbandi. We also explored using the LLaMA model (a free model) to reduce costs. One of our team members had worked on this earlier which helped.

Testing with LLaMA : 

LLaMA (Large Language Model Meta AI) is an open-source series of language models developed by Meta (formerly Facebook). It is designed for various natural language processing (NLP) tasks like text generation, summarization, translation, and question-answering. LLaMA is similar to models like OpenAI’s GPT but is trained on publicly available data and optimized to be smaller, faster, and more efficient.

Here are some of the Pros and Cons of using LLaMA model:

Pros:

  • Provides accurate answers when asked questions in English based on the provided documents.
  • Fast response and Open-source.
  • Occasionally responds accurately in Hindi when questions are asked in Hindi.
  • A strict prompt structure may yield expected results.

Cons:

  • Responses are sometimes delivered in the wrong language. For example, when a question is asked in Hinglish, the answer is received in English.
  • Poor understanding of the context, and lower quality responses.

Given the importance of handling multilingual queries effectively, LLaMA was not a viable option for us. GPT-4o outperformed it, especially in understanding and responding accurately to queries in Hindi, English, and Hinglish.

Final Decision: Why We Chose Our RAG System

After thorough testing and comparisons, our RAG model emerged as the best fit for our needs. The combination of Python for speed and MongoDB for efficient embedding storage provided a robust, scalable solution. The entire system was tailored to our unique requirements, ensuring that it met the needs of our users.

Learnings and Reflections

Building our RAG model was a challenging yet rewarding experience. It allowed us to explore various technologies, understand their strengths and limitations, and ultimately create a system that met our specific needs. This journey has opened up new possibilities for Udhyam, and we are excited to see where this technology will take us next.

This journey not only enhanced our chatbot but also empowered our team to delve into AI technologies, fostering a culture of innovation and continuous learning at Udhyam.

Finally, I would like to extend my sincere thanks to Ramesh Krishnamoorthy sir for their valuable insights and contributions to this blog. Their expertise was instrumental in shaping the ideas presented here.

Recent Blogs

Working on One : Lessons from working for Collective Impact through Saamuhika Shakti

Complex social challenges rarely exist in isolation. They are shaped by overlapping systems,

Jeeva’s Journey: Restarting a Dream, One Step at a Time

When Jeeva first approached Udhyam Vyapaar, she was already someone with tremendous potential.

Measuring Success: Jayshri’s Journey From Guesswork To Growth

In the bustling lanes of Subhash nagar, Shirpur, Jayshri Ravindra Pardhi has been

Shankar Maruwada is the Co-Founder and CEO of EkStep Foundation, a philanthropic mission he co-founded in 2015 along with Nandan Nilekani and Rohini Nilekani, to improve basic education for 200 million children in India. The Foundation has co-created an open-source free to use digital infrastructure called Sunbird (www.sunbird.org) which works towards achieving this purpose. DIKSHA, the national school education platform, is one of multiple national initiatives that leverage Sunbird to provide access to digital content for learners and for capacity building of teachers. He has more than 25 years of experience across corporate, entrepreneurial, nonprofit and government sectors. This allows him to bring the best of thinking from different lenses, which he has used in shaping EkStep’s mission and its strategic choices of achieving population scale impact for learning, using technology. Shankar is deeply passionate about leveraging technology for large scale transformation in society. He was part of the startup team at Aadhaar; in fact, he was responsible for naming it. He also set up one of India’s first data analytics companies – Marketics. This cross-sectorial experience and interdisciplinary approach has been a driving force in pursuing scale solutions, innovations and collaborations for EkStep’s education mission. He also mentors startups, social entrepreneurs and not-for-profits. Shankar is a member of the National Steering Committee tasked with developing the National Curriculum Frameworks based on the National Education Policy, 2020. He also been on multiple Government committees and task forces, including DIKSHA, NDEAR (National Digital Education Architecture), iGOT (Integrated Government Online Training). His alma mater IIT Kharagpur’s motto of ‘yogah karmasu kaushalam’ (Yoga is excellence at work) and its message of ‘Dedicated to the service of the nation’ is also his chosen path in life. He is also an alumnus of Indian Institute of Management, Ahmedabad.

R. Natarajan, fondly called Nats, co-founded Foundation partners in July 2018 advising companies on scaling, governance and profitability. Prior to this he was Chief Operating officer at UC RNT Fund, managing USD 400 Mn and Managing Director at Helion ventures, a VC firm with Asset under management of over USD 1 Bn for 10 years, and in various leadership roles in Tavant Technologies Inc., US, and Wipro for 13 years. He is a qualified finance professional and a certified black belt in Six sigma by Motorola University. He is on the Board of studies at Christ University and in the Advisory board of Shasun College for Women in Chennai, Bethany High school Bangalore and Byramjee Jeejeebhoy School and College at Mumbai. He also serves on the Board of PHFI (Public Health Foundation India) and also a trustee of Youth For Seva, a large NGO focusing on self-reliant communities powered by selfless individuals for the last 13 years, currently whose volunteers cross 1 lac and beneficiaries cross 10 Cr across 14 states.

“Every human being deserves a dignified life out of poverty, and it’s well in our collective means to achieve that goal.”After 17 years of starting, scaling and turning around various businesses in some of the largest and most respected organisations globally, Atul started The/Nudge Foundation to do poverty alleviation work. Atul is now serving both The/Nudge and Givelndia as their CEO. Over his 5-year stint at InMobi as its Chief Business Officer, Atul helped scale the organisation to a global leader in mobile advertising, with operations in 20+ countries. Atul also served on the Board of Mobile Marketing Association. Prior to InMobi, Atul was the Head of Mobile Business for Japan & Asia-Pacific at Google. Atul has also done various general management, business development and sales roles across technology companies, including Adobe, Samsung and Infosys. Atul also served EndPoverty, a non-profit, as Chairperson for two years, working on various aspects of poverty, including water, sanitation, education, skill development, sustainability and women empowerment, and continues to serve as their Board Member. Atul has been named in the #40underForty list by The Economic Times in 2017. Atul holds a Master in Business Administration (MBA) from the Indian School of Business and a B-Tech from the National Institute of Technology.

Binny Bansal is an Indian internet entrepreneur, who co-founded Flipkart, the leading e-commerce marketplace in the country. At Flipkart, he donned several hats including Chief Operating Officer, Chief Executive Officer, Group CEO, and Chairman. Post his graduation in Computer Science & Engineering from IIT Delhi, Binny worked at various companies, including a stint at Amazon India. In October 2007, along with his Amazon colleague, he co-founded Flipkart, an online book store based out of Bengaluru. In 2018, Binny steered Flipkart to close the largest global M&A deal in e-commerce, when Walmart acquired a majority stake in the company at an enterprise valuation of $22 billion. Binny is currently an entrepreneur-investor and mentor in the startup ecosystem. He has invested in several early stage startups, including Stellaps, Ather, Increff, Inshorts, Tracxn and Goodera. Growth stage startups include Acko, Cure.fit, Rupeek and GreyOrange, to name a few. Binny also co-founded xtolOx Technologies, offering technology tools, learning platforms and consultancy services to enable growth stage startups scale 10x. In December 2019, Binny relocated to Singapore with his family. Binny was ranked 26th among India’s 50 Most Powerful People in 2017 by India Today, and was awarded the 2016 “Asian of the Year” award by Straits Times of Singapore.

Narayan Ramachandran is an accomplished investment professional and social entrepreneur with over 20 years of experience in global finance and developmental economics. He previously served as the Country Head of Morgan Stanley India and was the lead portfolio manager of its Global Emerging Markets and Global Asset Allocation teams, managing assets worth over $25 billion. Narayan is currently the Chairman and CEO of KludeIn I Acquisition Corporation and co-Chairman of Unitus Capital, India’s largest social enterprise bank. He is also actively involved in social impact through InKlude Labs, which scales interventions in education and public health, and serves on several boards including Vivriti Asset Management and Caspian Debt. Narayan holds a B.Tech. in chemical engineering from IIT Bombay and an MBA from the University of Michigan. He is known for connecting ideas, people, and capital to drive impactful change in areas such as social enterprise, environment, and global finance.

linkedin.com/in/narayan-ramachandran-a6b2941b8

Abhishek Poddar is a prominent Indian industrialist, art collector, and patron of the arts. He is the Founder-Trustee of the Museum of Art & Photography (MAP) in Bengaluru, to which he has donated a substantial portion of his family’s art collection and the initial leadership gift. As Managing Director of Matheson Bosanquet, an 80-year-old company specializing in tea production, trading, and export, and Director at Sua Explosives & Accessories, a leading manufacturer of mining explosives in India, Poddar balances business leadership with cultural philanthropy. He also serves on advisory committees of several esteemed organizations including the India-Europe Foundation for New Dialogues and the Lincoln Centre Global Advisory Council. Recognized among Asia’s 2018 Heroes of Philanthropy by Forbes, he is deeply committed to promoting India’s rich artistic heritage.
He was born in Kolkata, attended Lamartiniere for Boys and The Doon School, and graduated from St. Xavier’s College, Kolkata.

linkedin.com/in/abhishek-poddar-map

Ireena Vittal is a leading adviser on sustainable growth, digital transformation, and organizational scale-up. She serves on the boards of Asian Paints, Godrej Consumer, Diageo PLC, and Compass PLC, and advises nonprofits in education, legal reform, rural livelihoods, water, and urbanization. A former McKinsey partner for 16 years, she worked with global companies and co-authored influential reports on economic growth, agriculture, and urbanization. She holds a degree in Electronics from Osmania University and a PGDBM from IIM Calcutta.

Please drop your name and email id to download Brand Logos