
    eim                        d dl Z d dlmZmZmZmZ d dlZd dlZd dl	m
Z
mZmZm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Zd dlmZ d dlmZ d dlmZm Z  d d	l!m"Z# d d
lm$Z$m%Z%  G d de
e         Z& G d de
e         Z'e G d de
e                Z(e G d de
e                Z)deddfdZ*deddfdZ+deddfdZ,deddfdZ-deddfdZ.deddfdZ/dDdZ0deddfdZ1deddfdZ2deddfdZ3e jh                  jk                  ee       dDd        Z6deddfd!Z7deddfd"Z8deddfd#Z9e jh                  jk                  ee       deddfd$       Z:e jh                  jk                  ee       deddfd%       Z;dEd&Z<e jh                  jk                  ee       deddfd'       Z=deddfd(Z>dEd)Z?deddfd*Z@deddfd+ZAdeddfd,ZBdeddfd-ZCdeddfd.ZDdeddfd/ZEdeddfd0ZFdeddfd1ZGdeddfd2ZHdeddfd3ZIdeddfd4ZJdeddfd5ZKe G d6 d7e
e                ZLdeddfd8ZMdeddfd9ZNdeddfd:ZOdeddfd;ZPdeddfd<ZQdeddfd=ZR G d> d?e%      ZSe G d@ dAe
e                ZTdeddfdBZUdeddfdCZVy)F    N)DictAnycastList)EmbeddingFunction
EmbeddingsSpace
Embeddable)	ClientAPI)	CreateCollectionConfigurationUpdateCollectionConfiguration'load_collection_configuration_from_jsonCreateHNSWConfigurationUpdateHNSWConfigurationCreateSpannConfigurationUpdateSpannConfigurationSpannConfigurationoverwrite_spann_configuration)register_embedding_function)ClientFactories)is_spann_disabled_modeskip_reason_spann_disabled)
Collection)Optional	TypedDictc                   $    e Zd ZddZdedefdZy)LegacyEmbeddingFunctionreturnNc                      y N selfs    /home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/chromadb/test/configurations/test_collection_configuration.py__init__z LegacyEmbeddingFunction.__init__            inputc                 n    t        t        t        j                  ddggt        j                              S N      ?       @dtyper   r   nparrayfloat32r#   r(   s     r$   __call__z LegacyEmbeddingFunction.__call__#   $    J3*RZZ HIIr'   r   N)__name__
__module____qualname__r%   r
   r   r4   r!   r'   r$   r   r      s    Jj JZ Jr'   r   c                   :    e Zd ZddZdedefdZedefd       Z	y)LegacyEmbeddingFunctionWithNamer   Nc                      y r    r!   r"   s    r$   r%   z(LegacyEmbeddingFunctionWithName.__init__(   r&   r'   r(   c                 n    t        t        t        j                  ddggt        j                              S r*   r/   r3   s     r$   r4   z(LegacyEmbeddingFunctionWithName.__call__+   r5   r'   c                       y)N	legacy_efr!   r!   r'   r$   namez$LegacyEmbeddingFunctionWithName.name.       r'   r6   )
r7   r8   r9   r%   r
   r   r4   staticmethodstrr@   r!   r'   r$   r;   r;   '   s8    Jj JZ J #  r'   r;   c                       e Zd ZddefdZdedefdZede	fd       Z
dee	ef   fdZedee	ef   dd fd	       Zdefd
Zy)CustomEmbeddingFunctiondimc                     || _         y r    _dimr#   rF   s     r$   r%   z CustomEmbeddingFunction.__init__5   	    	r'   r(   r   c                     t        t        t        j                  dg| j                  z  gt        j
                              S Nr+   r-   r   r   r0   r1   rI   r2   r3   s     r$   r4   z CustomEmbeddingFunction.__call__8   ,    J3%$))*;)<BJJ OPPr'   c                       y)N	custom_efr!   r!   r'   r$   r@   zCustomEmbeddingFunction.name;   rA   r'   c                     d| j                   iS NrF   rH   r"   s    r$   
get_configz"CustomEmbeddingFunction.get_config?       tyy!!r'   configc                      t        | d         S NrF   rF   )rE   rV   s    r$   build_from_configz)CustomEmbeddingFunction.build_from_configB   s    &6%=99r'   c                      yNl2r!   r"   s    r$   default_spacez%CustomEmbeddingFunction.default_spaceF       r'   N   r7   r8   r9   intr%   r
   r   r4   rB   rC   r@   r   r   rT   r[   r	   r_   r!   r'   r$   rE   rE   3   s    C Qj QZ Q #  "DcN " :$sCx. :5N : :u r'   rE   c                       e Zd ZddefdZdedefdZede	fd       Z
dee	ef   fdZedee	ef   dd fd	       Zdefd
Zy)CustomEmbeddingFunction2rF   c                     || _         y r    rH   rJ   s     r$   r%   z!CustomEmbeddingFunction2.__init__L   rK   r'   r(   r   c                     t        t        t        j                  dg| j                  z  gt        j
                              S )Nr,   r-   rN   r3   s     r$   r4   z!CustomEmbeddingFunction2.__call__O   rO   r'   c                       y)N
