o
    ɭRh%I                     @   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> d dl?m@Z@mAZAmBZBmCZC d dlmDZEmFZFmGZG d dlHZHeHIeJZKerd dlLmMZMmNZN e	d	d
dZOe	dZPdeQdeedePf gedePf f fddZRG dd deeO ZSd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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)UpdateCollectionConfiguration"overwrite_collection_configuration'load_collection_configuration_from_jsonCollectionConfiguration)
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.rB   c              	      s.   t  dtdtdtdtf fdd}|S )NselfargskwargsrB   c              
      sl   z | g|R i |W S  t y5 } zt| d d}|jr-|f|jdd   |_ d|_ d }~ww )Nz in .    )	ExceptionstrrE   )rD   rE   rF   emsg)rC   rA   rI   f/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/chromadb/api/models/CollectionCommon.pywrapper\   s   z6validation_context.<locals>.decorator.<locals>.wrapper)	functoolswrapsr   r@   )rC   rO   rA   )rC   rN   	decorator[   s   "
z%validation_context.<locals>.decorator)r   r@   )rA   rS   rI   rR   rN   validation_contextU   s   &rT   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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e0f
d/d0Z1e!d1d2ee$e"e% e"e& f  d3ee"e(  d4ee"e)  d5ee"e*  d#ee"e#  d6e2d,ee- d-ee. d.e/de3fd7d8Z4e!d9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:d;Z6e!d<					dWd#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e7fd=d>Z8e!d?d#ee9 d,ee- d-ee. de:fd@dAZ;dBe<de<fdCdDZ=dBe<d.e/de<fdEdFZ>dBe?d.e/de?fdGdHZ@dIee ddfdJdKZAdLee dIee dMeeB ddfdNdOZC	dXdPeDdQeeEe  deFfdRdSZGdTedeFfdUdVZHdS )Y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)rW   rV   r1   rX   rY   )rD   rZ   r[   r\   r]   rI   rI   rN   __init__r   s   
zCollectionCommon.__init__rB   c                 C      | j jS N)rV   idrD   rI   rI   rN   ra         zCollectionCommon.idc                 C   r_   r`   )rV   rA   rb   rI   rI   rN   rA      rc   zCollectionCommon.namec                 C   s   t | jjS r`   )r8   rV   configuration_jsonrb   rI   rI   rN   configuration   s   zCollectionCommon.configurationc                 C   r_   r`   )rV   rd   rb   rI   rI   rN   rd      rc   z#CollectionCommon.configuration_jsonc                 C   s   t t| jjS r`   )r   r   rV   metadatarb   rI   rI   rN   rf      s   zCollectionCommon.metadatac                 C   r_   r`   )rV   tenantrb   rI   rI   rN   rg      rc   zCollectionCommon.tenantc                 C   r_   r`   )rV   databaserb   rI   rI   rN   rh      rc   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)

