o
    \hE;                     @   sJ  d dl mZ d dl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 d dlmZ d dlmZ d dlmZ G dd	 d	eZG d
d deZeeeeedf ZG dd deZG dd dZG dd dZ edddZ!G dd ded Z"G dd de"Z#G dd de#Z$e$Z%G dd de"Z&G dd de&Z'e'Z(G dd de"Z)dS )     )abstractmethodN)override)	AnyClassVarDictListOptionalProtocolUnionTypeVarcast)Self)	cpu_count)JSONSerializablec                   @      e Zd ZdZdS )StaticParameterErrorz?Represents an error that occurs when a static parameter is set.N__name__
__module____qualname____doc__ r   r   ]/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/chromadb/api/configuration.pyr          r   c                   @   r   )InvalidConfigurationErrorz@Represents an error that occurs when a configuration is invalid.Nr   r   r   r   r   r      r   r   ConfigurationInternalc                   @   s&   e Zd ZdZededefddZdS )ParameterValidatorz+Represents an abstract parameter validator.valuereturnc                 C   s   t  )z)Returns whether the given value is valid.)NotImplementedError)selfr   r   r   r   __call__)   s   zParameterValidator.__call__N)r   r   r   r   r   ParameterValueboolr!   r   r   r   r   r   &   s    r   c                   @   sL   e Zd ZU dZeed< eed< eed< eed< dedededefddZ	dS )	ConfigurationDefinitionz-Represents the definition of a configuration.name	validator	is_staticdefault_valuec                 C   s   || _ || _|| _|| _d S Nr%   r&   r'   r(   )r    r%   r&   r'   r(   r   r   r   __init__7   s   
z ConfigurationDefinition.__init__N)
r   r   r   r   str__annotations__r   r#   r"   r+   r   r   r   r   r$   /   s   
 r$   c                   @   sT   e Zd ZU dZeed< eed< dedefddZdefddZd	e	de
fd
dZdS )ConfigurationParameterz*Represents a parameter of a configuration.r%   r   c                 C   s   || _ || _d S r)   r%   r   )r    r%   r   r   r   r   r+   J   s   
zConfigurationParameter.__init__r   c                 C   s   d| j  d| j dS )NzConfigurationParameter(z, )r/   r    r   r   r   __repr__N   s   zConfigurationParameter.__repr___ConfigurationParameter__valuec                 C   s&   t |tstS | j|jko| j|jkS r)   )
isinstancer.   NotImplementedr%   r   )r    r3   r   r   r   __eq__Q   s   
zConfigurationParameter.__eq__N)r   r   r   r   r,   r-   r"   r+   r2   objectr#   r6   r   r   r   r   r.   D   s   
 r.   T)boundc                   @   s*  e Zd ZU dZeeef ed< eeee	f  ed< d"de
ee  fddZdefd	d
ZdedefddZed#ddZdee fddZdedefddZdedeeeeef ddfddZedefddZeededefddZedeeef fddZeedeeef defd d!Z dS )$r   z@Represents an abstract configuration, used internally by Chroma.parameter_mapdefinitionsN
parametersc                 C   s  i | _ |durk|D ]a}|j| jvrtd|j | j|j }t|jtrCt |jdd}|du r<td|j |	|j|_t|jt
|jsTtd|j |j}||jsdtd|j || j |j< q	| j D ]\}}|| j vrt||jd| j |< qp|   dS )zbInitializes a new instance of the Configuration class. Respecting defaults and
        validators.NInvalid parameter name: _typezInvalid configuration type: zInvalid parameter value: r/   )r:   r%   r;   
ValueErrorr4   r   dictglobalsget	from_jsontyper(   r&   itemsr.   configuration_validator)r    r<   	parameter
definition
child_typeparameter_validatorr%   r   r   r   r+   b   s4   


