
    ZiW                     n   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	Z	d dl
mZmZmZ d dl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& d d
l'm(Z( d dl)m*Z* d dl+m,Z,m-Z- d dl+m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3m4Z4 d dl5m6Z6m7Z7m8Z8m9Z9  G d dee          Z: G d dee          Z;dS )    )OptionalSequence)TracebackType)UUID)overrideN)AdminAPI	ClientAPI	ServerAPI)CreateCollectionConfigurationUpdateCollectionConfiguration.validate_embedding_function_conflict_on_create+validate_embedding_function_conflict_on_get)SharedSystemClient)CollectionMetadata
DataLoader	Documents
EmbeddableEmbeddingFunction
Embeddings	GetResultIDsIncludeLoadable	MetadatasQueryResultSchemaURIsIncludeMetadataDocuments!IncludeMetadataDocumentsDistancesDefaultEmbeddingFunction)UserIdentity)maybe_set_tenant_and_database)SettingsSystem)DEFAULT_TENANTDEFAULT_DATABASE)
Collection)ChromaAuthErrorChromaError)DatabaseTenantWhereWhereDocumentc                   ~    e Zd ZU dZeZeed<   eZ	eed<   e
ed<   eed<   dZeed<   ee e            fdee         dee         ded	d
f fdZeeeefdededed	d fd                        Zed	efd            Zed	efd            Ze	 dFdee         dee         d	ee         fd            Zed	efd            Zed
d
d
 e            d
dfdedee         dee         dee          dee!e"                  dee#e$                  ded	efd            Z%e e            d
fdedee!e"                  dee#e$                  d	efd            Z&ed
d
d
 e            d
fdedee         dee         dee          dee!e"                  dee#e$                  d	efd            Z'e	 	 	 dGde(dee         d ee          d!ee)         d	d
f
d"            Z*eded	d
fd#            Z+e	 	 	 dGd$e,d%e(d&e-d'ee.         d(ee/         d)ee0         d	efd*            Z1e	 	 	 	 dHd%e(d$e,d&ee-         d'ee.         d(ee/         d)ee0         d	efd+            Z2e	 	 	 dGd%e(d$e,d&e-d'ee.         d(ee/         d)ee0         d	efd,            Z3ed%e(d	efd-            Z4edId%e(d/ed	e5fd0            Z6ed
d
d
d
d
e7fd%e(d$ee,         d1ee8         dee         dee         d2ee9         d3e:d	e5fd4            Z;	 	 dFd%e(d$ee,         d1ee8         d2ee9         d	d
f
d5Z<ed
d.d
d
e=fd%e(d6e-d$ee,         d7ed1ee8         d2ee9         d3e:d	e>fd8            Z?ed	efd9            Z@ed	efd:            ZAed	efd;            ZBed	efd<            ZCeefdeded	d
fd=            ZDeded	d
fd>            ZEdJd?ZFdKd@ZGdAeeHeI                  dBeeI         dCeeJ         d	d
fdDZKdeded	d
fdEZL xZMS )LClienta  A client for Chroma. This is the main entrypoint for interacting with Chroma.
    A client internally stores its tenant and database and proxies calls to a
    Server API instance of Chroma. It treats the Server API and corresponding System
    as a singleton, so multiple clients connecting to the same resource will share the
    same API instance.

    Client implementations should be implement their own API-caching strategies.
    tenantdatabase_server_admin_clientF_closedsettingsreturnNc                    t                                          |           	 ||| _        ||| _        | j                            t                    | _        |                                 }t          ||j
        ||          \  }}||t          d          ||t          d          |r|| _        |r|| _        t                              | j                  | _        |                     | j        | j                   |                                  d S # t"          $ rI t%          | d          rt'          j        | j        j                   t'          j        | j                    w xY w)Nr5   )4overwrite_singleton_tenant_database_access_from_authuser_provided_tenantuser_provided_databasez]Could not determine a tenant from the current authentication method. Please provide a tenant.zkCould not determine a database name from the current authentication method. Please provide a database name.r0   r1   r3   )super__init__r0   r1   _systeminstancer
   r2   get_user_identityr"   ;chroma_overwrite_singleton_tenant_database_access_from_authr(   AdminClientfrom_systemr3   _validate_tenant_database_submit_client_start_event	Exceptionhasattrr   _release_system_identifier)selfr0   r1   r5   user_identitymaybe_tenantmaybe_database	__class__s          P/root/projects/butler/venv/lib64/python3.11/site-packages/chromadb/api/client.pyr>   zClient.__init__>   s    	(+++-	!$# (  <00;;DL 2244M+HEM  FJ%+'/	, , ,(L. #%s   %(*:% B    +* / . "-!8!8!F!FD**$+*VVV++----- 	 	 	 t_-- S"243E3QRRR.t/?@@@	s   C6D AE0systemc                 T    t          j        |            | |||j                  }|S )N)r0   r1   r5   r   _populate_data_from_systemr5   )clsrQ   r0   r1   r@   s        rP   rD   zClient.from_systemt   s2     	5f===3fx&/RRR    c                     	 | j                                         S # t          j        $ r t	          d          t
          $ r}|d }~wt          $ r!}t	          t          |                    d }~ww xY w)NACould not connect to a Chroma server. Are you sure it is running?)r2   rA   httpxConnectError
