o
    g`D                     @   s  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  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lDZDeDEeFZGerd dlHmIZImJZJ e	dd	d
ZKe	dZLdeMdeedeLf gedeLf f fddZNG dd deeK ZOdS )    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                    s*   dt dtf dt dtf f fd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              	      s.   t  dtdtdtdtf fdd}|S )Nselfargskwargsr?   c              
      sZ   z | g|R i |W S  t y, } zt| d d}t|||jd }~ww )Nz in .)	Exceptionstrtypewith_traceback__traceback__)rA   rB   rC   emsg)r@   r>    [/var/www/visachat/venv/lib/python3.10/site-packages/chromadb/api/models/CollectionCommon.pywrapperW   s   z6validation_context.<locals>.decorator.<locals>.wrapper)	functoolswrapsr   r=   )r@   rN   r>   )r@   rM   	decoratorV   s   "z%validation_context.<locals>.decorator)r   r=   )r>   rR   rL   rQ   rM   validation_contextP   s   &rS   c                   @   s  e Zd ZU eed< eed< eee  ed< ee	e
  ed< e dfdededeee  d	ee	e
  fd
dZedefddZedefddZedeeef fddZedefddZedefddZedefddZdedefddZdefddZdefd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'd(Z*ed)d!ee e!  d*ee+ d+ee, d,e-de.f
d-d.Z/ed/d0ee"e e# e e$ f  d1ee e&  d2ee e'  d3ee e(  d4e0d*ee+ d+ee, d,e-de1fd5d6Z2ed7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d8d9Z4ed:					dTd!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;d<Z6ed=d!ee7 d*ee+ d+ee, de8fd>d?Z9d@e:de:fdAdBZ;d@e:d,e-de:fdCdDZ<d@e=d,e-de=fdEdFZ>dGee ddfdHdIZ?dJee dGee ddfdKdLZ@	dUdMeAdNeeBe  deCfdOdPZDdQedeCfdRdSZEdS )VCollectionCommon_model_client_embedding_function_data_loaderNclientmodelembedding_functiondata_loaderc                 C   s,   || _ || _|durt| || _|| _dS )z3Initializes a new instance of the Collection class.N)rV   rU   r2   rW   rX   )rA   rY   rZ   r[   r\   rL   rL   rM   __init__j   s   
zCollectionCommon.__init__r?   c                 C      | j jS N)rU   idrA   rL   rL   rM   r`         zCollectionCommon.idc                 C   r^   r_   )rU   r>   ra   rL   rL   rM   r>      rb   zCollectionCommon.namec                 C   r^   r_   )rU   configuration_jsonra   rL   rL   rM   rc      rb   z#CollectionCommon.configuration_jsonc                 C   s   t t| jjS r_   )r   r   rU   metadatara   rL   rL   rM   rd      s   zCollectionCommon.metadatac                 C   r^   r_   )rU   tenantra   rL   rL   rM   re      rb   zCollectionCommon.tenantc                 C   r^   r_   )rU   databasera   rL   rL   rM   rf      rb   zCollectionCommon.databaseotherc           
      C   s   t |tsdS | j|jk}| j|jk}| j|jk}| j|jk}| j|jk}| j|jk}| j|jk}| j	|j	k}	|oF|oF|oF|oF|oF|oF|oF|	S )NF)

