
    dh^0                        S SK Jr  S SKrS SKJr  S SKJrJrJrJ	r	J
r
JrJrJrJr  S SKJr  S SKJr  \(       a  S SKJr  S SKJr   " S	 S
\\5      r\" S\\\4   S9r\R4                  " \5      rSr " S S\5      rg)    )annotationsN)Enum)	TYPE_CHECKINGAnyDict	GeneratorIterableListOptionalTypeVarUnion)Document)VectorStore)
Embeddings)
Collectionc                  (    \ rS rSrSrSr Sr SrSrg)DocumentDBSimilarityType   z)DocumentDB Similarity Type as enumerator.cosine
dotProduct	euclidean N)	__name__
__module____qualname____firstlineno____doc__COSDOTEUC__static_attributes__r       c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/documentdb.pyr   r      s    3
C
C
Cr"   r   DocumentDBDocumentType)bound   c                     \ rS rSrSrSSSS.         SS jjr\SS j5       rSS	 jr\	          SS
 j5       r
SS jrS S jrS\R                  SS4         S!S jjr S"       S#S jjrS$S jr\	  S%           S&S jj5       rS"S'S jjrS"S(S jjr   S)         S*S jjr  S+SS.           S,S jjjrSrg)-DocumentDBVectorSearch,   aD  `Amazon DocumentDB (with MongoDB compatibility)` vector store.
Please refer to the official Vector Search documentation for more details:
https://docs.aws.amazon.com/documentdb/latest/developerguide/vector-search.html

To use, you should have both:
- the ``pymongo`` python package installed
- a connection string and credentials associated with a DocumentDB cluster

Example:
    . code-block:: python

        from langchain_community.vectorstores import DocumentDBVectorSearch
        from langchain_community.embeddings.openai import OpenAIEmbeddings
        from pymongo import MongoClient

        mongo_client = MongoClient("<YOUR-CONNECTION-STRING>")
        collection = mongo_client["<db_name>"]["<collection_name>"]
        embeddings = OpenAIEmbeddings()
        vectorstore = DocumentDBVectorSearch(collection, embeddings)
vectorSearchIndextextContentvectorContent)
index_nametext_keyembedding_keyc               j    Xl         X l        X0l        X@l        XPl        [
        R                  U l        g)aq  Constructor for DocumentDBVectorSearch

Args:
    collection: MongoDB collection to add the texts to.
    embedding: Text embedding model to use.
    index_name: Name of the Vector Search index.
    text_key: MongoDB field that will contain the text
        for each document.
    embedding_key: MongoDB field that will contain the embedding
        for each document.
N)_collection
_embedding_index_name	_text_key_embedding_keyr   r   _similarity_type)self
collection	embeddingr-   r.   r/   s         r#   __init__DocumentDBVectorSearch.__init__B   s.    ( &#%!+ 8 < <r"   c                    U R                   $ N)r2   r7   s    r#   
embeddings!DocumentDBVectorSearch.embeddings]   s    r"   c                    U R                   $ )z=Returns the index name

Returns:
    Returns the index name

)r3   r>   s    r#   get_index_name%DocumentDBVectorSearch.get_index_namea   s     r"   c                     SSK Jn  U" U5      nUR                  S5      u  pxXg   U   n	U " X40 UD6$ ! [         a    [        S5      ef = f)aF  Creates an Instance of DocumentDBVectorSearch from a Connection String

Args:
    connection_string: The DocumentDB cluster endpoint connection string
    namespace: The namespace (database.collection)
    embedding: The embedding utility
    **kwargs: Dynamic keyword arguments

Returns:
    an instance of the vector store

r   )MongoClientzGCould not import pymongo, please install it with `pip install pymongo`..)pymongorE   ImportErrorsplit)
clsconnection_string	namespacer9   kwargsrE   clientdb_namecollection_namer8   s
             r#   from_connection_string-DocumentDBVectorSearch.from_connection_stringj   sg    (	+ **;<#,??3#7 __5
:3F33  	) 	s	   3 A	c                    U R                   R                  5       nU R                  nU H  nUR                  S5      nXB:X  d  M    g   g)zVerifies if the specified index name during instance
    construction exists on the collection

