
    ZiuW                     "   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#j(        e)          Z*erd d	l+m,Z,  G d
 de
d                   Z-dS )    )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                   T   e Zd ZdefdZdefdZ	 	 	 	 	 d0d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d1d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	 	 	 d2d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#	 	 	 	 	 d0d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$	 	 	 	 	 d0d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%	 	 	 d2de	e&         de	e         de	e         ddfd&Z'	 d3d'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.	 d4ded.e,de,fd/Z/dS )5
Collectionreturnc                 Z    | j                             | j        | j        | j                  S )z/Return the number of records in the collection.collection_idtenantdatabase)_client_countidr*   r+   selfs    [/root/projects/butler/venv/lib64/python3.11/site-packages/chromadb/api/models/Collection.pycountzCollection.count*   s0    |""';] # 
 
 	
    c                 Z    | 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_status2   s2     |00';] 1 
 
 	
r3   Nids
embeddings	metadatas	documentsimagesurisc           
          |                      ||||||          }| j                            | j        |d         |d         |d         |d         |d         | j        | j                   dS )	a  Add records to the collection.

        Args:
            ids: Record IDs to add.
            embeddings: Embeddings to add. If None, embeddings are computed.
            metadatas: Optional metadata for each record.
            documents: Optional documents for each record.
            images: Optional images for each record.
            uris: Optional URIs for loading images.

        Raises:
            ValueError: If embeddings and documents are both missing.
            ValueError: If embeddings and documents are both provided.
            ValueError: If lengths of provided fields do not match.
            ValueError: If an ID 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.addB   s    > <<! = 
 
 	'E""<0!+.!+.V$;] 	 		
 		
 		
 		
 		
r3   wherelimitoffsetwhere_documentincludec                    |                      ||||          }| j                            | j        |d         |d         |d         |d         ||| j        | j        	  	        }|                     ||d                   S )a  Retrieve records from the collection.

        If no filters are provided, returns records up to ``limit`` starting at
        ``offset``.

        Args:
            ids: If provided, only return records with these IDs.
            where: A Where filter used to filter based on metadata values.
            limit: Maximum number of results to return.
            offset: Number of results to skip before returning.
            where_document: A WhereDocument filter used to filter based on K.DOCUMENT.
            include: Fields to include in results. Can contain "embeddings", "metadatas", "documents", "uris". Defaults to "metadatas" and "documents".

        Returns:
            GetResult: Retrieved records and requested fields as a GetResult object.
        )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.getu   s    2 <<)	 = 
 
 l'''E"g&&'78	*;] ( 

 

 ++ +i*@ , 
 
 	
r3   
   c                     |                      | j                            | j        || j        | j                            S )zReturn the first ``limit`` records from the collection.

        Args:
            limit: Maximum number of records to return.

        Returns:
            GetResult: Retrieved records and requested fields.
        )r)   nr*   r+   )_transform_peek_responser,   _peekr.   r*   r+   )r0   rE   s     r1   peekzCollection.peek   sH     ,,L"g{	   
 
 	
r3   )r9   r:   	distancesquery_embeddingsquery_textsquery_images
query_uris	n_resultsc
                 $   |                      |||||||||		  	        }
| j                            | j        |
d         |
d         |
d         |
d         |
d         |
d         | j        | j        	  	        }|                     ||
d         	          S )
a  Query for the K nearest neighbor records in the collection.

        This is a batch query API. Multiple queries can be performed at once
        by providing multiple embeddings, texts, or images.

        >>> query_1 = [0.1, 0.2, 0.3]
        >>> query_2 = [0.4, 0.5, 0.6]
        >>> results = collection.query(
        >>>     query_embeddings=[query_1, query_2],
        >>>     n_results=10,
        >>> )

        If query_texts, query_images, or query_uris are provided, the collection's
        embedding function will be used to create embeddings before querying
        the API.

        The `ids`, `where`, `where_document`, and `include` parameters are applied
        to all queries.

        Args:
            query_embeddings: Raw embeddings to query for.
            query_texts: Documents to embed and query against.
            query_images: Images to embed and query against.
            query_uris: URIs to be loaded and embedded.
            ids: Optional subset of IDs to search within.
            n_results: Number of neighbors to return per query.
            where: Metadata filter.
            where_document: Document content filter.
            include: Fields to include in results. Can contain "embeddings", "metadatas", "documents", "uris", "distances". Defaults to "metadatas", "documents", "distances".

        Returns:
            QueryResult: Nearest neighbor results.

        Raises:
            ValueError: If no query input is provided.
            ValueError: If multiple query input types are provided.
        )	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    v @@-#%!) A 

 

 ++'e$*<8#K0(()9:!),;] , 

 

 --"M),D . 
 
 	
r3   namemetadataconfigurationc                     |                      |           | j                            | j        |||| j        | j                   |                     |||           dS )zUpdate collection name, metadata, or configuration.

        Args:
            name: New collection name.
            metadata: New metadata for the collection.
            configuration: New configuration for the collection.
        )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  sq     	%%h///
 	w!+;] 	 	
 	
 	
 	//hNNNNNr3   ri   c                     | 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)	
 
 
 	
r3   searches
read_levelc                      t          |          }|g } fd|D             } j                             j        t	          t
          t                   |           j         j        |          S )a  Perform hybrid search on the collection.
        This is an experimental API that only works for distributed and 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)
        Nc                 :    g | ]}                     |          S  )_embed_search_string_queries).0searchr0   s     r1   
<listcomp>z%Collection.search.<locals>.<listcomp>  s4     
 
 
:@D--f55
 
 
r3   )r)   ry   r*   r+   rz   )	r   r,   _searchr.   r   r   r    r*   r+   )r0   ry   rz   searches_listembedded_searchess   `    r1   r   zCollection.searchE  s    P /x88 M
 
 
 
DQ
 
 
 |##'$v,(9::;]! $ 
 
 	
r3   c           
          |                      ||||||          }| j                            | j        |d         |d         |d         |d         |d         | j        | j                   dS )	a-  Update existing records by ID.

        Records are provided in columnar format. If provided, the `embeddings`, `metadatas`, `documents`, and `uris` lists must be the same length.
        Entries in each list correspond to the same record.

        >>> ids = ["id1", "id2", "id3"]
        >>> embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]
        >>> metadatas = [{"key": "value"}, {"key": "value"}, {"key": "value"}]
        >>> documents = ["document1", "document2", "document3"]
        >>> uris = ["uri1", "uri2", "uri3"]
        >>> collection.update(ids, embeddings, metadatas, documents, uris)

        If `embeddings` are not provided, the embeddings will be computed based on `documents` using the collection's embedding function.

        Args:
            ids: Record IDs to update.
            embeddings: Updated embeddings. If None, embeddings are computed.
            metadatas: Updated metadata.
            documents: Updated documents.
            images: Updated images.
            uris: Updated URIs for loading images.
        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  s    H BB! C 
 
 	'u%%l3$[1$[1';] 	 		
 		
 		
 		
 		
r3   c           
          |                      ||||||          }| j                            | j        |d         |d         |d         |d         |d         | j        | j                   dS )	ar  Create or update records by ID.

        Args:
            ids: Record IDs to upsert.
            embeddings: Embeddings to add or update. If None, embeddings are computed.
            metadatas: Metadata to add or update.
            documents: Documents to add or update.
            images: Images to add or update.
            uris: URIs for loading images.
        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  s    0 BB! C 
 
 	'u%%l3$[1$[1';] 	 		
 		
 		
 		
 		
r3   c                     |                      |||          }| j                            | j        |d         |d         |d         | j        | j                   dS )ai  Delete records by ID or filters.

        All documents that match the `ids` or `where` and `where_document` filters will be deleted.

        Args:
            ids: Record IDs to delete.
            where: Metadata filter.
            where_document: Document content filter.

        Raises:
            ValueError: If no IDs or filters are provided.
        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  ss    $ BB
 
 	'u% ))*:;;] 	 	
 	
 	
 	
 	
r3   functionoutput_collectionparamsr"   c           	          t          |t                    r|j        n|}| 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!  sX    > )38X(F(FThnnH|++# $/;] , 
 
 	
r3   c                 \    | 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_functionK  s5     |11 $;]	 2 
 
 	
r3   Fdelete_output_collectionc                 ^    | 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_function^  s8    " |++ $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   r}   r3   r1   r%   r%   )   s+       
s 
 
 
 

^ 
 
 
 
0 3737-1)-1
 1
r]1
 )$+&(
1
 Ih/01
 Ih/01
 5)*1
 y~&1
 
1
 1
 1
 1
j (,!%# $26'5-
 -
im$-
 -
 }	-

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

 

 
 
 
H ,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      s   O O O O O O O O O O O O O O O O O O O O A A A A A A                                         * P O O O O O 5 5 5 5 5 5  + + + + + + FEEEEEE		8	$	$ '&&&&&&L	
 L	
 L	
 L	
 L	
!+. L	
 L	
 L	
 L	
 L	
r3   