a
    bgm/                     @  s   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eZG dd deZdS )	    )annotationsN)AnyIterableListOptionalTypeDocument)
Embeddings)VectorStorec                   @  s   e Zd ZU dZdZded< d,dd	d
dddddddZed	dddZd-dddddddddZ	dddddZ
d.dddd d!d"d#Zed/d$dd	ddd
d
dddd%dd d&d'd(Zed0d$d d	dd
d
d
dddd%dd d)d*d+ZdS )1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)
    Zatlas_idstr_ATLAS_DEFAULT_ID_FIELDNA description for your projectTFzOptional[Embeddings]zOptional[str]boolNone)nameembedding_functionapi_keydescription	is_publicreset_project_if_existsreturnc           
      C  s   zddl }ddl m} W n ty2   tdY n0 |du rDtd|| || _d}	| jdurfd}	||||	||tjd| _| 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   Zunique_id_field)
nomicr   ImportError
ValueErrorlogin_embedding_functionr   r   projectZ_latest_project_state)
selfr   r   r   r   r   r   r   r   r    r$   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/atlas.py__init__"   s.    


zAtlasDB.__init__)r   c                 C  s   | j S )N)r!   )r#   r$   r$   r%   
embeddingsY   s    zAtlasDB.embeddingszIterable[str]zOptional[List[dict]]zOptional[List[str]]r   z	List[str])texts	metadatasidsrefreshkwargsr   c                   s  |dur,t |dkr,d|d  v r,tdt du rJdd D  | jdur| j}t|}|du r fddtD }n8t	t |D ]&}	 |	 ||	 t
j< |	 ||	 d< q|}| jjg t
j|d | j   | jj||d	 W d   n1 s0    Y  n|du r: fd
dtD }n6tD ](\}	}
||	 d<  |	 ||	 t
j< qB|}| jjg t
j|d | j  | j| W d   n1 s0    Y  |r
t | jjdkr
| j  | j  W d   n1 s 0    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                 S  s   g | ]}t t qS r$   )r   uuiduuid4).0_r$   r$   r%   
<listcomp>z       z%AtlasDB.add_texts.<locals>.<listcomp>c                   s&   g | ]\}}t j | d | iqS r   r   r   )r/   ir0   r*   r(   r$   r%   r1      s   )Zid_fielddata)r'   r7   c                   s"   g | ]\}}d |t j | iqS r3   r4   )r/   r5   r   r*   r$   r%   r1      s   )lenkeysr   listr!   embed_documentsnpstack	enumerateranger   r   r"   Z_validate_map_data_inputswait_for_project_lockZadd_embeddingsadd_textindicesZrebuild_maps)r#   r(   r)   r*   r+   r,   Z_embeddingsr'   r7   r5   r   r$   r6   r%   	add_texts]   sX    

2

,*zAtlasDB.add_texts)r,   r   c                 K  s@   | j  " | j jf i |W  d   S 1 s20    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.
        N)r"   rA   create_index)r#   r,   r$   r$   r%   rE      s    zAtlasDB.create_index   intzList[Document])querykr,   r   c           	        s   | j du rtd| j |gd }t|dd}| j < | jjd j	||d\}}| jj
|d d W d   n1 s0    Y   fd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      )ZqueriesrI   r8   c                   s(   g | ] \}}t  | d   | dqS )r   )page_contentmetadatar   )r/   r5   Zneighborr7   r$   r%   r1      s   z-AtlasDB.similarity_search.<locals>.<listcomp>)r!   NotImplementedErrorr<   r=   arrayZreshaper"   rA   ZprojectionsZvector_searchget_datar?   )	r#   rH   rI   r,   Z
_embeddingr   Z	neighborsr0   docsr$   rN   r%   similarity_search   s    

0
zAtlasDB.similarity_searchzType[AtlasDB]zOptional[dict])clsr(   r   r)   r*   r   r   r   r   r   index_kwargsr,   r   c                 K  s   |du s|du rt d|d dd}|
durH|
 D ]\}}|||< q6| |||d||	d}|j 0 |j|||d |jf i | W d   n1 s0    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   Zindexed_fieldr   )r   r   r   r   r   )r(   r)   r*   )r   itemsr"   rA   rD   rE   )rT   r(   r   r)   r*   r   r   r   r   r   rU   r,   Zall_index_kwargsrI   vZatlasDBr$   r$   r%   
from_texts   s$    %
.zAtlasDB.from_texts)rT   	documentsr   r*   r   r   persist_directoryr   r   r   rU   r,   r   c                 K  sR   |du s|du rt ddd |D }dd |D }| j|||||||||	|
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
        NrV   c                 S  s   g | ]
}|j qS r$   )rL   r/   docr$   r$   r%   r1   9  r2   z*AtlasDB.from_documents.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r$   )rM   r]   r$   r$   r%   r1   :  r2   )
r   r   r(   r   r)   r*   r   r   r   rU   )r   rZ   )rT   r[   r   r*   r   r   r\   r   r   r   rU   r,   r(   r)   r$   r$   r%   from_documents  s     $zAtlasDB.from_documents)NNr   TF)NNT)rF   )	NNNNNr   TFN)	NNNNNr   TFN)__name__
__module____qualname____doc__r   __annotations__r&   propertyr'   rD   rE   rS   classmethodrZ   r_   r$   r$   r$   r%   r      sN   
     7   N "         *;         r   )
__future__r   loggingr-   typingr   r   r   r   r   numpyr=   Zlangchain_core.documentsr	   Zlangchain_core.embeddingsr
   Zlangchain_core.vectorstoresr   	getLoggerr`   loggerr   r$   r$   r$   r%   <module>   s   
