
    hJ5                        d dl mZ d dlZd dlZd dl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 d dlmZ dZd	Z G d
 de      Zy)    )annotationsN)AnyDictIterableListOptionalTupleType)Document)
Embeddings)get_from_dict_or_env)VectorStorei   langchain_pg_embeddingc                     e Zd ZdZeeddf	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 ddZedd       Z	e
ddeedf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 dd	Z	 	 d	 	 	 	 	 	 	 	 	 dd
Z	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 ddZe
deeddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Ze
deeddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!d       Ze
eedf	 	 	 	 	 	 	 	 	 	 	 	 	 d"d       Ze
d#d       Ze
eeddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$d       Ze
	 	 	 	 	 	 	 	 	 	 	 	 d%d       Zy)&Hologresa  `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s t        j                  t              | _        | j                          y N)
connection_stringndims
table_nameembedding_functionpre_delete_tablelogging	getLogger__name__logger__post_init__)selfr   r   r   r   r   r   s          g/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/hologres.py__init__zHologres.__init__    sM     "3
$"4 0; 1 1( ;    c                    ddl m}  || j                  | j                  | j                  ddi| j
                        | _        y)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   zHologres.__post_init__1   s;     	3%""**$f-!22
r!   c                    | j                   S r   )r   )r   s    r   
embeddingszHologres.embeddingsA   s    &&&r!   c	                    |*|D 
cg c]  }
t        t        j                               ! }}
|s|D 
cg c]  }
i  }}
| j                  |	      } | |||||      } |j                  d||||d|	 |S c c}
w c c}
w )N)r   r   r   r   r   )textsr*   	metadatasids )struuiduuid4get_connection_stringadd_embeddings)clsr,   r*   r   r-   r.   r   r   r   kwargs_r   stores                r   __fromzHologres.__fromE   s     ;.343tzz|$4C4%*++I+55f=/1!-
 	 	
J)	
PV	
 ' 5 ,s
   $A5	A:c                    	 |D cg c]  }d|i }}| j                   j                  ||||       yc c}w # t        $ r%}| j                  j	                  |       Y d}~yd}~ww xY w)a/  Add 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   r,   r*   r-   r.   r6   tschema_datases	            r   r4   zHologres.add_embeddingsh   s`     	%5:;ZO;L;LL''
CLQ < 	%KK!!!$$	%s!   7 2 7 7 	A% A  A%c                   |*|D cg c]  }t        t        j                               ! }}| j                  j	                  t        |            }|s|D cg c]  }i  }} | j                  ||||fi | |S c c}w c c}w )ar  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.
        )r0   r1   r2   r   embed_documentslistr4   )r   r,   r-   r.   r6   r7   r*   s          r   	add_textszHologres.add_texts~   s    " ;.343tzz|$4C4,,<<T%[I
%*++I+E:y#HH
 5
 ,s   $A:	A?c                b    | j                   j                  |      }| j                  |||      S )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   queryrH   rI   r6   rG   s         r   similarity_searchzHologres.similarity_search   s>    " ++77U7C	// 0 
 	
