
    ug`D                        d dl Z d dl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Zd dlmZ d dlmc 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*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z? d dlm@ZAmBZBmCZC d dlDZD eDj                  eF      ZGerd dlHmIZImJZJ  e	dd	d
      ZK e	d      ZLdeMdeedeLf   gedeLf   f   fdZN G d deeK         ZOy)    N)
TYPE_CHECKINGCallableDictGenericOptionalAnySetTypeVarUnioncast)Metadata)UUID))URIURIs
AddRequestBaseRecordSetCollectionMetadata
DataLoaderDeleteRequest	Embedding
Embeddings	FilterSet
GetRequestIncludeEnumPyEmbedding
Embeddable	GetResultIncludeLoadableDocumentImageQueryRequestQueryResultIDsEmbeddingFunctionID	OneOrManyUpdateRequestUpsertRequest(get_default_embeddable_record_set_fieldsmaybe_cast_one_to_manynormalize_base_record_setnormalize_insert_record_setvalidate_base_record_setvalidate_idsvalidate_includevalidate_insert_record_setvalidate_metadatavalidate_embedding_functionvalidate_n_results validate_record_set_contains_any!validate_record_set_for_embeddingvalidate_filter_set)
CollectionWhereWhereDocument)	ServerAPIAsyncServerAPIClientTr;   r<   Tnamereturn.c                 P     dt         dt        f   dt         dt        f   f fd}|S )zA decorator that wraps a method with a try-except block that catches
    exceptions and adds the method name to the error message. This allows us to
    provide more context when an error occurs, without rewriting validators.
    func.r@   c           	      x     t        j                         dt        dt        dt        dt        f fd       }|S )Nselfargskwargsr@   c                     	  | g|i |S # t         $ r@}t        |       d d} t        |      |      j                  |j                        d }~ww xY w)Nz in .)	Exceptionstrtypewith_traceback__traceback__)rD   rE   rF   emsgrB   r?   s        Y/var/www/openai/venv/lib/python3.12/site-packages/chromadb/api/models/CollectionCommon.pywrapperz6validation_context.<locals>.decorator.<locals>.wrapperW   se    CD242622 CQTF!,d1gcl11!//BBCs    	A;AA)	functoolswrapsr   r>   )rB   rQ   r?   s   ` rP   	decoratorz%validation_context.<locals>.decoratorV   sG    			C# 	Cc 	CS 	CQ 	C 
	C     )r   r>   )r?   rT   s   ` rP   validation_contextrV   P   s0    	a( 	Xc1f-= 	 rU   c                      e Zd ZU eed<   eed<   eee      ed<   ee	e
      ed<    ej                         dfdededeee      d	ee	e
      fd
Zedefd       Zedefd       Zedeeef   fd       Zedefd       Zedefd       Zedefd       ZdedefdZdefdZdefdZ e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e)fd       Z* ed      dee e!      dee+   d ee,   d!e-de.f
d"       Z/ ed#      d$ee"e e#   e e$   f      d%ee e&      d&ee e'      d'ee e(      d(e0dee+   d ee,   d!e-de1fd)       Z2 e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e3fd+       Z4 ed,      	 	 	 	 	 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e5fd-       Z6 ed.      dee7   dee+   d ee,   de8fd/       Z9d0e:de:fd1Z;d0e:d!e-de:fd2Z<d0e=d!e-de=fd3Z>d4ee   ddfd5Z?d6ee   d4ee   ddfd7Z@	 d>d8eAd9eeBe      deCfd:ZDd;edeCfd<ZEy)?CollectionCommon_model_client_embedding_function_data_loaderNclientmodelembedding_functiondata_loaderc                 V    || _         || _        |t        |       || _        || _        y)z3Initializes a new instance of the Collection class.N)rZ   rY   r3   r[   r\   )rD   r]   r^   r_   r`   s        rP   __init__zCollectionCommon.__init__j   s3      )'(:;#5 'rU   r@   c                 .    | j                   j                  S N)rY   idrD   s    rP   re   zCollectionCommon.id   s    {{~~rU   c                 .    | j                   j                  S rd   )rY   r?   rf   s    rP   r?   zCollectionCommon.name   s    {{rU   c                 .    | j                   j                  S rd   )rY   configuration_jsonrf   s    rP   ri   z#CollectionCommon.configuration_json   s    {{---rU   c                 J    t        t        | j                  j                        S rd   )r   r   rY   metadatarf   s    rP   rk   zCollectionCommon.metadata   s    &(<(<==rU   c                 .    | j                   j                  S rd   )rY   tenantrf   s    rP   rm   zCollectionCommon.tenant   s    {{!!!rU   c                 .    | j                   j                  S rd   )rY   databaserf   s    rP   ro   zCollectionCommon.database   s    {{###rU   otherc                    t        |t              sy| j                  |j                  k(  }| j                  |j                  k(  }| j                  |j                  k(  }| j
                  |j
                  k(  }| j                  |j                  k(  }| j                  |j                  k(  }| j                  |j                  k(  }| j                  |j                  k(  }	|xr |xr |xr |xr |xr
 |xr |xr |	S )NF)

