
    Ngm/                        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	 d dl
Zd dlmZ d dlmZ d dlmZ  ej        e          Z G d de          ZdS )	    )annotationsN)AnyIterableListOptionalTypeDocument)
Embeddings)VectorStorec                      e Zd ZU dZdZded<   	 	 	 	 	 d1d2dZed3d            Z	 	 	 d4d5d Z	d6d!Z
	 d7d8d'Ze	 	 	 	 	 	 	 	 	 d9d:d-            Ze	 	 	 	 	 	 	 	 	 d9d;d0            ZdS )<AtlasDBa  `Atlas` vector store.

     Atlas is the `Nomic's` neural database and `rhizomatic` instrument.

    To use, you should have the ``nomic`` python package installed.

    Example:
        .. code-block:: python

                from langchain_community.vectorstores import AtlasDB
                from langchain_community.embeddings.openai import OpenAIEmbeddings

                embeddings = OpenAIEmbeddings()
                vectorstore = AtlasDB("my_project", embeddings.embed_query)
    atlas_idstr_ATLAS_DEFAULT_ID_FIELDNA description for your projectTFnameembedding_functionOptional[Embeddings]api_keyOptional[str]description	is_publicboolreset_project_if_existsreturnNonec                :   	 ddl }ddl m} n# t          $ r t          d          w xY w|t          d          |                    |           || _        d}	| j        d}	 ||||	||t          j                  | _        | j        	                                 dS )	a  
        Initialize the Atlas Client

        Args:
            name (str): The name of your project. If the project already exists,
                it will be loaded.
            embedding_function (Optional[Embeddings]): An optional function used for
                embedding your data. If None, data will be embedded with
                Nomic's embed model.
            api_key (str): Your nomic API key
            description (str): A description for your project.
            is_public (bool): Whether your project is publicly accessible.
                True by default.
            reset_project_if_exists (bool): Whether to reset this project if it
                already exists. Default False.
                Generally useful during development and testing.
        r   N)AtlasProjectzRCould not import nomic python package. Please install it with `pip install nomic`.z/No API key provided. Sign up at atlas.nomic.ai!text	embedding)r   r   modalityr   r   unique_id_field)
nomicr   ImportError
ValueErrorlogin_embedding_functionr   r   project_latest_project_state)
selfr   r   r   r   r   r   r$   r   r"   s
             b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/atlas.py__init__zAtlasDB.__init__"   s    4	LLL******* 	 	 	>  	 ?NOOOG#5 #/"H $|#$;#;
 
 
 	**,,,,,s   
 'c                    | j         S )N)r(   )r+   s    r,   
embeddingszAtlasDB.embeddingsY   s    ''    textsIterable[str]	metadatasOptional[List[dict]]idsOptional[List[str]]refreshkwargsr   	List[str]c                ^   |>t          |          dk    r+d|d                                         v rt          d          t                    d D             | j        | j                                      }t          j        |          }|fdt                    D             }nMt          t          |                    D ].}	|	         ||	         t          j        <   |	         ||	         d<   /|}| j                            g t          j        |           | j                                        5  | j                            ||           ddd           n# 1 swxY w Y   n|fd	t                    D             }n=t                    D ]+\  }	}
||	         d<   |	         ||	         t          j        <   ,|}| j                            g t          j        |           | j                                        5  | j                            |           ddd           n# 1 swxY w Y   |rgt          | j        j                  dk    rJ| j                                        5  | j                                         ddd           n# 1 swxY w Y   S )
a  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts (Iterable[str]): Texts to add to the vectorstore.
            metadatas (Optional[List[dict]], optional): Optional list of metadatas.
            ids (Optional[List[str]]): An optional list of ids.
            refresh(bool): Whether or not to refresh indices with the updated data.
                Default True.
        Returns:
            List[str]: List of IDs of the added texts.
        Nr   r    z#Cannot accept key text in metadata!c                N    g | ]"}t          t          j                              #S  )r   uuiduuid4).0_s     r,   
