
    q9i}              	          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d dlmZ  G d ded	
      Z G d ded	
      Z G d ded
      ZdedefdZdeeef   defdZdedefdZdedeeef   fdZ G d ded	
      Zdeeef   defdZ G d ded	
      Zdeeef   defdZ G d d ed	
      Z d!e
de fd"Z!d!eeef   de fd#Z"deeef   de fd$Z#	 dJde d!ee
   defd%Z$	 dJde d!ee
   deeef   fd&Z%	 dJded'ee   defd(Z& G d) d*ed	
      Z'deeef   de'fd+Z( G d, d-ed	
      Z)deeef   de)fd.Z* G d/ d0ed	
      Z+d!e
de+fd1Z,d!ede+fd2Z-de+defd3Z.de+deeef   fd4Z/d5ede+fd6Z0deeef   de+fd7Z1d8ed9e'defd:Z2d;ed<e)defd=Z3d>ed?edefd@Z4dAedBe+defdCZ5dDee   dEee   ddfdFZ6dDee   dGeeeef      ddfdHZ7	 	 	 	 	 	 dKdIZ8y)L    )	TypedDictDictAnyOptionalcastget_argsN)SpaceCollectionMetadataUpdateMetadataEmbeddingFunction)known_embedding_functionsregister_embedding_function)	cpu_count)Schemac                   ^    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<   y	)
HNSWConfigurationspaceef_constructionmax_neighbors	ef_searchnum_threads
batch_sizesync_thresholdresize_factorN__name__
__module____qualname__r	   __annotations__intfloat     n/var/www/html/leadgen/airagagent/rag_env/lib/python3.12/site-packages/chromadb/api/collection_configuration.pyr   r      /    LNOr#   r   F)totalc                   h    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	<   y
)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(      6    LN  r#   r(   c                   >    e Zd ZU ee   ed<   ee   ed<   ee   ed<   y)CollectionConfigurationhnswspannembedding_functionN)r   r   r   r   r   r   r(   r   r"   r#   r$   r1   r1   *   s&    
$
%%&'' !233r#   r1   Tconfig_json_strreturnc                 B    t        j                  |       }t        |      S N)jsonloads'load_collection_configuration_from_json)r5   config_json_maps     r$   +load_collection_configuration_from_json_strr=   0   s     jj1O2?CCr#   r<   c           	         | j                  d      | j                  d      t        d      d }d }d }| j                  d      t        t        | d         }| j                  d      t        t        | d         }| j                  d      R| d   }|d   dk(  rt        j                  dt        d	       d }n(	 |d
   }	 t        |   }	 |j                  |d         }nd }t        |||      S # t        $ r t        d|       w xY w# t        $ r t        d| d      w xY w# t        $ r}t        d|d
    d|d    d|       d }~ww xY w)Nr3   r2   &hnsw and spann cannot both be providedr4   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: r2   r3   r4   )get
ValueErrorr   r   r(   warningswarnDeprecationWarningKeyErrorr   build_from_config	Exceptionr1   )r<   hnsw_configspann_config	ef_configefef_namees          r$   r;   r;   8   s    	G$0'3ABBKLI 6".,of.EF7#/.0HI /0<#$89	V(MM2"
 B#F+
.w7
)))H*=> " '   CI;O    )'  3B  C    9)F:K9LMZcdlZmYnnpqrpst s0   :C/  	D
 
D& /D
D#&	E/E		ErG   c                 >    t        j                  t        |             S r8   )r9   dumps collection_configuration_to_json)rG   s    r$   $collection_configuration_to_json_strrZ   r   s    ::6v>??r#   c                    t        | t              r4| j                  d      }| j                  d      }| j                  d      }nT	 | j                  d      j                  }	 | j                  d      j                  }	 | j                  d      j                  }d }|	 t        t        |      }|	 t        t        |      }|d }ddi}|L	 |j                         rddi}n6|j                         d|j                         d	}t        t        |             |||dS # t
        $ r d }Y w xY w# t
        $ r d }Y w xY w# t
        $ r d }Y w xY w# t        $ r}t        d|       d }~ww xY w# t        $ r}t        d|       d }~ww xY w# t        $ r/}t        j                   d