ValueErrorr)   rG   str)rK   es     rP   rA   zClient.get_user_identity   s    
	%<11333! 	 	 	S    	 	 	G 	% 	% 	%SVV$$$	%s!    'A2AA2A--A2c                 4    | j                                         S )z2Return the server time in nanoseconds since epoch.)r2   	heartbeatrK   s    rP   r_   zClient.heartbeat   s     |%%'''rV   limitoffsetc                 l      fd j                             || j         j                  D             S )zList collections for the current tenant and database, with pagination.

        Returns:
            Sequence[Collection]: Collection objects for the current tenant.
        c                 <    g | ]}t          j        |           S ))clientmodel)r'   r2   ).0rf   rK   s     rP   
<listcomp>z+Client.list_collections.<locals>.<listcomp>   s8     
 
 
 dl%888
 
 
rV   r<   )r2   list_collectionsr0   r1   )rK   ra   rb   s   `  rP   ri   zClient.list_collections   sR    
 
 
 
66vdkDM 7  
 
 
 	
rV   c                 N    | j                             | j        | j                  S )z9Return the number of collections in the current database.r<   )r2   count_collectionsr0   r1   r`   s    rP   rk   zClient.count_collections   s,     |--; . 
 
 	
rV   nameschemaconfigurationmetadataembedding_functiondata_loaderget_or_createc           	          |i }|                     d          }t          ||           ||||d<   | j                            |||| j        | j        ||          }	t          | j        |	||          S )aI  Create a collection with optional configuration and metadata.

        If using a schema, do not provide `embedding_function`. Instead,
        provide the `embedding_function` as part of the schema.

        Args:
            name: Collection name.
            schema: Optional collection schema for indexes and encryption.
            configuration: Optional collection configuration.
            metadata: Optional collection metadata.
            embedding_function: Optional embedding function for the collection.
            data_loader: Optional data loader for documents with URIs.
            get_or_create: Whether to return an existing collection if present.

        Returns:
            Collection: The created collection.

        Raises:
            ValueError: If the embedding function conflicts with configuration.
        Nrp   )rl   rm   ro   r0   r1   rr   rn   re   rf   rp   rq   )getr   r2   create_collectionr0   r1   r'   )
rK   rl   rm   rn   ro   rp   rq   rr   configuration_efrf   s
             rP   rv   zClient.create_collection   s    B  M(,,-ABB6 0	
 	
 	
 ).>.F2DM./..;]'' / 
 
 <1#	
 
 
 	
rV   c                     | j                             || j        | j                  }|j                            d          }t          ||           t          | j         |||          S )a  Get a collection by name.

        Args:
            name: Collection name.
            embedding_function: Optional embedding function for the collection.
            data_loader: Optional data loader for documents with URIs.

        Returns:
            Collection: The requested collection.

        Raises:
            ValueError: If the embedding function conflicts with configuration.
        rl   r0   r1   rp   rt   )r2   get_collectionr0   r1   configuration_jsonru   r   r'   )rK   rl   rp   rq   rf   persisted_ef_configs         rP   rz   zClient.get_collection   s    , ++;] , 
 

 $6::;OPP3 3	
 	
 	
 <1#	
 
 
 	