isinstancerU   ra   rA   rd   rf   rg   rh   rX   rY   )
rD   ri   id_match
name_matchconfiguration_matchmetadata_matchtenant_matchdatabase_matchembedding_function_matchdata_loader_matchrI   rI   rN   __eq__   s2   
zCollectionCommon.__eq__c                 C   s   d| j  dS )NzCollection(name=)rR   rb   rI   rI   rN   __repr__   s   zCollectionCommon.__repr__c                 C   s   | j S r`   )rV   rb   rI   rI   rN   	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rx   ry   rz   r{   r|   r}   
record_setrx   )r   contains_anyry   rz   r{   r}   rx   ry   rz   r{   r}   )r+   r/   r3   r4   _embed_record_setr   )	rD   rx   ry   rz   r{   r|   r}   add_recordsadd_embeddingsrI   rI   rN   !_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|dgd d|v r-| jd u r-td|}d|v r<d	|vr<|d	 t||d
 |d |dS )N)targetr   r   rx   
filter_set	distances)r   
dissaloweddataBYou must set a data loader on the collection if loading from URIs.r}   r   r   )rx   r   r   r   )	r)   r   r-   r5   r.   rY   
ValueErrorappendr   )rD   rx   r   r   r   unpacked_idsfiltersrequest_includerI   rI   rN   !_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|}t||d}t|
d t|d t|	d t|d |
d d u r8t|
d | j|
d}n|
d }|d }|d	 }|	}d
|v rSd|vrS|	d t
||||||dS )N)ry   r{   r|   r}   r   r   r   )r   )r   ry   r   r   r   r}   )ry   rx   r   r   r   r   )r*   r)   r   r,   r5   r.   r2   r4   r   r   r    )rD   r   r   r   r   rx   r   r   r   r   query_records
filter_idsr   request_embeddingsrequest_whererequest_where_documentr   rI   rI   rN   #_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   ry   r{   r|   )embeddable_fieldsrx   rz   r}   r   )r+   r/   r4   r   r&   )	rD   rx   ry   rz   r{   r|   r}   update_recordsupdate_embeddingsrI   rI   rN   $_validate_and_prepare_update_requestQ  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   ry   r{   r|   )r   r   rx   rz   r}   )rx   rz   ry   r{   r}   )r+   r/   r4   r   r'   )	rD   rx   ry   rz   r{   r|   r}   upsert_recordsupsert_embeddingsrI   rI   rN   $_validate_and_prepare_upsert_request  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   )rx   r   r   )r   r   r"   r)   r   r-   r5   r   )rD   rx   r   r   request_idsr   rI   rI   rN   $_validate_and_prepare_delete_request  s   

z5CollectionCommon._validate_and_prepare_delete_requestresponsec                 C   s"   |d d urt |d |d< |S )Nry   nparray)rD   r   rI   rI   rN   _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}   ry   )rY   r   r   rD   r   r   rI   rI   rN   _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 rI   rY   ).0r}   rb   rI   rN   
<listcomp>  s    z>CollectionCommon._transform_query_response.<locals>.<listcomp>ry   c                 S   s   g | ]}t |qS rI   r   )r   	embeddingrI   rI   rN   r     s    
r   r   rI   rb   rN   _transform_query_response  s   

z*CollectionCommon._transform_query_responserf   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.)r0   r   )rD   rf   rI   rI   rN   _validate_modify_request  s   z)CollectionCommon._validate_modify_requestrA   re   c                 C   s@   |r|| j d< |r|| j d< |r| j t| j  | d S d S )NrA   rf   )rV   set_configurationr7   get_configuration)rD   rA   rf   re   rI   rI   rN   "_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(   rY   r   _embedr   r   )rD   r   r   fieldrI   rI   rN   r     s*   

z"CollectionCommon._embed_record_setr   c                 C   s`   | j d urt| j tjs| j |dS | jd}|d ur!||dS | j d u r*td| j |dS )Nr   r\   ziYou must provide an embedding function to compute embeddings.https://docs.trychroma.com/guides/embeddings)rX   rj   efDefaultEmbeddingFunctionre   r   r   )rD   r   	config_efrI   rI   rN   r   .  s   

zCollectionCommon._embed)NNNNNr`   )I__name__
__module____qualname__CollectionModel__annotations__r?   r   r#   r   r   r   r   r   r^   propertyr   ra   rK   rA   r9   re   r   r   rd   r   rf   rg   rh   objectboolrs   ru   rv   rT   r%   r$   r
   r   r   r   r   r   r   r   r   r;   r<   r   r   r   intr    r   r&   r   r'   r   r"   r   r   r   r   r   r!   r   r   r6   r   r   r   r   r   r   rI   rI   rI   rN   rU   l   s|  
 


	

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


>
	

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



,




rU   )TrP   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   %chromadb.api.collection_configurationr6   r7   r8   r9   r:   r   r;   r<   logging	getLoggerr   loggerchromadb.apir=   r>   r?   r@   rK   rT   rU   rI   rI   rI   rN   <module>   s"    0*
,