
    eiBZ                        d dl mZmZmZmZmZ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mZmZ d dlm Z  d dl!m"Z" d dl#Z#d dl$m%Z% erd dl&m'Z'  e#jP                  e)      Z*erd d	l+m,Z,  G d
 de
d         Z-y)    )TYPE_CHECKINGOptionalUnionListcastDictAnyTuple)CollectionCommon)URICollectionMetadata	EmbeddingPyEmbeddingIncludeIndexingStatusMetadataDocumentImageWhereIDs	GetResultQueryResultID	OneOrMany	ReadLevelWhereDocumentSearchResultmaybe_cast_one_to_many)UpdateCollectionConfiguration)SearchN)Function)AttachedFunction)	ServerAPIc                      e Zd Zdef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dd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d0dedefdZddddddddg df	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ede	e   de	e   dedefdZ	 	 	 d1de	e   de	e   de	e   ddfdZdedd fd Zej@                  fd!ee!   d"e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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%	 	 	 d1de	e&   de	e   de	e   ddfd&Z'	 d2d'e(ded(ed)e	e)ee*f      de+d*e,f   f
d+Z-dedd*fd,Z.	 d3ded-e,de,fd.Z/y)4
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    h/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/chromadb/api/models/Collection.pycountzCollection.count*   s5     ||""'';;]] # 
 	
    c                 z    | j                   j                  | j                  | j                  | j                        S )a  Get the indexing status of this collection.

        Returns:
            IndexingStatus: An object containing:
                - num_indexed_ops: Number of user operations that have been indexed
                - num_unindexed_ops: Number of user operations pending indexing
                - total_ops: Total number of user operations in collection
                - op_indexing_progress: Proportion of user operations that have been indexed as a float between 0 and 1
        r(   )r,   _get_indexing_statusr.   r*   r+   r/   s    r1   get_indexing_statuszCollection.get_indexing_status7   s5     ||00'';;]] 1 
 	
r3   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

        r7   r8   r9   r:   r;   r<   r7   r8   r9   r:   r<   r)   r7   r8   r9   r:   r<   r*   r+   N)!_validate_and_prepare_add_requestr,   _addr.   r*   r+   )r0   r7   r8   r9   r:   r;   r<   add_requests           r1   addzCollection.addG   s    F <<! = 
 	''E""<0!+.!+.V$;;]] 	 		