| t"        d       d }ddi}Y d }~d }~ww xY w)Nr2   r3   r4   not a valid hnsw config: not a valid spann config: r@   rA   knownrF   r@   rG   "legacy embedding function config: rC   rD   rH   )
isinstancedictrI   get_parametervaluerJ   r   r   rP   r(   	is_legacyrF   
get_configr   r@   rK   rL   rM   )rG   rQ   rR   rT   rS   rV   s         r$   rY   rY   v   s   &$jj(zz'*ZZ,-	 ..v6<<K	 !//8>>L	%%&:;AAB (,I	>0+>K 	? 2LAL 
zX&		~	+||~#X.	 GGI# mmo	
 ,DH5 ' _  	K	  	 L	   	B	  	>8<==	>
  	?9!=>>	?$  	+MM4QC8"
 B*I	+s   D "D. >D? E 1E/ AF D+*D+.D<;D<?EE	E,E''E,/	F8FF	G%GGc                   ^    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<   y	)
CreateHNSWConfigurationr   r   r   r   r   r   r   r   Nr   r"   r#   r$   rh   rh      r%   r#   rh   json_mapc                    i }d| v r*| d   }|t        t              v r||d<   nt        d|       d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   d	| v r| d	   |d	<   |S )
Nr   not a valid space: r   r   r   r   r   r   r   r   r	   rJ   ri   rG   space_values      r$   !json_to_create_hnsw_configurationro      s     ')F(w'(5/))F7O2;-@AAH$$,->$? !(""*?";h&{3{  ( 7}x'5|8##+,<#= (""*?";Mr#   c                   h    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	<   y
)CreateSpannConfigurationr)   r*   r   r   r   r   r+   r,   r-   Nr.   r"   r#   r$   rq   rq      r/   r#   rq   c                     i }d| v r| d   |d<   d| v r| d   |d<   d| v r*| d   }|t        t              v r||d<   nt        d|       d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   |S )Nr)   r*   r   rk   r   r   r   rl   rm   s      r$   "json_to_create_spann_configurationrs      s     (*F(""*?";!!).!9~(w'(5/))F7O2;-@AAH$$,->$? !h&{3{(""*?";Mr#   c                   >    e Zd ZU ee   ed<   ee   ed<   ee   ed<   y)CreateCollectionConfigurationr2   r3   r4   N)r   r   r   r   rh   r   rq   r   r"   r#   r$   ru   ru      &    
*
++,-- !233r#   ru   metadatac                     t        |       S )FCreate a CreateCollectionConfiguration from legacy collection metadata)9create_collection_configuration_from_legacy_metadata_dict)rw   s    r$   ?create_collection_configuration_from_legacy_collection_metadatar{     s     EXNNr#   c           	          ddddddddd	}i }| j                         D ]  \  }}||v s||||   <    t        |      }t        |      }t        |
      S )ry   r   r   r   r   r   r   r   r   )
hnsw:spacezhnsw:construction_efzhnsw:Mhnsw:search_efhnsw:num_threadshnsw:batch_sizehnsw:sync_thresholdhnsw:resize_factorr2   )itemsro   populate_create_hnsw_defaultsru   rw   
old_to_newri   rF   rd   rQ   s         r$   rz   rz   
  s|    
  1!%)'/-	J H~~' /e:).HZ%&/ 4H=K/<K(k::r#   c                    | j                  d      | j                  d      t        d      t               }| j                  d      t        | d         |d<   | j                  d      t	        | d         |d<   | j                  d      N| d   }|d   dk(  rt        j                  dt        d	       |S t        |d
      }|j                  |d         |d<   |S )Nr2   r3   r?   r4   r@   rA   rB   rC   rD   rF   rG   )
rI   rJ   ru   ro   rs   rK   rL   rM   r   rO   )ri   resultrS   rT   s       r$   .load_create_collection_configuration_from_jsonr   #  s     ||F'HLL,A,MABB*,F ||F':8F;KLv||G(<Xg=NOw ||()512	V(MM2" M +9V+<=B+-+?+?	(@S+TF'(Mr#   c                 @    t        j                  t        | |            S )zFConvert a CreateCollection configuration to a JSON-serializable string)r9   rX   'create_collection_configuration_to_json)rG   rw   s     r$   +create_collection_configuration_to_json_strr   B  s    
 ::=fhOPPr#   c           	         d}| j                  d      }| j                  d      }|	 t        t        |      }|	 t        t
        |      }||t	        d      | j                  d      d}dd	i}|||d