zConfigurationInternal.__init__r   c                 C   s   d| j   dS )NzConfiguration(r0   )r:   valuesr1   r   r   r   r2      s   zConfigurationInternal.__repr___ConfigurationInternal__valuec                 C   s   t |tstS | j|jkS r)   )r4   r   r5   r:   )r    rL   r   r   r   r6      s   
zConfigurationInternal.__eq__c                 C   s   dS )zPerform custom validation when parameters are dependent on each other.

        Raises an InvalidConfigurationError if the configuration is invalid.
        Nr   r1   r   r   r   rF      s   z-ConfigurationInternal.configuration_validatorc                 C   s   t | j S )z,Returns the parameters of the configuration.)listr:   rK   r1   r   r   r   get_parameters   s   z$ConfigurationInternal.get_parametersr%   c                 C   s8   || j vrtd| d| jj tt| j |}|S )zIReturns the parameter with the given name, or except if it doesn't exist.r=   z for configuration )r:   r?   	__class__r   r   r.   rB   )r    r%   param_valuer   r   r   get_parameter   s   
z#ConfigurationInternal.get_parameterr   c                 C   sh   || j vrtd| | j | }| j| }|jr td| ||s/td| d| ||_dS )z:Sets the parameter with the given name to the given value.r=   zCannot set static parameter: zInvalid value for parameter z: N)r;   r?   r:   r'   r   r&   r   )r    r%   r   rH   rG   r   r   r   set_parameter   s   




z#ConfigurationInternal.set_parameterc                 C   s   t |  S )z5Returns the JSON representation of the configuration.)jsondumpsto_jsonr1   r   r   r   to_json_str   s   z!ConfigurationInternal.to_json_strjson_strc                 C   sB   zt |}W n t jy   td| w |r| |S |  S )3Returns a configuration from the given JSON string.z1Unable to decode configuration from JSON string: )rS   loadsJSONDecodeErrorr?   rC   )clsrW   config_jsonr   r   r   from_json_str   s   z#ConfigurationInternal.from_json_strc                 C   s$   dd | j  D }| jj|d< |S )zKReturns the JSON compatible dictionary representation of the configuration.c                 S   s.   i | ]\}}|t |jtr|j n|jqS r   )r4   r   r   rU   ).0r%   rG   r   r   r   
<dictcomp>   s    
z1ConfigurationInternal.to_json.<locals>.<dictcomp>r>   )r:   rE   rO   r   )r    	json_dictr   r   r   rU      s
   zConfigurationInternal.to_jsonjson_mapc                 C   sh   | j |ddkrtd| j  d|d  g }| D ]\}}|dkr%q|t||d q| |dS )rX   r>   Nz,Trying to instantiate configuration of type z from JSON with type r/   r<   )r   rB   r?   rE   appendr.   )r[   ra   r<   r%   r   r   r   r   rC      s   
zConfigurationInternal.from_jsonr)   r   N)!r   r   r   r   r   r,   r.   r-   r   r$   r   r   r+   r2   r7   r#   r6   r   rF   rN   rQ   r
   intfloatrR   r   rV   classmethodr   r]   r   rU   rC   r   r   r   r   r   Z   s,   
 ""	
"c                   @   s   e Zd ZdZeddd dddedd	d dd
deddd dd
deddd de deddd dddeddd dddeddd dd
deddd ddddZed#ddZe	d e
eef defd!d"ZdS )$HNSWConfigurationInternalzxInternal representation of the HNSW configuration.
    Used for validation, defaults, serialization and deserialization.spacec                 C   s   t | to| dv S )N)l2ipcosine)r4   r,   r   r   r   r   <lambda>   s   
 z"HNSWConfigurationInternal.<lambda>Trj   r*   ef_constructionc                 C      t | to| dkS N   r4   re   rm   r   r   r   rn          d   	ef_searchc                 C   rp   rq   rs   rm   r   r   r   rn      rt   Fnum_threadsc                 C   rp   rq   rs   rm   r   r   r   rn      rt   Mc                 C   rp   rq   rs   rm   r   r   r   rn      rt      resize_factorc                 C   rp   rq   )r4   rf   rm   r   r   r   rn     rt   333333?
