
    ug+;                        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	e      Z G d
 de      Zeeeeedf   Z G d de      Z G d d      Z G d d      Z  edd      Z! G d ded         Z" G d de"      Z# G d de#      Z$e$Z% G d de"      Z& G d de&      Z'e'Z( G d de"      Z)y)     )abstractmethodN)override)	AnyClassVarDictListOptionalProtocolUnionTypeVarcast)Self)	cpu_count)JSONSerializablec                       e Zd ZdZy)StaticParameterErrorz?Represents an error that occurs when a static parameter is set.N__name__
__module____qualname____doc__     O/var/www/openai/venv/lib/python3.12/site-packages/chromadb/api/configuration.pyr   r      s    Ir   r   c                       e Zd ZdZy)InvalidConfigurationErrorz@Represents an error that occurs when a configuration is invalid.Nr   r   r   r   r   r      s    Jr   r   ConfigurationInternalc                   *    e Zd ZdZededefd       Zy)ParameterValidatorz+Represents an abstract parameter validator.valuereturnc                     t               )z)Returns whether the given value is valid.)NotImplementedError)selfr    s     r   __call__zParameterValidator.__call__)   s     "##r   N)r   r   r   r   r   ParameterValueboolr%   r   r   r   r   r   &   s%    5$n $ $ $r   r   c                   R    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Z	y)ConfigurationDefinitionz-Represents the definition of a configuration.name	validator	is_staticdefault_valuec                 <    || _         || _        || _        || _        y Nr*   r+   r,   r-   )r$   r*   r+   r,   r-   s        r   __init__z ConfigurationDefinition.__init__7   s!     	""*r   N)
r   r   r   r   str__annotations__r   r'   r&   r1   r   r   r   r)   r)   /   sF    7
I!!O!!
+
+ &
+ 	
+
 &
+r   r)   c                   R    e Zd ZU dZeed<   eed<   dedefdZdefdZde	de
fdZy	)
ConfigurationParameterz*Represents a parameter of a configuration.r*   r    c                      || _         || _        y r/   r*   r    )r$   r*   r    s      r   r1   zConfigurationParameter.__init__J   s    	
r   r!   c                 <    d| j                    d| j                   dS )NzConfigurationParameter(z, )r7   r$   s    r   __repr__zConfigurationParameter.__repr__N   s    (2djj\CCr   _ConfigurationParameter__valuec                     t        |t              st        S | j                  |j                  k(  xr | j                  |j                  k(  S r/   )
isinstancer5   NotImplementedr*   r    )r$   r<   s     r   __eq__zConfigurationParameter.__eq__Q   s8    '#9:!!yyGLL(HTZZ7==-HHr   N)r   r   r   r   r2   r3   r&   r1   r;   objectr'   r@   r   r   r   r5   r5   D   sG    4
IS  D# DIf I Ir   r5   T)boundc                   T   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ZdefdZd	edefd
Zedd       Zdee   fdZdedefdZdedeeeeef   ddfdZedefd       Zeededefd              Zedeeef   fd       Zeedeeef   defd              Z y)r   z@Represents an abstract configuration, used internally by Chroma.parameter_mapdefinitionsN
parametersc                    i | _         |i|D ]c  }|j                  | j                  vrt        d|j                         | j                  |j                     }t	        |j
                  t              rmt               j                  |j
                  j                  dd            }|t        d|j
                         |j                  |j
                        |_        t	        |j
                  t        |j                              st        d|j
                         |j                  } ||j
                        st        d|j
                         || j                   |j                  <   f | j                  j                         D ]8  \  }}|| j                   vst        ||j                        | j                   |<   : | j                          y)zbInitializes a new instance of the Configuration class. Respecting defaults and
        validators.NInvalid parameter name: _typezInvalid configuration type: zInvalid parameter value: r7   )rE   r*   rF   