S 	 t        t        | j                  d            }|j                         rdd	i}n!|/|-||j                  d      t        |j                               }|$|j                  d      |j                         |d<   |$|j                  d      |j                         |d<   |q|j                  d      |j                         vrPt        j                  d|j                  d       d|j                          d|j                          t        d       |q|j                  d      |j                         vrPt        j                  d|j                  d       d|j                          d|j                          t        d       ||||j                  d      q|j                  d      |j                         vrPt        j                  d|j                  d       d|j                          d|j                          t        d       |j                         d|j                         d}t        t!        |             |||d
S # t        $ r}t	        d|       d}~ww xY w# t        $ r}t	        d|       d}~ww xY w# t        $ r/}t        j                  d| t"        d       d}dd	i}Y d}~wd}~ww xY w)zDConvert a CreateCollection configuration to a JSON-serializable dictNr2   r3   r\   r]   r?   r4   r@   rA   rH   r}   )r   r   zspace z is not supported by z. Supported spaces: rC   rD   r^   r_   r`   )rI   r   rh   rP   rJ   rq   r   re   default_spacesupported_spacesrK   rL   rF   UserWarningrf   r   r@   rM   )rG   rw   rS   rQ   rR   rV   rT   s          r$   r   r   K  s   
 (,I**V$K::g&L	>6DK 	? 8,GL <#;ABBzz&'/X&	!"+
 	
B'#VZZ0D%EF<<>*I "|';#x||L'A'I"9@P@P@R"SK &;??7+C+K')'7'7'9G$'L,<,<W,E,M(*(8(8(:W% &??7+23F3F3HHMM !9 ::OPRPWPWPY{Znoq  pC  pC  pE  oF  G##$
 '##G,B4G4G4IIMM !1!1'!: ;;PQSQXQXQZP[[opr  qD  qD  qF  pG  H##$ # ((LL.:<<-R5H5H5JJMM l!; <<QRTRYRYR[Q\\pqs  rE  rE  rG  qH  I##$ 	--/I
 (R1 ' q  	>8<==	>
  	?9!=>>	?T  '04	

 X&	'sG   K K3 8IL 	K0K++K03	L<L

L	M
%MM
rT   c                    | j                  d      |r|j                         nd| d<   | j                  d      d| d<   | j                  d      d| d<   | j                  d      d| d<   | j                  d      t               | d<   | j                  d	      d| d	<   | j                  d
      d| d
<   | j                  d      d| d<   | S )z7Populate a CreateHNSW configuration with default valuesr   l2r   d   r      r   r   r   r   i  r   g333333?)rI   r   r   )rG   rT   s     r$   r   r     s     zz'"02"**,wzz#$,$' !zz/"*"$zz+&!{zz- ( )}zz,'"|zz"#+#' zz/"*"%Mr#   c                   @    e Zd ZU eed<   eed<   eed<   eed<   eed<   y)UpdateHNSWConfigurationr   r   r   r   r   N)r   r   r   r    r   r!   r"   r#   r$   r   r     s    NOr#   r   c                     i }d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   d| v r| d   |d<   |S )Nr   r   r   r   r   r"   ri   rG   s     r$   !json_to_update_hnsw_configurationr     s     ')Fh&{3{  ( 7}x'5|8##+,<#= (""*?";Mr#   c                   "    e Zd ZU eed<   eed<   y)UpdateSpannConfigurationr)   r   N)r   r   r   r    r   r"   r#   r$   r   r     s    Nr#   r   c                 :    i }d| v r| d   |d<   d| v r| d   |d<   |S )Nr)   r   r"   r   s     r$   "json_to_update_spann_configurationr     s<     (*F(""*?";h&{3{Mr#   c                   >    e Zd ZU ee   ed<   ee   ed<   ee   ed<   y)UpdateCollectionConfigurationr2   r3   r4   N)r   r   r   r   r   r   r   r   r"   r#   r$   r   r     rv   r#   r   c                     dddddd}i }| j                         D ]  \  }}||v s||||   <    t        |      }t        |      S )zGCreate an UpdateCollectionConfiguration from legacy collection metadatar   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   s         r$   ?update_collection_configuration_from_legacy_collection_metadatar     i    
 &)'/-J H~~' /e:).HZ%&/ 4H=K(k::r#   c                     dddddd}i }| j                         D ]  \  }}||v s||||   <    t        |      }t        |      S )zCCreate an UpdateCollectionConfiguration from legacy update metadatar   r   r   r   r   r   r   r   r   s         r$   ;update_collection_configuration_from_legacy_update_metadatar     r   r#   c                 B    t        |       }t        j                  |      S )zFConvert an UpdateCollectionConfiguration to a JSON-serializable string)'update_collection_configuration_to_jsonr9   rX   )rG   	json_dicts     r$   +update_collection_configuration_to_json_strr   !  s     8?I::i  r#   c                 .   | j                  d      }| j                  d      }| j                  d      }|||i S |	 t        t        |      }|	 t        t
        |      }d}|k|j                         rddi}nX|j                  |j                                |j                         d	|j                         d
}t        t        |             nd}|||dS # t        $ r}t	        d|       d}~ww xY w# t        $ r}t	        d|       d}~ww xY w)zDConvert an UpdateCollectionConfiguration to a JSON-serializable dictr2   r3   r4   Nr\   r]   r@   rA   r^   r_   rH   )rI   r   r   rP   rJ   r   re   validate_configrf   rF   r   r@   )rG   rQ   rR   rT   rV   rS   s         r$   r   r   )  s5    **V$K::g&L	(	)B|3
		>6DK 	? 8,GL (,I	~<<>*Ir}}/	--/I
 (R1	 ' 1  	>8<==	>  	?9!=>>	?s/   C C8 	C5"C00C58	DDDjson_strc                 B    t        j                  |       }t        |      S r8   )r9   r:   .load_update_collection_configuration_from_json)r   ri   s     r$   2load_update_collection_configuration_from_json_strr   U  s     zz(#H9(CCr#   c                    | j                  d      | j                  d      t        d      t               }| j                  d      t        | d         |d<   | j                  d      t	        | d         |d<   | j                  d      R| d   d   dk(  rt        j                  dt        d	       |S t        | d   d
      }|j                  | d   d         |d<   |S )z7Convert a JSON dict to an UpdateCollectionConfigurationr2   r3   r?   r4   r@   rA   rB   rC   rD   rF   rG   )