rV   c                 >   |i }|                     d          }t          ||           ||||d<   | j                            |||| j        | j        |          }|j                             d          }	t          ||	           t          | j        |||          S )a9  Get an existing collection or create a new one.

        If the collection does not exist, it will be created. If the collection
        already exists, the schema, configuration, and metadata arguments
        will be ignored.

        Args:
            name: Collection name.
            schema: Optional collection schema for indexes and encryption.
            configuration: Optional collection configuration.
            metadata: Optional collection metadata.
            embedding_function: Optional embedding function for the collection.
            data_loader: Optional data loader for URI-backed data.

        Returns:
            Collection: The existing or newly created collection.

        Raises:
            ValueError: If the embedding function does not match the collection's embedding function.
        Nrp   )rl   rm   ro   r0   r1   rn   rt   )	ru   r   r2   get_or_create_collectionr0   r1   r{   r   r'   )
rK   rl   rm   rn   ro   rp   rq   rw   rf   r|   s
             rP   r~   zClient.get_or_create_collection  s    @  M(,,-ABB6 0	
 	
 	
 ).>.F2DM./55;]' 6 
 
 $6::;OPP3 3	
 	
 	
 <1#	
 
 
 	
rV   idnew_namenew_metadatanew_configurationc                 V    | j                             || j        | j        |||          S )N)r   r0   r1   r   r   r   )r2   _modifyr0   r1   )rK   r   r   r   r   s        rP   r   zClient._modifyV  s9     |##;]%/ $ 
 
 	
rV   c                 P    | j                             || j        | j                  S )Nry   )r2   delete_collectionr0   r1   rK   rl   s     rP   r   zClient.delete_collectiong  s0    
 |--;] . 
 
 	
rV   idscollection_id
embeddings	metadatas	documentsurisc           
      Z    | j                             || j        | j        |||||          S )N)r   r0   r1   r   r   r   r   r   )r2   _addr0   r1   )rK   r   r   r   r   r   r   s          rP   r   zClient._addv  s?     |  ;]'! ! 	
 	
 		
rV   c           
      Z    | j                             || j        | j        |||||          S N)r   r0   r1   r   r   r   r   r   )r2   _updater0   r1   rK   r   r   r   r   r   r   s          rP   r   zClient._update  ?     |##';]! $ 	
 	
 		
rV   c           
      Z    | j                             || j        | j        |||||          S r   )r2   _upsertr0   r1   r   s          rP   r   zClient._upsert  r   rV   c                 P    | j                             || j        | j                  S )N)r   r0   r1   )r2   _countr0   r1   )rK   r   s     rP   r   zClient._count  s.    |""';] # 
 
 	
rV   
   nc                 R    | j                             ||| j        | j                  S )N)r   r   r0   r1   )r2   _peekr0   r1   )rK   r   r   s      rP   r   zClient._peek  s1    |!!';]	 " 
 
 	
rV   wherewhere_documentincludec                 \    | j                             || j        | j        ||||||	  	        S )N)	r   r0   r1   r   r   ra   rb   r   r   )r2   _getr0   r1   )rK   r   r   r   ra   rb   r   r   s           rP   r   zClient._get  sB     |  ';]) ! 

 

 
	
rV   c                 Z    | j                             || j        | j        |||           d S )N)r   r0   r1   r   r   r   )r2   _deleter0   r1   )rK   r   r   r   r   s        rP   r   zClient._delete  sC     	';]) 	 	
 	
 	
 	
 	
rV   query_embeddings	n_resultsc                 \    | j                             ||| j        | j        |||||	  	        S )N)	r   r   r0   r1   r   r   r   r   r   )r2   _queryr0   r1   )rK   r   r   r   r   r   r   r   s           rP   r   zClient._query  sB     |""';]-) # 

 

 
	
