
    Bvh                         d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZmZmZmZmZmZ d dlZd dlZd dlZd dlmZ d dlZd dlZ G d de	e         Zy)    validate_config_schema)	Documents
EmbeddingsImagesis_documentis_image
EmbeddableEmbeddingFunctionSpace)ListDictAnyUnioncastOptionalN)BytesIOc            	           e Zd ZdZ	 	 	 ddee   dededdfdZdedefd	Z	e
defd
       ZdefdZdee   fdZe
deeef   ddfd       Zdeeef   fdZdeeef   deeef   ddfdZe
deeef   ddfd       Zy)RoboflowEmbeddingFunctionzi
    This class is used to generate embeddings for a list of texts or images using the Roboflow API.
    Napi_keyapi_urlapi_key_env_varreturnc                 d   |t        j                  dt               || _        |xs t	        j
                  |      | _        | j                  st        d| d      || _        	 t        j                  d      | _        t        j                  d      | _        y# t        $ r t        d      w xY w)ao  
        Create a RoboflowEmbeddingFunction.

        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the Roboflow API.
                Defaults to "CHROMA_ROBOFLOW_API_KEY".
            api_url (str, optional): The URL of the Roboflow API.
                Defaults to "https://infer.roboflow.com".
        NzDirect 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	PIL.ImagezTThe PIL python package is not installed. Please install it with `pip install pillow`httpx)warningswarnDeprecationWarningr   osgetenvr   
ValueErrorr   	importlibimport_module	_PILImageImportError_httpx)selfr   r   r   s       h/RAG/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/roboflow_embedding_function.py__init__z"RoboflowEmbeddingFunction.__init__   s      MM_"
  /<"))O"<||tO#44UVWW	&44[ADN  --g6  	f 	s   %B B/inputc                 V   g }|D ]  }t        |      r| j                  j                  |      }t               }|j	                  |d       t        j                  |j                               j                  d      }dd|di}| j                  j                  | j                   d| j                   |      }|j                         d	   }	|j                  t        j                   |	d
   t        j"                               t%        |      sd|i}
| j                  j                  | j                   d| j                   |
      }|j                         d	   }	|j                  t        j                   |	d
   t        j"                                t'        t(        |      S )z
        Generate embeddings for the given documents or images.

        Args:
            input: Documents or images to generate embeddings for.

        Returns:
            Embeddings for the documents or images.
        JPEG)formatzutf-8imagebase64)typevaluez/clip/embed_image?api_key=)json
embeddingsr   )dtypetextz/clip/embed_text?api_key=)r	   r$   	fromarrayr   saver/   	b64encodegetvaluedecoder&   postr   r   r2   appendnparrayfloat32r   r   r   )r'   r*   r3   itemr.   bufferbase64_imageinfer_clip_payload_imageresresultinfer_clip_payload_texts              r(   __call__z"RoboflowEmbeddingFunction.__call__@   su    
D~006 

6&
1%//0ABII'R  (!-,( kk&&||n$>t||nM1 ' 
 L1!!"((6!9BJJ"GHT"D+' kk&&||n$=dll^L0 ' 
 L1!!"((6!9BJJ"GHE J J
++    c                       y)Nroboflow rK   rH   r(   namezRoboflowEmbeddingFunction.names   s    rH   c                      y)NcosinerK   r'   s    r(   default_spacez'RoboflowEmbeddingFunction.default_spacew   s    rH   c                 
    g dS )N)rN   l2iprK   rO   s    r(   supported_spacesz*RoboflowEmbeddingFunction.supported_spacesz   s    %%rH   configz+EmbeddingFunction[Union[Documents, Images]]c                 v    | j                  d      }| j                  d      }||J d       t        ||      S )Nr   r   zThis code should not be reachedr   r   )getr   )rU   r   r   s      r(   build_from_configz+RoboflowEmbeddingFunction.build_from_config}   sH     !**%67**Y'"go;;;5(+W
 	
rH   c                 4    | j                   | j                  dS )NrW   rW   rO   s    r(   
get_configz$RoboflowEmbeddingFunction.get_config   s    #'#7#7DLLQQrH   
old_config
new_configc                      y )NrK   )r'   r\   r]   s      r(   validate_config_updatez0RoboflowEmbeddingFunction.validate_config_update   s     	rH   c                     t        | d       y)z
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        rJ   Nr   )rU   s    r(   validate_configz)RoboflowEmbeddingFunction.validate_config   s     	vz2rH   )Nzhttps://infer.roboflow.comCHROMA_ROBOFLOW_API_KEY)__name__
__module____qualname____doc__r   strr)   r
   r   rG   staticmethodrL   r   rP   r   rT   r   r   rY   r[   r_   ra   rK   rH   r(   r   r      s&    "&38	$7#$7 $7 	$7
 
$7L1,j 1,Z 1,f #  u &$u+ & 
S#X
	6
 
RDcN RsCx.6:38n	 
3S#X 
34 
3 
3rH   r   )*chromadb.utils.embedding_functions.schemasr   chromadb.api.typesr   r   r   r   r	   r
   r   r   typingr   r   r   r   r   r   r   r"   r/   ior   numpyr=   r   r   rK   rH   r(   <module>rn      sE    M	 	 	 : 9 	     J3 1* = J3rH   