
    hm/                        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y)	    )annotationsN)AnyIterableListOptionalType)Document)
Embeddings)VectorStorec                  X   e Zd ZU dZdZded<   	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	dd	Z
	 d	 	 	 	 	 	 	 dd
Ze	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zy)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_FIELDNc                *   	 ddl }ddl m} |t        d      |j	                  |       || _        d}	| j
                  d}	 ||||	||t        j                        | _        | j                  j                          y# t        $ r t        d      w xY w)	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)namedescriptionmodality	is_publicreset_project_if_existsunique_id_field)
nomicr   ImportError
ValueErrorlogin_embedding_functionr   r   project_latest_project_state)
selfr   embedding_functionapi_keyr   r   r   r   r   r   s
             d/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/atlas.py__init__zAtlasDB.__init__"   s    4	* ?NOOG#5 ##/"H $#$;#;;
 	**,1  	> 	s   
A= =Bc                    | j                   S )N)r   )r"   s    r%   
embeddingszAtlasDB.embeddingsY   s    '''    c           	        |.t        |      dkD  r d|d   j                         v rt        d      t        |      }|*|D cg c]  }t	        t        j                               ! }}| j                  | j                  j                  |      }t        j                  |      }|6t        |      D 	cg c]  \  }	}t        j                  ||	   d||	   i! }
}	}n?t        t        |            D ]&  }	||	   ||	   t        j                  <   ||	   ||	   d<   ( |}
| j                  j!                  g t        j                  |
       | j                  j#                         5  | j                  j%                  ||
       ddd       n|3t        |      D 	cg c]  \  }	}d|t        j                  ||	   i }
}	}n6t        |      D ]&  \  }	}|||	   d<   ||	   ||	   t        j                  <   ( |}
| j                  j!                  g t        j                  |
       | j                  j#                         5  | j                  j'                  |
       ddd       |rat        | j                  j(                        dkD  r?| j                  j#                         5  | j                  j+                          ddd       |S |S c c}w c c}}	w # 1 sw Y   yxY wc c}}	w # 1 sw Y   xY w# 1 sw Y   |S xY w)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!)id_fielddata)r(   r,   )lenkeysr   listr   uuiduuid4r   embed_documentsnpstack	enumerater   r   ranger    _validate_map_data_inputswait_for_project_lockadd_embeddingsadd_textindicesrebuild_maps)r"   texts	metadatasidsrefreshkwargs__embeddingsr(   ir,   r   s               r%   	add_textszAtlasDB.add_texts]   s   * !I")A,++--BCCU;.343tzz|$4C4 ##/22BB5IK+.J  !*% 01 44c!ffeAhO 
 s9~. 4ADGFIaL!@!@A+08IaL(4 !LL22W<<4 3  335 N++z+MN N   $-U#34 T7#B#BCFK 
  )/ KGAt+0IaL(DGFIaL!@!@AK !LL22W<<4 3  335 ,%%d+, 4<<''(1,\\779 0LL--/0 
s
_ 5N N
, ,
0 
s6   $K5$K"K!K:K K,K K),K6c                    | j                   j                         5   | j                   j                  di |cddd       S # 1 sw Y   yxY w)zCreates an index in your project.

        See
        https://docs.nomic.ai/atlas_api.html#nomic.project.AtlasProject.create_index
        for full detail.
        N )r    r8   create_index)r"   rA   s     r%   rH   zAtlasDB.create_index   s@     \\//1 	7,4<<,,6v6	7 	7 	7s   AA
c           	         | j                   t        d      | j                   j                  |g      d   }t        j                  |      j                  dd      }| j                  j                         5  | j                  j                  d   j                  ||      \  }}| j                  j                  |d         }ddd       t              D 	
cg c]  \  }	}
t        |	   d   ||	   	       }}	}
|S # 1 sw Y   <xY wc c}
}	w )
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      )queriesk)r?   r   )page_contentmetadata)r   NotImplementedErrorr2   r3   arrayreshaper    r8   projectionsvector_searchget_datar5   r	   )r"   queryrM   rA   
_embeddingr   	neighborsrB   r,   rD   neighbordocss               r%   similarity_searchzAtlasDB.similarity_search   s    ##+%T  --==ugFqI
HHZ(00B7	\\//1 	;<<33A6DD!Q E LIq <<((Yq\(:D		;  )3
8 $q'&/DGD
 
 	; 	;
s   6AC> D
>Dc                :   ||t        d      |dz   dd}|
|
j                         D ]
  \  }}|||<     | |||d||	      }|j                  j                         5  |j	                  |||        |j
                  d	i | ddd       |S # 1 sw Y   |S xY w)
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_fieldA description for your project)r#   r$   r   r   r   )r=   r>   r?   rG   )r   itemsr    r8   rE   rH   )clsr=   r   r>   r?   r   r$   r   r   r   index_kwargsrA   all_index_kwargsrM   vatlasDBs                   r%   
from_textszAtlasDB.from_texts   s    J <7?CDD %)8OfM#$**, (1&' #( (8$;
 __224 	5EYCH G  4#34	5 	5 s   'BBc                    ||t        d      |D cg c]  }|j                   }}|D cg c]  }|j                   }}| j                  |||||||||	|

      S c c}w c c}w )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
        r]   )
r   r$   r=   r   r>   r?   r   r   r   rc   )r   rN   rO   rg   )rb   	documentsr   r?   r   r$   persist_directoryr   r   r   rc   rA   docr=   r>   s                  r%   from_documentszAtlasDB.from_documents  s    H <7?CDD-67c!!77-67cS\\7	7~~#$;%  
 	
 87s
   AA")NNr`   TF)r   r   r#   Optional[Embeddings]r$   Optional[str]r   r   r   boolr   ro   returnNone)rp   rm   )NNT)r=   zIterable[str]r>   Optional[List[dict]]r?   Optional[List[str]]r@   ro   rA   r   rp   	List[str])rA   r   rp   r   )   )rV   r   rM   intrA   r   rp   List[Document])	NNNNNr`   TFN)rb   Type[AtlasDB]r=   rt   r   rm   r>   rr   r?   rs   r   rn   r$   rn   r   r   r   ro   r   ro   rc   Optional[dict]rA   r   rp   r   )rb   rx   ri   rw   r   rm   r?   rs   r   rn   r$   rn   rj   rn   r   r   r   ro   r   ro   rc   ry   rA   r   rp   r   )__name__
__module____qualname____doc__r   __annotations__r&   propertyr(   rE   rH   r[   classmethodrg   rl   rG   r)   r%   r   r      sj     $.S-
 48!%;(-5-5- 15- 	5-
 5- 5- "&5- 
5-n ( ( +/#'LL (L !	L
 L L 
L\7      	 
 
 D  +/*.#'"!%;(-'+999 (9 (	9
 !9 9 9 9 9 "&9 %9 9 
9 9v  +/#'"!%+/;(-'+2
2
!2
 (2
 !	2

 2
 2
 )2
 2
 2
 "&2
 %2
 2
 
2
 2
r)   r   )
__future__r   loggingr0   typingr   r   r   r   r   numpyr3   langchain_core.documentsr	   langchain_core.embeddingsr
   langchain_core.vectorstoresr   	getLoggerrz   loggerr   rG   r)   r%   <module>r      s@    "   6 6  - 0 3			8	$w
k w
r)   