š« 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: Add support for Qwen model platform (#1033) (#1137)
Xiaotian Jin11d79c3bf4a5
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.pyindex ffc9bc78a..72c3c3717 100644--- a/camel/configs/__init__.py+++ b/camel/configs/__init__.py@@ -19,6 +19,7 @@ from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfigfrom .mistral_config import MISTRAL_API_PARAMS, MistralConfigfrom .ollama_config import OLLAMA_API_PARAMS, OllamaConfigfrom .openai_config import OPENAI_API_PARAMS, ChatGPTConfig+from .qwen_config import QWEN_API_PARAMS, QwenConfigfrom .reka_config import REKA_API_PARAMS, RekaConfigfrom .samba_config import (SAMBA_CLOUD_API_PARAMS,@@ -61,4 +62,6 @@ __all__ = ['TOGETHERAI_API_PARAMS','YiConfig','YI_API_PARAMS',+ 'QwenConfig',+ 'QWEN_API_PARAMS',]diff --git a/camel/configs/qwen_config.py b/camel/configs/qwen_config.pynew file mode 100644index 000000000..eb5dcce7f--- /dev/null+++ b/camel/configs/qwen_config.py@@ -0,0 +1,91 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+from __future__ import annotations++from typing import ClassVar, Optional, Union++from camel.configs.base_config import BaseConfig+from camel.types import NOT_GIVEN, NotGiven+++class QwenConfig(BaseConfig):+ r"""Defines the parameters for generating chat completions using the+ Qwen API. You can refer to the following link for more details:+ https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api++ Args:+ stream (bool, optional): Whether to stream the response.+ (default: :obj:`False`)+ temperature (float, optional): Controls the diversity and focus of+ the generated results. Lower values make the output more focused,+ while higher values make it more diverse. (default: :obj:`0.3`)+ top_p (float, optional): Controls the diversity and focus of the+ generated results. Higher values make the output more diverse,+ while lower values make it more focused. (default: :obj:`0.9`)+ presence_penalty (float, optional): Controls the repetition of+ content in the generated results. Positive values reduce the+ repetition of content, while negative values increase it.+ (default: :obj:`0.0`)+ response_format (object, optional): Specifies the format of the+ returned content. The available values are `{"type": "text"}` or+ `{"type": "json_object"}`. Setting it to `{"type": "json_object"}`+ will output a standard JSON string.+ (default: :obj:`{"type": "text"}`)+ max_tokens (Union[int, NotGiven], optional): Allows the model to+ generate the maximum number of tokens.+ (default: :obj:`NOT_GIVEN`)+ seed (int, optional): Sets the seed parameter to make the text+ generation process more deterministic, typically used to ensure+ that the results are consistent across model runs. By passing the+ same seed value (specified by you) in each model call while+ keeping other parameters unchanged, the model is likely to return+ the same result.+ (default: :obj:`None`)+ stop (str or list, optional): Using the stop parameter, the model will+ automatically stop generating text when it is about to include the+ specified string or token_id. You can use the stop parameter to+ control the output of the model by passing sensitive words.+ (default: :obj:`None`)+ tools (list, optional): Specifies an array of tools that the model can+ call. It can contain one or more tool objects. During a function+ call process, the model will select one tool from the array.+ (default: :obj:`None`)+ extra_body (dict, optional): Additional parameters to be sent to the+ Qwen API. If you want to enable internet search, you can set this+ parameter to `{"enable_search": True}`.+ (default: :obj:`{"enable_search": False}`)+ include_usage (bool, optional): When streaming, specifies whether to+ include usage information in `stream_options`. (default:+ :obj:`True`)+ """++ stream: bool = False+ temperature: float = 0.3+ top_p: float = 0.9+ presence_penalty: float = 0.0+ response_format: ClassVar[dict] = {"type": "text"}+ max_tokens: Union[int, NotGiven] = NOT_GIVEN+ seed: Optional[int] = None+ stop: Optional[Union[str, list]] = None+ extra_body: ClassVar[dict] = {"enable_search": False}++ def __init__(self, include_usage: bool = True, **kwargs):+ super().__init__(**kwargs)+ # Only set stream_options when stream is True+ # Otherwise, it will raise error when calling the API+ if self.stream:+ self.stream_options = {"include_usage": include_usage}+++QWEN_API_PARAMS = {param for param in QwenConfig.model_fields.keys()}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex d8b32bc8c..0b48c0596 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -24,6 +24,7 @@ from .ollama_model import OllamaModelfrom .openai_audio_models import OpenAIAudioModelsfrom .openai_compatible_model import OpenAICompatibleModelfrom .openai_model import OpenAIModel+from .qwen_model import QwenModelfrom .reka_model import RekaModelfrom .samba_model import SambaModelfrom .stub_model import StubModel@@ -53,4 +54,5 @@ __all__ = ['SambaModel','TogetherAIModel','YiModel',+ 'QwenModel',]diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 83c4a6584..ca2a19fe5 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -23,6 +23,7 @@ from camel.models.mistral_model import MistralModelfrom camel.models.ollama_model import OllamaModelfrom camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.models.openai_model import OpenAIModel+from camel.models.qwen_model import QwenModelfrom camel.models.reka_model import RekaModelfrom camel.models.samba_model import SambaModelfrom camel.models.stub_model import StubModel@@ -107,10 +108,12 @@ class ModelFactory:model_class = MistralModelelif model_platform.is_reka and model_type.is_reka:model_class = RekaModel- elif model_type == ModelType.STUB:- model_class = StubModelelif model_platform.is_yi and model_type.is_yi:model_class = YiModel+ elif model_platform.is_qwen and model_type.is_qwen:+ model_class = QwenModel+ elif model_type == ModelType.STUB:+ model_class = StubModelif model_class is None:raise ValueError(diff --git a/camel/models/qwen_model.py b/camel/models/qwen_model.pynew file mode 100644index 000000000..13c2a8fd8--- /dev/null+++ b/camel/models/qwen_model.py@@ -0,0 +1,139 @@+# =========== Copyright 2023 @ 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 @ 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 QWEN_API_PARAMS, QwenConfig+from camel.messages import OpenAIMessage+from camel.models import BaseModelBackend+from camel.types import (+ ChatCompletion,+ ChatCompletionChunk,+ ModelType,+)+from camel.utils import (+ BaseTokenCounter,+ OpenAITokenCounter,+ api_keys_required,+)+++class QwenModel(BaseModelBackend):+ r"""Qwen API in a unified BaseModelBackend interface.++ Args:+ model_type (Union[ModelType, str]): Model for which a backend is+ created, one of Qwen series.+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary+ that will be fed into:obj:`openai.ChatCompletion.create()`. If+ :obj:`None`, :obj:`QwenConfig().as_dict()` will be used.+ (default: :obj:`None`)+ api_key (Optional[str], optional): The API key for authenticating with+ the Qwen service. (default: :obj:`None`)+ url (Optional[str], optional): The url to the Qwen service.+ (default: :obj:`https://dashscope.aliyuncs.com/compatible-mode/v1`)+ 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`)+ """++ 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 = QwenConfig().as_dict()+ api_key = api_key or os.environ.get("QWEN_API_KEY")+ url = url or os.environ.get(+ "QWEN_API_BASE_URL",+ "https://dashscope.aliyuncs.com/compatible-mode/v1",+ )+ super().__init__(+ model_type, model_config_dict, api_key, url, token_counter+ )+ self._client = OpenAI(+ timeout=60,+ max_retries=3,+ api_key=self._api_key,+ base_url=self._url,+ )++ @api_keys_required("QWEN_API_KEY")+ def run(+ self,+ messages: List[OpenAIMessage],+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ r"""Runs inference of Qwen 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.+ """+ response = self._client.chat.completions.create(+ messages=messages,+ model=self.model_type,+ **self.model_config_dict,+ )+ return response++ @property+ def token_counter(self) -> BaseTokenCounter:+ r"""Initialize the token counter for the model backend.++ Returns:+ OpenAITokenCounter: 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 Qwen API.++ Raises:+ ValueError: If the model configuration dictionary contains any+ unexpected arguments to Qwen API.+ """+ for param in self.model_config_dict:+ if param not in QWEN_API_PARAMS:+ raise ValueError(+ f"Unexpected argument `{param}` is "+ "input into Qwen model backend."+ )++ @property+ def stream(self) -> bool:+ r"""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/types/enums.py b/camel/types/enums.pyindex eb89a5a61..d0d797e3e 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -87,6 +87,17 @@ class ModelType(UnifiedModelType, Enum):REKA_FLASH = "reka-flash"REKA_EDGE = "reka-edge"+ # Qwen models (Aliyun)+ QWEN_MAX = "qwen-max"+ QWEN_PLUS = "qwen-plus"+ QWEN_TURBO = "qwen-turbo"+ QWEN_LONG = "qwen-long"+ QWEN_VL_MAX = "qwen-vl-max"+ QWEN_VL_PLUS = "qwen-vl-plus"+ QWEN_MATH_PLUS = "qwen-math-plus"+ QWEN_MATH_TURBO = "qwen-math-turbo"+ QWEN_CODER_TURBO = "qwen-coder-turbo"+# Yi models (01-ai)YI_LIGHTNING = "yi-lightning"YI_LARGE = "yi-large"@@ -250,6 +261,20 @@ class ModelType(UnifiedModelType, Enum):ModelType.YI_LARGE_FC,}+ @property+ def is_qwen(self) -> bool:+ return self in {+ ModelType.QWEN_MAX,+ ModelType.QWEN_PLUS,+ ModelType.QWEN_TURBO,+ ModelType.QWEN_LONG,+ ModelType.QWEN_VL_MAX,+ ModelType.QWEN_VL_PLUS,+ ModelType.QWEN_MATH_PLUS,+ ModelType.QWEN_MATH_TURBO,+ ModelType.QWEN_CODER_TURBO,+ }+@propertydef token_limit(self) -> int:r"""Returns the maximum token limit for a given model.@@ -265,6 +290,8 @@ class ModelType(UnifiedModelType, Enum):ModelType.REKA_CORE,ModelType.REKA_EDGE,ModelType.REKA_FLASH,+ ModelType.QWEN_MATH_PLUS,+ ModelType.QWEN_MATH_TURBO,}:return 4_096elif self in {@@ -275,6 +302,7 @@ class ModelType(UnifiedModelType, Enum):ModelType.GROQ_GEMMA_2_9B_IT,ModelType.GLM_3_TURBO,ModelType.GLM_4,+ ModelType.QWEN_VL_PLUS,}:return 8_192elif self in {@@ -294,6 +322,8 @@ class ModelType(UnifiedModelType, Enum):ModelType.GROQ_MIXTRAL_8_7B,ModelType.YI_LARGE,ModelType.YI_LARGE_FC,+ ModelType.QWEN_MAX,+ ModelType.QWEN_VL_MAX,}:return 32_768elif self in {ModelType.MISTRAL_MIXTRAL_8x22B}:@@ -320,6 +350,9 @@ class ModelType(UnifiedModelType, Enum):ModelType.GROQ_LLAMA_3_1_8B,ModelType.GROQ_LLAMA_3_1_70B,ModelType.GROQ_LLAMA_3_1_405B,+ ModelType.QWEN_PLUS,+ ModelType.QWEN_TURBO,+ ModelType.QWEN_CODER_TURBO,}:return 131_072elif self in {@@ -340,6 +373,10 @@ class ModelType(UnifiedModelType, Enum):ModelType.GEMINI_1_5_PRO,}:return 1_048_576+ elif self in {+ ModelType.QWEN_LONG,+ }:+ return 10_000_000else:raise ValueError("Unknown model type")@@ -485,6 +522,7 @@ class ModelPlatformType(Enum):OPENAI_COMPATIBLE_MODEL = "openai-compatible-model"SAMBA = "samba-nova"YI = "lingyiwanwu"+ QWEN = "tongyi-qianwen"@propertydef is_openai(self) -> bool:@@ -562,6 +600,11 @@ class ModelPlatformType(Enum):r"""Returns whether this platform is Yi."""return self is ModelPlatformType.YI+ @property+ def is_qwen(self) -> bool:+ r"""Returns whether this platform is Qwen."""+ return self is ModelPlatformType.QWEN+class AudioModelType(Enum):TTS_1 = "tts-1"diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.pyindex d3fed082d..e9eec7f45 100644--- a/camel/types/unified_model_type.py+++ b/camel/types/unified_model_type.py@@ -103,6 +103,11 @@ class UnifiedModelType(str):r"""Returns whether the model is a Yi model."""return True+ @property+ def is_qwen(self) -> bool:+ r"""Returns whether the model is a Qwen model."""+ return True+@propertydef support_native_tool_calling(self) -> bool:r"""Returns whether the model supports native tool calling."""diff --git a/docs/key_modules/models.md b/docs/key_modules/models.mdindex 813ad1a29..d15e8d192 100644--- a/docs/key_modules/models.md+++ b/docs/key_modules/models.md@@ -50,6 +50,15 @@ The following table lists currently supported model platforms by CAMEL.| Lingyiwanwu | yi-spark | N || Lingyiwanwu | yi-large-rag | N || Lingyiwanwu | yi-large-fc | N |+| Qwen | qwen-max | N |+| Qwen | qwen-plus | N |+| Qwen | qwen-turbo | N |+| Qwen | qwen-long | N |+| Qwen | qwen-vl-max | Y |+| Qwen | qwen-vl-plus | Y |+| Qwen | qwen-math-plus | N |+| Qwen | qwen-math-turbo | N |+| Qwen | qwen-coder-turbo | N || ZhipuAI | glm-4v | Y || ZhipuAI | glm-4 | N || ZhipuAI | glm-3-turbo | N |diff --git a/examples/models/qwen_model_example.py b/examples/models/qwen_model_example.pynew file mode 100644index 000000000..adc8d6d16--- /dev/null+++ b/examples/models/qwen_model_example.py@@ -0,0 +1,45 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========++from camel.agents import ChatAgent+from camel.configs import QwenConfig+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++model = ModelFactory.create(+ model_platform=ModelPlatformType.QWEN,+ model_type=ModelType.QWEN_MAX,+ model_config_dict=QwenConfig(temperature=0.2, stream=False).as_dict(),+)++# 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 community! It's great to see such dedication to the study of+autonomous and communicative agents. Your work is truly inspiring and+contributes significantly to the field of AI. Keep up the fantastic work!+===============================================================================+'''diff --git a/test/models/test_qwen_model.py b/test/models/test_qwen_model.pynew file mode 100644index 000000000..8360a7e71--- /dev/null+++ b/test/models/test_qwen_model.py@@ -0,0 +1,61 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========++import re++import pytest++from camel.configs import QwenConfig+from camel.models import QwenModel+from camel.types import ModelType++[email protected]_backend+ "model_type",+ [+ ModelType.QWEN_MAX,+ ModelType.QWEN_PLUS,+ ModelType.QWEN_TURBO,+ ModelType.QWEN_LONG,+ ModelType.QWEN_VL_MAX,+ ModelType.QWEN_VL_PLUS,+ ModelType.QWEN_MATH_PLUS,+ ModelType.QWEN_MATH_TURBO,+ ModelType.QWEN_CODER_TURBO,+ ],+)+def test_qwen_model(model_type: ModelType):+ model = QwenModel(model_type)+ assert model.model_type == model_type+ assert model.model_config_dict == QwenConfig().as_dict()+ assert isinstance(model.model_type.value_for_tiktoken, str)+ assert isinstance(model.model_type.token_limit, int)++[email protected]_backend+def test_qwen_model_unexpected_argument():+ model_type = ModelType.QWEN_MAX+ model_config_dict = {"model_path": "qwen-max"}++ with pytest.raises(+ ValueError,+ match=re.escape(+ (+ "Unexpected argument `model_path` is "+ "input into Qwen model backend."+ )+ ),+ ):+ _ = QwenModel(model_type, model_config_dict)