You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: tutorials/how-to-implement-rag/index.mdx
+54-1
Original file line number
Diff line number
Diff line change
@@ -80,6 +80,8 @@ With Scaleway's fully managed services, integrating RAG becomes a streamlined pr
80
80
cur.execute("CREATE EXTENSION IF NOT EXISTS vector;")
81
81
conn.commit()
82
82
```
83
+
The command above ensures that pgvector is installed on your database if it hasn't been already.
84
+
83
85
2. To avoid reprocessing documents that have already been loaded and vectorized, create a table in your PostgreSQL database to track them. This ensures that new documents added to your object storage bucket are processed only once, preventing duplicate downloads and redundant vectorization.
84
86
85
87
```python
@@ -89,6 +91,8 @@ With Scaleway's fully managed services, integrating RAG becomes a streamlined pr
89
91
90
92
### Set Up Document Loaders for Object Storage
91
93
94
+
The document loader pulls documents from your Scaleway Object Storage bucket. This loader will retrieve the contents of each document for further processing.s
95
+
92
96
```python
93
97
document_loader = S3DirectoryLoader(
94
98
bucket=os.getenv('SCW_BUCKET_NAME'),
@@ -101,25 +105,55 @@ With Scaleway's fully managed services, integrating RAG becomes a streamlined pr
101
105
102
106
### Embeddings and Vector Store Setup
103
107
104
-
We will utilize the OpenAIEmbeddings class from LangChain and store the embeddings in PostgreSQL using the PGVector integration.
108
+
1.We will utilize the OpenAIEmbeddings class from LangChain and store the embeddings in PostgreSQL using the PGVector integration.
- openai_api_key: This is your API key for accessing the OpenAI-powered embeddings service, in this case, deployed via Scaleway’s Managed Inference.
121
+
- openai_api_base: This is the base URL that points to your deployment of the sentence-transformers/sentence-t5-xxl model on Scaleway's Managed Inference. This URL serves as the entry point to make API calls for generating embeddings.
122
+
- model="sentence-transformers/sentence-t5-xxl": This defines the specific model being used for text embeddings. sentence-transformers/sentence-t5-xxl is a powerful model optimized for generating high-quality sentence embeddings, making it ideal for tasks like document retrieval in RAG systems.
123
+
- tiktoken_enabled=False: This is an important parameter, which disables the use of TikToken for tokenization within the embeddings process.
124
+
125
+
What is tiktoken_enabled?
126
+
127
+
tiktoken is a tokenization library developed by OpenAI, which is optimized for working with GPT-based models (like GPT-3.5 or GPT-4). It transforms text into smaller token units that the model can process.
128
+
129
+
Why set tiktoken_enabled=False?
130
+
131
+
In the context of using Scaleway’s Managed Inference and the sentence-t5-xxl model, TikToken tokenization is not necessary because the model you are using (sentence-transformers) works with raw text and handles its own tokenization internally.
132
+
Moreover, leaving tiktoken_enabled as True causes issues when sending data to Scaleway’s API because it results in tokenized vectors being sent instead of raw text. Since Scaleway's endpoint expects text and not pre-tokenized data, this mismatch can lead to errors or incorrect behavior.
133
+
By setting tiktoken_enabled=False, you ensure that raw text is sent to Scaleway's Managed Inference endpoint, which is what the sentence-transformers model expects to process. This guarantees that the embedding generation process works smoothly with Scaleway's infrastructure.
134
+
135
+
2. Next, configure the connection string for your PostgreSQL instance and create a PGVector store to store these embeddings.
PGVector: This creates the vector store in your PostgreSQL database to store the embeddings.
144
+
117
145
### Load and Process Documents
118
146
119
147
Use the S3FileLoader to load documents and split them into chunks. Then, embed and store them in your PostgreSQL database.
120
148
149
+
1. Lazy loadings documents: This method is designed to efficiently load and process documents one by one from Scaleway Object Storage. Instead of loading all documents at once, it loads them lazily, allowing us to inspect each file before deciding whether to embed it.
121
150
```python
122
151
files = document_loader.lazy_load()
152
+
```
153
+
Why lazy loading?
154
+
The key reason for using lazy loading here is to avoid reprocessing documents that have already been embedded. In the context of Retrieval-Augmented Generation (RAG), reprocessing the same document multiple times is redundant and inefficient. Lazy loading enables us to check if a document has already been embedded (by querying the database) before actually loading and embedding it.
@@ -140,6 +174,18 @@ Use the S3FileLoader to load documents and split them into chunks. Then, embed a
140
174
vector_store.add_embeddings(embedding, chunk)
141
175
```
142
176
177
+
The code iterates over each file retrieved from object storage using lazy loading.
178
+
For each file, a query is made to check if its corresponding object_key (a unique identifier from the file metadata) exists in the object_loaded table in PostgreSQL.
179
+
If the document has already been processed and embedded (i.e., the object_key is found in the database), the system skips loading the file and moves on to the next one.
180
+
If the document is new (not yet embedded), the file is fully loaded and processed.
181
+
182
+
This approach ensures that only new or modified documents are loaded into memory and embedded, saving significant computational resources and reducing redundant work.
183
+
184
+
Why store both chunk and embedding?
185
+
186
+
Storing both the chunk and its corresponding embedding allows for efficient document retrieval later.
187
+
When a query is made, the RAG system will retrieve the most relevant embeddings, and the corresponding text chunks will be used to generate the final response.
188
+
143
189
### Query the RAG System
144
190
145
191
Now, set up the RAG system to handle queries using RetrievalQA and the LLM.
@@ -159,3 +205,10 @@ Now, set up the RAG system to handle queries using RetrievalQA and the LLM.
159
205
160
206
print(response['result'])
161
207
```
208
+
209
+
210
+
### Conclusion
211
+
212
+
This step is essential for efficiently processing and storing large document datasets for RAG. By using lazy loading, the system handles large datasets without overwhelming memory, while chunking ensures that each document is processed in a way that maximizes the performance of the LLM. The embeddings are stored in PostgreSQL via pgvector, allowing for fast and scalable retrieval when responding to user queries.
213
+
214
+
By combining Scaleway’s Managed Object Storage, PostgreSQL with pgvector, and LangChain’s embedding tools, you can implement a powerful RAG system that scales with your data and offers robust information retrieval capabilities.
0 commit comments