Returns:
  Returns True on success and False if no such index exists
    on the collection
nameTF)r1   list_indexesr3   pop)r7   cursorr-   rescurrent_index_names        r#   index_exists#DocumentDBVectorSearch.index_exists   sI     !!..0%%
C!$!/ 
 r"   c                z    U R                  5       (       a&  U R                  R                  U R                  5        gg)zEDeletes the index specified during instance construction if it existsN)rZ   r1   
drop_indexr3   r>   s    r#   delete_index#DocumentDBVectorSearch.delete_index   s0    ''(8(89 r"   i      @   c           	         X l         U R                  R                  U R                  U R                  S0SUUUUS.S./S.nU R                  R
                  nUR                  U5      nU$ )aX  Creates an index using the index name specified at
    instance construction

Args:
    dimensions: Number of dimensions for vector similarity.
        The maximum number of supported dimensions is 2000

    similarity: Similarity algorithm to use with the HNSW index.
         Possible options are:
            - DocumentDBSimilarityType.COS (cosine distance),
            - DocumentDBSimilarityType.EUC (Euclidean distance), and
            - DocumentDBSimilarityType.DOT (dot product).

    m: Specifies the max number of connections for an HNSW index.
        Large impact on memory consumption.

    ef_construction: Specifies the size of the dynamic candidate list
        for constructing the graph for HNSW index. Higher values lead
        to more accurate results but slower indexing speed.


Returns:
    An object describing the created index

vectorhnsw)type
similarity
dimensionsmefConstruction)rT   keyvectorOptions)createIndexesindexes)r6   r1   rT   r3   r5   databasecommand)r7   rg   rf   rh   ef_constructioncreate_index_commandscurrent_databasecreate_index_responsess           r#   create_index#DocumentDBVectorSearch.create_index   s    @ !+ "--22 !,, //: &&0&0*9&
!
$  ++44 2B1I1I!2
 &%r"   Nc                   UR                  S[        5      nU=(       d	    S U 5       n/ n/ n/ n[        [        X5      5       H\  u  n	u  pUR	                  U
5        UR	                  U5        U	S-   U-  S:X  d  M8  UR                  U R                  Xg5      5        / n/ nM^     U(       a   UR                  U R                  Xg5      5        U$ )N
batch_sizec              3  $   #    U H  n0 v   M	     g 7fr=   r   ).0_s     r#   	<genexpr>3DocumentDBVectorSearch.add_texts.<locals>.<genexpr>   s     :Mu!2us      r   )getDEFAULT_INSERT_BATCH_SIZE	enumeratezipappendextend_insert_texts)r7   texts	metadatasrM   rw   
_metadatastexts_batchmetadatas_batch
result_idsitextmetadatas               r#   	add_texts DocumentDBVectorSearch.add_texts   s     ZZ.GH
-6-M:Mu:M

#,S-C#DAt$""8,A#q(!!$"4"4["RS "$ $E d00NOr"   c           	        U(       d  / $ U R                   R                  U5      n[        XU5       VVVs/ sH!  u  pEnU R                  X@R                  U0UEPM#     nnnnU R
                  R                  U5      nUR                  $ s  snnnf )zUsed to Load Documents into the collection

Args:
    texts: The list of documents strings to load
    metadatas: The list of metadata objects associated with each document

Returns:

)r2   embed_documentsr   r4   r5   r1   insert_manyinserted_ids)	r7   r   r   r?   trh   r9   	to_insertinsert_results	            r#   r   $DocumentDBVectorSearch._insert_texts   s     I __44U;
 $'u#D
#Di ^^Q 3 3YD!D#D 	 

 ((44Y?)))