rI   rJ   r   r   r   rK   rL   rM   r   rO   )ri   r   rT   s      r$   r   r   ]  s     ||F'HLL,A,MABB*,F ||F':8F;KLv||G(<Xg=NOw ||()5()&1X=MM2" M +84H+I&+QRB+-+?+?-.x8,F'( Mr#   existing_hnsw_configupdate_hnsw_configc                 h    t        |       }g d}|D ]  }||v s||   ||<    t        t        |      S )z6Overwrite a HNSWConfiguration with a new configuration)r   r   r   r   r   )rb   r   r   )r   r   r   update_fieldsfields        r$   overwrite_hnsw_configurationr   ~  sN    
 &'FM  6&&.u5F5M6 !6**r#   existing_spann_configupdate_spann_configc                 h    t        |       }ddg}|D ]  }||v s||   ||<    t        t        |      S )z7Overwrite a SpannConfiguration with a new configurationr)   r   )rb   r   r(   )r   r   r   r   r   s        r$   overwrite_spann_configurationr     sT    
 '(FM
  7''/6F5M7 "F++r#   existing_embedding_functionupdate_embedding_functionc                 |   | j                         s|j                         rt        j                  dt        d       | S | j	                         |j	                         k7  r.t        d| j	                          d|j	                          d      |j                  | j                         |j                                |S )z7Overwrite an EmbeddingFunction with a new configurationz.cannot update legacy embedding function configrC   rD   z6Cannot update embedding function: incompatible types ( vs ))re   rK   rL   rM   rF   rJ   validate_config_updaterf   )r   r   s     r$   overwrite_embedding_functionr     s     #,,.2K2U2U2W<	

 +* #'')-F-K-K-MM+002348Q8V8V8X7YYZ\
 	
 44#..02K2V2V2X %$r#   existing_configupdate_configc                 n   |j                  d      }|j                  d      }||t        d      | j                  d      }||t        ||      }| j                  d      }||t        ||      }| j                  d      }|j                  d      }||t	        ||      }n|}t        |||      S )z<Overwrite a CollectionConfiguration with a new configurationr3   r2   r?   r4   rH   )rI   rJ   r   r   r   r1   )r   r   update_spannupdate_hnswupdated_hnsw_configupdated_spann_configupdated_embedding_function	update_efs           r$   "overwrite_collection_configurationr     s    
 !$$W-L##F+KK$;ABB *--f5&;+B:

 +..w7'L,D< , 

 "1!4!45I!J!!"67I%1)E*I*& *3&" "5 r#   r4   configuration_efc                     | c|a| j                         dk7  rN| j                         |j                         k7  r-t        d| j                          d|j                                y)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   )rF   rJ   )r4   r   s     r$   .validate_embedding_function_conflict_on_creater     s    $ %*:*F##%2"'')-=-B-B-DDo  qC  qH  qH  qJ  pK  KO  P`  Pe  Pe  Pg  Oh  i  r#   persisted_ef_configc                     |v| t| j                         dk7  ra|j                  d      P|j                  d      | j                         k7  r.t        d| j                          d|j                  d             y)z
    Validates that there are no conflicting embedding functions between function parameter
    and collection configuration.
    Nr   rF   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: )rF   rI   rJ   )r4   r   s     r$   +validate_embedding_function_conflict_on_getr     s     &+=+I##%2#''/;#''/3E3J3J3LL Q  Rd  Ri  Ri  Rk  Ql  l{  |O  |S  |S  TZ  |[  {\  ]  r#   c                    | j                   j                   | j                   j                  j                  t        d      d}|| j                  vrt        d| d      | j                  |   }|j                  |j                  j                  t        d| d      | j                   j                  j                  |j                  j                  fD ]  }d|v r|d   |j
                  j                  t        d      |j
                  j                  }|d   }d|v r
