
    Bvh                      t    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y)    )
Embeddings	DocumentsEmbeddingFunctionSpace)ListDictAnyOptionalNvalidate_config_schemac                   V   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y)OpenAIEmbeddingFunctionNapi_key
model_nameorganization_idapi_baseapi_typeapi_versiondeployment_iddefault_headers
dimensionsapi_key_env_varc                    	 ddl }|t        j                  dt
               |
| _        |xs t        j                  |
      | _	        | j                  st        d|
 d      || _
        || _        || _        || _        || _        || _        || _        |	| _        d| j                  i}| j                  | j                  |d<   | j                  | j                  |d	<   | j                   | j                   |d
<    |j$                  di || _        | j                  dk(  r| j                  t        d      | j                  t        d      | j                  t        d      ddl m}  || j                  | j                  | j                  | j                  | j                         | _        yy# t        $ r t        d      w xY w)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.z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DeprecationWarningr   osgetenvr   r   r   r   r   r   r   r   r   OpenAIclientr   )selfr   r   r   r   r   r   r   r   r   r   r!   client_paramsr   s                 f/RAG/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/openai_embedding_function.py__init__z OpenAIEmbeddingFunction.__init__
   s   H	 MM_"  /<"))O"<||tO#44UVWW$.  &*.$ *3DLL(A+,0,@,@M.)==$(,M*%+/3/C/CM+,#fmm4m4 ==G#' !QRR!!) !STT}}$ !NOO*% ,,#}}!%!3!3 $ 4 4DK $O  	i 	s   F0 0Ginputreturnc                 ^   |sg S | j                   |d}| j                  d| j                   v r| j                  |d<    | j                  j                  j                  di |}|j
                  D cg c]1  }t        j                  |j                  t        j                        3 c}S c c}w )z
        Generate embeddings for the given documents.
        Args:
            input: Documents to generate embeddings for.
        Returns:
            Embeddings for the documents.
        )modelr/   ztext-embedding-3r   )dtyper    )
r   r   r*   
embeddingscreatedatanparray	embeddingfloat32)r+   r/   embedding_paramsresponser6   s        r-   __call__z OpenAIEmbeddingFunction.__call__i   s     I __,

 ??&+=+P-1__\* 14;;))00D3CD HP}}U}trzz:}UUUs   16B*c                       y)Nr!   r    r        r-   namezOpenAIEmbeddingFunction.name       r?   c                      y)Ncosiner    r+   s    r-   default_spacez%OpenAIEmbeddingFunction.default_space   rA   r?   c                 
    g dS )N)rC   l2ipr    rD   s    r-   supported_spacesz(OpenAIEmbeddingFunction.supported_spaces   s    %%r?   configzEmbeddingFunction[Documents]c                 r   | j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d	      }	||J d
       t        |||||||||		      S )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   )getr   )
rJ   r   r   r   r   r   r   r   r   r   s
             r-   build_from_configz)OpenAIEmbeddingFunction.build_from_config   s     !**%67ZZ-
 **%67::j)::j)jj/

?3 **%67ZZ-
"j&8;;;5 '+!+#'+!

 
	
r?   c           
          | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  d	S )NrL   rL   rD   s    r-   
get_configz"OpenAIEmbeddingFunction.get_config   sU    #33//#33++!//#33//

 
	
r?   
old_config
new_configc                 "    d|v rt        d      y )Nr   zSThe model name cannot be changed after the embedding function has been initialized.)r#   )r+   rQ   rR   s      r-   validate_config_updatez.OpenAIEmbeddingFunction.validate_config_update   s!     :%e  &r?   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
        r!   Nr   )rJ   s    r-   validate_configz'OpenAIEmbeddingFunction.validate_config   s     	vx0r?   )
Nztext-embedding-ada-002NNNNNNNCHROMA_OPENAI_API_KEY)__name__
__module____qualname__r
   strr   intr.   r   r   r=   staticmethodr@   r   rE   r   rI   r	   rN   rP   rT   rV   r    r?   r-   r   r   	   s    "&2)-"&"&%)'+48$(6]#] ] "#	]
 3-] 3-] c]]  }] "$sCx.1] SM] ]~Vi VJ V6 #  u &$u+ & 
$sCx. 
5S 
 
8
DcN 
sCx.6:38n	 
1S#X 
14 
1 
1r?   r   )chromadb.api.typesr   r   r   r   typingr   r   r	   r
   r'   numpyr7   *chromadb.utils.embedding_functions.schemasr   r$   r   r    r?   r-   <module>rb      s1    N N , , 	  M C1/	: C1r?   