ValueErrorr>   r    dictglobalsget	from_jsontyper-   r+   itemsr5   configuration_validator)r$   rG   	parameter
definition
child_typeparameter_validatorr*   s          r   r1   zConfigurationInternal.__init__b   s     !'	>>)9)99$'?	?O%PQQ!--inn=
ioot4!(y/B/B7D/Q!RJ!)(:9??:KL  '1&:&:9??&KIO!)//4
8P8P3QR$'@@Q%RSS&0&:&:#*9??;$'@@Q%RSS5>""9>>2' (* !% 0 0 6 6 8D*4---+AZ%=%=,""4( !9 	$$&r   r!   c                 >    d| j                   j                          dS )NzConfiguration(r9   )rE   valuesr:   s    r   r;   zConfigurationInternal.__repr__   s!     2 2 9 9 ;<A>>r   _ConfigurationInternal__valuec                 `    t        |t              st        S | j                  |j                  k(  S r/   )r>   r   r?   rE   )r$   rY   s     r   r@   zConfigurationInternal.__eq__   s*    '#89!!!!W%:%:::r   c                      y)zPerform custom validation when parameters are dependent on each other.

        Raises an InvalidConfigurationError if the configuration is invalid.
        Nr   r:   s    r   rR   z-ConfigurationInternal.configuration_validator   s     	r   c                 H    t        | j                  j                               S )z,Returns the parameters of the configuration.)listrE   rX   r:   s    r   get_parametersz$ConfigurationInternal.get_parameters   s    D&&--/00r   r*   c                     || j                   vr%t        d| d| j                  j                         t	        t
        | j                   j                  |            }|S )zIReturns the parameter with the given name, or except if it doesn't exist.rI   z for configuration )rE   rK   	__class__r   r   r5   rN   )r$   r*   param_values      r   get_parameterz#ConfigurationInternal.get_parameter   s_    t)))*4&0CDNND[D[C\]  143E3E3I3I$3OPr   r    c                     || j                   vrt        d|       | j                   |   }| j                  |   }|j                  rt	        d|       |j                  |      st        d| d|       ||_        y)z:Sets the parameter with the given name to the given value.rI   zCannot set static parameter: zInvalid value for parameter z: N)rF   rK   rE   r,   r   r+   r    )r$   r*   r    rT   rS   s        r   set_parameterz#ConfigurationInternal.set_parameter   s    t'''7v>??%%d+
&&t,	&)Ftf'MNN##E*;D6E7KLL	r   c                 H    t        j                  | j                               S )z5Returns the JSON representation of the configuration.)jsondumpsto_jsonr:   s    r   to_json_strz!ConfigurationInternal.to_json_str   s     zz$,,.))r   json_strc                     	 t        j                  |      }| j	                  |      S # t         j                  $ r t        d|       w xY w)3Returns a configuration from the given JSON string.z1Unable to decode configuration from JSON string: )rf   loadsJSONDecodeErrorrK   rO   )clsrj   config_jsons      r   from_json_strz#ConfigurationInternal.from_json_str   sT    	**X.K
 }}[))	 ## 	CH:N 	s	   ( "A
c           	         | j                   j                         D ci c]F  \  }}|t        |j                  t              r|j                  j                         n|j                  H }}}| j                  j                  |d<   |S c c}}w )zKReturns the JSON compatible dictionary representation of the configuration.rJ   )rE   rQ   r>   r    r   rh   r`   r   )r$   r*   rS   	json_dicts       r   rh   zConfigurationInternal.to_json   s     $(#5#5#;#;#=	
 $>i )//+@A //))+! $>	 	 
 "^^44	'
s   ABjson_mapc                    | j                   |j                  dd      k7  rt        d| j                    d|d          g }|j                         D ]'  \  }}|dk(  r|j	                  t        ||             )  | |      S )rl   rJ   Nz,Trying to instantiate configuration of type z from JSON with type r7   rG   )r   rN   rK   rQ   appendr5   )ro   rt   rG   r*   r    s        r   rO   zConfigurationInternal.from_json   s     <<8<<66>s||nLabjkrbsatu  
#>>+KD%w4$eLM	 ,
 j))r   r/   r!   N)!r   r   r   r   r   r2   r5   r3   r   r)   r	   r   r1   r;   rA   r'   r@   r   rR   r^   rb   r   intfloatrd   r   ri   classmethodr   rq   r   rh   rO   r   r   r   r   r   Z   sa   J 3344$s$;;<== '8D1G,H#I  'D?# ?;f ; ;
  1%; < 1# *@ 
 # 
 eCeT4I.J 
 t 
  *S * * *S *T *  * 
c3h 
 
 *c3h *D *  *r   c                      e Zd ZdZ edd dd       edd dd	       ed
d dd	       edd d e              edd dd       edd dd       edd dd	       edd dd      dZed d       Ze	de
eef   defd       Zy)!HNSWConfigurationInternalzxInternal representation of the HNSW configuration.
    Used for validation, defaults, serialization and deserialization.spacec                 .    t        | t              xr | dv S )N)l2ipcosine)r>   r2   r    s    r   <lambda>z"HNSWConfigurationInternal.<lambda>   s    Juc$: %0//%0r   Tr   r0   ef_constructionc                 0    t        | t              xr | dk\  S N   r>   ry   r   s    r   r   z"HNSWConfigurationInternal.<lambda>       Juc$:$Iuz$Ir   d   	ef_searchc                 0    t        | t              xr | dk\  S r   r   r   s    r   r   z"HNSWConfigurationInternal.<lambda>   r   r   Fnum_threadsc                 0    t        | t              xr | dk\  S r   r   r   s    r   r   z"HNSWConfigurationInternal.<lambda>   r   r   Mc                 0    t        | t              xr | dk\  S r   r   r   s    r   r   z"HNSWConfigurationInternal.<lambda>   r   r      resize_factorc                 0    t        | t              xr | dk\  S r   )r>   rz   r   s    r   r   z"HNSWConfigurationInternal.<lambda>  s    Jue$<$K!$Kr   333333?
batch_sizec                 0    t        | t              xr | dk\  S r   r   r   s    r   r   z"HNSWConfigurationInternal.<lambda>  r   r   sync_thresholdc                 0    t        | t              xr | dk\  S r   r   r   s    r   r   z"HNSWConfigurationInternal.<lambda>  r   r     )r~   r   r   r   r   r   r   r   r!   Nc                     | j                   j                  d      }| j                   j                  d      }|rD|rAt        t        |j                        t        t        |j                        kD  rt        d      y y y )Nr   r   z7batch_size must be less than or equal to sync_threshold)rE   rN   r   ry   r    r   )r$   r   r   s      r   rR   z1HNSWConfigurationInternal.configuration_validator  sy    ''++L9
++//0@A S***+d38L8L.MM+I  N  r   paramsc           	          ddddddddd	}g }|j                         D ]6  \  }}||vrt        d
|       |j                  t        ||   |             8  | |      S )zhReturns an HNSWConfiguration from a metadata dict containing legacy HNSW parameters. Used for migration.r~   r   r   r   r   r   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: r7   )rQ   rK   rw   r5   )ro   r   
old_to_newrG   r*   r    s         r   from_legacy_paramsz,HNSWConfigurationInternal.from_legacy_params"  s     "$5) -"1+#3	

 
!<<>KD%:% #Gv!NOO&Jt,<EJ * :r   rx   )r   r   r   r   r)   r   rF   r   rR   r{   r   r2   r   r   r   r   r   r   r}   r}      s   I )0
 3"I	
 -I	
 /I#+	
 %I	
 1 K	
 .I	
 2!I	
Y2Kh   S#X 4  r   r}   c                   `     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Z xZ	S )HNSWConfigurationInterfacezHNSW index configuration parameters.
    See https://docs.trychroma.com/guides#changing-the-distance-function for more information.
    r   r   r   r   r   r~   r   r   r   r   r   r   r   c	                     t        d|      t        d|      t        d|      t        d|      t        d|      t        d|      t        d|      t        d	|      g}	t        
