File size: 1,529 Bytes
37e46db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c01f927
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import pandas as pd
from tqdm import tqdm
from src.utils.vector_store_utils import *
from src.entity import VectorStoreConfig


class VectorStore:
    def __init__(self, config:VectorStoreConfig):
        self.config = config
        self.pc_index=pc_client.Index(self.config.pc_index)
    
    
    def initiate_vector_upsertion(self, data_ingestion_dir):
        files = os.listdir(data_ingestion_dir)
        
        for file in files:
            namespace = file.split(".csv")[0]
            
            df = pd.read_csv(os.path.join(data_ingestion_dir, file))
            df['description'] = df['title'] + ' \n ' + df['category']
            records = df.to_dict('records')
            
            print(f"Category:{namespace} vector store...")
            
            BATCH_SIZE = 64
            for i in tqdm(range(0, len(records), BATCH_SIZE)):
                try:
                    batch = records[i:i+BATCH_SIZE]
                    
                    pc_records = []
                    for product in batch:
                        product_ebd = get_embedding(product['description'])
                        pc_record = {
                            'id': product['objectID'],
                            'values': product_ebd.flatten().tolist()
                        }
                        pc_records.append(pc_record)

                    self.pc_index.upsert(vectors=pc_records, namespace=namespace)
                except Exception as e: 
                    raise CustomException(e, sys)