
    dhJ5                        S SK Jr  S SKrS SKrS SKJrJrJrJrJ	r	J
r
Jr  S SKJr  S SKJr  S SKJr  S SKJr  SrS	r " S
 S\5      rg)    )annotationsN)AnyDictIterableListOptionalTupleType)Document)
Embeddings)get_from_dict_or_env)VectorStorei   langchain_pg_embeddingc                     \ rS rSrSr\\SS4             SS jjr  SS jr\	SS j5       r
\SS\\S4                   SS jj5       r            SS	 jr  S         SS
 jjr  S         SS jjr  S         SS jjr  S       S S jjr  S       S!S jjr\S\\SS4                   S"S jj5       r\S\\SS4                 S#S jj5       r\\\S4             S$S jj5       r\S%S j5       r\\\SS4                 S&S jj5       r\            S'S j5       rSrg)(Hologres   a  `Hologres API` vector store.

- `connection_string` is a hologres connection string.
- `embedding_function` any embedding function implementing
    `langchain.embeddings.base.Embeddings` interface.
- `ndims` is the number of dimensions of the embedding output.
- `table_name` is the name of the table to store embeddings and data.
    (default: langchain_pg_embedding)
    - NOTE: The table will be created when initializing the store (if not exists)
        So, make sure the user has the right permissions to create tables.
- `pre_delete_table` if True, will delete the table if it exists.
    (default: False)
    - Useful for testing.
FNc                    Xl         X0l        X@l        X l        XPl        U=(       d    [
        R                  " [        5      U l        U R                  5         g N)
connection_stringndims
table_nameembedding_functionpre_delete_tablelogging	getLogger__name__logger__post_init__)selfr   r   r   r   r   r   s          a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/hologres.py__init__Hologres.__init__    sC     "3
$"4 0; 1 1( ;    c                    SSK Jn  U" U R                  U R                  U R                  SS0U R
                  S9U l        g)z
Initialize the store.
r   )HologresVectordocumenttext)r   r   table_schemar   N)hologres_vectorr%   r   r   r   r   storage)r   r%   s     r    r   Hologres.__post_init__1   s;     	3%""**$f-!22
r#   c                    U R                   $ r   )r   )r   s    r    
embeddingsHologres.embeddingsA   s    &&&r#   c	                
   Uc-  U V
s/ sH!  n
[        [        R                  " 5       5      PM#     nn
U(       d  U V
s/ sH  n
0 PM     nn
U R                  U	5      nU " UUUUUS9nUR                  " SXXES.U	D6  U$ s  sn
f s  sn
f )N)r   r   r   r   r   )textsr-   	metadatasids )struuiduuid4get_connection_stringadd_embeddings)clsr0   r-   r   r1   r2   r   r   r   kwargs_r   stores                r    __fromHologres.__fromE   s     ;.34e3tzz|$eC4%*+UUI+55f=/1!-
 	 	
)	
PV	
 ' 5 ,s
   'A;B c                     U Vs/ sH  nSU0PM	     nnU R                   R                  X$X75        gs  snf ! [         a%  nU R                  R	                  U5         SnAgSnAff = f)zAdd embeddings to the vectorstore.

Args:
    texts: Iterable of strings to add to the vectorstore.
    embeddings: List of list of embedding vectors.
    metadatas: List of metadatas associated with the texts.
    kwargs: vectorstore specific parameters
r&   N)r*   upsert_vectors	Exceptionr   	exception)	r   r0   r-   r1   r2   r:   tschema_datases	            r    r8   Hologres.add_embeddingsh   s]     	%5:;UZOUL;LL''
Q < 	%KK!!!$$	%s!   7 27 7 
A&A!!A&c                   Uc-  U Vs/ sH!  n[        [        R                  " 5       5      PM#     nnU R                  R	                  [        U5      5      nU(       d  U Vs/ sH  n0 PM     nnU R                  " XX#40 UD6  U$ s  snf s  snf )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 from adding the texts into the vectorstore.
)r4   r5   r6   r   embed_documentslistr8   )r   r0   r1   r2   r:   r;   r-   s          r    	add_textsHologres.add_texts~   s}    " ;.34e3tzz|$eC4,,<<T%[I
%*+UUI+EyHH
 5
 ,s   'B Bc                V    U R                   R                  US9nU R                  UUUS9$ )a&  Run similarity search with Hologres with distance.

