
    kg
                     h    d dl Z d dlZd dlZd dlZd dlZ ej        e          Z G d d          Z	dS )    Nc                   P    e Zd ZddededefdZd Zd Zd Zd	 Z	d
 Z
d ZddZdS )VectorStore  
index_pathmetadata_path	dimensionc                     || _         || _        || _        d | _        i | _        |                                  |                                  d S )N)r   r   r   indexmetadata_load_index_load_metadata)selfr   r   r   s       :E:\project-album-python\Demo Project\Utils\vector_store.py__init__zVectorStore.__init__
   sP    $*"
    c                    t           j                            | j                  rbt                              d           t          j        | j                  | _        t                              d| j        j	         d           dS t          
                    d           t          j        | j                  | _        dS )z3Load FAISS index from disk or initialize a new one.z Loading FAISS index from disk...zFAISS index loaded with z recordsz3FAISS index file not found. Initializing new index.N)ospathexistsr   loggerinfofaiss
read_indexr
   ntotalwarningIndexFlatL2r   r   s    r   r   zVectorStore._load_index   s    7>>$/** 	;KK:;;;)$/::DJKKN4:3DNNNOOOOONNPQQQ*4>::DJJJr   c                    t           j                            | j                  r	 t	          | j        d          5 }t          j        |          | _        ddd           n# 1 swxY w Y   t          	                    dt          | j                   d           dS # t          $ r<}t                              dt          |                      i | _        Y d}~dS d}~ww xY wdS )zLoad metadata from disk.rNz"Metadata loaded successfully with z entrieszError loading metadata: )r   r   r   r   openjsonloadr   r   r   len	Exceptionerrorstr)r   fes      r   r   zVectorStore._load_metadata   s   7>>$,-- 	##$,c22 1a$(IaLLDM1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]T]ASAS]]]^^^^^ # # #@A@@AAA "#	# 	#s:   B A!B !A%%B (A%)3B 
C$(1CC$c                 4    | j         duo| j         j        dk    S )z=Check if the FAISS index is initialized and contains records.Nr   )r
   r   r   s    r   is_index_initializedz VectorStore.is_index_initialized(   s    z%?$**;a*??r   c                 Z   t          j        | j        | j                   t	          | j        d          5 }t          j        | j        |           ddd           n# 1 swxY w Y   t          
                    d| j                    t          
                    d| j                    dS )z*Save the FAISS index and metadata to disk.wNzSaved FAISS index to zSaved metadata to )r   write_indexr
   r   r    r   r!   dumpr   r   r   )r   r'   s     r   
save_indexzVectorStore.save_index,   s    $*do666$$c** 	(aIdmQ'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(=DO==>>>=);==>>>>>s   AA #A c                     | j         sdS t          t          t          | j                                                   d          dz   S )z5Generate a unique file ID based on existing metadata.   r   )default)r   maxmapintkeysr   s    r   get_next_file_idzVectorStore.get_next_file_id4   sA    } 	13sDM..00111===AAr   c                    || j         v r t                              d| d           dS 	 t          j        |t          j                  }| j        t          d          |                                 }| j        	                    |           || j         t          |          <   |                                  t                              dt          |           d|            t                              d| j        j                    dS # t          $ r0}t                              d	t          |                       d}~ww xY w)
zDInsert new records into FAISS index if they are not already indexed.zSkipping file z as it's already indexed.NdtypezIndex not initializedz	Inserted z records for file ID zTotal records in FAISS index: zRecord insertion failed: )r   r   r   nparrayfloat32r
   
ValueErrorr7   addr&   r/   r#   r   r$   r%   )r   
embeddingsr   	file_namefile_idr(   s         r   insert_recordszVectorStore.insert_records:   s=   %%KKMMMMNNNF	*BJ???Jz! !8999++--GJNN:&&&*2DM#g,,'OOKKSC
OOSS'SSTTTKKL9JLLMMMMM 	 	 	LL=SVV==>>>	s   C#D 
E
+EE
   c                    |                                  st                              d           g S 	 t          j        |t          j                                      dd          }| j                            ||          \  }}g }t          |d         |d                   D ]k\  }}|dk    rt          |          }t          |          }| j                            t          |          i           }|                    |||d           lt                              dt#          |           d           |S # t$          $ r6}	t                              d	t          |	                      g cY d
}	~	S d
}	~	ww xY w)z5Search the FAISS index for the most relevant results.z$Search failed: FAISS index is empty.r9   r1   r   )r
   distancer   zFound z search results.zSearch failed: N)r*   r   r%   r;   r<   r=   reshaper
   searchzipr5   floatr   getr&   appendr   r#   r$   )
r   query_embeddingtop_k	distancesindicesresultsidxdistmetadata_entryr(   s
             r   search_indexzVectorStore.search_indexQ   s   ((** 	LL?@@@I	 hbjIIIQQRSUWXXO!%!2!2?E!J!JIwG Yq\::  	T"99 #hhT{{!%!2!23s88R!@!@  $ .        KK?W???@@@N 	 	 	LL33q6633444IIIIII	s   DD; ;
E;+E60E;6E;N)r   )rD   )__name__
__module____qualname__r&   r5   r   r   r   r*   r/   r7   rC   rV    r   r   r   r   	   s         3 s s    ; ; ;	# 	# 	#@ @ @? ? ?B B B  .           r   r   )
r   r!   loggingr   numpyr;   	getLoggerrW   r   r   rZ   r   r   <module>r^      s    				       		8	$	$h h h h h h h h h hr   