isinstancerX   re   r?   ri   rk   rm   ro   r[   r\   )
rD   rp   id_match
name_matchconfiguration_matchmetadata_matchtenant_matchdatabase_matchembedding_function_matchdata_loader_matchs
             rP   __eq__zCollectionCommon.__eq__   s    %!1277ehh&YY%**,
"559Q9QQ%..8{{ell2%..8#'#;#;u?X?X#X  --1C1CC ""#" " 	"
 " )" "		
rU   c                 "    d| j                    dS )NzCollection(name=))r?   rf   s    rP   __repr__zCollectionCommon.__repr__   s    !$))A..rU   c                     | j                   S rd   rY   rf   s    rP   	get_modelzCollectionCommon.get_model   s    {{rU   addids
embeddings	metadatas	documentsimagesurisc                     t        ||||||      }t        |       t        |dh       |d   t        |       | j	                  |      }n|d   }t        |d   ||d   |d   |d   	      S )
Nr   r   r   r   r   r   
record_setr   )r   contains_anyr   r   r   r   r   r   r   r   r   )r-   r1   r5   r6   _embed_record_setr   )	rD   r   r   r   r   r   r   add_recordsadd_embeddingss	            rP   !_validate_and_prepare_add_requestz2CollectionCommon._validate_and_prepare_add_request   s      2!
 	#k:(KugV |$,-E!33{3KN(6NE"%!+.!+.V$
 	
rU   getwherewhere_documentincludec                    t        |      }t        ||      }|t        |       t        |       t	        |t
        j                  g       t
        j                  |v r| j                  t        d      |}t
        j                  |v r1t
        j                  |vr|j                  t
        j                         t        ||d   |d   |	      S )
N)targetr   r   r   
filter_set)r   
dissalowedBYou must set a data loader on the collection if loading from URIs.r   r   )r   r   r   r   )r+   r   r/   r7   r0   r   	distancesdatar\   
ValueErrorr   appendr   )rD   r   r   r   r   unpacked_idsfiltersrequest_includes           rP   !_validate_and_prepare_get_requestz2CollectionCommon._validate_and_prepare_get_request   s     '=C&H%G #\*w/k6K6K5LMw&4+<+<+DT 
 "w&;+;+;7+J"";#3#34'""#34#	
 	
rU   queryquery_embeddingsquery_textsquery_images
query_uris	n_resultsc	                 r   t        ||||      }	t        ||      }
t        |	       t        |
       t	        |       t        |       |	d   t        |	       | j                  |	      }n|	d   }|
d   }|
d	   }|}d
|v r#d|vr|j                  t        j                         t        |||||      S )N)r   r   r   r   r   r   r   )r   )r   r   r   r   r   r   )r   r   r   r   r   )r,   r   r.   r7   r0   r4   r6   r   r   r   r   r"   )rD   r   r   r   r   r   r   r   r   query_recordsr   request_embeddingsrequest_whererequest_where_documentr   s                  rP   #_validate_and_prepare_query_requestz4CollectionCommon._validate_and_prepare_query_request  s    $ 2'!	
 )
 	!M:w/)Y/ &.-G!%!7!7=!7!Q!.|!<(!()9!: "_$)F"";#3#34)1#
 	
rU   updatec                     t        ||||||      }t        |       |d   /|d   |d   "t        |ddh       | j                  |      }nd }n|d   }t	        |d   ||d   |d   |d	   
      S )Nr   r   r   r   r   )embeddable_fieldsr   r   r   r   )r-   r1   r6   r   r(   )	rD   r   r   r   r   r   r   update_recordsupdate_embeddingss	            rP   $_validate_and_prepare_update_requestz5CollectionCommon._validate_and_prepare_update_requestA  s      5!
 	#n= ,'/ {+7!(+71"{H6M %)$:$:n$:$U!$(! .| <u%($[1$[1'
 	
rU   upsertc                     t        ||||||      }t        |       |d   "t        |ddh       | j                  |      }n|d   }t	        |d   |d   ||d   |d	   
      S )Nr   r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   )r-   r1   r6   r   r)   )	rD   r   r   r   r   r   r   upsert_recordsupsert_embeddingss	            rP   $_validate_and_prepare_upsert_requestz5CollectionCommon._validate_and_prepare_upsert_requestu  s      5!
 	#n= ,'/-)k8=T !% 6 6. 6 Q .| <u%$[1($[1'
 	