rV   c                 4    | j                                         S N)r2   resetr`   s    rP   r   zClient.reset  s    |!!###rV   c                 4    | j                                         S r   )r2   get_versionr`   s    rP   r   zClient.get_version  s    |'')))rV   c                 4    | j                                         S r   )r2   get_settingsr`   s    rP   r   zClient.get_settings  s    |((***rV   c                 4    | j                                         S r   )r2   get_max_batch_sizer`   s    rP   r   zClient.get_max_batch_size  s    |..000rV   c                 P    |                      ||           || _        || _        d S Nr<   rE   r0   r1   )rK   r0   r1   s      rP   
set_tenantzClient.set_tenant  s-    &&fx&HHH rV   c                 L    |                      | j        |           || _        d S r   r   )rK   r1   s     rP   set_databasezClient.set_database  s(    &&dkH&MMM rV   c                     | j         rdS d| _         t          | d          rt          j        | j        j                   t          j        | j                   dS )a  Close the client and release all resources.

        This method decrements the reference count for the underlying System.
        When the last client using a shared System calls close(), the System
        is stopped and all resources (database connections, etc.) are released.

        This is particularly important for PersistentClient to avoid SQLite
        file locking issues.

        Note: If multiple clients share the same System (e.g., multiple PersistentClient
        instances with the same path), the System will only be stopped when the last
        client is closed. This allows safe use of context managers with multiple clients.

        Example:
            >>> client = chromadb.PersistentClient(path="./chroma_db")
            >>> # ... use client ...
            >>> client.close()

            Or using context manager:
            >>> with chromadb.PersistentClient(path="./chroma_db") as client:
            ...     # ... use client ...
        NTr3   )r4   rH   r   rI   r3   rJ   r`   s    rP   closezClient.close#  sb    0 < 	F 4)) 	O.t/A/MNNN 	*4+;<<<<<rV   c                     | S )zContext manager entry. r`   s    rP   	__enter__zClient.__enter__G  s    rV   exc_typeexc_valexc_tbc                 .    |                                   dS )zContext manager exit.N)r   )rK   r   r   r   s       rP   __exit__zClient.__exit__K  s     	

rV   c                 X   	 | j                             |           nM# t          j        $ r t	          d          t
          $ r}|d }~wt          $ r t	          d| d          w xY w	 | j                             ||           d S # t          j        $ r t	          d          w xY w)Nrl   rX   zCould not connect to tenant z. Are you sure it exists?rl   r0   )r3   
get_tenantrY   rZ   r[   r)   rG   get_database)rK   r0   r1   r]   s       rP   rE   z Client._validate_tenant_databaseT  s    	))v)6666! 	 	 	S    	 	 	G 	 	 	PvPPP  	
	++&+IIIII! 	 	 	S  	s!    'A(A!A(,B
 
B))NN)NNN)NNNN)r   )r6   N)r6   r/   )N__name__
__module____qualname____doc__r%   r0   r\   __annotations__r&   r1   r
   r   r4   boolr#   r   r>   classmethodr   r$   rD   r!   rA   intr_   r   r'   ri   rk   r    r   r   r   r   r   r   r   rv   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r   typeBaseExceptionr   r   rE   __classcell__rO   s   @rP   r/   r/   +   s	          !FC   $Hc$$$GT
 !/"2%XZZ	4 44 3-4 	4
 