custom_ef2r!   r!   r'   r$   r@   zCustomEmbeddingFunction2.nameR   s    r'   c                     d| j                   iS rS   rH   r"   s    r$   rT   z#CustomEmbeddingFunction2.get_configV   rU   r'   rV   c                      t        | d         S rX   )rf   rZ   s    r$   r[   z*CustomEmbeddingFunction2.build_from_configY   s    'F5M::r'   c                      yr]   r!   r"   s    r$   r_   z&CustomEmbeddingFunction2.default_space]   r`   r'   N)   rc   r!   r'   r$   rf   rf   J   s    C Qj QZ Q #  "DcN " ;$sCx. ;5O ; ;u r'   rf   clientr   c                    | j                          | j                  dt                     }t        |j                  j
                        }|r%t        |t              r|j                  d      }|J J d|        | j                  dt                     }|j                  dgdg       |j                  dgd	
      }t        |d         d	k(  sJ yzETest creating and getting collections with legacy embedding functionstest_legacyr@   embedding_functionrt   Nzconfig: 1testids	documents   query_texts	n_resultsrx   )resetcreate_collectionr   r   _modelconfiguration_json
isinstancedictgetget_collectionaddquerylenro   collrV   efcoll2resultss         r$   test_legacy_embedding_functionr   a   s    
LLN ##24 $ D 5T[[5S5STF*VT*ZZ,-zz)))u !!24 " E 
II3%F8I,kkvh!k<Gwu~!###r'   c                    | j                          | j                  dt                     }t        |j                  j
                        }|r%t        |t              r|j                  d      }|J J d|        | j                  dt                     }|j                  dgdg       |j                  dgd	
      }t        |d         d	k(  sJ yrq   )r~   r   r;   r   r   r   r   r   r   r   r   r   r   r   s         r$   (test_legacy_embedding_function_with_namer      s    
LLN ##:< $ D 5T[[5S5STF*VT*ZZ,-zz)))u !!:< " E 
II3%F8I,kkvh!k<Gwu~!###r'   c                 $   | j                          dddd}| j                  d|      }t        |j                  j                        }|rt        |t              rt        t        |j                  di             }t        |j                  d            t        d      k(  sJ |j                  d	      dk(  sJ |j                  d
      dk(  sJ |j                  d      dk(  sJ |j                  d      }|J |j                         dk(  sJ yyy)z5Test creating collections with legacy metadata formatcosine   
   )
hnsw:spacezhnsw:construction_efzhnsw:Mtest_legacy_metadata)r@   metadatahnswspaceef_constructionmax_neighbors	ef_searchd   rt   Ndefault)r~   r   r   r   r   r   r   r   r   r   rC   r@   )ro   legacy_metadatar   rV   hnsw_configr   s         r$   r   r      s   
LLN  #O
 ###  $ D 5T[[5S5STF*VT*2FJJvr4JK;??7+,H===01S888/2555{+s222ZZ,-~~wwyI%%% +vr'   c                    | j                          dddddd}|t        d      d	}| j                  d
|      }t        |j                  j
                        }|rt        |t              ryt        t        |j                  di             }|j                  di       }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |J yyy)z7Test creating collections with new configuration formatr   r   r         )r   r   r   r   num_threads   rY   r   rt   test_new_configr@   configurationr   rt   r   r   r   N)r~   rE   r   r   r   r   r   r   r   r   r   )ro   r   rV   r   loaded_configr   s         r$   test_new_configurationr      s    
LLN ,K 5!<-F
 ## $ D <&&M M482M4E4Efb4QR3R8w'833301S888/2555~~ 9}r'   c                     | j                          t        j                  t              5 }ddd}| j	                  dd|i       dt        |j                        v sJ 	 ddd       y# 1 sw Y   yxY w)	z)Test validation of invalid configurationsr   )r   r   test_invalidr   r   zinvalid valueN)r~   pytestraises	Exceptionr   rC   value)ro   excinfoinvalid_hnsws      r$   test_invalid_configurationsr      sv    
LLN 
y	! 
5W!1
 	  !<0 	! 	

 #gmm"4444
5 
5 
5s   4A((A1c                    | j                          dddd}| j                  dd|i      }dd	d
}d|i}|j                  |       |j                  }|rt	        |t
              rz|j                  di       }t	        |t
              rX|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ | j                  d      }|j                  }|rt	        |t
              r||j                  di       }t	        |t
              rY|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ yyyy)'Test updating collection configurationsr   rz   r   r   r   r   test_updatesr   r   r   r   )r   r   r   r   r   r   r   r      r@   Nr~   r   modifyr   r   r   r   r   )ro   initial_hnswr   update_hnswupdate_configr   r   s          r$   test_hnsw_configuration_updatesr      s   
LLN -L
 ##|, $ D ,K
 	4M 	KKmK, ++MM48#''3k4(??;/2555??7+x777??#45<<<???3r999  n 5D++MM48#''3k4(??;/2555??7+x777??#45<<<???3r999	 ) 9}r'   c                    | j                         }|j                          dddd}|t        d      d}|j                  d|	       | j                         }|j	                  d
      }t        |j                  j                        }|rt        |t              rt        t        |j                  di             }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      }|J |j                         dk(  sJ yyy)z5Test configuration persistence across client restartsr   r   r   )r   r   r   r   rY   r   test_persist_configr   r   r   r   r   r   r   rt   NrQ   )create_client_from_systemr~   rE   r   r   r   r   r   r   r   r   r   r   r@   )client_factoriesro   r   rV   client2r   r   r   s           r$   test_configuration_persistencer     sX    779F
LLN ,K 5!<-F
 "   88:G!!" " D <&&M M482M4E4Efb4QRw'833301S888/2555{+s22234~~wwyK''' 9}r'   c                 4   | j                          dddd}| j                  dd|i      }|j                  j                  J |j                  j                  j	                  d      }|J |j	                  d	      dk(  sJ |j	                  d
      dk(  sJ y)r   r   r   r   r   r   r   r   Nr   r   )r~   r   r   r   r   )ro   r   r   r   s       r$    test_configuration_result_formatr   J  s    
LLN -L
 ##|, $ D
 ;;))555++0044V<K"""??;'2---??7#x///r'   c                    | j                          i }|t        d      d}t        r| j                  d|      }t	        |j
                  j                        }|rt        |t              rt        t        |j                  di             }|j                  d      }|j                  d      d	k(  sJ |j                  d
      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |J yyy| j                  d|      }t	        |j
                  j                        }|rt        |t              rt        t        |j                  di             }|j                  d      }|j                  d      d	k(  sJ |j                  d
      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |J yyy)9Test creating collections with SPANN configuration formatr   rY   spannrt   test_spann_configr   r   rt   r   r^   r   r   r   r   r   Nr   r   @   search_nprobe   write_nprober~   rE   r   r   r   r   r   r   r   r   r   r   r   ro   spann_configrV   r   r   hnsw_config_loadedr   spann_config_loadeds           r$   test_empty_spann_configurationr   a  s/   
LLN .0L5!<-F
 ''$  ( 
 @KK**
 Zt<!%'):):62)F" ""#78B%))'2d:::%))*;<CCC%))+6#===%))/:b@@@>!> == ''$  ( 
 @KK**
 Zt<"&(-*;*;GR*H# ""#78B&**73t;;;&**+<=DDD&**;73>>>&**?;rAAA&**?;sBBB&**>:cAAA>!> ==r'   c                    | j                          ddddddd}|t        d      d}t        r| j                  d	|
      }t	        |j
                  j                        }|rt        |t              rt        t        |j                  di             }|j                  d      }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |J yyy| j                  d	|
      }t	        |j
                  j                        }|rt        |t              rt        t        |j                  di             }|j                  d      }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |J yyy)r   r   r   r   r   r   )r   r   r   r   r   r   rY   r   r   r   r   rt   r   r   r   r   r   Nr   r   r   r   r   r   s           r$   test_spann_configurationr     sE   
LLN .L 5!<-F
 ''$  ( 
 @KK**
 Zt<!%'):):62)F" ""#78B%))'2h>>>%))*;<CCC%))+6#===%))/:b@@@>!> == ''$  ( 
 @KK**
 Zt<"&(-*;*;GR*H# ""#78B&**73x???&**+<=DDD&**;73>>>&**?;rAAA&**?;q@@@&**>:b@@@>!> ==r'   )reasonc                    | j                         }|j                          dddddd}|t        d      d}|j                  d|	       | j                         }|j	                  d
      }t        |j                  j                        }|rt        |t              rt        t        |j                  di             }|j                  d      }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |J yyy)z;Test SPANN configuration persistence across client restartsr   r   r   r   )r   r   r   r   r   rY   r   test_persist_spann_configr   r   r   rt   r   r   r   r   r   N)r   r~   rE   r   r   r   r   r   r   r   r   r   r   )r   ro   r   rV   r   r   r   r   s           r$   $test_spann_configuration_persistencer     sl    779F
LLN .L 5!<-F
 (  
 88:G!!( " D <&&M M484m6G6GQS6TU34(H444 12c9990B6660A555/2555~~ 9}r'   c                     | j                          ddd}ddd}t        j                  t        d      5  | j	                  d||d	
       ddd       y# 1 sw Y   yxY w)z@Test that HNSW and SPANN configurations cannot both be specifiedr   r   )r   r   r   )r   r   z&hnsw and spann cannot both be provided)matchtest_dual_configr   r   r   N)r~   r   r   
ValueErrorr   )ro   r   r   s      r$   'test_exclusive_hnsw_spann_configurationr     sp    
LLN ,K
 .L 
z)Q	R 
  ##% 	! 	

 
 
s   AAc                 :   | j                          ddi}d|i}t        r| j                  d|      }t        |j                  j
                        }|rt        |t              rt        t        |j                  di             }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d	      dk(  sJ |j                  d
      dk(  sJ |j                  d      }|J |j                         dk(  sJ yyy| j                  d|      }t        |j                  j
                        }|rt        |t              rt        t        |j                  di             }|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d
      dk(  sJ |j                  d	      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      }|J |j                         dk(  sJ yyy)z,Test the default values for SPANN parametersr   r   r   test_spann_defaultsr   r   r   r   r   r   r   rt   Nr   r   r   r   r   )r~   r   r   r   r   r   r   r   r   r   r   r@   r   r   s           r$   test_spann_default_parametersr     sZ   
LLN 	.L 	-F ''&  ( 
 @KK**
 Zt<!%'):):62)F" &))'2h>>>%))*;<CCC%))+6#===%))/:b@@@""#78B>!>779	))) == ''&  ( 
 @KK**
 Zt<"&(-*;*;GR*H# '**73x???&**+<=DDD&**?;rAAA&**;73>>>&**?;sBBB&**>:cAAA""#78B>!>779	))) ==r'   c                 t   | j                          d}t        t        j                  |            }i }|j	                  d      "t        t        |j	                  d            |d<   |j	                  d      |j	                  d      |d<   t        r| j                  d|      }t        |j                  j                        }|rt        |t              rt        t        |j	                  di             }|j	                  d      }|j	                  d      d	k(  sJ |j	                  d
      dk(  sJ |j	                  d      dk(  sJ |j	                  d      dk(  sJ |J yyy| j                  d|      }t        |j                  j                        }|rt        |t              rt        t        |j	                  di             }|j	                  d      d	k(  sJ |j	                  d      dk(  sJ |j	                  d      dk(  sJ |j	                  d
      dk(  sJ |j	                  d      dk(  sJ yyy)zCTest serializing and deserializing SPANN configuration to/from JSONaw  
    {
        "spann": {
            "space": "cosine",
            "search_nprobe": 7,
            "write_nprobe": 15,
            "ef_construction": 200,
            "ef_search": 150
        },
        "embedding_function": {
            "type": "known",
            "name": "custom_ef",
            "config": {
                "dim": 10
            }
        }
    }
    r   Nrt   test_spann_jsonr   r   r   r   r   r   r   r   r   r      r      r      )r~   r   jsonloadsr   r   r   r   r   r   r   r   r   r   )	ro   config_jsoncollection_configcreate_configr   r   r   r   r   s	            r$   test_spann_json_serializationr   X  se   
LLNK( @

;@WX 46MW%1!%$&7&;&;G&D"
g 12>.?.C.C /
*+ ''"' ( 
 @KK**
 Zt<!%'):):62)F" ""#78B%))'2h>>>%))*;<CCC%))+6#===%))/:b@@@>!> == ''"' ( 
 @KK**
 Zt<"&(-*;*;GR*H# '**73x???&**?;q@@@&**>:b@@@&**+<=DDD&**;73>>> ==r'   c                    | j                          dddd}| j                  dd|i      }dd	d
}d|i}|j                  |       |j                  }|rtt	        |t
              rd|j                  di       }t	        |t
              rB|j                  d      dk(  sJ |j                  d      d	k(  sJ |j                  d      dk(  sJ | j                  d      }|j                  }|rwt	        |t
              rf|j                  di       }t	        |t
              rC|j                  d      dk(  sJ |j                  d      d	k(  sJ |j                  d      dk(  sJ yyyy)z-Test updating SPANN collection configurationsr   r   r   )r   r   r   test_spann_updatesr   r   r   r   )r   r   r   r   r   r   Nr   )ro   initial_spannr   update_spannr   r   r   s          r$    test_configuration_spann_updatesr     s    LLN /M
 ##!. $ D .L
 	4M 	KKmK, ++MM48$(("5lD)##K0C777##O4:::##G,888  !56D++MM48$(("5lD)##K0C777##O4:::##G,888 * 9}r'   c                    | j                          ddddddd}| j                  dd	|i
      }t        t        dd            }|j	                  |       |j
                  }|rt        |t              r|j                  d	i       }t        |t              r|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ | j                  d      }|j
                  }|rt        |t              r|j                  d	i       }t        |t              r|j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ yyyy)z;Test updating SPANN configuration from JSON and applying itr   r   r   r      r   )r   r   r   r   r   r   test_spann_json_updater   r   r   r   r   r   )r   r   r   r   r   r   r   r   N)
r~   r   r   r   r   r   r   r   r   r   )ro   r   r   r   r   r   s         r$   test_spann_update_from_jsonr     s+    LLN /M ##%. $ D
 2&
M 	KKmK, ++MM48$(("5lD)##K0C777##O4:::  ##G,888##$56#===##O4:::##N3r999  !9:D++MM48$(("5lD)##K0C777##O4:::  ##G,888##$56#===##O4:::##N3r999 * 9}r'   c                  @   ddddddd} dd	d
}t        | |      }|j                  d      dk(  sJ |j                  d      d	k(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ |j                  d      dk(  sJ y)z8Test the overwrite_spann_configuration function directlyr   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N)r   r   )original_configr   updated_configs      r$   "test_overwrite_spann_configurationr    s     +O /M 3?MRN o."444k*c111 g&(222n-333/0C777o."444r'   c                     | j                          ddd}| j                  dd|i      }ddi}d	d
i}||d}t        j                  t              5  |j                  |       ddd       y# 1 sw Y   yxY w)zMTest that HNSW and SPANN configurations cannot both be specified in an updater   r   )r   r   test_exclusive_updater   r   r   r   r   r   r   r   N)r~   r   r   r   r   r   )ro   r   r   r   r   r   s         r$   .test_exclusive_update_hnsw_spann_configurationr  2  s     LLN -L ##$|, $ D 	R,K 	.L 4M 
z	" 1-01 1 1s   A--A6c                    | j                          | j                  d      }|J |j                  J t        |j                        }|J |j	                  d      }|J |j	                  d      dk(  sJ |j	                  d      dk(  sJ |j	                  d	      d
k(  sJ |j	                  d      dk(  sJ |j	                  d      dk(  sJ |j	                  d      J |j	                  d      }|J |j                         dk(  sJ y)z.Test creating a collection with default valuestest_default_creationr   Nr   r   r^   r   r   r   r   r   sync_threshold  r   rt   r   )r~   r   r   r   r   r@   )ro   r   rV   r   r   s        r$    test_default_collection_creationr	  R  s*   
LLN##)@#AD""...4T5L5LMF**V$K"""??7#t+++??,-444???+r111??;'3...??+,444::g&&&	(	)B>>779	!!!r'   c                      dddddddddd	i d
d} t        j                  t              5  t        |        ddd       y# 1 sw Y   yxY w)z9Test that on an invalid configuration, an error is raisedr^   r   r   g333333?r  )r   r   r   r   resize_factorr  NrQ   knownr@   typerV   r   r   rt   )r   r   r   r   )invalid_configs    r$   test_invalid_configurationr  k  sd     " "
 
&N  
z	" @/?@ @ @s   A  A	c           
         | j                          | j                  d      }dddi di}t        |j                  d|ddd|j                  |j
                  	      }|J |j                  dk(  sJ |j                  |k(  sJ ||_        t        j                  t              5  |j                  d
gdg       ddd       t        j                  t              5  |j                  dgd       ddd       y# 1 sw Y   @xY w# 1 sw Y   yxY w)z
    When an invalid confiugration is used, collection create, get, list_collections should all pass
    Only when trying to use the collection should an error be reaised
    test_invalid_configr   rt   rQ   r  r  test_invalid_config_collectionNidr@   r   serialized_schemar   	dimensiontenantdatabaseru   rv   rw   rz   r{   )r~   r   CollectionModelr  r  r  r@   r   r   r   r   r   r   r   )ro   r   invalid_config_jsoninvalid_collections       r$   /test_collection_load_with_invalid_configurationr    s   
 LLN ##)>#?D 	
 )77-.{{	 )))""&FFFF004GGGG$DK	z	" 0cUvh/0 
z	" 6

x1
56 60 06 6s   "C7D7D Dc                    | j                          dt        d      i}| j                  d|      }|j                  }|j                  }d|v sJ |j                  d      }|j                  d      }|J |J |j                  d      dk(  sJ |j                  d	      d
k(  sJ |j                         d
k(  sJ |j                         |j                  d      k(  sJ y)zHTest that configuration_json and configuration properties are consistentrt      rY   test_config_consistencyr   Nr  r  r@   rQ   rV   )r~   rE   r   r   r   r   r@   rT   )ro   rV   r   r   config_processedef_jsonef_processeds          r$   =test_configuration_json_vs_configuration_property_consistencyr%    s    LLN 	5!<-F ##& $ D ))K));... oo23G#''(<=L###;;v')));;v+---+---""$H(====r'   c                 Z   | j                          | j                  d      }|j                  }|j                  }d|v sJ |j	                  d      }|j	                  d      }|J |J |j	                  d      dk(  sJ |j	                  d      dk(  sJ |j                         dk(  sJ y)	zPTest that default configuration_json and configuration properties are consistenttest_default_config_consistencyr   rt   Nr  r  r@   r   )r~   r   r   r   r   r@   )ro   r   r   r"  r#  r$  s         r$   Etest_default_configuration_json_vs_configuration_property_consistencyr(    s     LLN ##)J#KD ))K));... oo23G#''(<=L###;;v')));;v)+++)+++r'   c                 &   | j                          | j                  d      }dddi diddddid	id
did
diddidg}t        |      D ]  \  }}t        |j                  d| |ddd|j
                  |j                        }||_        |j                  |j                  k(  sJ |j                  d| k(  sJ |j                  |k(  sJ t        j                  t              5  |j                   ddd       t        j                  t              5  |j                  dgdg       ddd       t        j                  t              5  |j                  dgd       ddd       t        j                  t              5  |j!                  dgdg       ddd       t        j                  t              5  |j#                  dg       ddd        y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   exY w# 1 sw Y   xY w)z
    Test that invalid configurations allow list_collections, get_collection to succeed,
    but fail when _embed is called (during add, query, upsert operations)
    test_invalid_operationsr   rt   nonexistent_efr  r  rF   rb   )r  rV   r   r^   r   r  legacyr  test_invalid_config_Nr  ru   rv   rw   rz   r{   )r~   r   	enumerater  r  r  r  r   r@   r   r   r   r   r   r   r   upsert_embed)ro   r   invalid_configsir  invalid_collection_models         r$   9test_invalid_configuration_operations_succeed_until_embedr4    s     LLN ##)B#CD !(#	
 ! !*#	
 dOx(#)8"4	
%-O2 '7 !">#2ww's+-";;]]	$
  / ww255555yy21#6666&&.888]]:& 		 ]]:& 	4HH#6(H3	4 ]]:& 	:JJF8qJ9	: ]]:& 	7KKSEfXK6	7 ]]:& 	"KK!	" 	"A!"(	 		4 	4	: 	:	7 	7	" 	"s<   %GG"
G. G:7HG	"G+	.G7	:H	H	c                 4   | j                          | j                  ddt        d      i      }dddddid	i}||j                  _        |j
                  dk(  sJ |j                  J |j                  |k(  sJ |j                  J |j                  J t        j                  t              5  |j                  dgdg       d
d
d
       t        j                  t              5  |j                  dgd       d
d
d
       t        j                  t              5  |j                  dgdg       d
d
d
       t        j                  t              5  |j                  }d
d
d
       y
# 1 sw Y   xY w# 1 sw Y   {xY w# 1 sw Y   PxY w# 1 sw Y   y
xY w)zv
    Test that get_collection works even with invalid configurations,
    but operations that require _embed fail
    test_get_invalidrt   rn   rY   r   nonexistent_functionr  rF   r  Nrv   ztest docrw   rz   r{   )r~   r   rE   r   r   r@   r  r  r  r   r   r   r   r   r/  r   )ro   
valid_collr  _s       r$   .test_get_collection_with_invalid_configurationr:  2  s   
 LLN ))+-D-KL * J 	*aj
N ,:J( ??0000==$$$((N:::(((*** 
z	" =F8
|<= 
z	" <fX;< 
z	" @vh:,?@ 
z	" %$$% %= =< <@ @% %s0   0E*'E6FF*E36E?FFc                    | j                          | j                  dt        d            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         ddik(  sJ | j                  dt        d            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         ddik(  sJ | j                  d	      }|j                  j	                  d      }|J |j                         dk(  sJ |j                         ddik(  sJ y)
z0Test creating a collection with no EF in config.test_no_configrb   rY   rs   Nrt   rQ   rF   r   	r~   r   rE   r   r   r@   rT   get_or_create_collectionr   ro   r   r   s      r$   test_ef_no_configr@  b  sb   
LLN##2Ia2P $ D 					 4	5B>>779###==?uaj(((**2Ia2P + D 					 4	5B>>779###==?uaj(((  &6 7D					 4	5B>>779###==?uaj(((r'   c                    | j                          | j                  dt        d      dddii      }|J |j                  j	                  d	      }|J |j                         d
k(  sJ |j                         ddik(  sJ | j                  dt        d      dddii      }|J |j                  j	                  d	      }|J |j                         d
k(  sJ |j                         ddik(  sJ | j                  d      }|j                  j	                  d	      }|J |j                         d
k(  sJ |j                         ddik(  sJ y)zATest creating a collection with EF in parameter, no EF in config. test_ef_with_config_exists_no_efrb   rY   r   r   r   r@   rt   r   Nrt   rQ   rF   r   r=  r?  s      r$   rB  rB  ~  s|   
LLN##/2q923 $ D
 					 4	5B>>779###==?uaj(((**/2q923 + D
 					 4	5B>>779###==?uaj(((  &H ID					 4	5B>>779###==?uaj(((r'   c                    | j                          | j                  dt        d      ddit        d      d      }|J |j                  j	                  d	      }|J |j                         d
k(  sJ |j                         ddik(  sJ | j                  dt        d      ddit        d      d      }|J |j                  j	                  d	      }|J |j                         d
k(  sJ |j                         ddik(  sJ | j                  d      }|j                  j	                  d	      }|J |j                         d
k(  sJ |j                         ddik(  sJ y)zQTest creating a collection with EF in parameter, EF in config. They are the same."test_ef_with_config_exists_with_efrb   rY   r   r   r   rC  Nrt   rQ   rF   r   r=  r?  s      r$   (test_ef_with_config_exists_with_ef_validrF    s   
LLN##12q9h'"9a"@
 $ D 					 4	5B>>779###==?uaj(((**12q9h'"9a"@
 + D 					 4	5B>>779###==?uaj(((  &J KD					 4	5B>>779###==?uaj(((r'   c           	          | j                          t        j                  t              5  | j	                  dt        d      ddit        d      d       ddd       y# 1 sw Y   yxY w)	zRTest creating a collection with EF in parameter, EF in config. They are different.rE  rb   rY   r   r   r   rC  N)r~   r   r   r   r   rE   rf   ro   s    r$   1test_create_ef_with_config_exists_with_ef_invalidrI    s_    
LLN	z	" 
  561= (+&>1&E 	! 	

 
 
   .A!!A*c           	          | j                          t        j                  t              5  | j	                  dt        d      ddit        d      d       ddd       y# 1 sw Y   yxY w)	zJTest get_or_create with EF in parameter, EF in config. They are different.rE  rb   rY   r   r   r   rC  N)r~   r   r   r   r>  rE   rf   rH  s    r$   8test_get_or_create_ef_with_config_exists_with_ef_invalidrL    s_    
LLN	z	" 
''561= (+&>1&E 	( 	

 
 
rJ  c                     | j                          | j                  dddit        d      d       t        j                  t
              5  | j                  dt        d             d	d	d	       y	# 1 sw Y   y	xY w)
zKTest get_collection with EF in parameter, EF in config. They are different.rE  r   r   rb   rY   r   r   rs   N)r~   r   rf   r   r   r   r   rE   rH  s    r$   9test_get_collection_ef_with_config_exists_with_ef_invalidrN    sy     LLN
1h'":q"A
   
z	" 
561= 	 	

 
 
s   A33A<c           	      B   | j                          | j                  dt        d            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         ddik(  sJ t        j                  t              5  | j                  dt        d      dt        d      i	       ddd       t        j                  t              5  | j                  dt        d             ddd       y# 1 sw Y   IxY w# 1 sw Y   yxY w)
z
    After creating a collection with an embedding function,
    get_or_create should raise an error before and after retrieval, if they had provided
    a different embedding function or if it differs from the persisted one.
    'test_get_or_create_after_create_with_efrb   rY   rs   Nrt   rQ   rF   rC  )r~   r   rE   r   r   r@   rT   r   r   r   r>  rf   r?  s      r$   rP  rP    s    LLN##62q9 $ D 					 4	5B>>779###==?uaj(((	z	" 
'':7A>/1HQ1OP 	( 	

 
z	" 
'':7A> 	( 	

 

 

 
s   +D	"D	DDc                       e Zd ZddedefdZdedefdZe	defd       Z
deeef   fdZe	d	eeef   dd fd
       ZdefdZdee   fdZy)#DefaultSpaceCustomEmbeddingFunction
model_namerF   c                      || _         || _        y r    )rI   _model_name)r#   rS  rF   s      r$   r%   z,DefaultSpaceCustomEmbeddingFunction.__init__  s    	%r'   r(   r   c                     t        t        t        j                  dg| j                  z  gt        j
                              S rM   rN   r3   s     r$   r4   z,DefaultSpaceCustomEmbeddingFunction.__call__  rO   r'   c                       y)Ndefault_space_custom_efr!   r!   r'   r$   r@   z(DefaultSpaceCustomEmbeddingFunction.name  s    (r'   c                 4    | j                   | j                  dS )NrS  rF   )rU  rI   r"   s    r$   rT   z.DefaultSpaceCustomEmbeddingFunction.get_config  s    "..tyyAAr'   rV   c                 (    t        | d   | d         S )NrS  rF   rZ  )rR  rZ   s    r$   r[   z5DefaultSpaceCustomEmbeddingFunction.build_from_config"  s     3l+
 	
r'   c                 d    | j                   dk(  ry| j                   dk(  ry| j                   dk(  ryy)Ni_want_cosiner   	i_want_l2r^   	i_want_ipiprU  r"   s    r$   r_   z1DefaultSpaceCustomEmbeddingFunction.default_space*  s8    .,,r'   c                     | j                   dk(  rdgS | j                   dk(  rdgS | j                   dk(  rdgS | j                   dk(  rg dS g dS )	Nr]  r   r^  r^   r_  r`  i_want_anythingr   r^   r`  ra  r"   s    r$   supported_spacesz4DefaultSpaceCustomEmbeddingFunction.supported_spaces4  s]    .:,6M,6M!22))))r'   Nra   )r7   r8   r9   rC   rd   r%   r
   r   r4   rB   r@   r   r   rT   r[   r	   r_   r   re  r!   r'   r$   rR  rR    s    &3 &S &Qj QZ Q )# ) )BDcN B 
S#X
	.
 
u 
*$u+ 
*r'   rR  c                 p   | j                          | j                  dt        dd            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         dgk(  sJ t        r5|j                  j	                  d	      }|J |j	                  d
      dk(  s7J |j                  j	                  d      }|J |j	                  d
      dk(  sJ | j                  dt        dd            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         dgk(  sJ t        r5|j                  j	                  d	      }|J |j	                  d
      dk(  s7J |j                  j	                  d      }|J |j	                  d
      dk(  sJ | j                  dt        dd            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         dgk(  sJ t        r5|j                  j	                  d	      }|J |j	                  d
      dk(  s7J |j                  j	                  d      }|J |j	                  d
      dk(  sJ | j                  dt        dd            }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         g dk(  sJ t        r6|j                  j	                  d	      }|J |j	                  d
      dk(  sJ y |j                  j	                  d      }|J |j	                  d
      dk(  sJ y )N,test_default_space_custom_embedding_functionr]  rb   rZ  rs   rt   rX  r   r   r   r   /test_default_space_custom_embedding_function_l2r^  r^   /test_default_space_custom_embedding_function_ipr_  r`  5test_default_space_custom_embedding_function_anythingrc  rd  )r~   r   rR  r   r   r@   rT   r_   re  r   r>  ro   r   r   r   r   s        r$   6test_default_space_custom_embedding_function_no_configrl  A  s   
