🐫 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
fix: unify token_limit property (#1071)
Wendong-Fanc66fde566044
diff --git a/camel/models/base_model.py b/camel/models/base_model.pyindex 84317fae8..8f08ca473 100644--- a/camel/models/base_model.py+++ b/camel/models/base_model.py@@ -118,6 +118,9 @@ class BaseModelBackend(ABC):def token_limit(self) -> int:r"""Returns the maximum token limit for a given model.+ This method retrieves the maximum token limit either from the+ `model_config_dict` or from the model's default token limit.+Returns:int: The maximum token limit for the given model."""@@ -128,8 +131,8 @@ class BaseModelBackend(ABC):@propertydef stream(self) -> bool:- r"""Returns whether the model is in stream mode,- which sends partial results each time.+ r"""Returns whether the model is in stream mode, which sends partial+ results each time.Returns:bool: Whether the model is in stream mode.diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.pyindex 1b00011d9..774e3edd1 100644--- a/camel/models/litellm_model.py+++ b/camel/models/litellm_model.py@@ -143,19 +143,3 @@ class LiteLLMModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into LiteLLM model backend.")-- @property- def token_limit(self) -> int:- r"""Returns the maximum token limit for the given model.-- Returns:- int: The maximum token limit for the given model.- """- max_tokens = self.model_config_dict.get("max_tokens")- if isinstance(max_tokens, int):- return max_tokens- print(- "Must set `max_tokens` as an integer in `model_config_dict` when"- " setting up the model. Using 4096 as default value."- )- return 4096diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.pyindex b298f2a61..c957a76de 100644--- a/camel/models/ollama_model.py+++ b/camel/models/ollama_model.py@@ -142,22 +142,6 @@ class OllamaModel(BaseModelBackend):)return response- @property- def token_limit(self) -> int:- r"""Returns the maximum token limit for the given model.-- Returns:- int: The maximum token limit for the given model.- """- max_tokens = self.model_config_dict.get("max_tokens")- if isinstance(max_tokens, int):- return max_tokens- print(- "Must set `max_tokens` as an integer in `model_config_dict` when"- " setting up the model. Using 4096 as default value."- )- return 4096-@propertydef stream(self) -> bool:r"""Returns whether the model is in stream mode, which sends partialdiff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.pyindex 3754cea43..1796f28c9 100644--- a/camel/models/openai_compatible_model.py+++ b/camel/models/openai_compatible_model.py@@ -112,21 +112,5 @@ class OpenAICompatibleModel(BaseModelBackend):"""return self.model_config_dict.get('stream', False)- @property- def token_limit(self) -> int:- r"""Returns the maximum token limit for the given model.-- Returns:- int: The maximum token limit for the given model.- """- max_tokens = self.model_config_dict.get("max_tokens")- if isinstance(max_tokens, int):- return max_tokens- print(- "Must set `max_tokens` as an integer in `model_config_dict` when"- " setting up the model. Using 4096 as default value."- )- return 4096-def check_model_config(self):passdiff --git a/camel/models/samba_model.py b/camel/models/samba_model.pyindex 53e025368..1ccb545e0 100644--- a/camel/models/samba_model.py+++ b/camel/models/samba_model.py@@ -346,16 +346,6 @@ class SambaModel(BaseModelBackend):return obj- @property- def token_limit(self) -> int:- r"""Returns the maximum token limit for the given model.-- Returns:- int: The maximum token limit for the given model.- """- max_tokens = self.model_config_dict["max_tokens"]- return max_tokens-@propertydef stream(self) -> bool:r"""Returns whether the model is in stream mode, which sends partialdiff --git a/camel/models/togetherai_model.py b/camel/models/togetherai_model.pyindex aa19c2871..905bc7757 100644--- a/camel/models/togetherai_model.py+++ b/camel/models/togetherai_model.py@@ -140,19 +140,3 @@ class TogetherAIModel(BaseModelBackend):bool: Whether the model is in stream mode."""return self.model_config_dict.get('stream', False)-- @property- def token_limit(self) -> int:- r"""Returns the maximum token limit for the given model.-- Returns:- int: The maximum token limit for the given model.- """- max_tokens = self.model_config_dict.get("max_tokens")- if isinstance(max_tokens, int):- return max_tokens- print(- "Must set `max_tokens` as an integer in `model_config_dict` when"- " setting up the model. Using 4096 as default value."- )- return 4096diff --git a/camel/models/vllm_model.py b/camel/models/vllm_model.pyindex 5523e0212..9b8652556 100644--- a/camel/models/vllm_model.py+++ b/camel/models/vllm_model.py@@ -144,22 +144,6 @@ class VLLMModel(BaseModelBackend):)return response- @property- def token_limit(self) -> int:- r"""Returns the maximum token limit for the given model.-- Returns:- int: The maximum token limit for the given model.- """- max_tokens = self.model_config_dict.get("max_tokens")- if isinstance(max_tokens, int):- return max_tokens- print(- "Must set `max_tokens` as an integer in `model_config_dict` when"- " setting up the model. Using 4096 as default value."- )- return 4096-@propertydef stream(self) -> bool:r"""Returns whether the model is in stream mode, which sends partialdiff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.pyindex 2f5eeb358..880b31f97 100644--- a/camel/types/unified_model_type.py+++ b/camel/types/unified_model_type.py@@ -11,7 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========-+import loggingfrom threading import Lockfrom typing import TYPE_CHECKING, ClassVar, Dict, Union, cast@@ -50,8 +50,13 @@ class UnifiedModelType(str):@propertydef token_limit(self) -> int:- r"""Returns the token limit for the model."""- return -1+ r"""Returns the token limit for the model. Here we set the default+ value as `999_999_999` if it's not provided from `model_config_dict`"""+ logging.warning(+ "Invalid or missing `max_tokens` in `model_config_dict`. "+ "Defaulting to 999_999_999 tokens."+ )+ return 999_999_999@propertydef is_openai(self) -> bool:diff --git a/docs/key_modules/models.md b/docs/key_modules/models.mdindex 7b0615eed..01f7b3663 100644--- a/docs/key_modules/models.md+++ b/docs/key_modules/models.md@@ -84,13 +84,12 @@ ChatAgent(system_msg, model=model)And if you want to use an OpenAI-compatible API, you can replace the `model` with the following code:```python-from camel.models.openai_compatibility_model import OpenAICompatibilityModel--model = OpenAICompatibilityModel(+model = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,model_type="a-string-representing-the-model-type",- model_config_dict={"max_tokens": 4096}, # and other parameters you want- url=os.environ.get("OPENAI_COMPATIBILIY_API_BASE_URL"),api_key=os.environ.get("OPENAI_COMPATIBILIY_API_KEY"),+ url=os.environ.get("OPENAI_COMPATIBILIY_API_BASE_URL"),+ model_config_dict={"temperature": 0.4, "max_tokens": 4096},)```diff --git a/examples/models/openai_compatibility_model_examples/nemotron.py b/examples/models/openai_compatibility_model_examples/nemotron.pyindex a2151190f..b44e05dc5 100644--- a/examples/models/openai_compatibility_model_examples/nemotron.py+++ b/examples/models/openai_compatibility_model_examples/nemotron.py@@ -19,9 +19,9 @@ from camel.types import ModelPlatformType# Take calling nemotron-70b-instruct model as an examplemodel = ModelFactory.create(- model_platform=ModelPlatformType.OPENAI_COMPATIBILITY_MODEL,+ model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,model_type="nvidia/llama-3.1-nemotron-70b-instruct",- api_key="nvapi-xxx",+ api_key="nvapi-xx",url="https://integrate.api.nvidia.com/v1",model_config_dict={"temperature": 0.4},)@@ -31,7 +31,7 @@ assistant_sys_msg = BaseMessage.make_assistant_message(content="You are a helpful assistant.",)-agent = ChatAgent(assistant_sys_msg, model=model, token_limit=4096)+agent = ChatAgent(assistant_sys_msg, model=model)user_msg = BaseMessage.make_user_message(role_name="User",