|d   |_        d	|v r
|d	   |_        d
|v r
|d
   |_	        d|v r
|d   |_
        d|v rl|d   |_        nad|v r]|d   X|j
                  j                  t        d      |j
                  j                  }|d   }d|v r
|d   |_        d|v r
|d   |_        d|v s|d   |d   |j
                  _         | S )a  
    Updates a schema with configuration changes.
    Only updates fields that are present in the configuration update.

    Args:
        schema: The existing Schema object
        configuration: The configuration updates to apply

    Returns:
        Updated Schema object
    z2Schema is missing defaults.float_list.vector_indexz
#embeddingzSchema is missing keys[]z].float_list.vector_indexr2   z1Trying to update HNSW config but schema has SPANNr   r   r   r   r   r3   z1Trying to update SPANN config but schema has HNSWr)   r4   )defaults
float_listvector_indexrJ   keysrG   r2   r   r   r   r   r   r3   r)   r4   )	schemaconfigurationembedding_keyembedding_value_typesr   rQ   r   rR   r   s	            r$   +update_schema_from_collection_configurationr   !  s=   " 	""*??%%22:MNN MFKK'2=/CDD"KK6((0 ++88@%m_4MN
 	
 	""//((55 +Y ]"}V'<'H""''/ !TUU&--22K'/K k)(3K(@%+*5m*D'{*)4\)B&;.-89I-J*+-,7,H)%-*@*L""((0 !TUU'..44L(1L ,.-9/-J*l*)5k)B& !M123?5BCW5XL2W+YZ Mr#   r8   )r   r   r   r   r6   r   )9typingr   r   r   r   r   r   r9   chromadb.api.typesr	   r
   r   r   "chromadb.utils.embedding_functionsr   r   multiprocessingr   rK   r   r   r(   r1   strr=   r;   rZ   rY   rh   ro   rq   rs   ru   r{   rz   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>r      s   A A   &  %	 	% 	4it 4DDD7#s(^77t@1H @S @;-D ;cSVh ;|iu 38n6	y 	38n.4IU 4O O"O;38n;";238n"B .2Q)Q)*Q 	Q .2g)g)*g 
#s(^gV HL#)12C)D.iu 38n"y 
38n4IU 4; ;";&;;";&!)!!)))	#s(^)XDD"D38n"B+++AX++*,-,1, ,&%!2%0% %8(,(0( (V !2301 
@ !23!$sCx.1 
(QQ%DQQr#   