# RAG decomposed

Hey folks, welcome back again to GenAI series

This is your friend [Mubashir](https://x.com/Mubashir_061)

Today we’ll be discussing an interesting thing that is RAG…

I have covered all the steps in detail, open your vs code or any IDE you use, and let’s code together

Rerieval-Augmented Generation — an AI technique that combines the power of retrieving information using LLMs, so that accuracy and context-awareness of AI models can be enhanced.

Getting relevant data to the user according to their needs is a difficult thing

1. Indexing → Data source - chunking - Vector embeddings - stored in vector DB
    
2. User asks a query → make vector embeddings - search in vector DB - we get relevant chunks - filter out the chunks - AI being called (with relevant chunks and user query)
    
3. Implementation- Langchain
    

Let’s make a RAG on PDFs - in simple terms, let’s talk to our pdfs…

first step is to install pdfloader

```python
pip install langchain_community pypdf
```

### Let’s first start by loading a PDF

So, we need to give path to our loader - here is a dynamic way to give path using pathlib library.

After calling load() function - print what the loader has, and you will see the contents of your pdf

```python
from langchain_community.document_loaders import PyPDFLoader
from pathlib import Path

file_path = Path(__file__).parent / "AI_Engineering.pdf"
loader = PyPDFLoader(file_path)

docs = loader.load()

print(docs[50])
```

### Now it’s time for splitting the text

```python
pip install langchain_text_splitters
```

There is one issue still, while splitting - which one is correct fit for us  
For PDFs we need RecursiveCharacterTextSplitter

```python
from langchain_text_splitters import RecursiveCharacterTextSplitter
```

Now, we’ll call the text splitter, we need to also mention how many chunks we need and for better context, do we need to have overlaps between chunks?

```python
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
```

Once we have set the number if chunks and overlaps, it is time for applying it on our document

```python
split_docs = text_splitter.split_documents(documents=docs)
```

Chunking part is done

Now let’s move on with “Embeddings“

Let’s install langchain’s openAI embedding model

```python
pip install langchain-openai
```

Now import it

```python
from langchain_openai import OpenAIEmbeddings
```

Let’s call the embedding model and specify a model and our API\_KEY

```python
embedder = OpenAIEmbeddings(
    model="text-embedding-3-large",
    api_key="YOUR_API_KEY"
)
```

Now it is time to embed our document as vectors and store it in a DB, not a regular DB but a vector database.

We’ll go with [Qdrant DB](https://qdrant.tech/)

Let’s install it

```python
pip install qdrant-client
```

After this, we have to setup DB on [Docker](https://www.docker.com/get-started/), so we’ll create a <mark>docker-compose.db.yml</mark> file

Inside that file we have to write the below code

```python
services:
  qdrant:
    image: qdrant/qdrant
    ports:
      - 6333:6333
```

To run this docker file, paste the below command

```python
docker compose -f docker-compose.db.yml up
```

Done

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744972325684/a249bef0-c238-4681-82f5-397b6df64198.png align="center")

Now it’s time to install langchain-qdrant

```python
pip install langchain-qdrant
```

Import Qdrant client and vector store in our code now

```python
from qdrant_client import QdrantClient
from langchain_qdrant import QdrantVectorStore
```

Create a collection now, it needs url of Qdrant db, collection name and the embedder.

This documents parameter will create a collection for us, if there are no collections in vector DB

```python
vector_store = QdrantVectorStore.from_documents(
    documents=[],
    url="http://localhost:6333",
    collection_name="langchain_learning",
    embedding=embedder
)
```

Also we need to add those split\_docs to our vector\_store

```python
vector_store.add_documents(documents=split_docs)
```

Now run the code and checkout this url

```python
http://localhost:6333/dashboard
```

Once you see the injection is done, you can check the dashboard

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744975507410/178340f8-0059-4478-b1c1-5292a2a4acd7.png align="center")

Now, comment that specific part of storing the data in vector DB as we are done with it

```python
# vector_store = QdrantVectorStore.from_documents(
#     documents=[],
#     url="http://localhost:6333",
#     collection_name="langchain_learning",
#     embedding=embedder
# )

# vector_store.add_documents(documents=split_docs)
```

This same we have to do now, but for retrieving the data from the DB

```python
retriever = QdrantVectorStore.from_existing_collection(
    url="http://localhost:6333",
    collection_name="langchain_learning",
    embedding=embedder
)
```

Now, let’s do the main thing, getting the data from the DB for which we were writing these many lines of code

```python
relevant_chunks =retriever.similarity_search(
    query="What are vector embeddings?"
)
```

Done, now just we have to set up the OpenAI’s response for user’s queries (last and the most important part)

Install OpenAI

```python
pip install openai
```

import it and write a system prompt for it also include our final output - ‘relevant\_chunks‘ variable

```python
from openai import OpenAI
client = OpenAI()

SYSTEM_PROMPT = f"""
    You are a helpful AI assistant who has access to a specific document of user,
    and user will ask questions from it, answer only those,
    i have given you context from where you have to answer it

    context={relevant_chunks}
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": user_query},
    ]
)

print(response.choices[0].message.content)
```

boom, you’re done

let’s look at the output for our query

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745043057490/51c244a4-1926-4ec4-adb8-f0102116edcf.png align="center")

Hope you’ve enjoyed this, let me know in the comments.

Let’s connect on [twitter](https://x.com/Mubashir_061) for further discussions

Peace out ✌️