| 	  |	
       y )Nr~   r7   r   r   r   r   r   r   r   rv   )r5   superr1   )r$   r~   r   r   r   r   r   r   r   rG   r`   s             r   r1   z#HNSWConfigurationInterface.__init__G  sp     #u="(9Q"9E"[I"15"}M"JG"(8O	

 	J/r   )
r   r   r   r   r   r2   ry   rz   r1   __classcell__r`   s   @r   r   r   B  s|     "$;""00 0 	0
 0 0 0 0 0 0r   r   c                   J    e Zd ZdZd edd d e             iZedd       Zy)	CollectionConfigurationInternalzInternal representation of the collection configuration.
    Used for validation, defaults, and serialization / deserialization.hnsw_configurationc                 "    t        | t              S r/   )r>   r}   r   s    r   r   z(CollectionConfigurationInternal.<lambda>k  s    Ju6O$Pr   Tr0   Nc                      y r/   r   r:   s    r   rR   z7CollectionConfigurationInternal.configuration_validatorq      r   rx   )	r   r   r   r   r)   r}   rF   r   rR   r   r   r   r   r   d  s?    K 	5%P35	
K  r   r   c                   .     e Zd ZdZdee   f fdZ xZS ) CollectionConfigurationInterfacez3Configuration parameters for creating a collection.r   c                 Z    |
t               }t        d|      g}t        |   |       y)zInitializes a new instance of the CollectionConfiguration class.
        Args:
            hnsw_configuration: The HNSW configuration to use for the collection.
        Nr   r7   rv   )r}   r5   r   r1   )r$   r   rG   r`   s      r   r1   z)CollectionConfigurationInterface.__init__}  s9    
 %!:!<"(<DVW

 	J/r   )r   r   r   r   r	   r}   r1   r   r   s   @r   r   r   z  s    =
084M+N 
0 
0r   r   c                   <    e Zd Zd edd dd      iZedd       Zy)	$EmbeddingsQueueConfigurationInternalautomatically_purgec                 "    t        | t              S r/   )r>   r'   r   s    r   r   z-EmbeddingsQueueConfigurationInternal.<lambda>  s    Jud$;r   FTr0   Nc                      y r/   r   r:   s    r   rR   z<EmbeddingsQueueConfigurationInternal.configuration_validator  r   r   rx   )r   r   r   r)   rF   r   rR   r   r   r   r   r     s4    6&;	 
K  r   r   )*abcr   rf   	overridesr   typingr   r   r   r   r	   r
   r   r   r   typing_extensionsr   multiprocessingr   chromadb.serder   	Exceptionr   rK   r   r2   ry   rz   r'   r&   r   r)   r5   rB   r   r}   r   HNSWConfigurationr   r   CollectionConfigurationr   r   r   r   <module>r      s      
 
 
 # % +
	9 		
 	 sC.EEF$ $+ +*I I& C./*,-DE *D_ 5 _L0!: 0> / &; ,0'F 0" ; +@ r   