🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Integrate AIML model platform (#1580)
Wendong-Fan82fa1a16c2d3
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.ymlindex 449550efa..43c5cd7aa 100644--- a/.github/workflows/build_package.yml+++ b/.github/workflows/build_package.yml@@ -82,6 +82,7 @@ jobs:JINA_API_KEY: "${{ secrets.JINA_API_KEY }}"SILICONFLOW_API_KEY: "${{ secrets.SILICONFLOW_API_KEY }}"MOONSHOT_API_KEY: "${{ secrets.MOONSHOT_API_KEY }}"+ AIML_API_KEY: "${{ secrets.AIML_API_KEY }}"run: |source venv/bin/activatepytest --fast-test-mode ./testdiff --git a/.github/workflows/pytest_apps.yml b/.github/workflows/pytest_apps.ymlindex 5b078d00b..d767e3de3 100644--- a/.github/workflows/pytest_apps.yml+++ b/.github/workflows/pytest_apps.yml@@ -28,10 +28,6 @@ jobs:OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"GOOGLE_API_KEY: "${{ secrets.GOOGLE_API_KEY }}"SEARCH_ENGINE_ID: "${{ secrets.SEARCH_ENGINE_ID }}"- COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"- INTERNLM_API_KEY: "${{ secrets.INTERNLM_API_KEY }}"- MOONSHOT_API_KEY: "${{ secrets.MOONSHOT_API_KEY }}"- SILICONFLOW_API_KEY: "${{ secrets.SILICONFLOW_API_KEY }}"run: poetry run pytest -v apps/pytest_examples:@@ -53,4 +49,5 @@ jobs:INTERNLM_API_KEY: "${{ secrets.INTERNLM_API_KEY }}"MOONSHOT_API_KEY: "${{ secrets.MOONSHOT_API_KEY }}"SILICONFLOW_API_KEY: "${{ secrets.SILICONFLOW_API_KEY }}"+ AIML_API_KEY: "${{ secrets.AIML_API_KEY }}"run: poetry run pytest -v examples/diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.ymlindex 3c0526ef5..8b7050f26 100644--- a/.github/workflows/pytest_package.yml+++ b/.github/workflows/pytest_package.yml@@ -61,6 +61,7 @@ jobs:JINA_API_KEY: "${{ secrets.JINA_API_KEY }}"MOONSHOT_API_KEY: "${{ secrets.MOONSHOT_API_KEY }}"SILICONFLOW_API_KEY: "${{ secrets.SILICONFLOW_API_KEY }}"+ AIML_API_KEY: "${{ secrets.AIML_API_KEY }}"run: poetry run pytest --fast-test-mode test/pytest_package_llm_test:@@ -111,6 +112,7 @@ jobs:JINA_API_KEY: "${{ secrets.JINA_API_KEY }}"MOONSHOT_API_KEY: "${{ secrets.MOONSHOT_API_KEY }}"SILICONFLOW_API_KEY: "${{ secrets.SILICONFLOW_API_KEY }}"+ AIML_API_KEY: "${{ secrets.AIML_API_KEY }}"run: poetry run pytest --llm-test-only test/pytest_package_very_slow_test:diff --git a/camel/configs/__init__.py b/camel/configs/__init__.pyindex 9b3df6039..d52c35608 100644--- a/camel/configs/__init__.py+++ b/camel/configs/__init__.py@@ -11,6 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from .aiml_config import AIML_API_PARAMS, AIMLConfigfrom .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfigfrom .base_config import BaseConfigfrom .cohere_config import COHERE_API_PARAMS, CohereConfig@@ -85,4 +86,6 @@ __all__ = ["MOONSHOT_API_PARAMS",'SiliconFlowConfig','SILICONFLOW_API_PARAMS',+ 'AIMLConfig',+ 'AIML_API_PARAMS',]diff --git a/camel/configs/aiml_config.py b/camel/configs/aiml_config.pynew file mode 100644index 000000000..61bf34cc9--- /dev/null+++ b/camel/configs/aiml_config.py@@ -0,0 +1,80 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from __future__ import annotations++from typing import Sequence, Type, Union++from pydantic import BaseModel, Field++from camel.configs.base_config import BaseConfig+from camel.types import NOT_GIVEN, NotGiven+++class AIMLConfig(BaseConfig):+ r"""Defines the parameters for generating chat completions using the+ AIML API.++ Args:+ temperature (float, optional): Determines the degree of randomness+ in the response. (default: :obj:`0.7`)+ top_p (float, optional): The top_p (nucleus) parameter is used to+ dynamically adjust the number of choices for each predicted token+ based on the cumulative probabilities. (default: :obj:`0.7`)+ n (int, optional): Number of generations to return. (default::obj:`1`)+ response_format (object, optional): An object specifying the format+ that the model must output.+ stream (bool, optional): If set, tokens are returned as Server-Sent+ Events as they are made available. (default: :obj:`False`)+ stop (str or list, optional): Up to :obj:`4` sequences where the API+ will stop generating further tokens. (default: :obj:`None`)+ max_tokens (int, optional): The maximum number of tokens to generate.+ (default: :obj:`None`)+ logit_bias (dict, optional): Modify the likelihood of specified tokens+ appearing in the completion. Accepts a json object that maps tokens+ (specified by their token ID in the tokenizer) to an associated+ bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias+ is added to the logits generated by the model prior to sampling.+ The exact effect will vary per model, but values between:obj:` -1`+ and :obj:`1` should decrease or increase likelihood of selection;+ values like :obj:`-100` or :obj:`100` should result in a ban or+ exclusive selection of the relevant token. (default: :obj:`{}`)+ frequency_penalty (float, optional): Number between :obj:`-2.0` and+ :obj:`2.0`. Positive values penalize new tokens based on their+ existing frequency in the text so far, decreasing the model's+ likelihood to repeat the same line verbatim. See more information+ about frequency and presence penalties. (default: :obj:`0.0`)+ presence_penalty (float, optional): Number between :obj:`-2.0` and+ :obj:`2.0`. Positive values penalize new tokens based on whether+ they appear in the text so far, increasing the model's likelihood+ to talk about new topics. See more information about frequency and+ presence penalties. (default: :obj:`0.0`)+ tools (list[FunctionTool], optional): A list of tools the model may+ call. Currently, only functions are supported as a tool. Use this+ to provide a list of functions the model may generate JSON inputs+ for. A max of 128 functions are supported.+ """++ temperature: float = 0.7+ top_p: float = 0.7+ n: int = 1+ stream: bool = False+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN+ max_tokens: Union[int, NotGiven] = NOT_GIVEN+ logit_bias: dict = Field(default_factory=dict)+ response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN+ presence_penalty: float = 0.0+ frequency_penalty: float = 0.0+++AIML_API_PARAMS = {param for param in AIMLConfig.model_fields.keys()}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 138f5dadd..cdee39cd4 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -11,6 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from .aiml_model import AIMLModelfrom .anthropic_model import AnthropicModelfrom .azure_openai_model import AzureOpenAIModelfrom .base_model import BaseModelBackend@@ -72,4 +73,5 @@ __all__ = ['FishAudioModel','InternLMModel','MoonshotModel',+ 'AIMLModel',]diff --git a/camel/models/aiml_model.py b/camel/models/aiml_model.pynew file mode 100644index 000000000..9bc737d9d--- /dev/null+++ b/camel/models/aiml_model.py@@ -0,0 +1,147 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import os+from typing import Any, Dict, List, Optional, Union++from openai import OpenAI, Stream++from camel.configs import AIML_API_PARAMS, AIMLConfig+from camel.messages import OpenAIMessage+from camel.models.base_model import BaseModelBackend+from camel.types import (+ ChatCompletion,+ ChatCompletionChunk,+ ModelType,+)+from camel.utils import (+ BaseTokenCounter,+ OpenAITokenCounter,+ api_keys_required,+)+++class AIMLModel(BaseModelBackend):+ r"""AIML API in a unified BaseModelBackend interface.++ Args:+ model_type (Union[ModelType, str]): Model for which a backend is+ created.+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary+ that will be fed into OpenAI client. If :obj:`None`,+ :obj:`AIMLConfig().as_dict()` will be used.+ (default: :obj:`None`)+ api_key (Optional[str], optional): The API key for authenticating with+ the AIML service. (default: :obj:`None`)+ url (Optional[str], optional): The URL to the AIML service. If+ not provided, :obj:`https://api.aimlapi.com/v1` will be used.+ (default: :obj:`None`)+ token_counter (Optional[BaseTokenCounter], optional): Token counter to+ use for the model. If not provided, :obj:`OpenAITokenCounter(+ ModelType.GPT_4O_MINI)` will be used.+ (default: :obj:`None`)+ """++ @api_keys_required(+ [+ ("api_key", 'AIML_API_KEY'),+ ]+ )+ def __init__(+ self,+ model_type: Union[ModelType, str],+ model_config_dict: Optional[Dict[str, Any]] = None,+ api_key: Optional[str] = None,+ url: Optional[str] = None,+ token_counter: Optional[BaseTokenCounter] = None,+ ) -> None:+ if model_config_dict is None:+ model_config_dict = AIMLConfig().as_dict()+ api_key = api_key or os.environ.get("AIML_API_KEY")+ url = url or os.environ.get(+ "AIML_API_BASE_URL",+ "https://api.aimlapi.com/v1",+ )+ super().__init__(+ model_type, model_config_dict, api_key, url, token_counter+ )+ self._client = OpenAI(+ timeout=180,+ max_retries=3,+ api_key=self._api_key,+ base_url=self._url,+ )++ def run(+ self,+ messages: List[OpenAIMessage],+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ r"""Runs inference of OpenAI chat completion.++ Args:+ messages (List[OpenAIMessage]): Message list with the chat history+ in OpenAI API format.++ Returns:+ Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ `ChatCompletion` in the non-stream mode, or+ `Stream[ChatCompletionChunk]` in the stream mode.+ """+ # Process model configuration parameters+ model_config = self.model_config_dict.copy()++ # Handle special case for tools parameter+ if model_config.get('tools') is None:+ model_config['tools'] = []++ response = self._client.chat.completions.create(+ messages=messages, model=self.model_type, **model_config+ )+ return response++ @property+ def token_counter(self) -> BaseTokenCounter:+ r"""Initialize the token counter for the model backend.++ Returns:+ BaseTokenCounter: The token counter following the model's+ tokenization style.+ """+ if not self._token_counter:+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)+ return self._token_counter++ def check_model_config(self):+ r"""Check whether the model configuration contains any+ unexpected arguments to AIML API.++ Raises:+ ValueError: If the model configuration dictionary contains any+ unexpected arguments to AIML API.+ """+ for param in self.model_config_dict:+ if param not in AIML_API_PARAMS:+ raise ValueError(+ f"Unexpected argument `{param}` is "+ "input into AIML model backend."+ )++ @property+ def stream(self) -> bool:+ """Returns whether the model is in stream mode, which sends partial+ results each time.++ Returns:+ bool: Whether the model is in stream mode.+ """+ return self.model_config_dict.get('stream', False)diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 560dcdc24..eabdab58f 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -13,6 +13,7 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========from typing import Dict, Optional, Type, Union+from camel.models.aiml_model import AIMLModelfrom camel.models.anthropic_model import AnthropicModelfrom camel.models.azure_openai_model import AzureOpenAIModelfrom camel.models.base_model import BaseModelBackend@@ -104,6 +105,8 @@ class ModelFactory:model_class = NvidiaModelelif model_platform.is_siliconflow:model_class = SiliconFlowModel+ elif model_platform.is_aiml:+ model_class = AIMLModelelif model_platform.is_openai and model_type.is_openai:model_class = OpenAIModeldiff --git a/camel/models/siliconflow_model.py b/camel/models/siliconflow_model.pyindex f2197a7d9..c981472bc 100644--- a/camel/models/siliconflow_model.py+++ b/camel/models/siliconflow_model.py@@ -18,7 +18,7 @@ from openai import OpenAI, Streamfrom camel.configs import SILICONFLOW_API_PARAMS, SiliconFlowConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackend+from camel.models.base_model import BaseModelBackendfrom camel.types import (ChatCompletion,ChatCompletionChunk,diff --git a/camel/types/enums.py b/camel/types/enums.pyindex b549b8d9a..29cb6ff7e 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -204,6 +204,10 @@ class ModelType(UnifiedModelType, Enum):SILICONFLOW_THUDM_GLM_4_9B_CHAT = "THUDM/glm-4-9b-chat"SILICONFLOW_PRO_THUDM_GLM_4_9B_CHAT = "Pro/THUDM/glm-4-9b-chat"+ # AIML models support tool calling+ AIML_MIXTRAL_8X7B = "mistralai/Mixtral-8x7B-Instruct-v0.1"+ AIML_MISTRAL_7B_INSTRUCT = "mistralai/Mistral-7B-Instruct-v0.1"+def __str__(self):return self.value@@ -242,6 +246,7 @@ class ModelType(UnifiedModelType, Enum):self.is_moonshot,self.is_siliconflow,self.is_zhipuai,+ self.is_aiml,])@@ -517,6 +522,13 @@ class ModelType(UnifiedModelType, Enum):ModelType.SILICONFLOW_PRO_THUDM_GLM_4_9B_CHAT,}+ @property+ def is_aiml(self) -> bool:+ return self in {+ ModelType.AIML_MIXTRAL_8X7B,+ ModelType.AIML_MISTRAL_7B_INSTRUCT,+ }+@propertydef token_limit(self) -> int:r"""Returns the maximum token limit for a given model.@@ -590,6 +602,8 @@ class ModelType(UnifiedModelType, Enum):ModelType.TOGETHER_MIXTRAL_8_7B,ModelType.SGLANG_MISTRAL_7B,ModelType.MOONSHOT_V1_32K,+ ModelType.AIML_MIXTRAL_8X7B,+ ModelType.AIML_MISTRAL_7B_INSTRUCT,}:return 32_768elif self in {@@ -868,6 +882,7 @@ class ModelPlatformType(Enum):INTERNLM = "internlm"MOONSHOT = "moonshot"SILICONFLOW = "siliconflow"+ AIML = "aiml"@propertydef is_openai(self) -> bool:@@ -985,6 +1000,11 @@ class ModelPlatformType(Enum):r"""Returns whether this platform is SiliconFlow."""return self is ModelPlatformType.SILICONFLOW+ @property+ def is_aiml(self) -> bool:+ r"""Returns whether this platform is AIML."""+ return self is ModelPlatformType.AIML+class AudioModelType(Enum):TTS_1 = "tts-1"diff --git a/docs/key_modules/models.md b/docs/key_modules/models.mdindex 88c7c7002..fff51d937 100644--- a/docs/key_modules/models.md+++ b/docs/key_modules/models.md@@ -101,6 +101,7 @@ The following table lists currently supported model platforms by CAMEL.| Together AI | https://docs.together.ai/docs/chat-models | ----- || LiteLLM | https://docs.litellm.ai/docs/providers | ----- || SGLang | https://sgl-project.github.io/references/supported_models.html | ----- |+| AIML | https://docs.aimlapi.com/api-overview/model-database/text-models | ----- |## 3. Using Models by API callingdiff --git a/examples/models/aiml_model_example.py b/examples/models/aiml_model_example.pynew file mode 100644index 000000000..86163886b--- /dev/null+++ b/examples/models/aiml_model_example.py@@ -0,0 +1,41 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from camel.agents import ChatAgent+from camel.models import ModelFactory+from camel.types import ModelPlatformType++model = ModelFactory.create(+ model_platform=ModelPlatformType.AIML,+ model_type="mistralai/Mixtral-8x7B-Instruct-v0.1",+)++# Define system message+sys_msg = "You are a helpful assistant."++# Set agent+camel_agent = ChatAgent(system_message=sys_msg, model=model)++user_msg = """Say hi to CAMEL AI, one open-source community+ dedicated to the study of autonomous and communicative agents."""++# Get response information+response = camel_agent.step(user_msg)+print(response.msgs[0].content)++'''+===============================================================================+ Hello CAMEL AI! It's great to meet a community dedicated to the study of+ autonomous and communicative agents. How can I assist you today?+===============================================================================+'''diff --git a/examples/toolkits/sympy_toolkit.py b/examples/toolkits/sympy_toolkit.pyindex 51f11675f..4f2579308 100644--- a/examples/toolkits/sympy_toolkit.py+++ b/examples/toolkits/sympy_toolkit.py@@ -19,9 +19,8 @@ from camel.toolkits import SymPyToolkitfrom camel.types import ModelPlatformType, ModelType# Define system message-sys_msg = (- "You are a helpful math assistant that can perform symbolic computations"-)+sys_msg = """You are a helpful math assistant that can perform symbolic+computations"""# Set model configtools = SymPyToolkit().get_tools()@@ -43,16 +42,18 @@ camel_agent = ChatAgent()camel_agent.reset()-# Define a user message-usr_msg = """Simplify the expression: x^2 + 2*x + 1"""+# Define a user message with a complex expression+usr_msg = """Simplify the expression: (x^4 - 16)/(x^2 - 4) + sin(x)^2 + cos(x)+^2 + (x^3 + 6*x^2 + 12*x + 8)/(x + 2)"""# Get response informationresponse = camel_agent.step(usr_msg)print(response.info['tool_calls'])'''===============================================================================-[FunctionCallingRecord(func_name='simplify_expression', args={'expression':-'x^2 + 2*x + 1'}, result='{"status": "success", "result": "x**2 + 2*x + 1"}',-tool_call_id='call_USLSZHjQwLHHCa8oLiNsm2AH')]+[ToolCallingRecord(tool_name='simplify_expression', args={'expression': '(x**4+- 16)/(x**2 - 4) + sin(x)**2 + cos(x)**2 + (x**3 + 6*x**2 + 12*x + 8)/(x + 2)+'}, result='{"status": "success", "result": "2*x**2 + 4*x + 9"}',+tool_call_id='call_CdoZsLWeagT0yBM13RYuz09W')]==============================================================================='''diff --git a/test/models/test_aiml_model.py b/test/models/test_aiml_model.pynew file mode 100644index 000000000..0e8b7c83a--- /dev/null+++ b/test/models/test_aiml_model.py@@ -0,0 +1,54 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++import re++import pytest++from camel.configs import AIMLConfig+from camel.models import AIMLModel+from camel.types import ModelType++[email protected]_backend+ "model_type",+ [+ ModelType.AIML_MIXTRAL_8X7B,+ ModelType.AIML_MISTRAL_7B_INSTRUCT,+ ],+)+def test_aiml_model(model_type: ModelType):+ model = AIMLModel(model_type)+ assert model.model_type == model_type+ assert model.model_config_dict == AIMLConfig().as_dict()+ assert isinstance(model.model_type.value_for_tiktoken, str)+ assert isinstance(model.model_type.token_limit, int)++[email protected]_backend+def test_aiml_model_unexpected_argument():+ model_type = ModelType.AIML_MIXTRAL_8X7B+ model_config_dict = {"model_path": "aiml_v1"}++ with pytest.raises(+ ValueError,+ match=re.escape(+ (+ "Unexpected argument `model_path` is "+ "input into AIML model backend."+ )+ ),+ ):+ _ = AIMLModel(model_type, model_config_dict)