
    ug=                         d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlZ ej2                  e      Zerd dlmZ  G d ded         Z G d	 d
e      Z y)    N)TYPE_CHECKINGOptionalUnion)CollectionCommon)URICollectionMetadata	EmbeddingIncludeEnumPyEmbeddingIncludeMetadataDocumentImageWhereIDs	GetResultQueryResultID	OneOrManyWhereDocumentr
   )	ServerAPIc                   T   e Zd ZdefdZ	 	 	 	 	 ddee   deeee	   ee
   f      deee      deee      deee      d	eee      ddfd
Zdddddej"                  ej$                  gfdeee      dee   dee   dee   dee   dedefdZd dedefdZdddddddej"                  ej$                  ej2                  gfdeeee	   ee
   f      deee      deee      deee      dedee   dee   dedefdZ	 d!dee   dee   ddfdZ	 	 	 	 	 ddee   deeee	   ee
   f      deee      deee      deee      d	eee      ddfdZ	 	 	 	 	 ddee   deeee	   ee
   f      deee      deee      deee      d	eee      ddfdZ 	 	 	 d"dee!   dee   dee   ddfdZ"y)#
Collectionreturnc                 z    | j                   j                  | j                  | j                  | j                        S )zThe total number of embeddings added to the database

        Returns:
            int: The total number of embeddings added to the database

        )collection_idtenantdatabase)_client_countidr   r   )selfs    S/var/www/openai/venv/lib/python3.12/site-packages/chromadb/api/models/Collection.pycountzCollection.count"   s5     ||""'';;]] # 
 	
    Nids
embeddings	metadatas	documentsimagesurisc           
          | j                  ||||||      }| j                  j                  | j                  |d   |d   |d   |d   |d   | j                  | j
                         y)	a]  Add embeddings to the data store.
        Args:
            ids: The ids of the embeddings you wish to add
            embeddings: The embeddings to add. If None, embeddings will be computed based on the documents or images using the embedding_function set for the Collection. Optional.
            metadatas: The metadata to associate with the embeddings. When querying, you can filter on this metadata. Optional.
            documents: The documents to associate with the embeddings. Optional.
            images: The images to associate with the embeddings. Optional.
            uris: The uris of the images to associate with the embeddings. Optional.

        Returns:
            None

        Raises:
            ValueError: If you don't provide either embeddings or documents
            ValueError: If the length of ids, embeddings, metadatas, or documents don't match
            ValueError: If you don't provide an embedding function and don't provide embeddings
            ValueError: If you provide both embeddings and documents
            ValueError: If you provide an id that already exists

        r&   r'   r(   r)   r*   r+   r&   r'   r(   r)   r+   r   r&   r'   r(   r)   r+   r   r   N)!_validate_and_prepare_add_requestr   _addr!   r   r   )r"   r&   r'   r(   r)   r*   r+   add_requests           r#   addzCollection.add/   s    F <<! = 
 	''E""<0!+.!+.V$;;]] 	 		
r%   wherelimitoffsetwhere_documentincludec                     | j                  ||||      }| j                  j                  | j                  |d   |d   |d   |d   d||| j                  | j
                  
      }| j                  ||d         S )	a  Get embeddings and their associate data from the data store. If no ids or where filter is provided returns
        all embeddings up to limit starting at offset.

        Args:
            ids: The ids of the embeddings to get. Optional.
            where: A Where type dict used to filter results by. E.g. `{"$and": [{"color" : "red"}, {"price": {"$gte": 4.20}}]}`. Optional.
            limit: The number of documents to return. Optional.
            offset: The offset to start returning results from. Useful for paging results with limit. Optional.
            where_document: A WhereDocument type dict used to filter by the documents. E.g. `{$contains: {"text": "hello"}}`. Optional.
            include: A list of what to include in the results. Can contain `"embeddings"`, `"metadatas"`, `"documents"`. Ids are always included. Defaults to `["metadatas", "documents"]`. Optional.

        Returns:
            GetResult: A GetResult object containing the results.

        )r&   r3   r6   r7   r&   r3   r6   r7   N)
