
    ugo                     t    d dl Z d dlmZmZmZ d dlmZmZmZ  e j                  e
      Z G d dee         Zy)    N)MappingOptionalcast)	DocumentsEmbeddingFunction
Embeddingsc                       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   fdZdede	fdZ
y)OpenAIEmbeddingFunctionNapi_key
model_nameorganization_idapi_baseapi_typeapi_versiondeployment_iddefault_headers
dimensionsc
                 H   	 ddl }
|xs |
j                  | _        | j                  t        d      |||
_        |||
_        || _        |||
_        |||
_	        |
j                  j                  d      | _        | j                  rN|dk(  r%|
j                  ||||      j                  | _        n5|
j!                  |||      j                  | _        n|
j"                  | _        || _        || _        |	xs |
j(                  | _        y# t        $ r t        d      w xY w)	al  
        Initialize the OpenAIEmbeddingFunction.
        Args:
            api_key (str, optional): Your API key for the OpenAI API. If not
                provided, it will raise an error to provide an OpenAI API key.
            organization_id(str, optional): The OpenAI organization ID if applicable
            model_name (str, optional): The name of the model to use for text
                embeddings. Defaults to "text-embedding-ada-002".
            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 (Mapping, 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`zaPlease provide an OpenAI API key. You can get one at https://platform.openai.com/account/api-keysz1.azure)r   r   azure_endpointr   )r   base_urlr   )openaiImportError
ValueErrorr   _api_keyr   r   	_api_typer   organization__version__
startswith_v1AzureOpenAI
embeddings_clientOpenAI	Embedding_model_name_deployment_id	NOT_GIVEN_dimensions)selfr   r   r   r   r   r   r   r   r   r   s              q/var/www/openai/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/openai_embedding_function.py__init__z OpenAIEmbeddingFunction.__init__
   sD   H	  16>>== s  &FO"!,F!&FO&"1F%%006887"%11# +#+$3	  2  
 *   &}}#h  -  *  "++DL%+%9)9)9U  	i 	s   D D!inputreturnc                    |D cg c]  }|j                  dd       }}| j                  r| j                  j                  || j                  xs | j
                  | j                        j                  }t        |d       }t        t        |D cg c]  }|j                   c}      S | j                  dk(  r9| j                  j                  || j                  xs | j
                        d   }n*| j                  j                  || j
                  	      d   }t        |d
       }t        t        |D cg c]  }|d   	 c}      S c c}w c c}w c c}w )aB  
        Generate the embeddings for the given `input`.

        # About ignoring types
        We are not enforcing the openai library, therefore, `mypy` has hard times trying
        to figure out what the types are for `self._client.create()` which throws an
        error when trying to sort the list. If, eventually we include the `openai` lib
        we can remove the type ignore tag.

        Args:
            input (Documents): A list of texts to get embeddings for.

        Returns:
            Embeddings: The embeddings for the given input sorted by index
        
 )r-   modelr   c                     | j                   S )N)indexes    r+   <lambda>z2OpenAIEmbeddingFunction.__call__.<locals>.<lambda>y   s    !''    )keyr   )r-   enginedata)r-   r2   c                     | d   S )Nr4    r5   s    r+   r7   z2OpenAIEmbeddingFunction.__call__.<locals>.<lambda>   s    !G*r8   	embedding)replacer    r#   creater'   r&   r)   r;   sortedr   r   r>   r   )r*   r-   tr"   sorted_embeddingsresults         r+   __call__z OpenAIEmbeddingFunction.__call__\   se   " 055u!4%u5 88,,))=T-=-=++ -  d	  !' 1!
 
DU$VDU&V%5%5DU$VWW~~(!\\00(;(;(Ot?O?O 1 
 "\\00uDDTDT0U

 !' 4!
 ?PQ?PVVK0?PQ C 6  %W$ Rs   EE
:E
)	Nztext-embedding-ada-002NNNNNNN)__name__
__module____qualname__r   strr   intr,   r   r   rE   r=   r8   r+   r
   r
   	   s     "&2)-"&"&%)'+7;$(P:#P: P: "#	P:
 3-P: 3-P: c]P:  }P: "'#s("34P: SMP:d4i 4J 4r8   r
   )loggingtypingr   r   r   chromadb.api.typesr   r   r   	getLoggerrF   loggerr
   r=   r8   r+   <module>rP      s:     * * G G			8	$G/	: Gr8   