batch_sizec                 C   rp   rq   rs   rm   r   r   r   rn     rt   sync_thresholdc                 C   rp   rq   rs   rm   r   r   r   rn     rt     )ri   ro   rv   rw   rx   rz   r|   r}   r   Nc                 C   sL   | j d}| j d}|r |r"tt|jtt|jkr$tdd S d S d S )Nr|   r}   z7batch_size must be less than or equal to sync_threshold)r:   rB   r   re   r   r   )r    r|   r}   r   r   r   rF     s   z1HNSWConfigurationInternal.configuration_validatorparamsc              	   C   s`   ddddddddd	}g }|  D ]\}}||vr td
| |t|| |d q| |S )zhReturns an HNSWConfiguration from a metadata dict containing legacy HNSW parameters. Used for migration.ri   ro   rv   rx   rw   rz   r|   r}   )z
hnsw:spacezhnsw:construction_efzhnsw:search_efzhnsw:Mzhnsw:num_threadszhnsw:resize_factorzhnsw:batch_sizezhnsw:sync_thresholdz$Invalid legacy HNSW parameter name: r/   )rE   r?   rc   r.   )r[   r   
old_to_newr<   r%   r   r   r   r   from_legacy_params"  s"   z,HNSWConfigurationInternal.from_legacy_paramsrd   )r   r   r   r   r$   r   r;   r   rF   rg   r   r,   r   r   r   r   r   r   r   rh      sn    4 rh   c                       sV   e Zd ZdZddde ddddfdeded	ed
ededededef fddZ  Z	S )HNSWConfigurationInterfacezHNSW index configuration parameters.
    See https://docs.trychroma.com/guides#changing-the-distance-function for more information.
    rj   ru   ry   r{   r~   ri   ro   rv   rw   rx   rz   r|   r}   c	           
         sf   t d|dt d|dt d|dt d|dt d|dt d|dt d|dt d	|dg}	t j|	d
 d S )Nri   r/   ro   rv   rw   rx   rz   r|   r}   rb   )r.   superr+   )
r    ri   ro   rv   rw   rx   rz   r|   r}   r<   rO   r   r   r+   G  s   







z#HNSWConfigurationInterface.__init__)
r   r   r   r   r   r,   re   rf   r+   __classcell__r   r   r   r   r   B  s6    	r   c                   @   s8   e Zd ZdZdeddd de diZedd	d
ZdS )CollectionConfigurationInternalzInternal representation of the collection configuration.
    Used for validation, defaults, and serialization / deserialization.hnsw_configurationc                 C   
   t | tS r)   )r4   rh   rm   r   r   r   rn   k     
 z(CollectionConfigurationInternal.<lambda>Tr*   r   Nc                 C      d S r)   r   r1   r   r   r   rF   q     z7CollectionConfigurationInternal.configuration_validatorrd   )	r   r   r   r   r$   rh   r;   r   rF   r   r   r   r   r   d  s    	r   c                       s*   e Zd ZdZdee f fddZ  ZS ) CollectionConfigurationInterfacez3Configuration parameters for creating a collection.r   c                    s.   |du rt  }td|dg}t j|d dS )zInitializes a new instance of the CollectionConfiguration class.
        Args:
            hnsw_configuration: The HNSW configuration to use for the collection.
        Nr   r/   rb   )rh   r.   r   r+   )r    r   r<   r   r   r   r+   }  s
   
z)CollectionConfigurationInterface.__init__)r   r   r   r   r   rh   r+   r   r   r   r   r   r   z  s    r   c                   @   s2   e Zd Zdeddd dddiZedd	d
ZdS )$EmbeddingsQueueConfigurationInternalautomatically_purgec                 C   r   r)   )r4   r#   rm   r   r   r   rn     r   z-EmbeddingsQueueConfigurationInternal.<lambda>FTr*   r   Nc                 C   r   r)   r   r1   r   r   r   rF     r   z<EmbeddingsQueueConfigurationInternal.configuration_validatorrd   )r   r   r   r$   r;   r   rF   r   r   r   r   r     s    	r   )*abcr   rS   	overridesr   typingr   r   r   r   r   r	   r
   r   r   typing_extensionsr   multiprocessingr   chromadb.serder   	Exceptionr   r?   r   r,   re   rf   r#   r"   r   r$   r.   r8   r   rh   r   HNSWConfigurationr   r   CollectionConfigurationr   r   r   r   r   <module>   s.    ,	 f