Notebooks
M
MongoDB
Rag Mongodb Llama3 Huggingface Open Source

Rag Mongodb Llama3 Huggingface Open Source

agentsartificial-intelligencellmsmongodb-genai-showcasenotebooksgenerative-airag

Open In Colab

Implementing RAG pipelines with MongoDB and Llama3 and Open models From Hugging Face

This notebook is designed to demonstrate how to integrate and utilize Hugging Face's open-source models, specifically Llama3, with MongoDB to implement Retrieval-Augmented Generation (RAG) pipelines for enhanced question answering capabilities.

The process involves preparing a dataset of arXiv papers, transforming their data for effective retrieval, setting up a MongoDB database with vector search capabilities, and using llama3 model for generating answers based on the retrieved documents.

Key Highlights:

  • Usage of Hugging Face open-source models and MongoDB for creating RAG pipelines.
  • Steps include dataset preparation, database setup, data ingestion, and query processing.
  • Detailed guidance on setting up MongoDB collections and vector search indexes.
  • Integration with the Llama3 model from Hugging Face for answering complex queries.

Follow the following instruction to set up a MongoDB database and enable vector search:

  1. Register a free Atlas account or sign in to your existing Atlas account.

  2. Follow the instructions (select Atlas UI as the procedure) to deploy your first cluster, which distributes your data across multiple servers for improved performance and redundancy.

image.png

  1. For a free Cluser, be sure to select "Shared" option when creating your new cluster. See image below for details

image-2.png

  1. Create the database: knowledge_base, and collection research_papers

Import Libraries

Import libaries into development environment

[ ]

Dataset Loading and Preparation

Load the dataset from HuggingFace.

Only using the first 100 datapoint for demo purposes.

[ ]
[ ]

Generate Embeddings

[5]

Database and Collection Setup

Complete the steps below if not already carried out previously. Creating a database and collection within MongoDB is made simple with MongoDB Atlas.

  1. Register a free Atlas account or sign in to your existing Atlas account.

  2. Follow the instructions (select Atlas UI as the procedure) to deploy your first cluster.

  3. Create the database: knowledge_base.

  4. Within the database knowledge_base, create the following collections: research_papers

  5. Create a vector search index named vector_index for the research_papers collection. This index enables the RAG application to retrieve records as additional context to supplement user queries via vector search. Below is the JSON definition of the data collection vector search index.

Below is a snipper of what the vector search index definition should looks like:

	   {
     "fields": [
       {
         "numDimensions": 1024,
         "path": "embedding",
         "similarity": "cosine",
         "type": "vector"
       }
     ]
   }

[6]
Connection to MongoDB successful
[7]
DeleteResult({'n': 100, 'electionId': ObjectId('7fffffff000000000000001f'), 'opTime': {'ts': Timestamp(1713636955, 100), 't': 31}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1713636955, 100), 'signature': {'hash': b'J\x17\x95:(\x9f\xb4\x96\xcdv:"\xbc\x0c)\x98\xd3\tq\x89', 'keyId': 7320226449804230662}}, 'operationTime': Timestamp(1713636955, 100)}, acknowledged=True)

Data Ingestion

[8]
Data ingestion into MongoDB completed

Vector Search

[9]
[10]

Handling User Queries

[11]
[{'role': 'system', 'content': 'You are a research assitant!'}, {'role': 'user', 'content': 'Query: Get me papers on Artificial Intelligence?\nContinue to answer the query by using the Search Results:\n.'}]

Loading and Using Llama3

[12]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Loading checkpoint shards:   0%|          | 0/4 [00:00<?, ?it/s]
[13]
The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
Setting `pad_token_id` to `eos_token_id`:128001 for open-end generation.
I'd be happy to help you with that. Here are some research papers on Artificial Intelligence that I found using a search engine:

**1. "Deep Learning" by Yann LeCun, Yoshua Bengio, and Geoffrey Hinton (2015)**

This paper is a seminal work on deep learning, a subset of AI that involves training neural networks to perform tasks such as image recognition, speech recognition, and natural language processing.

Source: LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep learning. Nature, 521(7553), 436-444. doi: 10.1038/nature14539

**2. "AlphaGo: Mastering the Game of Go with Deep Neural Networks and Tree Search" by Demis Hassabis, Shane Legg, and Joseph Modayil (2015)**

This paper describes the development of AlphaGo, a computer program that uses AI to play the game of Go. AlphaGo was able to defeat a human world champion in a five-game match, marking a significant milestone in AI research.

Source: Hassabis, D., Legg, S., & Modayil, J. (2015). AlphaGo: Mastering the