
    _j                          d dl mZmZmZm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 d dlZ G d dee                   ZdS )    )
Embeddings	DocumentsEmbeddingFunctionSpace)ListDictAnyOptionalNvalidate_config_schemac                      e Zd Z	 	 	 	 	 	 	 	 	 	 ddee         dedee         dee         dee         d	ee         d
ee         deeeef                  dee         de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dS )OpenAIEmbeddingFunctionNtext-embedding-ada-002CHROMA_OPENAI_API_KEYapi_key
model_nameorganization_idapi_baseapi_typeapi_versiondeployment_iddefault_headers
dimensionsapi_key_env_varc                 Z   	 ddl }n# t          $ r t          d          w xY w|t          j        dt
                     t          j        d          d| _        n|
| _        |pt          j        | j                  | _	        | j	        st          d| j         d          || _
        || _        || _        || _        || _        || _        || _        |	| _        d| j	        i}| j        
| j        |d	<   | j        
| j        |d
<   | j        
| j        |d<    |j        di || _        | j        dk    rx| j        t          d          | j        t          d          | j        t          d          ddl m}  || j	        | j        | j        | j        | j                  | _        dS dS )a  
        Initialize the OpenAIEmbeddingFunction.
        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the OpenAI API.
                Defaults to "CHROMA_OPENAI_API_KEY".
            model_name (str, optional): The name of the model to use for text
                embeddings. Defaults to "text-embedding-ada-002".
            organization_id(str, optional): The OpenAI organization ID if applicable
            api_base (str, optional): The base path for the API. If not provided,
                it will use the base path for the OpenAI API. This can be used to
                point to a different deployment, such as an Azure deployment.
            api_type (str, optional): The type of the API deployment. This can be
                used to specify a different deployment, such as 'azure'. If not
                provided, it will use the default OpenAI deployment.
            api_version (str, optional): The api version for the API. If not provided,
                it will use the api version for the OpenAI API. This can be used to
                point to a different deployment, such as an Azure deployment.
            deployment_id (str, optional): Deployment ID for Azure OpenAI.
            default_headers (Dict[str, str], optional): A mapping of default headers to be sent with each API request.
            dimensions (int, optional): The number of dimensions for the embeddings.
                Only supported for `text-embedding-3` or later models from OpenAI.
                https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-dimensions
        r   NzWThe openai python package is not installed. Please install it with `pip install openai`zDirect api_key configuration will not be persisted. Please use environment variables via api_key_env_var for persistent storage.OPENAI_API_KEYzThe z! environment variable is not set.r   organizationbase_urlr   azurez.api_version must be specified for Azure OpenAIz0deployment_id must be specified for Azure OpenAIz+api_base must be specified for Azure OpenAI)AzureOpenAI)r   r   azure_endpointazure_deploymentr    )openaiImportError
ValueErrorwarningswarnDeprecationWarningosgetenvr   r   r   r   r   r   r   r   r   r   OpenAIclientr    )selfr   r   r   r   r   r   r   r   r   r   r$   client_paramsr    s                 y/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/utils/embedding_functions/openai_embedding_function.py__init__z OpenAIEmbeddingFunction.__init__
   s"   H	MMMM 	 	 	i  	
 M_"   9%&&2#3D  #2D A")D,@"A"A| 	Nt+NNN   %.  &*.$ *3DL(A+,0,@M.)=$(,M*%+/3/CM+,#fm44m44 =G##' !QRRR!) !STTT}$ !NOOO******%+ ,#}!%!3 $ 4  DKKK $#s    !inputreturnc                     |sg S | j         |d}| j        d| j         v r
| j        |d<    | j        j        j        di |}d |j        D             S )z
        Generate embeddings for the given documents.
        Args:
            input: Documents to generate embeddings for.
        Returns:
            Embeddings for the documents.
        )modelr2   Nztext-embedding-3r   c                 X    g | ]'}t          j        |j        t           j                   (S ))dtype)nparray	embeddingfloat32).0datas     r0   
<listcomp>z4OpenAIEmbeddingFunction.__call__.<locals>.<listcomp>   s+    UUUtrz:::UUU    r#   )r   r   r-   
embeddingscreater=   )r.   r2   embedding_paramsresponses       r0   __call__z OpenAIEmbeddingFunction.__call__o   s      	I _,
 ,

 ?&+=+P+P-1_\* 14;)0DD3CDD VUx}UUUUr?   c                      dS )Nr$   r#   r#   r?   r0   namezOpenAIEmbeddingFunction.name       xr?   c                     dS )Ncosiner#   r.   s    r0   default_spacez%OpenAIEmbeddingFunction.default_space   rG   r?   c                 
    g dS )N)rI   l2ipr#   rJ   s    r0   supported_spacesz(OpenAIEmbeddingFunction.supported_spaces   s    %%%%r?   configzEmbeddingFunction[Documents]c                    |                      d          }|                      d          }|                      d          }|                      d          }|                      d          }|                      d          }|                      d          }|                      d          }|                      d	          }	||
J d            t          |||||||||		  	        S )Nr   r   r   r   r   r   r   r   r   FzThis code should not be reached	r   r   r   r   r   r   r   r   r   )getr   )
rP   r   r   r   r   r   r   r   r   r   s
             r0   build_from_configz)OpenAIEmbeddingFunction.build_from_config   s     !**%677ZZ--
 **%677::j))::j))jj//

?33 **%677ZZ--
"j&8;;;;; '+!+#'+!

 

 

 
	
r?   c           
      t    | j         | j        | j        | j        | j        | j        | j        | j        | j        d	S )NrR   rR   rJ   s    r0   
get_configz"OpenAIEmbeddingFunction.get_config   sC    #3/#3+!/#3/

 

 
	
r?   
old_config
new_configc                 ,    d|v rt          d          d S )Nr   zSThe model name cannot be changed after the embedding function has been initialized.)r&   )r.   rW   rX   s      r0   validate_config_updatez.OpenAIEmbeddingFunction.validate_config_update   s,     :%%e   &%r?   c                 &    t          | d           dS )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   )rP   s    r0   validate_configz'OpenAIEmbeddingFunction.validate_config   s     	vx00000r?   )
Nr   NNNNNNNr   )__name__
__module____qualname__r
   strr   intr1   r   r   rD   staticmethodrF   r   rK   r   rO   r	   rT   rV   rZ   r\   r#   r?   r0   r   r   	   s=        "&2)-"&"&%)'+48$(6c c#c c "#	c
 3-c 3-c c]c  }c "$sCx.1c SMc c c c cJVi VJ V V V V6 #    \u    &$u+ & & & & 
$sCx. 
5S 
 
 
 \
8
DcN 
 
 
 
sCx.6:38n	    
1S#X 
14 
1 
1 
1 \
1 
1 
1r?   r   )chromadb.api.typesr   r   r   r   typingr   r   r	   r
   r*   numpyr8   *chromadb.utils.embedding_functions.schemasr   r'   r   r#   r?   r0   <module>rg      s    N N N N N N N N N N N N , , , , , , , , , , , , 				     M M M M M M I1 I1 I1 I1 I1/	: I1 I1 I1 I1 I1r?   