
    3i&                         S SK JrJrJrJrJrJr  S SKJr  S SK	J
r
JrJrJrJrJr  S SKrS SKrS SKrS SKrS SKrS SKr " S S\5      r " S S\\   5      rg)	    )
EmbeddingsEmbeddingFunctionSpace
Embeddableis_imageis_documentvalidate_config_schema)ListDictAnyUnionOptional	TypedDictNc                        \ rS rSr% \\S'   Srg)JinaQueryConfig   task N)__name__
__module____qualname____firstlineno__str__annotations____static_attributes__r       t/home/james-whalen/.local/lib/python3.13/site-packages/chromadb/utils/embedding_functions/jina_embedding_function.pyr   r      s    
Ir   r   c                      \ rS rSrSr          S#S\\   S\S\S\\   S\\   S	\\   S
\\   S\\   S\\   S\\	   4S jjr
S\S\S\\\4   4S jrS$S\S\S\4S jjrS\S\4S jrS\S\4S jr\S\4S j5       rS\4S jrS\\   4S jr\S\\\4   SS4S j5       rS\\\4   4S jrS\\\4   S\\\4   SS4S  jr\S\\\4   SS4S! j5       rS"rg)%JinaEmbeddingFunction   z
This class is used to get embeddings for a list of texts using the Jina AI API.
It requires an API key and a model name. The default model name is "jina-embeddings-v2-base-en".
Napi_key
model_nameapi_key_env_varr   late_chunkingtruncate
dimensionsembedding_type
normalizedquery_configc                 |    SSK n [        R                  " S5      U l        Ub  [        R                  " S[        5        X0l	        U=(       d    [        R                  " U5      U l        U R                  (       d  [        SU S35      eX l        X@l        XPl        X`l        Xpl        Xl        Xl        Xl        S	U l        UR-                  5       U l        U R.                  R0                  R3                  S
U R                   3SS.5        g! [         a    [        S5      ef = f! [         a    [        S5      ef = f)a  
Initialize the JinaEmbeddingFunction.

Args:
    api_key_env_var (str, optional): Environment variable name that contains your API key for the Jina AI API.
        Defaults to "CHROMA_JINA_API_KEY".
    model_name (str, optional): The name of the model to use for text embeddings.
        Defaults to "jina-embeddings-v2-base-en".
    task (str, optional): The task to use for the Jina AI API.
        Defaults to None.
    late_chunking (bool, optional): Whether to use late chunking for the Jina AI API.
        Defaults to None.
    truncate (bool, optional): Whether to truncate the Jina AI API.
        Defaults to None.
    dimensions (int, optional): The number of dimensions to use for the Jina AI API.
        Defaults to None.
    embedding_type (str, optional): The type of embedding to use for the Jina AI API.
        Defaults to None.
    normalized (bool, optional): Whether to normalize the Jina AI API.
        Defaults to None.

r   NzUThe httpx python package is not installed. Please install it with `pip install httpx`z	PIL.ImagezTThe PIL python package is not installed. Please install it with `pip install pillow`zDirect api_key configuration will not be persisted. Please use environment variables via api_key_env_var for persistent storage.zThe z! environment variable is not set.z!https://api.jina.ai/v1/embeddingszBearer identity)AuthorizationzAccept-Encoding)httpxImportError
ValueError	importlibimport_module	_PILImagewarningswarnDeprecationWarningr$   osgetenvr"   r#   r   r%   r&   r'   r(   r)   r*   _api_urlClient_sessionheadersupdate)selfr"   r#   r$   r   r%   r&   r'   r(   r)   r*   r.   s               r   __init__JinaEmbeddingFunction.__init__   s'   F	
	&44[ADN MM_"  /<"))O"<||tO#44UVWW$ 	* $,$(;$$ '~6:V	
I  	g 	  	f 	s   D D% D"%D;inputis_queryreturnc                    / U R                   S.n[        S U 5       5      (       a  XS'   OU H  n[        U5      (       a  US   R                  SU05        M+  [	        U5      (       d  M=   U R
                  R                  U5      n[        R                  " 5       nUR                  USS9  UR                  5       n[        R                  " U5      R                  S5      nUS   R                  S	U05        M     U R                   b  U R                   US
'   U R"                  b  U R"                  US'   U R$                  b  U R$                  US'   U R&                  b  U R&                  US'   U R(                  b  U R(                  US'   U R*                  b  U R*                  US'   U(       a4  U R,                  b'  U R,                  R/                  5        H	  u  pXU
'   M     U$ ! [         a  n	[        SU	 35      U	eS n	A	ff = f)N)rA   modelc              3   8   #    U  H  n[        U5      v   M     g 7f)N)r   ).0items     r   	<genexpr>7JinaEmbeddingFunction._build_payload.<locals>.<genexpr>o   s     3UT{4  Us   rA   textPNG)formatzutf-8z8Failed to convert image numpy array to base64 data URI: imager   r%   r&   r'   r(   r)   )r#   allr   appendr   r3   	fromarrayioBytesIOsavegetvaluebase64	b64encodedecode	Exceptionr0   r   r%   r&   r'   r(   r)   r*   items)r>   rA   rB   payloadrH   	pil_imagebuffer	img_bytesbase64_stringekeyvalues               r   _build_payload$JinaEmbeddingFunction._build_payloadj   s   __#
 3U333$Gt$$G$++VTN;d^^!$(NN$<$<T$B	!#!ve<$*OO$5	 )/(8(8(C(J(J7(S G$++Wm,DE% ( 99 "iiGFO)'+'9'9GO$==$"&--GJ??&$(OOGL!*(,(;(;G$%??&$(OOGL! ))5"//557
$ 8 1 % !(VWXVYZ !!s   ,A5G  
G>*G99G>respc                     SU;  a  [        UR                  SS5      5      eUS   n[        US S9nU Vs/ s H)  n[        R                  " US   [        R
                  S9PM+     sn$ s  snf )z
Convert the response from the Jina AI API to a list of numpy arrays.

Args:
    resp (Any): The response from the Jina AI API.

Returns:
    Embeddings: A list of numpy arrays representing the embeddings.
datadetailzUnknown errorc                     U S   $ )Nindexr   )r`   s    r   <lambda>5JinaEmbeddingFunction._convert_resp.<locals>.<lambda>   s    !G*r   )ra   	embedding)dtype)RuntimeErrorgetsortednparrayfloat32)r>   re   rB   embeddings_datasorted_embeddingsresults         r   _convert_resp#JinaEmbeddingFunction._convert_resp   sv     txx/BCCDHL #?8LM
 ,
+ HHVK(

;+
 	
 
s   0A)c                     U R                  USS9nU R                  R                  U R                  USS9R	                  5       nU R                  U5      $ )aC  
Get the embeddings for a list of texts.

Args:
    input (Embeddable): A list of texts and/or images to get embeddings for.

Returns:
    Embeddings: The embeddings for the texts.

Example:
    >>> jina_ai_fn = JinaEmbeddingFunction(api_key_env_var="CHROMA_JINA_API_KEY")
    >>> input = ["Hello, world!", "How are you?"]
FrB   <   jsontimeoutrc   r;   postr9   r~   rx   r>   rA   r[   re   s       r   __call__JinaEmbeddingFunction.__call__   sT     %%ee%< }}!!$--gr!JOOQ!!$''r   c                     U R                  USS9nU R                  R                  U R                  USS9R	                  5       nU R                  USS9$ )NTr{   r|   r}   r   r   s       r   embed_query!JinaEmbeddingFunction.embed_query   sW    %%ed%; }}!!$--gr!JOOQ!!$!66r   c                      g)Njinar   r   r   r   nameJinaEmbeddingFunction.name   s    r   c                     g)Ncosiner   r>   s    r   default_space#JinaEmbeddingFunction.default_space   s    r   c                 
    / SQ$ )N)r   l2ipr   r   s    r   supported_spaces&JinaEmbeddingFunction.supported_spaces   s    %%r   configzEmbeddingFunction[Embeddable]c                 p   U R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU R                  S5      nU R                  S	5      n	Ub  Uc   S
5       e[        UUUUUUUUU	S9	$ )Nr$   r#   r   r%   r&   r'   r(   r)   r*   zThis code should not be reached	r$   r#   r   r%   r&   r'   r(   r)   r*   )rp   r    )
r   r$   r#   r   r%   r&   r'   r(   r)   r*   s
             r   build_from_config'JinaEmbeddingFunction.build_from_config   s     **%67ZZ-
zz&!

?3::j)ZZ-
$45ZZ-
zz.1"j&8;;;5$+!'!)!%

 
	
r   c           
          U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  S.	$ )Nr   r   r   s    r   
get_config JinaEmbeddingFunction.get_config   sS    #33//II!////"11// --

 
	
r   
old_config
new_configc                 &    SU;   a  [        S5      eg )Nr#   zSThe model name cannot be changed after the embedding function has been initialized.)r0   )r>   r   r   s      r   validate_config_update,JinaEmbeddingFunction.validate_config_update  s!     :%e  &r   c                     [        U S5        g)z
Validate the configuration using the JSON schema.

Args:
    config: Configuration to validate

Raises:
    ValidationError: If the configuration does not match the schema
r   Nr	   )r   s    r   validate_config%JinaEmbeddingFunction.validate_config
  s     	vv.r   )r3   r9   r;   r"   r$   r'   r(   r%   r#   r)   r*   r   r&   )
Nzjina-embeddings-v2-base-enCHROMA_JINA_API_KEYNNNNNNN)F)r   r   r   r   __doc__r   r   boolintr   r?   r   r   r   rc   r   rx   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r    r       s    "&64"(,#'$((,%)26K
#K
 K
 	K

 smK
  ~K
 4.K
 SMK
 !K
 TNK
 /K
Z.J .$ .4S> .`
# 
 
* 
0(j (Z (,7 7
 7 #  u &$u+ & 
$sCx. 
5T 
 
4
DcN 
sCx.6:38n	 
/S#X 
/4 
/ 
/r   r    )chromadb.api.typesr   r   r   r   r   r   *chromadb.utils.embedding_functions.schemasr
   typingr   r   r   r   r   r   r7   numpyrr   r4   r1   rV   rR   r   r    r   r   r   <module>r      sM     N > > 	     	i ~/-j9 ~/r   