LLN##;>&A
 $ D 					 4	5B>>7791111==?_QGGGG))) XJ...((,,V4&&&w'8333))--g6'''(H444**>>"
 + D 					 4	5B>>7791111==?[CCCC%%% TF***((,,V4&&&w'4///))--g6'''(D000**>>"
 + D 					 4	5B>>7791111==?[CCCC%%% TF***((,,V4&&&w'4///))--g6'''(D000**D>(a
 + D 					 4	5B>>7791111==?->qIIII))) $::::((,,V4&&&w'8333))--g6'''(H444r'   c                    | j                          t        r$| j                  dt        dd      dddii      }n#| j                  dt        dd      d	ddii      }|J |j                  j                  d
      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         g dk(  sJ t        r6|j                  j                  d      }|J |j                  d      dk(  sJ y |j                  j                  d	      }|J |j                  d      dk(  sJ y )N>test_default_space_custom_embedding_function_with_valid_configrc  rb   rZ  r   r   r^   rC  r   rt   rX  r   rd  )
r~   r   r   rR  r   r   r@   rT   r_   re  rk  s        r$   rn  rn    s|    LLN''QB,!  "GT?3 ( 
 ''QB,!  #WdO4 ( 
 					 4	5B>>7791111==?->qIIII))) $::::((,,V4&&&w'4///))--g6'''(D000r'   c                 $   | j                          t        j                  d      5 }t        j                  d       t        r$| j                  dt        dd      dd	d
ii      }n#| j                  dt        dd      dd	d
ii      }t        |      dkD  sJ |D cg c]  }t        |j                         }}t        d |D              sJ |J |j                  j                  d      }|J |j                         dk(  sJ t        r5|j                  j                  d      }|J |j                  d	      d
k(  s7J |j                  j                  d      }|J |j                  d	      d
k(  sJ d d d        y c c}w # 1 sw Y   y xY w)NTrecordalways@test_default_space_custom_embedding_function_with_invalid_configr]  rb   rZ  r   r   r^   rC  r   r   c              3   @   K   | ]  }d |j                         v   ywzspace l2 is not supportedNlower.0msgs     r$   	<genexpr>zStest_default_space_custom_embedding_function_with_invalid_config.<locals>.<genexpr>  !      
;>'399;6
   rt   rX  )r~   warningscatch_warningssimplefilterr   r>  rR  r   rC   messageanyr   r   r@   ro   wr   warningwarning_messagesr   r   r   s           r$   rs  rs    s    LLN		 	 	- &5h'!22W#F.A$  &7 3 D 22W#F.A$  '$8 3 D 1vzz@ABWC0BB 
BR
 
 	
 
 ##$89~~wwy5555!,,008K***??7+t333--11':L+++##G,444M&5 &5* C+&5 &5s   A7FF:B>FFFc                 @   | j                          | j                  dt        dd      ddi      }|J |j                  j	                  d      }|J |j                         d	k(  sJ |j                         dddk(  sJ |j                         d
k(  sJ |j                         g dk(  sJ t        r6|j                  j	                  d      }|J |j	                  d      dk(  sJ y |j                  j	                  d      }|J |j	                  d      dk(  sJ y )N:test_default_space_custom_embedding_function_with_metadatarc  rb   rZ  r   r`  r@   rt   r   rt   rX  r   rd  r   r   r   
r~   r   rR  r   r   r@   rT   r_   re  r   rk  s        r$   r  r    s@    LLN##I>(a
 % $ D 					 4	5B>>7791111==?->qIIII))) $::::((,,V4&&&w'4///))--g6'''(D000r'   c                 R   | j                          t        j                  d      5 }t        j                  d       | j	                  dt        dd      dd	i
      }t        |      dkD  sJ |D cg c]  }t        |j                         }}t        d |D              sJ |J |j                  j                  d      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         dgk(  sJ t         r5|j                  j                  d      }|J |j                  d      d	k(  s7J |j                  j                  d      }|J |j                  d      d	k(  sJ d d d        y c c}w # 1 sw Y   y xY w)NTrp  rr  Btest_default_space_custom_embedding_function_with_invalid_metadatar]  rb   rZ  r   r^   r  r   c              3   @   K   | ]  }d |j                         v   ywru  rv  rx  s     r$   r{  zUtest_default_space_custom_embedding_function_with_invalid_metadata.<locals>.<genexpr>#  r|  r}  rt   rX  r   r   r   r   )r~   r~  r  r  r   rR  r   rC   r  r  r   r   r@   rT   r_   re  r   r  s           r$   r  r    s    LLN		 	 	-  5h'''UB*  #D) ( 
 1vzz@ABWC0BB 
BR
 
 	
 
 ##$89~~wwy5555}}"KKKK!X---""$
222!,,008K***??7+t333--11':L+++##G,444A 5  5 C 5  5s   AF2FDFFF&c                 J   | j                          | j                  ddddiit        dd      dd	i
      }|J |j                  j	                  d      }|J |j                         dk(  sJ |j                         dddk(  sJ |j                         dk(  sJ |j                         g dk(  sJ t        r6|j                  j	                  d      }|J |j	                  d      dk(  sJ y |j                  j	                  d      }|J |j	                  d      dk(  sJ y )NEtest_default_space_custom_embedding_function_with_metadata_and_configr   r   r`  rc  rb   rZ  r   r^   )r@   r   rt   r   rt   rX  r   rd  r   r  rk  s        r$   r  r  9  sL    LLN##T/>(a
 % $ D 					 4	5B>>7791111==?->qIIII))) $::::((,,V4&&&w'4///))--g6'''(D000r'   c                       e Zd ZU eed<   y)"CustomEmbeddingFunctionQueryConfigtaskN)r7   r8   r9   rC   __annotations__r!   r'   r$   r  r  X  s    
Ir'   r  c            
           e Zd Z	 	 ddedededee   fdZdede	fd	Z
dede	fd
Zedefd       Zdeeef   fdZedeeef   dd fd       ZdefdZdee   fdZy)&CustomEmbeddingFunctionWithQueryConfigNr  rS  rF   query_configc                 <    || _         || _        || _        || _        y r    )rI   rU  _task_query_config)r#   r  rS  rF   r  s        r$   r%   z/CustomEmbeddingFunctionWithQueryConfig.__init__^  s"     	%
)r'   r(   r   c                     t        t        t        j                  dg| j                  z  gt        j
                              S rM   rN   r3   s     r$   r4   z/CustomEmbeddingFunctionWithQueryConfig.__call__j  rO   r'   c                     | j                   `| j                   j                  d      dk(  rBt        t        t	        j
                  dg| j                  z  gt        j                              S | j                  |      S )Nr  r   r,   r-   )	r  r   r   r   r0   r1   rI   r2   r4   r3   s     r$   embed_queryz2CustomEmbeddingFunctionWithQueryConfig.embed_querym  s`    )d.@.@.D.DV.LPW.W
BHHsedii.?-@

$STT==''r'   c                       y)Ncustom_ef_with_query_configr!   r!   r'   r$   r@   z+CustomEmbeddingFunctionWithQueryConfig.names  s    ,r'   c                 `    | j                   | j                  | j                  | j                  dS )NrS  rF   r  r  )rU  rI   r  r  r"   s    r$   rT   z1CustomEmbeddingFunctionWithQueryConfig.get_configw  s,    **99JJ ..	
 	
