o
    ɭRhh                     @   s  d dl mZmZmZmZmZmZ d dlZd dlm	Z	m
Z
mZmZ d dlmZmZ d dlmZ d dlZG dd dedd	ZG d
d dedd	ZG dd dedd	ZdedefddZdeeef defddZdedefddZdedeeef fddZG dd dedd	Zdeeef defddZG d d! d!edd	Zdeeef defd"d#ZG d$d% d%edd	Zdedefd&d'Z d(e
defd)d*Z!d(eeef defd+d,Z"d-edefd.d/Z#deeef defd0d1Z$dedefd2d3Z%dedeeef fd4d5Z&	dfded6ee defd7d8Z'G d9d: d:edd	Z(deeef de(fd;d<Z)G d=d> d>edd	Z*deeef de*fd?d@Z+G dAdB dBedd	Z,d(e
de,fdCdDZ-d(ede,fdEdFZ.de,defdGdHZ/de,deeef fdIdJZ0d-ede,fdKdLZ1deeef de,fdMdNZ2dOedPe(defdQdRZ3dSedTe*defdUdVZ4dWedXedefdYdZZ5d[ed\e,defd]d^Z6d_ee d`ee ddfdadbZ7d_ee dceeeef  ddfdddeZ8dS )g    )	TypedDictDictAnyOptionalcastget_argsN)SpaceCollectionMetadataUpdateMetadataEmbeddingFunction)known_embedding_functionsregister_embedding_function)	cpu_countc                   @   N   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< d	S )
HNSWConfigurationspaceef_constructionmax_neighbors	ef_searchnum_threads
batch_sizesync_thresholdresize_factorN__name__
__module____qualname__r   __annotations__intfloat r    r    g/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/chromadb/api/collection_configuration.pyr         
 r   F)totalc                   @   V   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< eed	< d
S )SpannConfigurationsearch_nprobewrite_nprober   r   r   r   reassign_neighbor_countsplit_thresholdmerge_thresholdNr   r   r   r   r   r   r    r    r    r!   r%         
 r%   c                   @   2   e Zd ZU ee ed< ee ed< ee ed< dS )CollectionConfigurationhnswspannembedding_functionN)r   r   r   r   r   r   r%   r   r    r    r    r!   r.   (      
 r.   Tconfig_json_strreturnc                 C      t | }t|S N)jsonloads'load_collection_configuration_from_json)r3   config_json_mapr    r    r!   +load_collection_configuration_from_json_str.      
r;   r:   c              
   C   sV  |  dd ur|  dd urtdd }d }d }|  dd ur&tt| d }|  dd ur4tt| d }|  dd ur| d }|d dkrPtjdtdd	 d }nTz|d
 }W n tyd   td| w zt	| }W n tyz   td| dw z	|
|d }W n ty } ztd|d
  d|d  d| d }~ww d }t|||dS )Nr0   r/   &hnsw and spann cannot both be providedr1   typelegacy legacy embedding function config   
stacklevelnamez-Embedding function name not found in config: zEmbedding function zO not found. Add @register_embedding_function decorator to the class definition.configz#Could not build embedding function z from config z: r/   r0   r1   )get
ValueErrorr   r   r%   warningswarnDeprecationWarningKeyErrorr   build_from_config	Exceptionr.   )r:   hnsw_configspann_config	ef_configefef_nameer    r    r!   r9   6   s^   
r9   rE   c                 C      t t| S r6   )r7   dumps collection_configuration_to_jsonrE   r    r    r!   $collection_configuration_to_json_strq   s   rY   c              
   C   s  t | tr| d}| d}| d}n<z| dj}W n ty(   d }Y nw z| dj}W n ty<   d }Y nw z| dj}W n tyP   d }Y nw d }|d urrztt|}W n tyq } ztd| d }~ww |d urztt	|}W n ty } ztd| d }~ww |d u rd }ddi}|||dS |d urz|
 rddi}n| d	| d
}tt| W n# ty } ztjd| tdd d }ddi}W Y d }~nd }~ww |||dS )Nr/   r0   r1   not a valid hnsw config: not a valid spann config: r>   r?   rF   knownrD   r>   rE   "legacy embedding function config: rA   rB   )
isinstancedictrG   get_parametervaluerH   r   r   rN   r%   	is_legacyrD   
get_configr   r>   rI   rJ   rK   )rE   rO   rP   rR   rQ   rT   r    r    r!   rW   u   s   




rW   c                   @   r   )
CreateHNSWConfigurationr   r   r   r   r   r   r   r   Nr   r    r    r    r!   re      r"   re   json_mapc                 C   s   i }d| v r| d }|t tv r||d< ntd| d| v r&| d |d< d| v r0| d |d< d| v r:| d |d< d| v rD| d |d< d| v rN| d |d< d| v rX| d |d< d	| v rb| d	 |d	< |S )
Nr   not a valid space: r   r   r   r   r   r   r   r   r   rH   rf   rE   space_valuer    r    r!   !json_to_create_hnsw_configuration   s*   
rk   c                   @   r$   )CreateSpannConfigurationr&   r'   r   r   r   r   r(   r)   r*   Nr+   r    r    r    r!   rl      r,   rl   c                 C   s   i }d| v r| d |d< d| v r| d |d< d| v r0| d }|t tv r)||d< ntd| d| v r:| d |d< d| v rD| d |d< d| v rN| d |d< |S )Nr&   r'   r   rg   r   r   r   rh   ri   r    r    r!   "json_to_create_spann_configuration   s"   
rm   c                   @   r-   )CreateCollectionConfigurationr/   r0   r1   N)r   r   r   r   re   r   rl   r   r    r    r    r!   rn     r2   rn   c                 C   s    t | d| d| ddS )Nr/   r0   r1   rF   )r.   rG   rX   r    r    r!   Bload_collection_configuration_from_create_collection_configuration  s
   ro   metadatac                 C   s   t | S )FCreate a CreateCollectionConfiguration from legacy collection metadata)9create_collection_configuration_from_legacy_metadata_dict)rp   r    r    r!   ?create_collection_configuration_from_legacy_collection_metadata  s   rs   c              	   C   sZ   ddddddddd	}i }|   D ]\}}||v r|||| < qt|}t|}t|d
S )rq   r   r   r   r   r   r   r   r   )z
hnsw:spacezhnsw:construction_efzhnsw:Mhnsw:search_efhnsw:num_threadshnsw:batch_sizehnsw:sync_thresholdhnsw:resize_factorr/   )itemsrk   populate_create_hnsw_defaultsrn   rp   
old_to_newrf   rD   rb   rO   r    r    r!   rr     s"   

rr   json_strc                 C   r5   r6   )r7   r8   .load_create_collection_configuration_from_jsonr~   rf   r    r    r!   2load_create_collection_configuration_from_json_str0  r<   r   c                 C   s   |  dd ur|  dd urtdt }|  dd ur$t| d |d< |  dd ur3t| d |d< |  dd ur]| d }|d dkrNtjdtdd	 |S t|d
  }|	|d |d< |S )Nr/   r0   r=   r1   r>   r?   r@   rA   rB   rD   rE   )
rG   rH   rn   rk   rm   rI   rJ   rK   r   rM   )rf   resultrQ   rR   r    r    r!   r   8  s&   	r   c                 C   rU   )zFConvert a CreateCollection configuration to a JSON-serializable string)r7   rV   'create_collection_configuration_to_jsonrX   r    r    r!   +create_collection_configuration_to_json_strW  s   r   c              
   C   sl  d}|  d}|  d}|dur+ztt|}W n ty* } ztd| d}~ww |durJztt|}W n tyI } ztd| d}~ww |durV|durVtd|  ddu rid}dd	i}|||d
S z#tt|  d}| r{dd	i}n| d|	 d}t
t| W n# ty } ztjd| tdd d}dd	i}W Y d}~nd}~ww |||d
S )zDConvert a CreateCollection configuration to a JSON-serializable dictNr/   r0   rZ   r[   r=   r1   r>   r?   rF   r\   r]   r^   rA   rB   )rG   r   re   rN   rH   rl   r   rc   rD   rd   r   r>   rI   rJ   rK   )rE   rQ   rO   rP   rT   rR   r    r    r!   r   _  sd   



r   rR   c                 C   s   |  ddu r|r| nd| d< |  ddu rd| d< |  ddu r'd| d< |  ddu r2d| d< |  d	du r>t | d	< |  d
du rId| d
< |  ddu rTd| d< |  ddu r_d| d< | S )z7Populate a CreateHNSW configuration with default valuesr   Nl2r   d   r      r   r   r   r   i  r   g333333?)rG   default_spacer   )rE   rR   r    r    r!   r{     s"   
r{   c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )UpdateHNSWConfigurationr   r   r   r   r   N)r   r   r   r   r   r   r    r    r    r!   r     s   
 r   c                 C   sl   i }d| v r| d |d< d| v r| d |d< d| v r | d |d< d| v r*| d |d< d| v r4| d |d< |S )Nr   r   r   r   r   r    rf   rE   r    r    r!   !json_to_update_hnsw_configuration  s   r   c                   @   s   e Zd ZU eed< eed< dS )UpdateSpannConfigurationr&   r   N)r   r   r   r   r   r    r    r    r!   r     s   
 r   c                 C   s0   i }d| v r| d |d< d| v r| d |d< |S )Nr&   r   r    r   r    r    r!   "json_to_update_spann_configuration  s   r   c                   @   r-   )UpdateCollectionConfigurationr/   r0   r1   N)r   r   r   r   r   r   r   r   r    r    r    r!   r     r2   r   c                 C   L   dddddd}i }|   D ]\}}||v r|||| < qt|}t|dS )zGCreate an UpdateCollectionConfiguration from legacy collection metadatar   r   r   r   r   rt   ru   rv   rw   rx   ry   rz   r   r   r|   r    r    r!   ?update_collection_configuration_from_legacy_collection_metadata     
r   c                 C   r   )zCCreate an UpdateCollectionConfiguration from legacy update metadatar   r   r   r   r   r   ry   r   r|   r    r    r!   ;update_collection_configuration_from_legacy_update_metadata  r   r   c                 C   s   t | }t|S )zFConvert an UpdateCollectionConfiguration to a JSON-serializable string)'update_collection_configuration_to_jsonr7   rV   )rE   	json_dictr    r    r!   +update_collection_configuration_to_json_str  s   
r   c              
   C   s  |  d}|  d}|  d}|du r|du r|du ri S |dur<ztt|}W n ty; } ztd| d}~ww |dur[ztt|}W n tyZ } ztd| d}~ww d}|dur| rjddi}n||  |	 d	| d
}t
t| nd}|||dS )zDConvert an UpdateCollectionConfiguration to a JSON-serializable dictr/   r0   r1   NrZ   r[   r>   r?   r\   r]   rF   )rG   r   r   rN   rH   r   rc   validate_configrd   rD   r   r>   )rE   rO   rP   rR   rT   rQ   r    r    r!   r     sD   



r   c                 C   r5   r6   )r7   r8   .load_update_collection_configuration_from_jsonr   r    r    r!   2load_update_collection_configuration_from_json_str8  r<   r   c                 C   s   |  ddur|  ddurtdt }|  ddur$t| d |d< |  ddur3t| d |d< |  ddur_| d d dkrLtjdtd	d
 |S t| d d  }|	| d d |d< |S )z7Convert a JSON dict to an UpdateCollectionConfigurationr/   Nr0   r=   r1   r>   r?   r@   rA   rB   rD   rE   )
rG   rH   r   r   r   rI   rJ   rK   r   rM   )rf   r   rR   r    r    r!   r   @  s(   
r   existing_hnsw_configupdate_hnsw_configc                 C   s8   t | }g d}|D ]}||v r|| ||< q
tt|S )z6Overwrite a HNSWConfiguration with a new configuration)r   r   r   r   r   )r`   r   r   )r   r   r   update_fieldsfieldr    r    r!   overwrite_hnsw_configurationa  s   
r   existing_spann_configupdate_spann_configc                 C   s8   t | }ddg}|D ]}||v r|| ||< q
tt|S )z7Overwrite a SpannConfiguration with a new configurationr&   r   )r`   r   r%   )r   r   r   r   r   r    r    r!   overwrite_spann_configurationv  s   
r   existing_embedding_functionupdate_embedding_functionc                 C   sj   |   s|  rtjdtdd | S |  | kr)td|   d|  d||  |  |S )z7Overwrite an EmbeddingFunction with a new configurationz.cannot update legacy embedding function configrA   rB   z6Cannot update embedding function: incompatible types ( vs ))rc   rI   rJ   rK   rD   rH   validate_config_updaterd   )r   r   r    r    r!   overwrite_embedding_function  s&   r   existing_configupdate_configc                 C   s   | d}| d}|dur|durtd|  d}|dur(|dur(t||}|  d}|dur:|dur:t||}|  d}| d}|durT|durRt||}n|}t|||dS )z<Overwrite a CollectionConfiguration with a new configurationr0   r/   Nr=   r1   rF   )rG   rH   r   r   r   r.   )r   r   update_spannupdate_hnswupdated_hnsw_configupdated_spann_configupdated_embedding_function	update_efr    r    r!   "overwrite_collection_configuration  s6   





r   r1   configuration_efc                 C   sL   | dur$|dur$|   dkr$|   |  kr$td|    d|   dS )ap  
    Validates that there are no conflicting embedding functions between function parameter
    and collection configuration.

    Args:
        embedding_function: The embedding function provided as a parameter
        configuration_ef: The embedding function from collection configuration

    Returns:
        bool: True if there is a conflict, False otherwise
    Ndefaultz]Multiple embedding functions provided. Please provide only one. Embedding function conflict: r   )rD   rH   )r1   r   r    r    r!   .validate_embedding_function_conflict_on_create  s   r   persisted_ef_configc                 C   s^   |dur-| dur-|   dkr-|ddur-|d|   kr-td|    d|d dS )z
    Validates that there are no conflicting embedding functions between function parameter
    and collection configuration.
    Nr   rD   zAn embedding function already exists in the collection configuration, and a new one is provided. If this is intentional, please embed documents separately. Embedding function conflict: new: z vs persisted: )rD   rG   rH   )r1   r   r    r    r!   +validate_embedding_function_conflict_on_get  s   r   r6   )9typingr   r   r   r   r   r   r7   chromadb.api.typesr   r	   r
   r   "chromadb.utils.embedding_functionsr   r   multiprocessingr   rI   r   r%   r.   strr;   r9   rY   rW   re   rk   rl   rm   rn   ro   rs   rr   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!   <module>   sD    


;C














:









,


!



+
 