Skip to content

Commit c9b993f

Browse files
authored
fix(genapi): RAG tutorial using Generative APIs
WIP - fixing content and snippets not working anymore
1 parent 02bc8a2 commit c9b993f

File tree

1 file changed

+21
-27
lines changed
  • tutorials/how-to-implement-rag-generativeapis

1 file changed

+21
-27
lines changed

tutorials/how-to-implement-rag-generativeapis/index.mdx

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ In this tutorial, you will learn how to implement RAG using LangChain, a leading
3434

3535
### Install required packages
3636

37-
Run the following command to install the required packages:
37+
Run the following command to install the required python packages:
3838

3939
```sh
40-
pip install langchain psycopg2 python-dotenv langchainhub
40+
pip install langchain langchainhub langchain_openai langchain_community langchain_postgres unstructured "unstructured[pdf]" libmagic psycopg2 python-dotenv boto3
4141
```
42+
43+
If you are on MacOS, run the following command to install dependencies required by `unstructured` package:
44+
```sh
45+
brew install libmagic poppler tesseract qpdf
46+
```
47+
4248
### Create a .env file
4349

4450
Create a .env file and add the following variables. These will store your API keys, database connection details, and other configuration values.
@@ -49,7 +55,7 @@ Create a .env file and add the following variables. These will store your API ke
4955
# Scaleway API credentials https://console.scaleway.com/iam/api-keys
5056
## Will be used to authenticate to Scaleway Object Storage and Scaleway Generative APIs
5157
SCW_ACCESS_KEY=your_scaleway_access_key_id
52-
SCW_API_KEY=your_scaleway_secret_key
58+
SCW_SECRET_KEY=your_scaleway_secret_key
5359

5460
# Scaleway Managed Database (PostgreSQL) credentials
5561
## Will be used to store embeddings of your proprietary data
@@ -83,13 +89,6 @@ You can use any PostgreSQL client, such as [psql](https://www.postgresql.org/doc
8389
```sql
8490
CREATE EXTENSION IF NOT EXISTS vector;
8591
```
86-
### Create a table to track processed documents
87-
88-
To prevent reprocessing documents that have already been loaded and vectorized, you should create a table to keep track of them. This will ensure that new documents added to your object storage bucket are only processed once, avoiding duplicate downloads and redundant vectorization:
89-
90-
```sql
91-
CREATE TABLE IF NOT EXISTS object_loaded (id SERIAL PRIMARY KEY, object_key TEXT);
92-
```
9392

9493
### Connect to PostgreSQL programmatically
9594

@@ -101,6 +100,7 @@ Connect to your PostgreSQL instance and perform tasks programmatically.
101100
from dotenv import load_dotenv
102101
import psycopg2
103102
import os
103+
import logging
104104

105105
# Load environment variables
106106
load_dotenv()
@@ -129,30 +129,24 @@ from langchain_openai import OpenAIEmbeddings
129129
from langchain_postgres import PGVector
130130
```
131131

132-
### Configure OpenAI Embeddings
132+
### Configure embeddings client
133133

134-
We will use the [OpenAIEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html) class from LangChain and store the embeddings in PostgreSQL using the PGVector integration.
134+
Configure [OpenAIEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html) class from LangChain to use your API Secret Key, Generative APIs Endpoint URL and a supported model (`bge-multilingual-gemma2` in our example).
135135

136136
```python
137137
# rag.py
138138

139139
embeddings = OpenAIEmbeddings(
140-
openai_api_key=os.getenv("SCW_API_KEY"),
140+
openai_api_key=os.getenv("SCW_SECRET_KEY"),
141141
openai_api_base=os.getenv("SCW_GENERATIVE_APIs_ENDPOINT"),
142-
model="sentence-t5-xxl",
143-
tiktoken_enabled=False,
142+
model="bge-multilingual-gemma2",
143+
check_embedding_ctx_length=False
144144
)
145145
```
146146

147-
#### Key parameters:
148-
- `openai_api_key`: This is your API key for accessing the OpenAI-powered embeddings service, in this case, hosted by Scaleway’s Generative APIs.
149-
- `openai_api_base`: This is the base URL that points Scaleway Generative APIs where the embedding model is hosted. This URL serves as the entry point to make API calls for generating embeddings.
150-
- `model="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.
151-
- `tiktoken_enabled=False`: This parameter disables the use of TikToken for tokenization within the embeddings process.
147+
### Configure vector store client
152148

153-
### Create a pgvector store
154-
155-
Configure the connection string for your PostgreSQL instance and create a pgvector store to store these embeddings.
149+
Configure connection to your PostgreSQL instance storing vectors.
156150

157151
```python
158152
# rag.py
@@ -189,7 +183,7 @@ By loading the metadata for all objects in your bucket, you can speed up the pro
189183
session = boto3.session.Session()
190184
client_s3 = session.client(service_name='s3', endpoint_url=os.getenv("SCW_BUCKET_ENDPOINT", ""),
191185
aws_access_key_id=os.getenv("SCW_ACCESS_KEY", ""),
192-
aws_secret_access_key=os.getenv("SCW_API_KEY", ""))
186+
aws_secret_access_key=os.getenv("SCW_SECRET_KEY", ""))
193187
paginator = client_s3.get_paginator('list_objects_v2')
194188
page_iterator = paginator.paginate(Bucket=os.getenv("SCW_BUCKET_NAME", ""))
195189
```
@@ -218,7 +212,7 @@ for page in page_iterator:
218212
key=obj['Key'],
219213
endpoint_url=os.getenv("SCW_BUCKET_ENDPOINT", ""),
220214
aws_access_key_id=os.getenv("SCW_ACCESS_KEY", ""),
221-
aws_secret_access_key=os.getenv("SCW_API_KEY", "")
215+
aws_secret_access_key=os.getenv("SCW_SECRET_KEY", "")
222216
)
223217
file_to_load = file_loader.load()
224218
cur.execute("INSERT INTO object_loaded (object_key) VALUES (%s)", (obj['Key'],))
@@ -271,7 +265,7 @@ Now, set up the RAG system to handle queries
271265

272266
llm = ChatOpenAI(
273267
base_url=os.getenv("SCW_GENERATIVE_APIs_ENDPOINT"),
274-
api_key=os.getenv("SCW_API_KEY"),
268+
api_key=os.getenv("SCW_SECRET_KEY"),
275269
model="llama-3.1-8b-instruct",
276270
)
277271

@@ -354,4 +348,4 @@ Furthermore, you can continually enhance your RAG system by implementing mechani
354348

355349
By integrating Scaleway Object Storage, Managed Database for PostgreSQL with pgvector, and LangChain’s embedding tools, you have the foundation to build a powerful RAG system that scales with your data while offering robust information retrieval capabilities. This approach equips you with the tools necessary to handle complex queries and deliver accurate, relevant results efficiently.
356350

357-
With ongoing refinement and adaptation, your RAG system can evolve to meet the changing needs of your users, ensuring that it remains a valuable asset in your AI toolkit.
351+
With ongoing refinement and adaptation, your RAG system can evolve to meet the changing needs of your users, ensuring that it remains a valuable asset in your AI toolkit.

0 commit comments

Comments
 (0)