diff --git a/camel/models/base_model.py b/camel/models/base_model.py index 84317fae8..8f08ca473 100644 --- a/camel/models/base_model.py +++ b/camel/models/base_model.py @@ -118,6 +118,9 @@ def count_tokens_from_messages(self, messages: List[OpenAIMessage]) -> int: 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 @@ def token_limit(self) -> int: @property def 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.py index 1b00011d9..774e3edd1 100644 --- a/camel/models/litellm_model.py +++ b/camel/models/litellm_model.py @@ -143,19 +143,3 @@ def check_model_config(self): 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 4096 diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.py index b298f2a61..c957a76de 100644 --- a/camel/models/ollama_model.py +++ b/camel/models/ollama_model.py @@ -142,22 +142,6 @@ def run( ) 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 - @property def stream(self) -> bool: r"""Returns whether the model is in stream mode, which sends partial diff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.py index 3754cea43..1796f28c9 100644 --- a/camel/models/openai_compatible_model.py +++ b/camel/models/openai_compatible_model.py @@ -112,21 +112,5 @@ def stream(self) -> bool: """ 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): pass diff --git a/camel/models/samba_model.py b/camel/models/samba_model.py index 53e025368..1ccb545e0 100644 --- a/camel/models/samba_model.py +++ b/camel/models/samba_model.py @@ -346,16 +346,6 @@ def _sambaverse_to_openai_response( 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 - @property def stream(self) -> bool: r"""Returns whether the model is in stream mode, which sends partial diff --git a/camel/models/togetherai_model.py b/camel/models/togetherai_model.py index aa19c2871..905bc7757 100644 --- a/camel/models/togetherai_model.py +++ b/camel/models/togetherai_model.py @@ -140,19 +140,3 @@ def stream(self) -> bool: 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 4096 diff --git a/camel/models/vllm_model.py b/camel/models/vllm_model.py index 5523e0212..9b8652556 100644 --- a/camel/models/vllm_model.py +++ b/camel/models/vllm_model.py @@ -144,22 +144,6 @@ def run( ) 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 - @property def stream(self) -> bool: r"""Returns whether the model is in stream mode, which sends partial diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.py index 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 logging from threading import Lock from typing import TYPE_CHECKING, ClassVar, Dict, Union, cast @@ -50,8 +50,13 @@ def value_for_tiktoken(self) -> str: @property def 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 @property def is_openai(self) -> bool: diff --git a/docs/key_modules/models.md b/docs/key_modules/models.md index 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.py index 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 @@ # Take calling nemotron-70b-instruct model as an example model = 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 @@ 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",