<listcomp>z%AtlasDB.add_texts.<locals>.<listcomp>z   s&    4443tz||$$444r0   c                N    g | ]!\  }}t           j        |         d |         i"S r    r   r   )r?   ir@   r5   r1   s      r,   rA   z%AtlasDB.add_texts.<locals>.<listcomp>   sA       1 4c!ffeAhO  r0   )id_fielddata)r/   rG   c                B    g | ]\  }}d |t           j        |         iS rC   rD   )r?   rE   r    r5   s      r,   rA   z%AtlasDB.add_texts.<locals>.<listcomp>   s=       4 T7#BCFK  r0   )lenkeysr&   listr(   embed_documentsnpstack	enumerateranger   r   r)   _validate_map_data_inputswait_for_project_lockadd_embeddingsadd_textindicesrebuild_maps)r+   r1   r3   r5   r7   r8   _embeddingsr/   rG   rE   r    s    ` `       r,   	add_textszAtlasDB.add_texts]   s   * !I"")A,++----BCCCU;44e444C #/2BB5IIK+..J      )% 0 0  
 s9~~.. 4 4ADGFIaL!@A+08IaL(( L22W<4 3    3355 N N++z+MMMN N N N N N N N N N N N N N N     #,U#3#3  
  )// K KGAt+0IaL(DGFIaL!@AA L22W<4 3    3355 , ,%%d+++, , , , , , , , , , , , , , ,  	04<'((1,,\7799 0 0L--///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
s6   E++E/2E/H99H= H=<J""J&)J&c                    | j                                         5   | j         j        di |cddd           S # 1 swxY w Y   dS )zCreates an index in your project.

        See
        https://docs.nomic.ai/atlas_api.html#nomic.project.AtlasProject.create_index
        for full detail.
        Nr<   )r)   rR   create_index)r+   r8   s     r,   rZ   zAtlasDB.create_index   s     \//11 	7 	7,4<,66v66	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   9= =   querykintList[Document]c                  	 | j         t          d          | j                             |g          d         }t          j        |                              dd          }| j                                        5  | j        j        d         	                    ||          \  }}| j        
                    |d                   	ddd           n# 1 swxY w Y   	fdt          |          D             }|S )	a  Run similarity search with AtlasDB

        Args:
            query (str): Query text to search for.
            k (int): Number of results to return. Defaults to 4.

        Returns:
            List[Document]: List of documents most similar to the query text.
        NzBAtlasDB requires an embedding_function for text similarity search!r      )queriesr]   )r5   c                \    g | ](\  }}t          |         d          |                   )S )r    )page_contentmetadatar	   )r?   rE   neighborrG   s      r,   rA   z-AtlasDB.similarity_search.<locals>.<listcomp>   sF     
 
 
8 $q'&/DGDDD
 
 
r0   )r(   NotImplementedErrorrL   rM   arrayreshaper)   rR   projectionsvector_searchget_datarO   )
r+   r\   r]   r8   
_embeddingr!   	neighborsr@   docsrG   s
            @r,   similarity_searchzAtlasDB.similarity_search   sM    #+%T   -==ugFFqI
HZ((00B77	\//11 	; 	;<3A6DD!Q E  LIq <((Yq\(::D		; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;
 
 
 
(33
 
 
 s   :ACCCclsType[AtlasDB]r!   index_kwargsOptional[dict]c                L   ||t          d          |dz   dd}|
|
                                D ]
\  }}|||<    | |||d||	          }|j                                        5  |                    |||            |j        d	i | ddd           n# 1 swxY w Y   |S )
a^  Create an AtlasDB vectorstore from a raw documents.

        Args:
            texts (List[str]): The list of texts to ingest.
            name (str): Name of the project to create.
            api_key (str): Your nomic API key,
            embedding (Optional[Embeddings]): Embedding function. Defaults to None.
            metadatas (Optional[List[dict]]): List of metadatas. Defaults to None.
            ids (Optional[List[str]]): Optional list of document IDs. If None,
                ids will be auto created
            description (str): A description for your project.
            is_public (bool): Whether your project is publicly accessible.
                True by default.
            reset_project_if_exists (bool): Whether to reset this project if it
                already exists. Default False.
                Generally useful during development and testing.
            index_kwargs (Optional[dict]): Dict of kwargs for index creation.
                See https://docs.nomic.ai/atlas_api.html

        Returns:
            AtlasDB: Nomic's neural database and finest rhizomatic instrument
        N$`name` and `api_key` cannot be None._indexr    )r   indexed_fieldr   )r   r   r   r   r   )r1   r3   r5   r<   )r&   itemsr)   rR   rX   rZ   )rr   r1   r!   r3   r5   r   r   r   r   r   rt   r8   all_index_kwargsr]   vatlasDBs                   r,   