r3   wherelimitoffsetwhere_documentincludec                     | j                  ||||      }| j                  j                  | j                  |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": "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.

        )r7   rD   rG   rH   r7   rD   rG   rH   )	r)   r7   rD   rG   rH   rE   rF   r*   r+   responserH   )!_validate_and_prepare_get_requestr,   _getr.   r*   r+   _transform_get_response)	r0   r7   rD   rE   rF   rG   rH   get_requestget_resultss	            r1   getzCollection.get~   s    0 <<)	 = 
 ll''''E"g&&'78	*;;]] ( 

 ++ +i*@ , 
 	
r3   
   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+   )r0   rE   s     r1   peekzCollection.peek   sF     ,,LL"gg{{	  
 	
r3   )r9   r:   	distancesquery_embeddingsquery_textsquery_images
query_uris	n_resultsc
                 
   | j                  |||||||||		      }
| j                  j                  | j                  |
d   |
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.
            ids: A subset of ids to search within. 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": "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

        )	rY   rZ   r[   r\   r7   r]   rD   rG   rH   r7   r8   r]   rD   rG   rH   )	r)   r7   rY   r]   rD   rG   rH   r*   r+   rJ   )#_validate_and_prepare_query_requestr,   _queryr.   r*   r+   _transform_query_response)r0   rY   rZ   r[   r\   r7   r]   rD   rG   rH   query_requestquery_resultss               r1   queryzCollection.query   s    Z @@-#%!) A 

 ++''e$*<8#K0(()9:!),;;]] , 

 --"M),D . 
 	
r3   namemetadataconfigurationc                     | 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_metadatanew_configurationr*   r+   N)_validate_modify_requestr,   _modifyr.   r*   r+   "_update_model_after_modify_success)r0   re   rf   rg   s       r1   modifyzCollection.modify  s]      	%%h/
 	ww!+;;]] 	 	
 	//hNr3   ri   c                     | j                   j                  | j                  || j                  | j                        }t        | j                   || j                  | j                        S )a  Fork the current collection under a new name. The returning collection should contain identical data to the current collection.
        This is an experimental API that only works for Hosted Chroma for now.

        Args:
            new_name: The name of the new collection.

        Returns:
            Collection: A new collection with the specified name and containing identical data to the current collection.
        )r)   ri   r*   r+   )clientmodelembedding_functiondata_loader)r,   _forkr.   r*   r+   r%   _embedding_function_data_loader)r0   ri   rr   s      r1   forkzCollection.fork'  s`     ""'';;]]	 # 
 <<#77))	
 	
r3   searches
read_levelc                    t        |      }|g }|D cg c]  }| j                  |       }}| j                  j                  | j                  t        t        t           |      | j                  | j                  |      S c c}w )a
  Perform hybrid search on the collection.
        This is an experimental API that only works for Hosted Chroma for now.

        Args:
            searches: A single Search object or a list of Search objects, each containing:
                - where: Where expression for filtering
                - rank: Ranking expression for hybrid search (defaults to Val(0.0))
                - limit: Limit configuration for pagination (defaults to no limit)
                - select: Select configuration for keys to return (defaults to empty)
            read_level: Controls whether to read from the write-ahead log (WAL):
                - ReadLevel.INDEX_AND_WAL: Read from both the compacted index and WAL (default).
                  All committed writes will be visible.
                - ReadLevel.INDEX_ONLY: Read only from the compacted index, skipping the WAL.
                  Faster, but recent writes that haven't been compacted may not be visible.

        Returns:
            SearchResult: Column-major format response with:
                - ids: List of result IDs for each search payload
                - documents: Optional documents for each payload
                - embeddings: Optional embeddings for each payload
                - metadatas: Optional metadata for each payload
                - scores: Optional scores for each payload
                - select: List of selected keys for each payload

        Raises:
            NotImplementedError: For local/segment API implementations

        Examples:
            # Using builder pattern with Key constants
            from chromadb.execution.expression import (
                Search, Key, K, Knn, Val
            )

            # Note: K is an alias for Key, so K.DOCUMENT == Key.DOCUMENT
            search = (Search()
                .where((K("category") == "science") & (K("score") > 0.5))
                .rank(Knn(query=[0.1, 0.2, 0.3]) * 0.8 + Val(0.5) * 0.2)
                .limit(10, offset=0)
                .select(K.DOCUMENT, K.SCORE, "title"))

            # Direct construction
            from chromadb.execution.expression import (
                Search, Eq, And, Gt, Knn, Limit, Select, Key
            )

            search = Search(
                where=And([Eq("category", "science"), Gt("score", 0.5)]),
                rank=Knn(query=[0.1, 0.2, 0.3]),
                limit=Limit(offset=0, limit=10),
                select=Select(keys={Key.DOCUMENT, Key.SCORE, "title"})
            )

            # Single search
            result = collection.search(search)

            # Multiple searches at once
            searches = [
                Search().where(K("type") == "article").rank(Knn(query=[0.1, 0.2])),
                Search().where(K("type") == "paper").rank(Knn(query=[0.3, 0.4]))
            ]
            results = collection.search(searches)

            # Skip WAL for faster queries (may miss recent uncommitted writes)
            from chromadb.api.types import ReadLevel
            result = collection.search(search, read_level=ReadLevel.INDEX_ONLY)
        )r)   ry   r*   r+   rz   )
r   _embed_search_string_queriesr,   _searchr.   r   r   r    r*   r+   )r0   ry   rz   searches_listsearchembedded_searchess         r1   r   zCollection.searchA  s    P /x8 M ER
:@D--f5
 
 ||##''$v,(9:;;]]! $ 
 	
	
s   B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>   r7   r8   r9   r:   r<   r?   N)$_validate_and_prepare_update_requestr,   _updater.   r*   r+   )r0   r7   r8   r9   r:   r;   r<   update_requests           r1   updatezCollection.update      2 BB! C 
 	''u%%l3$[1$[1';;]] 	 		
r3   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>   r7   r8   r9   r:   r<   r?   N)$_validate_and_prepare_upsert_requestr,   _upsertr.   r*   r+   )r0   r7   r8   r9   r:   r;   r<   upsert_requests           r1   upsertzCollection.upsert  r   r3   c                     | j                  |||      }| j                  j                  | j                  |d   |d   |d   | j                  | j
                         y)a0  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": "hello"}`. Optional.

        Returns:
            None

        Raises:
            ValueError: If you don't provide either ids, where, or where_document
        r7   rD   rG   )r)   r7   rD   rG   r*   r+   N)$_validate_and_prepare_delete_requestr,   _deleter.   r*   r+   )r0   r7   rD   rG   delete_requests        r1   deletezCollection.delete  sb    & BB
 	''u% ))*:;;;]] 	 	
r3   functionoutput_collectionparamsr"   c           	          t        |t              r|j                  n|}| j                  j	                  ||| j
                  ||| j                  | j                        S )a  Attach a function to this collection.

        Args:
            function: A Function enum value (e.g., STATISTICS_FUNCTION, RECORD_COUNTER_FUNCTION)
            name: Unique name for this attached function
            output_collection: Name of the collection where function output will be stored
            params: Optional dictionary with function-specific parameters

        Returns:
            Tuple of (AttachedFunction, created) where created is True if newly created,
            False if already existed (idempotent request)

        Example:
            >>> from chromadb.api.functions import STATISTICS_FUNCTION
            >>> attached_fn = collection.attach_function(
            ...     function=STATISTICS_FUNCTION,
            ...     name="mycoll_stats_fn",
            ...     output_collection="mycoll_stats",
            ... )
            >>> if created:
            ...     print("New function attached")
            ... else:
            ...     print("Function already existed")
        )function_idre   input_collection_idr   r   r*   r+   )
isinstancer!   valuer,   attach_functionr.   r*   r+   )r0   r   re   r   r   r   s         r1   r   zCollection.attach_function  sU    > )38X(FhnnH||++# $/;;]] , 
 	
r3   c                 |    | j                   j                  || j                  | j                  | j                        S )a  Get an attached function by name for this collection.

        Args:
            name: Name of the attached function

        Returns:
            AttachedFunction: The attached function object

        Raises:
            NotFoundError: If the attached function doesn't exist
        )re   r   r*   r+   )r,   get_attached_functionr.   r*   r+   )r0   re   s     r1   r   z Collection.get_attached_function>  s8     ||11 $;;]]	 2 
 	
r3   delete_output_collectionc                 ~    | j                   j                  || j                  || j                  | j                        S )a  Detach a function from this collection.

        Args:
            name: The name of the attached function
            delete_output_collection: Whether to also delete the output collection. Defaults to False.

        Returns:
            bool: True if successful

        Example:
            >>> success = collection.detach_function("my_function", delete_output_collection=True)
        )re   r   delete_outputr*   r+   )r,   detach_functionr.   r*   r+   )r0   re   r   s      r1   r   zCollection.detach_functionQ  s;    " ||++ $2;;]] , 
 	
r3   )NNNNN)rR   )NNN)N)F)0__name__
__module____qualname__intr2   r   r6   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   rQ   rW   r   rd   strr   r   ro   rx   r   INDEX_AND_WALr    r   r   r   r   r   r   r!   r   r	   r
   boolr   r   r    r3   r1   r%   r%   )   s   
s 

^ 
0 3737-1)-5
r]5
 )$+&(
5
 Ih/05
 Ih/05
 5)*5
 y~&5
 
5
r (,!%# $26'5,
im$,
 ,
 }	,

 ,
 !/,
 ,
 
,
\
# 
y 
2 5937/3'+!%26
G
")$+&(
G
 i12G
 y/0G
 Ys^,G
 im$G
 G
 G
 !/G
 G
( 
)G
V #15AE	OsmO -.O   =>	O
 
O@

 

: !* 7 7W
F#W
 W
 
	W
B 3737-1)-+
r]+
 )$+&(
+
 Ih/0+
 Ih/0+
 5)*+
 y~&+
 
+
j 3737-1)-+
r]+
 )$+&(
+
 Ih/0+
 Ih/0+
 5)*+
 y~&+
 
+
^ "!%26	
c]
 
 !/	

 

J ,0(
(
 (
 	(

 c3h((
 
!4'	((
T
# 
2D 
, */

 #'
 
	
r3   r%   r#   ).typingr   r   r   r   r   r   r	   r
   $chromadb.api.models.CollectionCommonr   chromadb.api.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   %chromadb.api.collection_configurationr   "chromadb.execution.expression.planr    loggingchromadb.api.functionsr!   $chromadb.api.models.AttachedFunctionr"   	getLoggerr   loggerchromadb.apir#   r%   r   r3   r1   <module>r      sp    O O O A     * P 5  +E			8	$&
!+. 
r3   