rU   deletec                     |||t        d      |t        t        t        |            }nd }t	        ||      }|t        |       t        |       t        |||      S )Nz>At least one of ids, where, or where_document must be providedr   r   r   )r   r   r   )r   r   r$   r+   r   r/   r7   r   )rD   r   r   r   request_idsr   s         rP   $_validate_and_prepare_delete_requestz5CollectionCommon._validate_and_prepare_delete_request  sx     ;5=^-CP 
 ?s$:3$?@KK%G "[)w/5
 	
rU   responsec                 F    |d   t        j                  |d         |d<   |S )Nr   )nparray)rD   r   s     rP   _transform_peek_responsez)CollectionCommon._transform_peek_response  s*    L!-%'XXh|.D%EH\"rU   c                     d|v r(| j                   |d   | j                  |d         |d<   d|v rt        j                  |d         |d<   d|vrd |d<   |S Nr   r   r   r\   r   r   )rD   r   r   s      rP   _transform_get_responsez(CollectionCommon._transform_get_response  sv     g!!- ,#00&1ABHV7"%'XXh|.D%EH\"  #HVrU   c                    d|v r6| j                   *|d   %|d   D cg c]  }| j                  |       c}|d<   d|v r.|d   )|d   D cg c]  }t        j                  |       c}|d<   d|vrd |d<   |S c c}w c c}w r   r   )rD   r   r   r   	embeddings        rP   _transform_query_responsez*CollectionCommon._transform_query_response  s     g!!- ,DLVDTUDTD 1 1$ 7DTUHV7"x'='I5=l5K&5K	#5K&H\"
  #HV  V&s   A8A=rk   c                 >    |t        |       d|v rt        d      y y )Nz
hnsw:spacez]Changing the distance function of a collection once it is created is not supported currently.)r2   r   )rD   rk   s     rP   _validate_modify_requestz)CollectionCommon._validate_modify_request  s2    h'x' s  (  rU   r?   c                 J    |r|| j                   d<   |r|| j                   d<   y y )Nr?   rk   r   )rD   r?   rk   s      rP   "_update_model_after_modify_successz3CollectionCommon._update_model_after_modify_success  s+     "&DKK&.DKK
# rU   r   r   c           
      $   |
t               }|D ]o  }||   	|dk(  rL| j                  t        d      | j                  | j                  t	        t
        ||                     c S | j                  ||         c S  t        d| d|       )Nr   r   )r   inputzTRecord does not contain any non-None fields that can be embedded.Embeddable Fields: zRecord Fields: )r*   r\   r   _embedr   r   )rD   r   r   fields       rP   r   z"CollectionCommon._embed_record_set  s     $ H J&E% ,F?((0(`   ;;"//T$
5@Q5R/S '    ;;Z->;?? ' ""3!4j\+
 	
rU   r   c                 T    | j                   t        d      | j                  |      S )NziYou must provide an embedding function to compute embeddings.https://docs.trychroma.com/guides/embeddingsr   )r[   r   )rD   r   s     rP   r   zCollectionCommon._embed  s6    ##+?  ''e'44rU   )NNNNNrd   )F__name__
__module____qualname__CollectionModel__annotations__r=   r   r%   r   r   r   efDefaultEmbeddingFunctionrb   propertyr   re   rJ   r?   r   r   ri   r   rk   rm   ro   objectboolr{   r~   r   rV   r'   r&   r   r   r   r   r    r!   r   r   r   r9   r:   r   r   r   intr"   r   r(   r   r)   r   r$   r   r   r   r   r   r#   r   r   r   r   r	   r   r   r    rU   rP   rX   rX   d   s   !"3J"?@@:h/00 (B'')6:(( ( %j)
	( j23(. D    c     .DcN . . >, > > " " " $# $ $
F 
t 
,/# /?  )
r])
 )$+&(
)
 Ih/0)
 Ih/0)
 5)*)
 y~&)
 
)
 )
V "
im$"
 "
 !/	"

 "
 
"
 "
H  8
")$+&(
8
 i128
 y/08
 Ys^,8
 8
 8
 !/8
 8
 
8
 !8
t !1
r]1
 )$+&(
1
 Ih/01
 Ih/01
 5)*1
 y~&1
 
1
 "1
f ! 3737-1)-*
r]*
 )$+&(
*
 Ih/0*
 Ih/0*
 5)**
 y~&*
 
*
 "*
X !
c]
 
 !/	

 

 "
6 y !,3	&#.5	*:L1M RV /SM/-56H-I/	/ RV
'
<DSX<N
	
25C 5J 5rU   rX   )PrR   typingr   r   r   r   r   r   r	   r
   r   r   chromadb.typesr   numpyr   uuidr   "chromadb.utils.embedding_functionsutilsembedding_functionsr   chromadb.api.typesr   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r   r9   r:   logging	getLoggerr   loggerchromadb.apir;   r<   r=   r>   rJ   rV   rX   r   rU   rP   <module>r      s       $   / /* * * * * * * * * * *` O N 			8	$6
)[*:
;CLS XxQ/?.@(3PQ6BR.R%S (w5ww' w5rU   