isinstancerT   r`   r>   rc   rd   re   rf   rW   rX   )
rA   rg   id_match
name_matchconfiguration_matchmetadata_matchtenant_matchdatabase_matchembedding_function_matchdata_loader_matchrL   rL   rM   __eq__   s2   
zCollectionCommon.__eq__c                 C   s   d| j  dS )NzCollection(name=)rQ   ra   rL   rL   rM   __repr__   s   zCollectionCommon.__repr__c                 C   s   | j S r_   rU   ra   rL   rL   rM   	get_model   s   zCollectionCommon.get_modeladdids
embeddings	metadatas	documentsimagesurisc           	      C   sz   t ||||||d}t|d t|dhd |d d u r(t|d | j|d}n|d }t|d ||d |d |d d	S )
Nrw   rx   ry   rz   r{   r|   
record_setrw   )r   contains_anyrx   ry   rz   r|   rw   rx   ry   rz   r|   )r,   r0   r4   r5   _embed_record_setr   )	rA   rw   rx   ry   rz   r{   r|   add_recordsadd_embeddingsrL   rL   rM   !_validate_and_prepare_add_request   s*   


z2CollectionCommon._validate_and_prepare_add_requestgetwherewhere_documentincludec                 C   s   t |d}t||d}|d urt|d t|d t|tjgd tj|v r/| jd u r/t	d|}tj|v rAtj
|vrA|tj
 t||d |d |d	S )
N)targetr   r   rw   
filter_set)r   
dissalowedBYou must set a data loader on the collection if loading from URIs.r   r   )rw   r   r   r   )r*   r   r.   r6   r/   r   	distancesdatarX   
ValueErrorr|   appendr   )rA   rw   r   r   r   unpacked_idsfiltersrequest_includerL   rL   rM   !_validate_and_prepare_get_request   s&   
	

z2CollectionCommon._validate_and_prepare_get_requestqueryquery_embeddingsquery_textsquery_images
query_uris	n_resultsc	                 C   s   t ||||d}	t||d}
t|	d t|
d t|d t|d |	d d u r4t|	d | j|	d}n|	d }|
d }|
d	 }|}d
|v rPd|vrP|t	j
 t|||||dS )N)rx   rz   r{   r|   r   r~   r   )r   )r   rx   r   r   r   r|   )rx   r   r   r   r   )r+   r   r-   r6   r/   r3   r5   r   r   r   r|   r!   )rA   r   r   r   r   r   r   r   r   query_recordsr   request_embeddingsrequest_whererequest_where_documentr   rL   rL   rM   #_validate_and_prepare_query_request  s<   




z4CollectionCommon._validate_and_prepare_query_requestupdatec           	      C   s   t ||||||d}t|d |d d u r3|d d us!|d d ur0t|ddhd | j|d}nd }n|d }t|d ||d |d |d	 d
S )Nr}   r~   rx   rz   r{   )embeddable_fieldsrw   ry   r|   r   )r,   r0   r5   r   r'   )	rA   rw   rx   ry   rz   r{   r|   update_recordsupdate_embeddingsrL   rL   rM   $_validate_and_prepare_update_requestA  s2   

z5CollectionCommon._validate_and_prepare_update_requestupsertc           	      C   sr   t ||||||d}t|d |d d u r$t|ddhd | j|d}n|d }t|d |d ||d |d	 d
S )Nr}   r~   rx   rz   r{   )r   r   rw   ry   r|   )rw   ry   rx   rz   r|   )r,   r0   r5   r   r(   )	rA   rw   rx   ry   rz   r{   r|   upsert_recordsupsert_embeddingsrL   rL   rM   $_validate_and_prepare_upsert_requestu  s,   

z5CollectionCommon._validate_and_prepare_upsert_requestdeletec                 C   sr   |d u r|d u r|d u rt d|d urttt|}nd }t||d}|d ur-t|d t|d t|||dS )Nz>At least one of ids, where, or where_document must be providedr   r   r   )rw   r   r   )r   r   r#   r*   r   r.   r6   r   )rA   rw   r   r   request_idsr   rL   rL   rM   $_validate_and_prepare_delete_request  s   

z5CollectionCommon._validate_and_prepare_delete_requestresponsec                 C   s"   |d d urt |d |d< |S )Nrx   nparray)rA   r   rL   rL   rM   _transform_peek_response  s   z)CollectionCommon._transform_peek_responsec                 C   s^   d|v r| j d ur|d d ur|  |d |d< d|v r%t|d |d< d|vr-d |d< |S )Nr   r|   rx   )rX   r   r   rA   r   r   rL   rL   rM   _transform_get_response  s   
z(CollectionCommon._transform_get_responsec                    sv   d|v r j d ur|d d ur fdd|d D |d< d|v r1|d d ur1dd |d D |d< d|vr9d |d< |S )Nr   r|   c                    s   g | ]}  |qS rL   rX   ).0r|   ra   rL   rM   
<listcomp>  s    z>CollectionCommon._transform_query_response.<locals>.<listcomp>rx   c                 S   s   g | ]}t |qS rL   r   )r   	embeddingrL   rL   rM   r     s    
r   r   rL   ra   rM   _transform_query_response  s   

z*CollectionCommon._transform_query_responserd   c                 C   s(   |d urt | d|v rtdd S d S )Nz
hnsw:spacez]Changing the distance function of a collection once it is created is not supported currently.)r1   r   )rA   rd   rL   rL   rM   _validate_modify_request  s   z)CollectionCommon._validate_modify_requestr>   c                 C   s$   |r|| j d< |r|| j d< d S d S )Nr>   rd   rt   )rA   r>   rd   rL   rL   rM   "_update_model_after_modify_success  s
   
z3CollectionCommon._update_model_after_modify_successr   r   c                 C   s   |d u rt  }|D ]0}|| d ur9|dkr/| jd u rtd| j| jtt|| dd  S | j|| d  S q	td| d| )Nr|   r   )r|   inputzTRecord does not contain any non-None fields that can be embedded.Embeddable Fields: zRecord Fields: )r)   rX   r   _embedr   r   )rA   r   r   fieldrL   rL   rM   r     s*   

z"CollectionCommon._embed_record_setr   c                 C   s   | j d u r	td| j |dS )NziYou must provide an embedding function to compute embeddings.https://docs.trychroma.com/guides/embeddingsr   )rW   r   )rA   r   rL   rL   rM   r     s
   
zCollectionCommon._embed)NNNNNr_   )F__name__
__module____qualname__CollectionModel__annotations__r<   r   r$   r   r   r   efDefaultEmbeddingFunctionr]   propertyr   r`   rF   r>   r   r   rc   r   rd   re   rf   objectboolrq   rs   ru   rS   r&   r%   r
   r   r   r   r   r    r   r   r   r8   r9   r   r   r   intr!   r   r'   r   r(   r   r#   r   r   r   r   r   r"   r   r   r   r   r   r   r   r   rL   rL   rL   rM   rT   d   sp  
 


	



+
$

	

:
	



3	
	



,


	

rT   )PrO   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.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r   r8   r9   logging	getLoggerr   loggerchromadb.apir:   r;   r<   r=   rF   rS   rT   rL   rL   rL   rM   <module>   s     00
,