o
    Zhm/                     @  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< 					d8d9ddZed:ddZ			d;d<d"d#Z	d=d$d%Z
	&d>d?d+d,Ze									d@dAd2d3Ze									d@dBd6d7ZdS )C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 projectTFnameembedding_functionOptional[Embeddings]api_keyOptional[str]description	is_publicboolreset_project_if_existsreturnNonec           
      C  s   zddl }ddl m} W n ty   tdw |du r td|| || _d}	| jdur1d}	||||	||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&   ]/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/vectorstores/atlas.py__init__"   s0   

zAtlasDB.__init__c                 C  s   | j S )N)r#   )r%   r&   r&   r'   
embeddingsY   s   zAtlasDB.embeddingstextsIterable[str]	metadatasOptional[List[dict]]idsOptional[List[str]]refreshkwargsr   	List[str]c                   s   |durt |dkrd|d  v rtdt du r%dd D  | jdur| j}t|}|du rF fddtD }nt	t |D ]}	 |	 ||	 t
j< |	 ||	 d< qL|}| jjg t
j|d | j  | jj||d	 W d   n1 sw   Y  nO|du r fd
dtD }ntD ]\}	}
||	 d<  |	 ||	 t
j< q|}| jjg t
j|d | j  | j| W d   n1 sw   Y  |rt | jjdkr| j  | j  W d    S 1 sw   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   s    z%AtlasDB.add_texts.<locals>.<listcomp>c                   s&   g | ]\}}t j | d | iqS r   r   r   )r5   ir6   r.   r*   r&   r'   r7      s    )Zid_fielddata)r)   r<   c                   s"   g | ]\}}d |t j | iqS r8   r9   )r5   r:   r   r.   r&   r'   r7      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.   r0   r1   Z_embeddingsr)   r<   r:   r   r&   r;   r'   	add_texts]   s^   



zAtlasDB.add_textsc                 K  s@   | j   | j jdi |W  d   S 1 s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$   rF   create_index)r%   r1   r&   r&   r'   rJ      s   $zAtlasDB.create_index   querykintList[Document]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 sAw   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queriesrM   r=   c                   s(   g | ]\}}t  | d   | dqS )r   )page_contentmetadatar   )r5   r:   Zneighborr<   r&   r'   r7      s    z-AtlasDB.similarity_search.<locals>.<listcomp>)r#   NotImplementedErrorrA   rB   arrayZreshaper$   rF   ZprojectionsZvector_searchget_datarD   )	r%   rL   rM   r1   Z
_embeddingr   Z	neighborsr6   docsr&   rT   r'   similarity_search   s    


zAtlasDB.similarity_searchclsType[AtlasDB]r   index_kwargsOptional[dict]c                 K  s   |du s|du rt d|d dd}|
dur$|
 D ]\}}|||< q| |||d||	d}|j  |j|||d |jd	i | W d   |S 1 sOw   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&   )r!   itemsr$   rF   rI   rJ   )rZ   r*   r   r,   r.   r   r   r   r   r   r\   r1   Zall_index_kwargsrM   vZatlasDBr&   r&   r'   
from_texts   s*   %

zAtlasDB.from_texts	documentspersist_directoryc                 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
        Nr^   c                 S     g | ]}|j qS r&   )rR   r5   docr&   r&   r'   r7   9      z*AtlasDB.from_documents.<locals>.<listcomp>c                 S  re   r&   )rS   rf   r&   r&   r'   r7   :  rh   )
r   r   r*   r   r,   r.   r   r   r   r\   )r!   rb   )rZ   rc   r   r.   r   r   rd   r   r   r   r\   r1   r*   r,   r&   r&   r'   from_documents  s    $zAtlasDB.from_documents)NNr   TF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )NNT)r*   r+   r,   r-   r.   r/   r0   r   r1   r   r   r2   )r1   r   r   r   )rK   )rL   r   rM   rN   r1   r   r   rO   )	NNNNNr   TFN)rZ   r[   r*   r2   r   r   r,   r-   r.   r/   r   r   r   r   r   r   r   r   r   r   r\   r]   r1   r   r   r   )rZ   r[   rc   rO   r   r   r.   r/   r   r   r   r   rd   r   r   r   r   r   r   r   r\   r]   r1   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r(   propertyr)   rI   rJ   rY   classmethodrb   ri   r&   r&   r&   r'   r      sP   
 7
N";r   )
__future__r   loggingr3   typingr   r   r   r   r   numpyrB   Zlangchain_core.documentsr	   Zlangchain_core.embeddingsr
   Zlangchain_core.vectorstoresr   	getLoggerrj   loggerr   r&   r&   r&   r'   <module>   s    