4 4 4 4 4 4l  %(	   	
 
   X [ %< % % % X% (3 ( ( ( X( CG
 
c]
3;C=
	*	
 
 
 X
 
3 
 
 
 X
  $(AE15 %$&&6:#<
 <
<
  <
   =>	<

 -.<
 %j)
<
 j23<
 <
 
<
 <
 <
 X<
|  %$&&6:%
 %
%
 %j)
%
 j23%
 
%
 %
 %
 X%
N  $(AE15 %$&&6:>
 >
>
  >
   =>	>

 -.>
 %j)
>
 j23>
 
>
 >
 >
 X>
@  #'59EI
 

 3-
 12	

 $$AB
 

 
 
 X
  

 

 
 
 X
  *.)-#
 

 
 	

 I&
 I&
 tn
 

 
 
 X
( 
 ,0)-)-#
 

 
 Z(	

 I&
 I&
 tn
 

 
 
 X
(  *.)-#
 

 
 	

 I&
 I&
 tn
 

 
 
 X
( 
D 
S 
 
 
 X
 
 
4 
C 
 
 
 
 X
  "!%# $263
 

 c]
 	

 }
 
 !/
 
 

 
 
 X
4 "&26
 

 c]
 	

 !/
 

 
 
 
  
 "!%26<
 

 %
 c]	

 
 
 !/
 
 

 
 
 X
, $t $ $ $ X$ *S * * * X* +h + + + X+ 1C 1 1 1 X1 6F ! ! ! !4 ! ! ! X!
 !S !T ! ! ! X!"= "= "= "=H   4./ -( '	
 
    s t        rV   r/   c                       e Zd ZU dZeed<    e            fdeddf fdZee	fde
de
ddfd	            Zee	fde
de
defd
            Zee	fde
de
ddfd            Zedde	fdee         dee         de
dee         fd            Zede
ddfd            Zede
defd            Zeededd fd                        Z xZS )rC   z0Admin client for managing tenants and databases.r2   r5   r6   Nc                     t                                          |           | j                            t                    | _        d S r   )r=   r>   r?   r@   r
   r2   )rK   r5   rO   s     rP   r>   zAdminClient.__init__r  s6    """|,,Y77rV   rl   r0   c                 :    | j                             ||          S )zCreate a database in a tenant.

        Args:
            name: Database name.
            tenant: Tenant that owns the database.
        r   )r2   create_databaserK   rl   r0   s      rP   r   zAdminClient.create_databasev       |++f+EEErV   c                 :    | j                             ||          S )zGet a database by name.

        Args:
            name: Database name.
            tenant: Tenant that owns the database.

        Returns:
            Database: The database record.
        r   )r2   r   r   s      rP   r   zAdminClient.get_database  s     |((d6(BBBrV   c                 :    | j                             ||          S )zDelete a database by name.

        Args:
            name: Database name.
            tenant: Tenant that owns the database.
        r   )r2   delete_databaser   s      rP   r   zAdminClient.delete_database  r   rV   ra   rb   c                 <    | j                             |||          S )N)r0   )r2   list_databases)rK   ra   rb   r0   s       rP   r   zAdminClient.list_databases  s!     |**5&*HHHrV   c                 8    | j                             |          S Nr   )r2   create_tenantr   s     rP   r   zAdminClient.create_tenant  s    |))t)444rV   c                 8    | j                             |          S r   )r2   r   r   s     rP   r   zAdminClient.get_tenant  s    |&&D&111rV   rQ   c                 P    t          j        |            | |j                  }|S )Nr8   rS   )rU   rQ   r@   s      rP   rD   zAdminClient.from_system  s.     	5f===3000rV   )r   r   r   r   r
   r   r#   r>   r   r%   r\   r   r*   r   r   r   r   r   r   r   r+   r   r   r$   rD   r   r   s   @rP   rC   rC   m  s@        ::,4HJJ 8 8 84 8 8 8 8 8 8 7E F FC F F$ F F F XF 4B 
C 
C 
Cc 
Cx 
C 
C 
C X
C 7E F FC F F$ F F F XF   $ $$	I I}I I 	I
 
(	I I I XI 5# 5$ 5 5 5 X5 2s 2v 2 2 2 X2  
   X [    rV   rC   )<typingr   r   typesr   uuidr   	overridesr   rY   chromadb.apir   r	   r
   %chromadb.api.collection_configurationr   r   r   r   !chromadb.api.shared_system_clientr   chromadb.api.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    chromadb.authr!   chromadb.auth.utilsr"   chromadb.configr#   r$   r%   r&   chromadb.api.models.Collectionr'   chromadb.errorsr(   r)   chromadb.typesr*   r+   r,   r-   r/   rC   r   rV   rP   <module>r      s   % % % % % % % %                    7 7 7 7 7 7 7 7 7 7            A @ @ @ @ @                                     & ' & & & & & = = = = = = , , , , , , , , < < < < < < < < 5 5 5 5 5 5 8 8 8 8 8 8 8 8 A A A A A A A A A A A A} } } } } } } }DC C C C C$h C C C C CrV   