r   r&   r3   r6   r7   sortr4   r5   r   r   responser7   )!_validate_and_prepare_get_requestr   _getr!   r   r   _transform_get_response)	r"   r&   r3   r4   r5   r6   r7   get_requestget_resultss	            r#   getzCollection.getf   s    0 <<)	 = 
 ll''''E"g&&'78	*;;]] ( 
 ++ +i*@ , 
 	
r%   
   c                     | j                  | j                  j                  | j                  || j                  | j
                              S )zGet the first few results in the database up to limit

        Args:
            limit: The number of results to return.

        Returns:
            GetResult: A GetResult object containing the results.
        )r   nr   r   )_transform_peek_responser   _peekr!   r   r   )r"   r4   s     r#   peekzCollection.peek   sF     ,,LL"gg{{	  
 	
r%   query_embeddingsquery_textsquery_images
query_uris	n_resultsc	           
          | j                  ||||||||      }	| j                  j                  | j                  |	d   |	d   |	d   |	d   |	d   | j                  | j
                        }
| j                  |
|	d         S )	a  Get the n_results nearest neighbor embeddings for provided query_embeddings or query_texts.

        Args:
            query_embeddings: The embeddings to get the closes neighbors of. Optional.
            query_texts: The document texts to get the closes neighbors of. Optional.
            query_images: The images to get the closes neighbors of. Optional.
            query_uris: The URIs to be used with data loader. Optional.
            n_results: The number of neighbors to return for each query_embedding or query_texts. Optional.
            where: A Where type dict used to filter results by. E.g. `{"$and": [{"color" : "red"}, {"price": {"$gte": 4.20}}]}`. Optional.
            where_document: A WhereDocument type dict used to filter by the documents. E.g. `{$contains: {"text": "hello"}}`. Optional.
            include: A list of what to include in the results. Can contain `"embeddings"`, `"metadatas"`, `"documents"`, `"distances"`. Ids are always included. Defaults to `["metadatas", "documents", "distances"]`. Optional.

        Returns:
            QueryResult: A QueryResult object containing the results.

        Raises:
            ValueError: If you don't provide either query_embeddings, query_texts, or query_images
            ValueError: If you provide both query_embeddings and query_texts
            ValueError: If you provide both query_embeddings and query_images
            ValueError: If you provide both query_texts and query_images

        )rH   rI   rJ   rK   rL   r3   r6   r7   r'   rL   r3   r6   r7   )r   rH   rL   r3   r6   r7   r   r   r:   )#_validate_and_prepare_query_requestr   _queryr!   r   r   _transform_query_response)r"   rH   rI   rJ   rK   rL   r3   r6   r7   query_requestquery_resultss              r#   queryzCollection.query   s    V @@-#%!) A 	
 ++''*<8#K0(()9:!),;;]] , 	
 --"M),D . 
 	
r%   namemetadatac                     | j                  |       | j                  j                  | j                  ||| j                  | j
                         | j                  ||       y)zModify the collection name or metadata

        Args:
            name: The updated name for the collection. Optional.
            metadata: The updated metadata for the collection. Optional.

        Returns:
            None
        )r!   new_namenew_metadatar   r   N)_validate_modify_requestr   _modifyr!   r   r   "_update_model_after_modify_success)r"   rT   rU   s      r#   modifyzCollection.modify   sX     	%%h/
 	ww!;;]] 	 	
 	//h?r%   c           
          | j                  ||||||      }| j                  j                  | j                  |d   |d   |d   |d   |d   | j                  | j
                         y)	a  Update the embeddings, metadatas or documents for provided ids.

        Args:
            ids: The ids of the embeddings to update
            embeddings: The embeddings to update. If None, embeddings will be computed based on the documents or images using the embedding_function set for the Collection. Optional.
            metadatas:  The metadata to associate with the embeddings. When querying, you can filter on this metadata. Optional.
            documents: The documents to associate with the embeddings. Optional.
            images: The images to associate with the embeddings. Optional.
        Returns:
            None
        r-   r&   r'   r(   r)   r+   r.   N)$_validate_and_prepare_update_requestr   _updater!   r   r   )r"   r&   r'   r(   r)   r*   r+   update_requests           r#   updatezCollection.update	      2 BB! C 
 	''u%%l3$[1$[1';;]] 	 		
r%   c           
          | j                  ||||||      }| j                  j                  | j                  |d   |d   |d   |d   |d   | j                  | j
                         y)	aO  Update the embeddings, metadatas or documents for provided ids, or create them if they don't exist.

        Args:
            ids: The ids of the embeddings to update
            embeddings: The embeddings to add. If None, embeddings will be computed based on the documents using the embedding_function set for the Collection. Optional.
            metadatas:  The metadata to associate with the embeddings. When querying, you can filter on this metadata. Optional.
            documents: The documents to associate with the embeddings. Optional.

        Returns:
            None
        r-   r&   r'   r(   r)   r+   r.   N)$_validate_and_prepare_upsert_requestr   _upsertr!   r   r   )r"   r&   r'   r(   r)   r*   r+   upsert_requests           r#   upsertzCollection.upsert6  rb   r%   c                     | j                  |||      }| j                  j                  | j                  |d   |d   |d   | j                  | j
                         y)a8  Delete the embeddings based on ids and/or a where filter

        Args:
            ids: The ids of the embeddings to delete
            where: A Where type dict used to filter the delection by. E.g. `{"$and": [{"color" : "red"}, {"price": {"$gte": 4.20}]}}`. Optional.
            where_document: A WhereDocument type dict used to filter the deletion by the document content. E.g. `{$contains: {"text": "hello"}}`. Optional.

        Returns:
            None

        Raises:
            ValueError: If you don't provide either ids, where, or where_document
        r&   r3   r6   )r   r&   r3   r6   r   r   N)$_validate_and_prepare_delete_requestr   _deleter!   r   r   )r"   r&   r3   r6   delete_requests        r#   deletezCollection.deletec  sb    & BB
 	''u% ))*:;;;]] 	 	
r%   )NNNNN)rB   )NN)NNN)#__name__
__module____qualname__intr$   r   r   r   r   r	   r   r   r   r   r   r2   r
   r(   r)   r   r   r   r   rA   rG   	distancesr   rS   strr   r\   ra   rg   r   rl    r%   r#   r   r   !   s   
s 
* 3737-1)-5
r]5
 )$+&(
5
 Ih/05
 Ih/05
 5)*5
 y~&5
 
5
r (,!%# $26'11;3H3HI.
im$.
 .
 }	.

 .
 !/.
 .
 
.
`
# 
y 
2 5937/3!%26!!!!!!
C
")$+&(
C
 i12C
 y/0C
 Ys^,C
 C
 C
 !/C
 C
& 
'C
L TX@SM@4<=O4P@	@H 3737-1)-+
r]+
 )$+&(
+
 Ih/0+
 Ih/0+
 5)*+
 y~&+
 
+
j 3737-1)-+
r]+
 )$+&(
+
 Ih/0+
 Ih/0+
 5)*+
 y~&+
 
+
^ "!%26	
c]
 
 !/	

 

r%   r   r   c                       e Zd ZdZd Zy)CollectionNamea  
    A string wrapper to supply users with indicative message about list_collections only
    returning collection names, in lieu of Collection object.

    When a user will try to access an attribute on a CollectionName string, the __getattribute__ method
    of str is invoked first. If a valid str method or property is found, it will be used. Otherwise, the fallback
    __getattr__ defined here is invoked next. It will error if the requested attribute is a Collection
    method or property.

    For example:
    collection_name = client.list_collections()[0] # collection_name = "test"

    collection_name.startsWith("t") # Evaluates to True.
    # __getattribute__ is invoked first, selecting startsWith from str.

    collection_name.add(ids=[...], documents=[...]) # Raises the error defined below
    # __getattribute__ is invoked first, not finding a match in str.
    # __getattr__ from this class is invoked and raises an error

    c                     t        j                  t              D cg c]  \  }}|j                  d      s| }}}||v rt	        dt        |        d| d      t        d| d      c c}}w )N_z\In Chroma v0.6.0, list_collections only returns collection names. Use Client.get_collection(z) to access zK. See https://docs.trychroma.com/deployment/migration for more information.z*'CollectionName' object has no attribute '')inspect
getmembersr   
startswithNotImplementedErrorrr   AttributeError)r"   itemmemberrw   !collection_attributes_and_methodss        r#   __getattr__zCollectionName.__getattr__  s     %//
;-
;	$$S) ; 	* -
 44%--0YK|D6 J\]  I$qQRR-
s   A+N)rm   rn   ro   __doc__r   rs   r%   r#   ru   ru     s    *Sr%   ru   )!ry   typingr   r   r   $chromadb.api.models.CollectionCommonr   chromadb.api.typesr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   logging	getLoggerrm   loggerchromadb.apir   r   rr   ru   rs   r%   r#   <module>r      sj     1 1 A    ( 			8	$&`
!+. `
F$SS $Sr%   