s   'Bc                R    Uc  [        S5      eU " XB40 UD6nUR                  XS9  U$ )Nz*Must provide 'collection' named parameter.)r   )
ValueErrorr   )rJ   r   r9   r   r8   rM   vectorstores          r#   
from_texts!DocumentDBVectorSearch.from_texts  s<     IJJ*:6:e9r"   c                T    Uc  [        S5      eU H  nU R                  U5        M     g)Nz#No document ids provided to delete.T)r   delete_document_by_id)r7   idsrM   document_ids       r#   deleteDocumentDBVectorSearch.delete  s.    ;BCCK&&{3 r"   c                     SSK Jn  Uc  [        S5      eU R                  R                  SU" U5      05        g! [         a  n[        S5      UeSnAff = f)zRRemoves a Specific Document by Id

Args:
    document_id: The document identifier
r   )ObjectIdz>Unable to import bson, please install with `pip install bson`.Nz"No document id provided to delete._id)bson.objectidr   rH   r   r1   
delete_one)r7   r   r   es       r#   r   ,DocumentDBVectorSearch.delete_document_by_id&  sa    	.
 ABB##UH[,A$BC  	P	s   : 
AAAc           	        U(       d  0 nSU0SSUU R                   U R                  UUS.00/nU R                  R                  U5      n/ nU H6  nUR	                  U R
                  5      n	UR                  [        XS95        M8     U$ )a  Returns a list of documents.

Args:
    embeddings: The query vector
    k: the number of documents to return
    ef_search: Specifies the size of the dynamic candidate list
        that HNSW index uses during search. A higher value of
        efSearch provides better recall at cost of speed.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.
Returns:
    A list of documents closest to the query vector
z$matchz$searchvectorSearch)rc   pathrf   kefSearch)page_contentr   )r5   r6   r1   	aggregaterV   r4   r   r   )
r7   r?   r   	ef_searchfilterpipelinerW   docsrX   r   s
             r#    _similarity_search_without_score7DocumentDBVectorSearch._similarity_search_without_score7  s    * Fv"", $ 3 3&*&;&;$-%
*
 !!++H5C774>>*DKKdAB  r"   )r   c                   U R                   R                  U5      nU R                  XbX4S9nU Vs/ sH  oPM     sn$ s  snf )N)r?   r   r   r   )r2   embed_queryr   )	r7   queryr   r   r   rM   r?   r   docs	            r#   similarity_search(DocumentDBVectorSearch.similarity_searchg  sL     __007
44!) 5 
  $$tt$$$s   
=)r1   r2   r5   r3   r6   r4   )
r8   z"Collection[DocumentDBDocumentType]r9   r   r-   strr.   r   r/   r   )returnr   )r   r   )
rK   r   rL   r   r9   r   rM   r   r   r(   )r   bool)r   None)
rg   intrf   r   rh   r   rp   r   r   zdict[str, Any]r=   )r   zIterable[str]r   zOptional[List[Dict[str, Any]]]rM   r   r   r
   )r   	List[str]r   zList[Dict[str, Any]]r   r
   )NN)r   r   r9   r   r   zOptional[List[dict]]r8   z,Optional[Collection[DocumentDBDocumentType]]rM   r   r   r(   )r   zOptional[List[str]]rM   r   r   zOptional[bool])r   zOptional[str]r   r   )   (   N)
r?   zList[float]r   r   r   r   r   Optional[Dict[str, Any]]r   List[Document])r   r   )r   r   r   r   r   r   r   r   rM   r   r   r   )r   r   r   r   r   r:   propertyr?   rB   classmethodrQ   rZ   r^   r   r   rt   r   r   r   r   r   r   r   r!   r   r"   r#   r(   r(   ,   s&   4 .%,=6= =
 = = =6    44 4 	4
 4 
 4 4>$: /G/K/K!<&<& -<& 	<&
 <& 
<&B 59 2 	
 
,*0 
 +/CG  (	
 A  
  D( +/.. . 	.
 ). 
.f 	% ,0%% % 	% )% % 
% %r"   r(   )
__future__r   loggingenumr   typingr   r   r   r   r	   r
   r   r   r   langchain_core.documentsr   langchain_core.vectorstoresr   langchain_core.embeddingsr   pymongo.collectionr   r   r   r$   	getLoggerr   loggerr   r(   r   r"   r#   <module>r      sx    "  
 
 
 . 34-sD  !!9c3hP 			8	$ H%[ H%r"   