🐫 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 Cerebras Platform Models. (#3424)
Srimadhava Keedd5bcb8198
diff --git a/.env.example b/.env.exampleindex f6c752d5c..b4c96a3d2 100644--- a/.env.example+++ b/.env.example@@ -10,6 +10,9 @@# Models API#===========================================+# Cerebras API (https://chat.cerebras.ai/)+# CEREBRAS_API_KEY="Fill your API key here"+# OpenAI API (https://platform.openai.com/signup)# OPENAI_API_KEY="Fill your API key here"diff --git a/camel/configs/__init__.py b/camel/configs/__init__.pyindex 620ab9150..dd3cead2e 100644--- a/camel/configs/__init__.py+++ b/camel/configs/__init__.py@@ -17,6 +17,7 @@ from .amd_config import AMD_API_PARAMS, AMDConfigfrom .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfigfrom .base_config import BaseConfigfrom .bedrock_config import BEDROCK_API_PARAMS, BedrockConfig+from .cerebras_config import CEREBRAS_API_PARAMS, CerebrasConfigfrom .cohere_config import COHERE_API_PARAMS, CohereConfigfrom .cometapi_config import COMETAPI_API_PARAMS, CometAPIConfigfrom .crynux_config import CRYNUX_API_PARAMS, CrynuxConfig@@ -93,6 +94,8 @@ __all__ = ['SAMBA_CLOUD_API_PARAMS','TogetherAIConfig','TOGETHERAI_API_PARAMS',+ 'CerebrasConfig',+ 'CEREBRAS_API_PARAMS','CohereConfig','COHERE_API_PARAMS','CometAPIConfig',diff --git a/camel/configs/cerebras_config.py b/camel/configs/cerebras_config.pynew file mode 100644index 000000000..31839c100--- /dev/null+++ b/camel/configs/cerebras_config.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. =========+from __future__ import annotations++from typing import Dict, Optional, Sequence, Union++from camel.configs.base_config import BaseConfig+++class CerebrasConfig(BaseConfig):+ r"""Defines the parameters for generating chat completions using Cerebras+ compatibility.++ Reference: https://inference-docs.cerebras.ai/resources/openai++ Args:+ temperature (float, optional): Sampling temperature to use, between+ :obj:`0` and :obj:`2`. Higher values make the output more random,+ while lower values make it more focused and deterministic.+ (default: :obj:`None`)++ top_p (float, optional): An alternative to sampling with temperature,+ called nucleus sampling, where the model considers the results of+ the tokens with top_p probability mass. So :obj:`0.1` means only+ the tokens comprising the top 10% probability mass are considered.+ (default: :obj:`None`)++ response_format (object, optional): An object specifying the format+ that the model must output.Setting to {"type": "json_object"}+ enables JSON mode, which guarantees the message the model+ generates is valid JSON. (default: :obj:`None`)++ stream (bool, optional): If True, partial message deltas will be sent+ as data-only server-sent events as they become available.+ (default: :obj:`None`)++ 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+ in the chat completion. The total length of input tokens and+ generated tokens is limited by the model's context length.+ (default: :obj:`None`)++ user (str, optional): A unique identifier representing your end-user,+ which can help OpenAI to monitor and detect abuse.+ (default: :obj:`None`)++ 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.++ tool_choice (Union[dict[str, str], str], optional): Controls which (if+ any) tool is called by the model. :obj:`"none"` means the model+ will not call any tool and instead generates a message.+ :obj:`"auto"` means the model can pick between generating a+ message or calling one or more tools. :obj:`"required"` means the+ model must call one or more tools. Specifying a particular tool+ via {"type": "function", "function": {"name": "my_function"}}+ forces the model to call that tool. :obj:`"none"` is the default+ when no tools are present. :obj:`"auto"` is the default if tools+ are present.++ reasoning_effort(str, optional): A parameter specifying the level of+ reasoning used by certain model types. Valid values are :obj:+ `"low"`, :obj:`"medium"`, or :obj:`"high"`. If set, it is only+ applied to the model types that support it (e.g., :obj:`o1`,+ :obj:`o1mini`, :obj:`o1preview`, :obj:`o3mini`). If not provided+ or if the model type does not support it, this parameter is+ ignored. (default: :obj:`None`)+ """++ temperature: Optional[float] = None+ top_p: Optional[float] = None+ stream: Optional[bool] = None+ stop: Optional[Union[str, Sequence[str]]] = None+ max_tokens: Optional[int] = None+ response_format: Optional[Dict] = None+ user: Optional[str] = None+ tool_choice: Optional[Union[Dict[str, str], str]] = None+ reasoning_effort: Optional[str] = None+++CEREBRAS_API_PARAMS = {param for param in CerebrasConfig.model_fields.keys()}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 55f728a40..bab622970 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -19,6 +19,7 @@ from .aws_bedrock_model import AWSBedrockModelfrom .azure_openai_model import AzureOpenAIModelfrom .base_audio_model import BaseAudioModelfrom .base_model import BaseModelBackend+from .cerebras_model import CerebrasModelfrom .cohere_model import CohereModelfrom .cometapi_model import CometAPIModelfrom .crynux_model import CrynuxModel@@ -71,6 +72,7 @@ __all__ = ['GroqModel','StubModel','ZhipuAIModel',+ 'CerebrasModel','CohereModel','CometAPIModel','ModelFactory',diff --git a/camel/models/cerebras_model.py b/camel/models/cerebras_model.pynew file mode 100644index 000000000..a1859a0f0--- /dev/null+++ b/camel/models/cerebras_model.py@@ -0,0 +1,83 @@+# ========= 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 CerebrasConfig+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelType+from camel.utils import (+ BaseTokenCounter,+ api_keys_required,+)+++class CerebrasModel(OpenAICompatibleModel):+ r"""LLM API served by Cerebras in a unified+ OpenAICompatibleModel 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:obj:`openai.ChatCompletion.create()`.+ If:obj:`None`, :obj:`CerebrasConfig().as_dict()` will be used.+ (default: :obj:`None`)+ api_key (Optional[str], optional): The API key for authenticating+ with the Cerebras service. (default: :obj:`None`).+ url (Optional[str], optional): The url to the Cerebras service.+ (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`)+ 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`)+ max_retries (int, optional): Maximum number of retries for API calls.+ (default: :obj:`3`)+ **kwargs (Any): Additional arguments to pass to the client+ initialization.+ """++ @api_keys_required([("api_key", "CEREBRAS_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,+ max_retries: int = 3,+ **kwargs: Any,+ ) -> None:+ if model_config_dict is None:+ model_config_dict = CerebrasConfig().as_dict()+ api_key = api_key or os.environ.get("CEREBRAS_API_KEY")+ url = url or os.environ.get(+ "CEREBRAS_API_BASE_URL", "https://api.cerebras.ai/v1"+ )+ 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,+ max_retries=max_retries,+ **kwargs,+ )diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 6d05dcdfe..ad9a0da89 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -22,6 +22,7 @@ from camel.models.anthropic_model import AnthropicModelfrom camel.models.aws_bedrock_model import AWSBedrockModelfrom camel.models.azure_openai_model import AzureOpenAIModelfrom camel.models.base_model import BaseModelBackend+from camel.models.cerebras_model import CerebrasModelfrom camel.models.cohere_model import CohereModelfrom camel.models.cometapi_model import CometAPIModelfrom camel.models.crynux_model import CrynuxModel@@ -89,6 +90,7 @@ class ModelFactory:ModelPlatformType.AZURE: AzureOpenAIModel,ModelPlatformType.ANTHROPIC: AnthropicModel,ModelPlatformType.GROQ: GroqModel,+ ModelPlatformType.CEREBRAS: CerebrasModel,ModelPlatformType.COMETAPI: CometAPIModel,ModelPlatformType.NEBIUS: NebiusModel,ModelPlatformType.LMSTUDIO: LMStudioModel,diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 656ec643c..61fdbb5fb 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -91,6 +91,12 @@ class ModelType(UnifiedModelType, Enum):GROQ_MIXTRAL_8_7B = "mixtral-8x7b-32768"GROQ_GEMMA_2_9B_IT = "gemma2-9b-it"+ # Cerebras platform models+ CEREBRAS_GPT_OSS_120B = "gpt-oss-120b"+ CEREBRAS_LLAMA_3_1_8B = "llama3.1-8b"+ CEREBRAS_LLAMA_3_3_70B = "llama3.3-70b"+ CEREBRAS_QWEN_3_32B = "qwen-3-32b"+# Nebius AI Studio platform modelsNEBIUS_GPT_OSS_120B = "gpt-oss-120b"NEBIUS_GPT_OSS_20B = "gpt-oss-20b"@@ -554,6 +560,7 @@ class ModelType(UnifiedModelType, Enum):self.is_together,self.is_sambanova,self.is_groq,+ self.is_cerebras,self.is_openrouter,self.is_lmstudio,self.is_sglang,@@ -696,6 +703,16 @@ class ModelType(UnifiedModelType, Enum):ModelType.GROQ_GEMMA_2_9B_IT,}+ @property+ def is_cerebras(self) -> bool:+ r"""Returns whether this type of models is served by Cerebras."""+ return self in {+ ModelType.CEREBRAS_GPT_OSS_120B,+ ModelType.CEREBRAS_LLAMA_3_1_8B,+ ModelType.CEREBRAS_LLAMA_3_3_70B,+ ModelType.CEREBRAS_QWEN_3_32B,+ }+@propertydef is_nebius(self) -> bool:r"""Returns whether this type of models is served by Nebius AI@@ -1165,6 +1182,7 @@ class ModelType(UnifiedModelType, Enum):}:return 4_096elif self in {+ ModelType.CEREBRAS_LLAMA_3_1_8B,ModelType.GPT_4,ModelType.GROQ_LLAMA_3_8B,ModelType.GROQ_LLAMA_3_70B,@@ -1312,6 +1330,9 @@ class ModelType(UnifiedModelType, Enum):return 32_768elif self in {ModelType.MISTRAL_MIXTRAL_8x22B,+ ModelType.CEREBRAS_GPT_OSS_120B,+ ModelType.CEREBRAS_LLAMA_3_3_70B,+ ModelType.CEREBRAS_QWEN_3_32B,ModelType.DEEPSEEK_CHAT,ModelType.DEEPSEEK_REASONER,ModelType.PPIO_DEEPSEEK_R1_TURBO,@@ -1801,6 +1822,7 @@ class ModelPlatformType(Enum):CRYNUX = "crynux"AIHUBMIX = "aihubmix"MINIMAX = "minimax"+ CEREBRAS = "cerebras"@classmethoddef from_name(cls, name):@@ -1991,6 +2013,11 @@ class ModelPlatformType(Enum):r"""Returns whether this platform is Minimax M2."""return self is ModelPlatformType.MINIMAX+ @property+ def is_cerebras(self) -> bool:+ r"""Returns whether this platform is Cerebras."""+ return self is ModelPlatformType.CEREBRAS+class AudioModelType(Enum):TTS_1 = "tts-1"diff --git a/examples/models/cerebras_model_example.py b/examples/models/cerebras_model_example.pynew file mode 100644index 000000000..5734a2c48--- /dev/null+++ b/examples/models/cerebras_model_example.py@@ -0,0 +1,48 @@+# ========= 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 CerebrasConfig+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++# Define system message+model = ModelFactory.create(+ model_platform=ModelPlatformType.CEREBRAS,+ model_type=ModelType.CEREBRAS_LLAMA_3_3_70B,+ model_config_dict=CerebrasConfig(temperature=0.2).as_dict(),+)++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 to the CAMEL AI community. It's great to see a group of like-minded+individuals coming together to explore and advance the field of autonomous and+communicative agents. Your open-source approach is truly commendable, as it+fosters collaboration, innovation, and transparency. I'm excited to learn more+about your projects and initiatives, and I'm happy to help in any way I can.+Keep pushing the boundaries of AI research and development!+===============================================================================+'''diff --git a/test/models/test_cerebras_model.py b/test/models/test_cerebras_model.pynew file mode 100644index 000000000..4f78f64b0--- /dev/null+++ b/test/models/test_cerebras_model.py@@ -0,0 +1,40 @@+# ========= 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 pytest++from camel.configs import CerebrasConfig+from camel.models import CerebrasModel+from camel.types import ModelType+from camel.utils import OpenAITokenCounter++[email protected]_backend+ "model_type",+ [+ ModelType.CEREBRAS_GPT_OSS_120B,+ ModelType.CEREBRAS_LLAMA_3_1_8B,+ ModelType.CEREBRAS_LLAMA_3_3_70B,+ ModelType.CEREBRAS_QWEN_3_32B,+ ],+)+def test_cerebras_model(model_type: ModelType):+ model_config_dict = CerebrasConfig().as_dict()+ model = CerebrasModel(model_type, model_config_dict)+ assert model.model_type == model_type+ assert model.model_config_dict == model_config_dict+ assert isinstance(model.token_counter, OpenAITokenCounter)+ assert isinstance(model.model_type.value_for_tiktoken, str)+ assert isinstance(model.model_type.token_limit, int)