Args:
    query (str): Query text to search for.
    k (int): Number of results to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List of Documents most similar to the query.
)r'   	embeddingkfilter)r   embed_querysimilarity_search_by_vector)r   queryrO   rP   r:   rN   s         r    similarity_searchHologres.similarity_search   s>    " ++77U7C	// 0 
 	
r#   c                Z    U R                  XUS9nU VVs/ sH  u  pgUPM	     snn$ s  snnf )a3  Return docs most similar to embedding vector.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List of Documents most similar to the query vector.
rM   )&similarity_search_with_score_by_vector)r   rN   rO   rP   r:   docs_and_scoresdocr;   s           r    rR   $Hologres.similarity_search_by_vector   s;    " EEV F 
 #22//222s   'c                \    U R                   R                  U5      nU R                  XBUS9nU$ )a*  Return docs most similar to query.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List of Documents most similar to the query and score for each
rM   )r   rQ   rW   )r   rS   rO   rP   rN   docss         r    similarity_search_with_score%Hologres.similarity_search_with_score   s;      ++77>	::V ; 
 r#   c                    U R                   R                  XS/US9nU Vs/ sH  n[        US   US   S9US   4PM     nnU$ s  snf )Nr&   )rO   select_columnsmetadata_filtersmetadata)page_contentrb   distance)r*   searchr   )r   rN   rO   rP   resultsresultr\   s          r    rW   /Hologres.similarity_search_with_score_by_vector   s|     )-(;(;J<& )< )
 "	
 " !'
!3#J/ z" " 	 	
 	
s   Ac           	     l    UR                  [        U5      5      n	U R                  " UU	U4UUUUUS.UD6$ )a  
Return VectorStore initialized from texts and embeddings.
Hologres connection string is required
"Either pass it as a parameter
or set the HOLOGRES_CONNECTION_STRING environment variable.
Create the connection string by calling
HologresVector.connection_string_from_db_params
r1   r2   r   r   r   )rH   rI   _Hologres__from)
r9   r0   rN   r1   r   r   r2   r   r:   r-   s
             r    
from_textsHologres.from_texts   sR    ( ..tE{;
zz

  !-

 

 
	
r#   c           	         U V	s/ sH  oS   PM	     n
n	U V	s/ sH  oS   PM	     nn	U R                   " U
UU4UUUUUS.UD6$ s  sn	f s  sn	f )a  Construct Hologres wrapper from raw documents and pre-
generated embeddings.

Return VectorStore initialized from documents and embeddings.
Hologres connection string is required
"Either pass it as a parameter
or set the HOLOGRES_CONNECTION_STRING environment variable.
Create the connection string by calling
HologresVector.connection_string_from_db_params

Example:
    .. code-block:: python

        from langchain_community.vectorstores import Hologres
        from langchain_community.embeddings import OpenAIEmbeddings
        embeddings = OpenAIEmbeddings()
        text_embeddings = embeddings.embed_documents(texts)
        text_embedding_pairs = list(zip(texts, text_embeddings))
        faiss = Hologres.from_embeddings(text_embedding_pairs, embeddings)
r      rj   )rk   )r9   text_embeddingsrN   r1   r   r   r2   r   r:   rC   r0   r-   s               r    from_embeddingsHologres.from_embeddings  ss    @  //!1/$34OqdO
4zz

  !-

 

 
	
 04s
   AAc                <    U R                  U5      nU " UUUUUS9nU$ )z
Get instance of an existing Hologres store.This method will
return the instance of the store without inserting any new
embeddings
)r   r   r   r   r   )r7   )r9   rN   r   r   r   r:   r   r<   s           r    from_existing_indexHologres.from_existing_indexF  s5      55f=/!(-
 r#   c                @    [        USSS9nU(       d  [        S5      eU$ )Nr   HOLOGRES_CONNECTION_STRING)datakeyenv_keyzHologres connection string is requiredEither pass it as a parameteror set the HOLOGRES_CONNECTION_STRING environment variable.Create the connection string by callingHologresVector.connection_string_from_db_params)r   
ValueError)r9   r:   r   s      r    r7   Hologres.get_connection_stringa  s6    !5#0"
 !B  ! r#   c                    U Vs/ sH  oR                   PM     n	nU Vs/ sH  oR                  PM     n