r'   rV   c                     | j                  d      }| j                  d      }| j                  d      }| j                  d      }||J d       t        ||||      S )NrS  rF   r  r  zThis code should not be reachedr  )r   r  )rV   rS  rF   r  r  s        r$   r[   z8CustomEmbeddingFunctionWithQueryConfig.build_from_config  sg     ZZ-
jjzz&!zz.1;;;55!sL
 	
r'   c                      yNr   r!   r"   s    r$   r_   z4CustomEmbeddingFunctionWithQueryConfig.default_space  s    r'   c                     dgS r  r!   r"   s    r$   re  z7CustomEmbeddingFunctionWithQueryConfig.supported_spaces  s
    zr'   )rb   N)r7   r8   r9   rC   rd   r   r  r%   r
   r   r4   r  rB   r@   r   r   rT   r[   r	   r_   r   re  r!   r'   r$   r  r  \  s     EI
*
* 
* 	
*
 AB
*Qj QZ Q( (
 ( -# - -
DcN 
 
S#X
	1
 
u $u+ r'   r  c           
         | j                          | j                  dt        dddddi            }|J |j                  j	                  d	      }|J |j                         d
k(  sJ |j                         dddddidk(  sJ |j                         dk(  sJ |j                         dgk(  sJ t        j                  |j                  d      t        j                  g dgt        j                              sJ y )N0test_custom_embedding_function_with_query_configdocumentrc  rb   r  r   )r  rS  rF   r  rs   rt   r  r  r   How many people in Berlin?r(   )r,   r,   r,   r-   )r~   r   r  r   r   r@   rT   r_   re  r0   array_equalr  r1   r2   r?  s      r$   r  r    s   
LLN##?A( '*	
 $ D 					 4	5B>>7795555==?')	    ))) XJ...>>
9:
/""**5  r'   c                    d}t        t        j                  |            }|J |j                  d      J |j                  d      }|J |j	                         dddd dk(  sJ |j                         dk(  sJ |j                         dgk(  sJ t        j                  |j                  d	      t        j                  g d
gt        j                              sJ y )Nz
    {
        "embedding_function": {
            "type": "known",
            "name": "custom_ef_with_query_config",
            "config": {"model_name": "i_want_anything", "dim": 3, "task": "document"}
        }
    }
    rt   rc  rb   r  r  r   r  r  )r+   r+   r+   r-   )r   r   r   r   rT   r_   re  r0   r  r  r1   r2   )ro   json_stringrV   r   s       r$   Ntest_deserializing_custom_embedding_function_with_query_config_no_query_configr    s    K 5TZZ5LMF::*+777	(	)B>>==?'	    ))) XJ...>>
