|
| 1 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | + |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import os |
| 19 | +from typing import Any, Union |
| 20 | + |
| 21 | +from openai import AzureOpenAI |
| 22 | + |
| 23 | +from camel.embeddings.base import BaseEmbedding |
| 24 | +from camel.types import EmbeddingModelType |
| 25 | +from camel.utils import api_keys_required # Add this import |
| 26 | + |
| 27 | + |
| 28 | +class AzureEmbedding(BaseEmbedding[str]): |
| 29 | + r"""Provides text embedding functionalities using Azure's OpenAI models. |
| 30 | +
|
| 31 | + Args: |
| 32 | + model_type (EmbeddingModelType, optional): The model type to be |
| 33 | + used for text embeddings. |
| 34 | + (default: :obj:`TEXT_EMBEDDING_ADA_2`) |
| 35 | + url (Optional[str], optional): The url to the Azure OpenAI service. |
| 36 | + (default: :obj:`None`) |
| 37 | + api_key (str, optional): The API key for authenticating with the |
| 38 | + Azure OpenAI service. (default: :obj:`None`) |
| 39 | + api_version (str, optional): The API version for Azure OpenAI service. |
| 40 | + (default: :obj:`None`) |
| 41 | + dimensions (Optional[int], optional): The text embedding output |
| 42 | + dimensions. (default: :obj:`None`) |
| 43 | +
|
| 44 | + Raises: |
| 45 | + RuntimeError: If an unsupported model type is specified. |
| 46 | + ValueError: If required API configuration is missing. |
| 47 | + """ |
| 48 | + |
| 49 | + @api_keys_required( |
| 50 | + [ |
| 51 | + ("api_key", 'AZURE_OPENAI_API_KEY'), |
| 52 | + ("url", 'AZURE_OPENAI_BASE_URL'), |
| 53 | + ] |
| 54 | + ) |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + model_type: EmbeddingModelType = ( |
| 58 | + EmbeddingModelType.TEXT_EMBEDDING_3_SMALL |
| 59 | + ), |
| 60 | + url: Union[str, None] = None, |
| 61 | + api_key: Union[str, None] = None, |
| 62 | + api_version: Union[str, None] = None, |
| 63 | + dimensions: Union[int, None] = None, |
| 64 | + ) -> None: |
| 65 | + self.model_type = model_type |
| 66 | + self.api_version = api_version or os.environ.get("AZURE_API_VERSION") |
| 67 | + if dimensions is None: |
| 68 | + self.output_dim = model_type.output_dim |
| 69 | + else: |
| 70 | + if not isinstance(dimensions, int): |
| 71 | + raise ValueError("dimensions must be an integer") |
| 72 | + self.output_dim = dimensions |
| 73 | + |
| 74 | + self._api_key = api_key or os.environ.get("AZURE_OPENAI_API_KEY") |
| 75 | + self._url = url or os.environ.get("AZURE_OPENAI_BASE_URL") |
| 76 | + |
| 77 | + self.client = AzureOpenAI( |
| 78 | + api_key=self._api_key, |
| 79 | + api_version=self.api_version, |
| 80 | + azure_endpoint=str(self._url), |
| 81 | + ) |
| 82 | + |
| 83 | + def embed_list( |
| 84 | + self, |
| 85 | + objs: list[str], |
| 86 | + **kwargs: Any, |
| 87 | + ) -> list[list[float]]: |
| 88 | + r"""Embeds a list of texts using the Azure OpenAI model. |
| 89 | +
|
| 90 | + Args: |
| 91 | + objs (list[str]): The list of texts to embed. |
| 92 | + **kwargs (Any): Additional keyword arguments to pass to the API. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + list[list[float]]: The embeddings for the input texts. |
| 96 | + """ |
| 97 | + if self.model_type == EmbeddingModelType.TEXT_EMBEDDING_ADA_2: |
| 98 | + response = self.client.embeddings.create( |
| 99 | + input=objs, |
| 100 | + model=self.model_type.value, |
| 101 | + **kwargs, |
| 102 | + ) |
| 103 | + return [data.embedding for data in response.data] |
| 104 | + |
| 105 | + response = self.client.embeddings.create( |
| 106 | + input=objs, |
| 107 | + model=self.model_type.value, |
| 108 | + dimensions=self.output_dim, |
| 109 | + **kwargs, |
| 110 | + ) |
| 111 | + return [data.embedding for data in response.data] |
| 112 | + |
| 113 | + def get_output_dim(self) -> int: |
| 114 | + r"""Returns the output dimension of the embeddings. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + int: The dimensionality of the embedding for the current model. |
| 118 | + """ |
| 119 | + return self.output_dim |
0 commit comments