
    _j]                     &   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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DeleteResultmaybe_cast_one_to_many)UpdateCollectionConfiguration)SearchN)Function)AttachedFunction)	ServerAPIc                      e Zd Zej        fdedefdZdefdZ	 	 	 	 	 d1d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d2d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	 	 	 d3dee         dee         dee         ddfdZd edd fd!Z defd"Z!ej        fd#e	e"         dede#fd$Z$	 	 	 	 	 d1d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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&	 	 	 	 d4dee'         dee         dee         dee         de(f
d'Z)	 d5d(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-Z0	 d6ded/e.de.fd0Z1dS )7
Collection
read_levelreturnc                 \    | j                             | j        | j        | j        |          S )a  Return the number of records in the collection.

        Args:
            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.
                - ReadLevel.INDEX_AND_BOUNDED_WAL: Read from the index and up to a
                  server-configured number of WAL entries for bounded query latency.
        )collection_idtenantdatabaser'   )_client_countidr+   r,   )selfr'   s     [/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/api/models/Collection.pycountzCollection.count+   s5     |""';]!	 # 
 
 	
    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+   r,   )r-   _get_indexing_statusr/   r+   r,   r0   s    r1   get_indexing_statuszCollection.get_indexing_status>   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.
        r9   r:   r;   r<   r=   r>   r9   r:   r;   r<   r>   r*   r9   r:   r;   r<   r>   r+   r,   N)!_validate_and_prepare_add_requestr-   _addr/   r+   r,   )r0   r9   r:   r;   r<   r=   r>   add_requests           r1   addzCollection.addN   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.
        )r9   rF   rI   rJ   r9   rF   rI   rJ   )	r*   r9   rF   rI   rJ   rG   rH   r+   r,   responserJ   )!_validate_and_prepare_get_requestr-   _getr/   r+   r,   _transform_get_response)	r0   r9   rF   rG   rH   rI   rJ   get_requestget_resultss	            r1   getzCollection.get   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   rG   s     r1   peekzCollection.peek   sH     ,,L"g{	   
 
 	
r3   )r;   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.
        )	r[   r\   r]   r^   r9   r_   rF   rI   rJ   r9   r:   r_   rF   rI   rJ   )	r*   r9   r[   r_   rF   rI   rJ   r+   r,   rL   )#_validate_and_prepare_query_requestr-   _queryr/   r+   r,   _transform_query_response)r0   r[   r\   r]   r^   r9   r_   rF   rI   rJ   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   rg   rh   ri   s       r1   modifyzCollection.modify  sq     	%%h///
 	w!+;] 	 	
 	
 	
 	//hNNNNNr3   rk   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 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*   rk   r+   r,   )clientmodelembedding_functiondata_loader)r-   _forkr/   r+   r,   r&   _embedding_function_data_loader)r0   rk   rt   s      r1   forkzCollection.fork7  s_     ""';]	 # 
 
 <#7)	
 
 
 	
r3   c                 Z    | j                             | j        | j        | j                  S )zGet the number of forks that exist for this collection.
        This only works for Hosted Chroma for now.

        Returns:
            int: The number of forks for this collection.
        r5   )r-   _fork_countr/   r+   r,   r7   s    r1   
fork_countzCollection.fork_countQ  s2     |''';] ( 
 
 	
r3   searchesc                      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.
                - ReadLevel.INDEX_AND_BOUNDED_WAL: Read from the index and up to a
                  server-configured number of WAL entries for bounded query latency.

        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*   r~   r+   r,   r'   )	r   r-   _searchr/   r   r   r!   r+   r,   )r0   r~   r'   searches_listembedded_searchess   `    r1   r   zCollection.search^  s    T /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@   r9   r:   r;   r<   r>   rA   N)$_validate_and_prepare_update_requestr-   _updater/   r+   r,   )r0   r9   r:   r;   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@   r9   r:   r;   r<   r>   rA   N)$_validate_and_prepare_upsert_requestr-   _upsertr/   r+   r,   )r0   r9   r:   r;   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         |d         | j        | j                  S )a  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.
            limit: Maximum number of records to delete. Can only be used with where or where_document filters.

        Returns:
            DeleteResult: A dict containing the number of records deleted.

        Raises:
            ValueError: If no IDs or filters are provided.
            ValueError: If limit is specified without a where or where_document clause.
        )rG   r9   rF   rI   rG   )r*   r9   rF   rI   rG   r+   r,   )$_validate_and_prepare_delete_requestr-   _deleter/   r+   r,   )r0   r9   rF   rI   rG   delete_requests         r1   deletezCollection.delete  sy    0 BBe C 
 
 |##'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_idrg   input_collection_idr   r   r+   r,   )
isinstancer"   valuer-   attach_functionr/   r+   r,   )r0   r   rg   r   r   r   s         r1   r   zCollection.attach_functionC  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
        )rg   r   r+   r,   )r-   get_attached_functionr/   r+   r,   )r0   rg   s     r1   r   z Collection.get_attached_functionm  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)
        )rg   r   delete_outputr+   r,   )r-   detach_functionr/   r+   r,   )r0   rg   r   s      r1   r   zCollection.detach_function  s8    " |++ $2;] , 
 
 	
r3   )NNNNN)rT   )NNN)NNNN)N)F)2__name__
__module____qualname__r   INDEX_AND_WALintr2   r   r8   r   r   r   r   r   r   r   r   r   r   rE   r   r   r   r   rS   rY   r   rf   strr   r    rq   rz   r}   r!   r   r   r   r   r   r   r   r"   r   r	   r
   boolr   r   r   r   r3   r1   r&   r&   *   si       ,5,C 
 
	 
 
 
 
 
&
^ 
 
 
 
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<

 

 
 
 
4
C 
 
 
 
  !* 7Y
 Y
F#Y
 Y
 
	Y
 Y
 Y
 Y
F 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]$
 $
 !/	$

 }$
 
$
 $
 $
 $
V ,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   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	$	$ '&&&&&&m	
 m	
 m	
 m	
 m	
!+. m	
 m	
 m	
 m	
 m	
r3   