Sponsored Content
Skip to content

Latest commit

ย 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

NotionLM

This project demonstrates the use of RAG, The Front-end is implemented using Nextjs and then the Back-end using FastAPI and LlamaIndex which would enable the RAG pipeline.

How RAG Works

flowchart LR
  A[Notion API] --> B[Ingest + Chunk]
  B --> C[Embeddings + Index]
  D[User Question] --> E[Retrieve Top-K Chunks]
  E --> F[LLM Answer + Sources]

Loading

The first step would be ingesting and chunking, basically it means that we are fetching the content inside the Notion pages, cleaning it, splitting it into chunks and storing those chunks in an index so they can be retrieved later.

So in this project, in the app/api/notion/ingest/route.ts file we would be fetching the pages content, then we call the /ingest endpoint and it will validate the input, creates the dataset index (Dataset is the index namespace (stored at .rag/indexes/), so you can keep multiple separate indexes) and then the chunking is done using SentenceSplitter from Llamaindex, specifically:

SentenceSplitter(chunk_size=512, chunk_overlap=64)

So here we split the text into 512 token chunks with 64 token overlap. Then we build the data document using the Document class and then using VectorStoreIndex.from_documents or index.insert(), Llamaindex embeds those chunks and stores them in the index.

Then when we query, we call the /query endpoint which would load the dataset index from disk then using load_index_from_storage it would retrieve the index and then:

retriever = index.as_retriever(similarity_top_k=4)
nodes = retriever.retrieve(question)

Here as_retriever embeds the question, compares it to the stored chunk embeddings and returns the top 4 similar chunks.

query_engine = index.as_query_engine(similarity_top_k=4)
response = query_engine.query(question)

Here query_engine.query uses those chunks as context to generate the answer. So basically the model would be using the knowledge it got from these chunks to be able to give you an answer.

Environment Variables

Make sure to add the following Environment Variables:

export OPENAI_API_KEY=<openai-key>
export RAG_SERVICE_URL=<url>
export NOTION_API_KEY=<notion-key>

How To Run

To run it locally, first execute:

npm install

Then navigate to the rag_service and create a python virtual environment install the packages using requirements.txt:

cd rag_service
uv venv
source .venv/bin/activate
uv pip install -r requirements.txt

I'm using uv package manager. Then after installation you can run the FastAPI server using:

uvicorn app:app --reload

And then in another terminal set the RAG_SERVICE_URL:

export RAG_SERVICE_URL=http://localhost:8000
npm run dev

Note: Make sure to set the OPENAI_API_KEY before running it

NotionLM

About

Talk to your notion documents using RAG

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages