|
| 1 | +# ========= Copyright 2023-2026 @ 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-2026 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | +import os |
| 15 | +from typing import Any, Dict, Optional, Union |
| 16 | + |
| 17 | +from camel.configs import AtlasCloudConfig |
| 18 | +from camel.models.openai_compatible_model import OpenAICompatibleModel |
| 19 | +from camel.types import ModelType |
| 20 | +from camel.utils import ( |
| 21 | + BaseTokenCounter, |
| 22 | + api_keys_required, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +class AtlasCloudModel(OpenAICompatibleModel): |
| 27 | + r"""LLM API served by AtlasCloud in a unified OpenAICompatibleModel |
| 28 | + interface. |
| 29 | +
|
| 30 | + Args: |
| 31 | + model_type (Union[ModelType, str]): Model for which a backend is |
| 32 | + created. |
| 33 | + model_config_dict (Optional[Dict[str, Any]], optional): A dictionary |
| 34 | + that will be fed into:obj:`openai.ChatCompletion.create()`. |
| 35 | + If:obj:`None`, :obj:`AtlasCloudConfig().as_dict()` will be used. |
| 36 | + (default: :obj:`None`) |
| 37 | + api_key (Optional[str], optional): The API key for authenticating |
| 38 | + with the AtlasCloud service. (default: :obj:`None`). |
| 39 | + url (Optional[str], optional): The url to the AtlasCloud service. |
| 40 | + (default: :obj:`None`) |
| 41 | + token_counter (Optional[BaseTokenCounter], optional): Token counter to |
| 42 | + use for the model. If not provided, :obj:`OpenAITokenCounter( |
| 43 | + ModelType.GPT_4O_MINI)` will be used. |
| 44 | + (default: :obj:`None`) |
| 45 | + timeout (Optional[float], optional): The timeout value in seconds for |
| 46 | + API calls. If not provided, will fall back to the MODEL_TIMEOUT |
| 47 | + environment variable or default to 180 seconds. |
| 48 | + (default: :obj:`None`) |
| 49 | + max_retries (int, optional): Maximum number of retries for API calls. |
| 50 | + (default: :obj:`3`) |
| 51 | + **kwargs (Any): Additional arguments to pass to the client |
| 52 | + initialization. |
| 53 | + """ |
| 54 | + |
| 55 | + @api_keys_required([("api_key", "ATLASCLOUD_API_KEY")]) |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + model_type: Union[ModelType, str], |
| 59 | + model_config_dict: Optional[Dict[str, Any]] = None, |
| 60 | + api_key: Optional[str] = None, |
| 61 | + url: Optional[str] = None, |
| 62 | + token_counter: Optional[BaseTokenCounter] = None, |
| 63 | + timeout: Optional[float] = None, |
| 64 | + max_retries: int = 3, |
| 65 | + **kwargs: Any, |
| 66 | + ) -> None: |
| 67 | + if model_config_dict is None: |
| 68 | + model_config_dict = AtlasCloudConfig().as_dict() |
| 69 | + api_key = api_key or os.environ.get("ATLASCLOUD_API_KEY") |
| 70 | + url = url or os.environ.get( |
| 71 | + "ATLASCLOUD_API_BASE_URL", "https://api.atlascloud.ai/v1" |
| 72 | + ) |
| 73 | + timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180)) |
| 74 | + super().__init__( |
| 75 | + model_type=model_type, |
| 76 | + model_config_dict=model_config_dict, |
| 77 | + api_key=api_key, |
| 78 | + url=url, |
| 79 | + token_counter=token_counter, |
| 80 | + timeout=timeout, |
| 81 | + max_retries=max_retries, |
| 82 | + **kwargs, |
| 83 | + ) |
0 commit comments