r!   c                `    | j                  |||      }|D cg c]  \  }}|	 c}}S c c}}w )ak  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.
        rF   )&similarity_search_with_score_by_vector)r   rG   rH   rI   r6   docs_and_scoresdocr7   s           r   rK   z$Hologres.similarity_search_by_vector   s;    " EE1V F 
 #22Q222s   *c                d    | j                   j                  |      }| j                  |||      }|S )ab  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
        rF   )r   rJ   rO   )r   rL   rH   rI   rG   docss         r   similarity_search_with_scorez%Hologres.similarity_search_with_score   s=      ++77>	::1V ; 
 r!   c                    | j                   j                  ||dg|      }|D cg c]  }t        |d   |d         |d   f }}|S c c}w )Nr$   )rH   select_columnsmetadata_filtersmetadata)page_contentrX   distance)r(   searchr   )r   rG   rH   rI   resultsresultrS   s          r   rO   z/Hologres.similarity_search_with_score_by_vector   sy     )-(;(;J<& )< )
 "	
  !'
!3#J/ z"	
 	
 	
s   Ac           	     l    |j                  t        |            }	 | j                  ||	|f|||||d|S )aM  
        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
        r-   r.   r   r   r   )rB   rC   _Hologres__from)
r5   r,   rG   r-   r   r   r.   r   r6   r*   s
             r   
from_textszHologres.from_texts   sT    ( ..tE{;
szz

  !-

 

 
	
r!   c           	         |D 	cg c]  }	|	d   	 }
}	|D 	cg c]  }	|	d   	 }}	 | j                   |
||f|||||d|S c c}	w c c}	w )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      r_   )r`   )r5   text_embeddingsrG   r-   r   r   r.   r   r6   r>   r,   r*   s               r   from_embeddingszHologres.from_embeddings  sw    @  //!1//$34qad4
4szz

  !-

 

 
	
 04s
   A Ac                B    | j                  |      } | |||||      }|S )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   )r3   )r5   rG   r   r   r   r6   r   r8   s           r   from_existing_indexzHologres.from_existing_indexF  s5      55f=/!(-
 r!   c                <    t        |dd      }|st        d      |S )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)r5   r6   r   s      r   r3   zHologres.get_connection_stringa  s6    !5#0"
 !B  ! r!   c                    |D cg c]  }|j                    }	}|D cg c]  }|j                   }
}| j                  |      }||d<    | j                  d|	|||
|||d|S c c}w c c}w )aQ  
        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   )r,   pre_delete_collectionrG   r-   r.   r   r   r/   )rY   rX   r3   ra   )r5   	documentsrG   r   r   r.   ro   r6   dr,   r-   r   s               r   from_documentszHologres.from_documentst  s    ( *33A33)23AQZZ3	355f=&7"#s~~ 	
"7!	
 	
 		
 43s
   A#A(c           
     $    d| d| d| d| d| 
S )z2Return connection string from database parameters.zdbname=z user=z
 password=z host=z port=r/   )r5   hostportdatabaseuserpasswords         r    connection_string_from_db_paramsz)Hologres.connection_string_from_db_params  s-     hZvdV:hZvdV6RVQWX	
r!   )r   r0   r   r   r   intr   r0   r   boolr   zOptional[logging.Logger]returnNone)r|   r}   )r|   r   )r,   	List[str]r*   List[List[float]]r   r   r-   Optional[List[dict]]r.   Optional[List[str]]r   rz   r   r0   r   r{   r6   r   r|   r   )r,   Iterable[str]r*   r   r-   z
List[dict]r.   r~   r6   r   r|   r}   )NN)
r,   r   r-   r   r.   r   r6   r   r|   r~   )   N)
rL   r0   rH   rz   rI   Optional[dict]r6   r   r|   List[Document])
rG   List[float]rH   rz   rI   r   r6   r   r|   r   )rL   r0   rH   rz   rI   r   r|   List[Tuple[Document, float]])rG   r   rH   rz   rI   r   r|   r   )r5   Type[Hologres]r,   r~   rG   r   r-   r   r   rz   r   r0   r.   r   r   r{   r6   r   r|   r   )rd   zList[Tuple[str, List[float]]]rG   r   r-   r   r   rz   r   r0   r.   r   r   r{   r6   r   r|   r   )r5   r   rG   r   r   rz   r   r0   r   r{   r6   r   r|   r   )r6   zDict[str, Any]r|   r0   )r5   r   rp   r   rG   r   r   rz   r   r0   r.   r   ro   r{   r6   r   r|   r   )rt   r0   ru   rz   rv   r0   rw   r0   rx   r0   r|   r0   )r   
__module____qualname____doc__ADA_TOKEN_COUNT_LANGCHAIN_DEFAULT_TABLE_NAMEr    r   propertyr*   classmethodr`   r4   rD   rM   rK   rT   rO   ra   re   rg   r3   rr   ry   r/   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   r1   typingr   r   r   r   r   r	   r
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   r   r   r   r/   r!   r   <module>r      s<    "   C C C - 0 5 3 8 U
{ U
r!   