Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat:integrate qianfan platform (#2620)

Yifeng Wang(正经人王同学)df99a1763acc
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.yml
index 266eb226d..2e586ec90 100644
--- a/.github/workflows/build_package.yml
+++ b/.github/workflows/build_package.yml
@@ -136,6 +136,7 @@ jobs:
NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"
WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"
WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"
+ QIANFAN_API_KEY: "${{ secrets.QIANFAN_API_KEY }}"
ACI_API_KEY: "${{ secrets.ACI_API_KEY }}"
BOHRIUM_API_KEY: "${{ secrets.BOHRIUM_API_KEY }}"
CRYNUX_API_KEY: "${{ secrets.CRYNUX_API_KEY }}"
diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.yml
index 2318a240d..59ef22a42 100644
--- a/.github/workflows/pytest_package.yml
+++ b/.github/workflows/pytest_package.yml
@@ -75,6 +75,7 @@ jobs:
NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"
WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"
WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"
+ QIANFAN_API_KEY: "${{ secrets.QIANFAN_API_KEY }}"
ACI_API_KEY: "${{ secrets.ACI_API_KEY }}"
BOHRIUM_API_KEY: "${{ secrets.BOHRIUM_API_KEY }}"
CRYNUX_API_KEY: "${{ secrets.CRYNUX_API_KEY }}"
@@ -158,6 +159,7 @@ jobs:
NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"
WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"
WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"
+ QIANFAN_API_KEY: "${{ secrets.QIANFAN_API_KEY }}"
ACI_API_KEY: "${{ secrets.ACI_API_KEY }}"
BOHRIUM_API_KEY: "${{ secrets.BOHRIUM_API_KEY }}"
CRYNUX_API_KEY: "${{ secrets.CRYNUX_API_KEY }}"
@@ -239,6 +241,7 @@ jobs:
NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"
WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"
WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"
+ QIANFAN_API_KEY: "${{ secrets.QIANFAN_API_KEY }}"
ACI_API_KEY: "${{ secrets.ACI_API_KEY }}"
BOHRIUM_API_KEY: "${{ secrets.BOHRIUM_API_KEY }}"
CRYNUX_API_KEY: "${{ secrets.CRYNUX_API_KEY }}"
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.py
index 0edf927db..73d26adfd 100644
--- a/camel/configs/__init__.py
+++ b/camel/configs/__init__.py
@@ -33,6 +33,7 @@ from .ollama_config import OLLAMA_API_PARAMS, OllamaConfig
from .openai_config import OPENAI_API_PARAMS, ChatGPTConfig
from .openrouter_config import OPENROUTER_API_PARAMS, OpenRouterConfig
from .ppio_config import PPIO_API_PARAMS, PPIOConfig
+from .qianfan_config import QIANFAN_API_PARAMS, QianfanConfig
from .qwen_config import QWEN_API_PARAMS, QwenConfig
from .reka_config import REKA_API_PARAMS, RekaConfig
from .samba_config import (
@@ -113,6 +114,8 @@ __all__ = [
'LMStudioConfig',
'WatsonXConfig',
'WATSONX_API_PARAMS',
+ 'QianfanConfig',
+ 'QIANFAN_API_PARAMS',
'CrynuxConfig',
'CRYNUX_API_PARAMS',
]
diff --git a/camel/configs/qianfan_config.py b/camel/configs/qianfan_config.py
new file mode 100644
index 000000000..f9fa33b65
--- /dev/null
+++ b/camel/configs/qianfan_config.py
@@ -0,0 +1,85 @@
+# ========= 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 Dict, List, Optional, Union
+
+from pydantic import Field
+
+from camel.configs.base_config import BaseConfig
+from camel.types import NotGiven
+
+
+class QianfanConfig(BaseConfig):
+ r"""Defines the parameters for generating chat completions using OpenAI
+ compatibility.
+
+ Reference: https://cloud.baidu.com/doc/qianfan-api/s/3m7of64lb
+
+ Args:
+ 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:`None`)
+ 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:`None`)
+ repetition_penalty (float, optional): Penalizes new tokens based on
+ their appearance in the prompt and generated text.
+ (default: :obj:`None`)
+ stream (bool, optional): Whether to stream the response.
+ (default: :obj:`None`)
+ temperature (float, optional): Controls randomness in the response.
+ Higher values make output more random, lower values make it more
+ deterministic. Range: [0.0, 2.0]. (default: :obj:`None`)
+ top_p (float, optional): Controls diversity via nucleus sampling.
+ Range: [0.0, 1.0]. (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:`None`)
+ max_tokens (Union[int, NotGiven], optional): Maximum number of tokens
+ to generate. If not provided, model will use its default maximum.
+ (default: :obj:`None`)
+ stop (Optional[List[str]], optional): List of stop sequences.
+ (default: :obj:`None`)
+ n (Optional[int], optional): Number of chat completion choices to
+ generate for each input message. (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`)
+ """
+
+ stream: Optional[bool] = Field(default=None)
+ temperature: Optional[float] = Field(default=None)
+ top_p: Optional[float] = Field(default=None)
+ presence_penalty: Optional[float] = Field(default=None)
+ frequency_penalty: Optional[float] = Field(default=None)
+ repetition_penalty: Optional[float] = Field(default=None)
+ max_tokens: Optional[Union[int, NotGiven]] = Field(default=None)
+ stop: Optional[List[str]] = Field(default=None)
+ n: Optional[int] = Field(default=None)
+ logit_bias: Optional[Dict[str, float]] = Field(default=None)
+
+
+QIANFAN_API_PARAMS = {param for param in QianfanConfig.model_fields.keys()}
diff --git a/camel/models/__init__.py b/camel/models/__init__.py
index e7152122e..86dd4a007 100644
--- a/camel/models/__init__.py
+++ b/camel/models/__init__.py
@@ -41,6 +41,7 @@ from .openai_compatible_model import OpenAICompatibleModel
from .openai_model import OpenAIModel
from .openrouter_model import OpenRouterModel
from .ppio_model import PPIOModel
+from .qianfan_model import QianfanModel
from .qwen_model import QwenModel
from .reka_model import RekaModel
from .samba_model import SambaModel
@@ -97,5 +98,6 @@ __all__ = [
'VolcanoModel',
'LMStudioModel',
'WatsonXModel',
+ 'QianfanModel',
'CrynuxModel',
]
diff --git a/camel/models/model_factory.py b/camel/models/model_factory.py
index 7f7303d12..2213a9b32 100644
--- a/camel/models/model_factory.py
+++ b/camel/models/model_factory.py
@@ -39,6 +39,7 @@ from camel.models.openai_compatible_model import OpenAICompatibleModel
from camel.models.openai_model import OpenAIModel
from camel.models.openrouter_model import OpenRouterModel
from camel.models.ppio_model import PPIOModel
+from camel.models.qianfan_model import QianfanModel
from camel.models.qwen_model import QwenModel
from camel.models.reka_model import RekaModel
from camel.models.samba_model import SambaModel
@@ -98,6 +99,7 @@ class ModelFactory:
ModelPlatformType.MODELSCOPE: ModelScopeModel,
ModelPlatformType.NOVITA: NovitaModel,
ModelPlatformType.WATSONX: WatsonXModel,
+ ModelPlatformType.QIANFAN: QianfanModel,
ModelPlatformType.CRYNUX: CrynuxModel,
}
diff --git a/camel/models/qianfan_model.py b/camel/models/qianfan_model.py
new file mode 100644
index 000000000..f3d479ba8
--- /dev/null
+++ b/camel/models/qianfan_model.py
@@ -0,0 +1,96 @@
+# ========= 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, Optional, Union
+
+from camel.configs import QIANFAN_API_PARAMS, QianfanConfig
+from camel.models.openai_compatible_model import OpenAICompatibleModel
+from camel.types import ModelType
+from camel.utils import (
+ BaseTokenCounter,
+ api_keys_required,
+)
+
+
+class QianfanModel(OpenAICompatibleModel):
+ r"""Constructor for Qianfan backend with OpenAI compatibility.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, supported model can be found here:
+ https://cloud.baidu.com/doc/QIANFANWORKSHOP/s/Wm9cvy6rl
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`QianfanConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the Qianfan service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Qianfan service.
+ If not provided, "https://qianfan.baidubce.com/v2/chat/completions"
+ 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.
+ timeout (Optional[float], optional): The timeout value in seconds for
+ API calls. If not provided, will fall back to the MODEL_TIMEOUT
+ environment variable or default to 180 seconds.
+ (default: :obj:`None`)
+ """
+
+ @api_keys_required(
+ [
+ ("api_key", 'QIANFAN_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,
+ timeout: Optional[float] = None,
+ ) -> None:
+ if model_config_dict is None:
+ model_config_dict = QianfanConfig().as_dict()
+ api_key = api_key or os.environ.get("QIANFAN_API_KEY")
+ url = url or os.environ.get(
+ "QIANFAN_API_BASE_URL",
+ "https://qianfan.baidubce.com/v2",
+ )
+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
+ super().__init__(
+ model_type=model_type,
+ model_config_dict=model_config_dict,
+ api_key=api_key,
+ url=url,
+ token_counter=token_counter,
+ timeout=timeout,
+ )
+
+ def check_model_config(self):
+ r"""Check whether the model configuration contains any
+ unexpected arguments to Qianfan API.
+
+ Raises:
+ ValueError: If the model configuration dictionary contains any
+ unexpected arguments to Qianfan API.
+ """
+ for param in self.model_config_dict:
+ if param not in QIANFAN_API_PARAMS:
+ raise ValueError(
+ f"Unexpected argument `{param}` is "
+ "input into QIANFAN model backend."
+ )
diff --git a/camel/types/enums.py b/camel/types/enums.py
index d55842520..1dbf1a75d 100644
--- a/camel/types/enums.py
+++ b/camel/types/enums.py
@@ -386,6 +386,16 @@ class ModelType(UnifiedModelType, Enum):
)
WATSONX_MISTRAL_LARGE = "mistralai/mistral-large"
+ # Qianfan models
+ QIANFAN_ERNIE_X1_TURBO_32K = "ernie-x1-turbo-32k"
+ QIANFAN_ERNIE_X1_32K = "ernie-x1-32k"
+ QIANFAN_ERNIE_X1_32K_PREVIEW = "ernie-x1-32k-preview"
+ QIANFAN_ERNIE_4_5_TURBO_128K = "ernie-4.5-turbo-128k"
+ QIANFAN_ERNIE_4_5_TURBO_32K = "ernie-4.5-turbo-32k"
+ QIANFAN_DEEPSEEK_V3 = "deepseek-v3"
+ QIANFAN_DEEPSEEK_R1 = "deepseek-r1"
+ QIANFAN_QWEN3_235B_A22B = "qwen3-235b-a22b"
+
# Crynux models
CRYNUX_DEEPSEEK_R1_DISTILL_QWEN_1_5B = (
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
@@ -867,6 +877,19 @@ class ModelType(UnifiedModelType, Enum):
ModelType.WATSONX_MISTRAL_LARGE,
}
+ @property
+ def is_qianfan(self) -> bool:
+ return self in {
+ ModelType.QIANFAN_ERNIE_X1_TURBO_32K,
+ ModelType.QIANFAN_ERNIE_X1_32K,
+ ModelType.QIANFAN_ERNIE_X1_32K_PREVIEW,
+ ModelType.QIANFAN_ERNIE_4_5_TURBO_128K,
+ ModelType.QIANFAN_ERNIE_4_5_TURBO_32K,
+ ModelType.QIANFAN_DEEPSEEK_V3,
+ ModelType.QIANFAN_DEEPSEEK_R1,
+ ModelType.QIANFAN_QWEN3_235B_A22B,
+ }
+
@property
def is_novita(self) -> bool:
return self in {
@@ -1038,6 +1061,11 @@ class ModelType(UnifiedModelType, Enum):
ModelType.CRYNUX_QWEN_2_5_7B_INSTRUCT,
ModelType.CRYNUX_NOUS_HERMES_3_LLAMA_3_1_8B,
ModelType.CRYNUX_NOUS_HERMES_3_LLAMA_3_2_3B,
+ ModelType.QIANFAN_ERNIE_X1_TURBO_32K,
+ ModelType.QIANFAN_ERNIE_X1_32K,
+ ModelType.QIANFAN_ERNIE_X1_32K_PREVIEW,
+ ModelType.QIANFAN_ERNIE_4_5_TURBO_32K,
+ ModelType.QIANFAN_QWEN3_235B_A22B,
}:
return 32_000
elif self in {
@@ -1118,6 +1146,7 @@ class ModelType(UnifiedModelType, Enum):
return 65_535
elif self in {
ModelType.NOVITA_QWEN_2_5_V1_72B,
+ ModelType.QIANFAN_DEEPSEEK_R1,
}:
return 96_000
elif self in {
@@ -1171,6 +1200,8 @@ class ModelType(UnifiedModelType, Enum):
ModelType.NETMIND_DEEPSEEK_V3,
ModelType.NOVITA_DEEPSEEK_V3_0324,
ModelType.MISTRAL_MEDIUM_3,
+ ModelType.QIANFAN_ERNIE_4_5_TURBO_128K,
+ ModelType.QIANFAN_DEEPSEEK_V3,
}:
return 128_000
elif self in {
@@ -1502,6 +1533,7 @@ class ModelPlatformType(Enum):
NETMIND = "netmind"
NOVITA = "novita"
WATSONX = "watsonx"
+ QIANFAN = "qianfan"
CRYNUX = "crynux"
@classmethod
diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.py
index 3ecef7b23..e47d0666b 100644
--- a/camel/types/unified_model_type.py
+++ b/camel/types/unified_model_type.py
@@ -163,6 +163,11 @@ class UnifiedModelType(str):
r"""Returns whether the model is a WatsonX served model."""
return True
+ @property
+ def is_qianfan(self) -> bool:
+ r"""Returns whether the model is a Qianfan served model."""
+ return True
+
@property
def is_crynux(self) -> bool:
r"""Returns whether the model is a Crynux served model."""
diff --git a/docs/key_modules/models.md b/docs/key_modules/models.md
index 12c1054b6..abe42d229 100644
--- a/docs/key_modules/models.md
+++ b/docs/key_modules/models.md
@@ -41,6 +41,8 @@ CAMEL supports a wide range of models, including [OpenAI’s GPT series](https:/
| **AWS Bedrock** | [supported models](https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/) |
| **IBM WatsonX** | [supported models](https://jp-tok.dataplatform.cloud.ibm.com/samples?context=wx&tab=foundation-model) |
| **Crynux** | [supported models](https://docs.crynux.ai/application-development/how-to-run-llm-using-crynux-network/supported-models) |
+| **qianfan** | [supported models](https://cloud.baidu.com/doc/QIANFANWORKSHOP/s/Wm9cvy6rl) |
+
## 3. How to Use Models via API Calls
diff --git a/examples/models/qianfan_model_example.py b/examples/models/qianfan_model_example.py
new file mode 100644
index 000000000..7e5bb382e
--- /dev/null
+++ b/examples/models/qianfan_model_example.py
@@ -0,0 +1,55 @@
+# ========= 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.configs import QianfanConfig
+from camel.models import ModelFactory
+from camel.types import ModelPlatformType, ModelType
+
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.QIANFAN,
+ model_type=ModelType.QIANFAN_ERNIE_X1_TURBO_32K,
+ model_config_dict=QianfanConfig(temperature=0.2).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 = """how many 'r' in the word 'strawberry?'"""
+
+# Get response information
+response = camel_agent.step(user_msg)
+print(response.msgs[0].content)
+'''
+===============================================================================
+The word 'strawberry' is spelled **S-T-R-A-W-B-E-R-R-Y**.
+
+Breaking it down letter by letter:
+1. S
+2. T
+3. **R**
+4. A
+5. W
+6. B
+7. E
+8. **R**
+9. **R**
+10. Y
+
+There are **3 instances of the letter 'r'** in 'strawberry'.
+===============================================================================
+
+'''
diff --git a/test/models/test_qianfan_model.py b/test/models/test_qianfan_model.py
new file mode 100644
index 000000000..679cf629b
--- /dev/null
+++ b/test/models/test_qianfan_model.py
@@ -0,0 +1,74 @@
+# ========= 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 QianfanConfig
+from camel.models import QianfanModel
+from camel.types import ModelType
+from camel.utils import OpenAITokenCounter
+
+
+ "model_type",
+ [
+ "ernie-x1-turbo-32k",
+ "ernie-x1-32k",
+ "ernie-x1-32k-preview",
+ "ernie-4.5-turbo-128k",
+ "ernie-4.5-turbo-32k",
+ "deepseek-v3",
+ "deepseek-r1",
+ "qwen3-235b-a22b",
+ ],
+)
+def test_qianfan_model(model_type: ModelType):
+ model = QianfanModel(model_type)
+
+ assert model.model_type == model_type
+
+ assert model.model_config_dict == QianfanConfig().as_dict()
+
+ assert isinstance(model.token_counter, OpenAITokenCounter)
+
+ assert isinstance(model.model_type.value_for_tiktoken, str)
+
+ assert isinstance(model.model_type.token_limit, int)
+
+
+def test_qianfan_model_unexpected_argument():
+ model_type = ModelType.QIANFAN_ERNIE_X1_TURBO_32K
+
+ model_config_dict = {"model_path": "ernie-x1-turbo-32k"}
+
+ with pytest.raises(
+ ValueError,
+ match=re.escape(
+ (
+ "Unexpected argument `model_path` is "
+ "input into QIANFAN model backend."
+ )
+ ),
+ ):
+ _ = QianfanModel(model_type, model_config_dict)
+
+
+def test_qianfan_model_stream_property():
+ model = QianfanModel(ModelType.QIANFAN_ERNIE_X1_TURBO_32K)
+
+ assert model.stream is False