nU R                  U5      nXS'   U R                  " SU	UUU
UUUS.UD6$ s  snf s  snf )a  
Return VectorStore initialized from documents and embeddings.
Hologres connection string is required
"Either pass it as a parameter
or set the HOLOGRES_CONNECTION_STRING environment variable.
Create the connection string by calling
HologresVector.connection_string_from_db_params
r   )r0   pre_delete_collectionrN   r1   r2   r   r   r3   )rc   rb   r7   rl   )r9   	documentsrN   r   r   r2   r~   r:   dr0   r1   r   s               r    from_documentsHologres.from_documentst  s    ( *33A3)23AZZ	355f=&7"#~~ 	
"7!	
 	
 		
 43s
   A$A)c           
     $    SU SU SU SU SU 3
$ )z2Return connection string from database parameters.zdbname=z user=z
 password=z host=z port=r3   )r9   hostportdatabaseuserpasswords         r     connection_string_from_db_params)Hologres.connection_string_from_db_params  s-     hZvdV:hZvdV6RVQWX	
r#   )r   r   r   r   r   r*   r   )r   r4   r   r   r   intr   r4   r   boolr   zOptional[logging.Logger]returnNone)r   r   )r   r   )r0   	List[str]r-   List[List[float]]r   r   r1   Optional[List[dict]]r2   Optional[List[str]]r   r   r   r4   r   r   r:   r   r   r   )r0   Iterable[str]r-   r   r1   z
List[dict]r2   r   r:   r   r   r   )NN)
r0   r   r1   r   r2   r   r:   r   r   r   )   N)
rS   r4   rO   r   rP   Optional[dict]r:   r   r   List[Document])
rN   List[float]rO   r   rP   r   r:   r   r   r   )rS   r4   rO   r   rP   r   r   List[Tuple[Document, float]])rN   r   rO   r   rP   r   r   r   )r9   Type[Hologres]r0   r   rN   r   r1   r   r   r   r   r4   r2   r   r   r   r:   r   r   r   )rp   zList[Tuple[str, List[float]]]rN   r   r1   r   r   r   r   r4   r2   r   r   r   r:   r   r   r   )r9   r   rN   r   r   r   r   r4   r   r   r:   r   r   r   )r:   zDict[str, Any]r   r4   )r9   r   r   r   rN   r   r   r   r   r4   r2   r   r~   r   r:   r   r   r   )r   r4   r   r   r   r4   r   r4   r   r4   r   r4   )r   
__module____qualname____firstlineno____doc__ADA_TOKEN_COUNT_LANGCHAIN_DEFAULT_TABLE_NAMEr!   r   propertyr-   classmethodrk   r8   rJ   rT   rR   r]   rW   rl   rq   rt   r7   r   r   __static_attributes__r3   r#   r    r   r      s   & %7!&+/ ' 	
   ) 
"
	
  ' '  +/#'$7!&   &  '	 
 (  !          
   D%% &% 	%
 % % 
%2 +/#'	 ( !	
  
@ !%	

 
 	

 
 

6 !%	33 3 	3
 3 
32 !%	  	
 
&2 !%	  	
 
&, 
 +/$7#'!&


 
 (	

 
 
 !
 
 
 

 
B 
 +/$7#'!&,
6,
 ,
 (	,

 ,
 ,
 !,
 ,
 ,
 
,
 ,
\  %7!&  	
   
 4 ! !$ 
 %7#'&+"
"
!"
 "
 	"

 "
 !"
  $"
 "
 
"
 "
H 

 
 	

 
 
 

 
r#   r   )
__future__r   r   r5   typingr   r   r   r   r   r	   r
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   r   r   r   r3   r#   r    <module>r      s<    "   C C C - 0 5 3 8 U
{ U
r#   