9:
/""**5  r'   )r   r   r   Nr6   )Wr   typingr   r   r   r   numpyr0   r~  chromadb.api.typesr   r   r	   r
   chromadb.apir   %chromadb.api.collection_configurationr   r   r   r   r   r   r   r   r   r   "chromadb.utils.embedding_functionsr   chromadb.test.conftestr   r   r   chromadb.typesr   r  r   r   r   r;   rE   rf   r   r   r   r   r   r   r   r   r   r   markskipifr   r   r   r   r   r   r  r  r	  r  r  r%  r(  r4  r:  r@  rB  rF  rI  rL  rN  rP  rR  rl  rn  rs  r  r  r  r  r  r  r  r!   r'   r$   <module>r     s    ( (    #
 
 
  J 2 U 8 &J/
; J	&7
&C 	 /
;  , 0<  ,$9 $ $<$Y $4 $<& &t &: 9    F5	 5d 5$,:I ,:$ ,:^)(X0Y 04 0.4"9 4" 4"n;"Y ;"4 ;"| *3MN( O(V
I 
$ 
49*) 9* 9*xM?) M? M?` *3MN*9Y *94 *9 O*9Z *3MN8:	 8:d 8: O8:v5@ *3MN19 1 1 O1>"Y "4 "2@,*6I *6$ *6Z>>	>@,,	,2H"H"	H"V-%9 -% -%`)i )D )8)Y )4 )@#)Y #)4 #)L
i 
D 

Y 
SW 


	
&
I 
$ 
> +**;J*G +* +*\\59 \5QU \5~$1$1	$1N*5*5	*5Z11	1<$5$5	$5N11	1>  6->z-J 6 6rY 4 :	r'   