
    3fi                    z    d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ  G d de      Zy)	    )annotationsN)AnyDictIterableListLiteralOptionalTupleUnion)Document)
Embeddings)VectorStorec                     e Zd ZU dZdZded<   	 d	 	 	 	 	 	 	 	 	 	 	 ddZedd e e	j                         j                        f	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zed e e	j                         j                        f	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 dd	       Z	 d	 	 	 	 	 	 	 dd
ZddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 ddZy)
PGVecto_rsz!VectorStore backed by pgvecto_rs.Nr   
_embeddingc                |    	 ddl m}  |||||      | _        || _        y# t        $ r}t        d      |d}~ww xY w)a  Initialize a PGVecto_rs vectorstore.

        Args:
            embedding: Embeddings to use.
            dimension: Dimension of the embeddings.
            db_url: Database URL.
            collection_name: Name of the collection.
            new_table: Whether to create a new table or connect to an existing one.
            If true, the table will be dropped if exists, then recreated.
            Defaults to False.
        r   )	PGVectoRszWUnable to import pgvector_rs.sdk , please install with `pip install "pgvecto_rs[sdk]"`.N)db_urlcollection_name	dimensionrecreate)pgvecto_rs.sdkr   ImportError_storer   )self	embeddingr   r   r   	new_tabler   es           i/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/vectorstores/pgvecto_rs.py__init__zPGVecto_rs.__init__   sV    &	0  +	
 $  	3 	s   ! 	;6; c                    |j                  d      }t        |      }|t        d       | ||||      }	 |	j                  ||fi | |	S )zAReturn VectorStore initialized from texts and optional metadatas.Hello pgvecto_rs!zdb_url must be providedr   r   r   r   )embed_querylen
ValueError	add_texts)
clstextsr   	metadatasr   r   kwargssample_embeddingr   _selfs
             r   
from_textszPGVecto_rs.from_texts4   sd     %001DE()	>677+	
 	y3F3    c                    |D cg c]  }|j                    }}|D cg c]  }|j                   }} | j                  |||||fi |S c c}w c c}w )z.Return VectorStore initialized from documents.)page_contentmetadatar/   )	r)   	documentsr   r   r   r,   documentr*   r+   s	            r   from_documentszPGVecto_rs.from_documentsL   sf     8AA8&&AA7@A8X&&A	As~~9i
DJ
 	
 BAs
   A
Ac                N    |j                  d      } | |t        |      ||      S )zCreate new empty vectorstore with collection_name.
        Or connect to an existing vectorstore in database if exists.
        Arguments should be the same as when the vectorstore was created.r#   r$   )r%   r&   )r)   r   r   r   r-   s        r   from_collection_namezPGVecto_rs.from_collection_name\   s5     %001DE*++	
 	
r0   c           
     X   ddl m} | j                  j                  t	        |            }t        |||xs g       D cg c]  \  }}}|j                  |||       }	}}}| j                  j                  |	       |	D 
cg c]  }
t        |
j                         c}
S c c}}}w c c}
w )a[  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            kwargs: vectorstore specific parameters

        Returns:
            List of ids of the added texts.

        r   )Record)r   r:   r   embed_documentslistzip	from_textr   insertstrid)r   r*   r+   r,   r:   
embeddingstextr   metarecordsrecords              r   r(   zPGVecto_rs.add_textsp   s    " 	*__44T%[A
 *-UJ	R)P
 
%i T9d3
 
 	7#-456FII55

 6s    B B'c                     | j                   |D cg c]  }|j                   c}|D cg c]  }|j                   c}fi |S c c}w c c}w )zRun more documents through the embeddings and add to the vectorstore.

        Args:
            documents (List[Document]): List of documents to add to the vectorstore.

        Returns:
            List of ids of the added documents.
        )r(   r2   r3   )r   r4   r,   r5   s       r   add_documentszPGVecto_rs.add_documents   sO     t~~3<=xX""=/898X9
 
 	
=9s
   AA

c                   ddl m} dddd}|d}nt        |t              r	 ||      }n|}| j                  j                  |||   ||      }	|	D 
cg c].  }
t        |
d   j                  |
d   j                  	      |
d
   f0 c}
S c c}
w )z9Return docs most similar to query vector, with its score.r   )meta_containsz<->z<#>z<=>)sqrt_euclidneg_dot_prodned_cosN)filter)r2   r3      )	pgvecto_rs.sdk.filtersrJ   
isinstancedictr   searchr   rC   rD   )r   query_vectorkdistance_funcrN   r,   rJ   distance_func_mapreal_filterresultsress              r   &similarity_search_with_score_by_vectorz1PGVecto_rs.similarity_search_with_score_by_vector   s     	9 !!

 >K%'/K K++$$m,	 % 
 	
  !$Q V[[ A	
 		
 	
s   3B	c                ^     | j                   |||fi |D cg c]  \  }}|	 c}}S c c}}w N)r[   )r   r   rU   rV   rN   r,   doc_scores           r   similarity_search_by_vectorz&PGVecto_rs.similarity_search_by_vector   sE      KtJJ1m /5 
V 
 	
 
s   )c                b    | j                   j                  |      } | j                  |||fi |S r]   r   r%   r[   )r   queryrU   rV   r,   rT   s         r   similarity_search_with_scorez'PGVecto_rs.similarity_search_with_score   s<     2259:t::!]
.4
 	
r0   c                    | j                   j                  |      } | j                  |||fi |D cg c]  \  }}|	 c}}S c c}}w )z"Return docs most similar to query.rb   )r   rc   rU   rV   r,   rT   r^   r_   s           r   similarity_searchzPGVecto_rs.similarity_search   s[     2259  KtJJa 28 
V 
 	
 
s   A)F)r   r   r   intr   r@   r   r@   r   boolreturnNone)r*   	List[str]r   r   r+   Optional[List[dict]]r   r@   r   r@   r,   r   ri   r   )r4   List[Document]r   r   r   r@   r   r@   r,   r   ri   r   )r   r   r   r@   r   r@   ri   r   r]   )r*   zIterable[str]r+   rl   r,   r   ri   rk   )r4   rm   r,   r   ri   rk   )   rK   N)rT   List[float]rU   rg   rV   1Literal['sqrt_euclid', 'neg_dot_prod', 'ned_cos']rN   z Union[None, Dict[str, Any], Any]r,   r   ri   List[Tuple[Document, float]])r   ro   rU   rg   rV   rp   rN   zOptional[Any]r,   r   ri   rm   )rn   rK   )
rc   r@   rU   rg   rV   rp   r,   r   ri   rq   )
rc   r@   rU   rg   rV   rp   r,   r   ri   rm   )__name__
__module____qualname____doc__r   __annotations__r    classmethodr@   uuiduuid4hexr/   r6   r8   r(   rH   r[   r`   rd   rf    r0   r   r   r      s   +F   $ $  $ 	 $
  $  $ 
 $F 
 +/":4::<#3#34  (	
    
 . 
 ":4::<#3#34
!
 
 	

 
 
 

 
 

 
 	

 

 
, +/66 (6 	6
 
66
&  37)
!)
 )

	)
 1)
 )
 
&)
\   $

 

	
 
 
 

(  

 

	
 
 
&
"  

 

	
 
 

r0   r   )
__future__r   rx   typingr   r   r   r   r   r	   r
   r   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   r   r{   r0   r   <module>r      s+    "  M M M - 0 3j
 j
r0   