from_textszAtlasDB.from_texts   s5   J <7?CDDD %)8OfMM#$**,, ( (1&' ## #(8$;
 
 
 _2244 	5 	5EYCHHH G 44#3444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 s   '&BB B	documentspersist_directoryc                    ||t          d          d |D             }d |D             }|                     |||||||||	|

  
        S )a+  Create an AtlasDB vectorstore from a list of documents.

        Args:
            name (str): Name of the collection to create.
            api_key (str): Your nomic API key,
            documents (List[Document]): List of documents to add to the vectorstore.
            embedding (Optional[Embeddings]): Embedding function. Defaults to None.
            ids (Optional[List[str]]): Optional list of document IDs. If None,
                ids will be auto created
            description (str): A description for your project.
            is_public (bool): Whether your project is publicly accessible.
                True by default.
            reset_project_if_exists (bool): Whether to reset this project if
                it already exists. Default False.
                Generally useful during development and testing.
            index_kwargs (Optional[dict]): Dict of kwargs for index creation.
                See https://docs.nomic.ai/atlas_api.html

        Returns:
            AtlasDB: Nomic's neural database and finest rhizomatic instrument
        Nrw   c                    g | ]	}|j         
S r<   )re   r?   docs     r,   rA   z*AtlasDB.from_documents.<locals>.<listcomp>9  s    777c!777r0   c                    g | ]	}|j         
S r<   )rf   r   s     r,   rA   z*AtlasDB.from_documents.<locals>.<listcomp>:  s    777cS\777r0   )
r   r   r1   r!   r3   r5   r   r   r   rt   )r&   r~   )rr   r   r!   r5   r   r   r   r   r   r   rt   r8   r1   r3   s                 r,   from_documentszAtlasDB.from_documents  s|    H <7?CDDD77Y77777Y777	~~#$;%  
 
 	
r0   )NNr   TF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )NNT)r1   r2   r3   r4   r5   r6   r7   r   r8   r   r   r9   )r8   r   r   r   )r[   )r\   r   r]   r^   r8   r   r   r_   )	NNNNNr   TFN)rr   rs   r1   r9   r!   r   r3   r4   r5   r6   r   r   r   r   r   r   r   r   r   r   rt   ru   r8   r   r   r   )rr   rs   r   r_   r!   r   r5   r6   r   r   r   r   r   r   r   r   r   r   r   r   rt   ru   r8   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r-   propertyr/   rX   rZ   rq   classmethodr~   r   r<   r0   r,   r   r      sa           $.----
 48!%;(-5- 5- 5- 5- 5-n ( ( ( X( +/#'L L L L L\7 7 7 7          D  +/*.#'"!%;(-'+9 9 9 9 [9v  +/#'"!%+/;(-'+2
 2
 2
 2
 [2
 2
 2
r0   r   )
__future__r   loggingr=   typingr   r   r   r   r   numpyrM   langchain_core.documentsr
   langchain_core.embeddingsr   langchain_core.vectorstoresr   	getLoggerr   loggerr   r<   r0   r,   <module>r      s    " " " " " "   6 6 6 6 6 6 6 6 6 6 6 6 6 6     - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3		8	$	$w
 w
 w
 w
 w
k w
 w
 w
 w
 w
r0   