🐫 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: Remove <think> content in model request (#1613)
Wendong-Fan457e7c5dffc8
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex 75924e689..227c4e974 100644--- a/.github/ISSUE_TEMPLATE/bug_report.yml+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -26,7 +26,7 @@ body:attributes:label: What version of camel are you using?description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.- placeholder: E.g., 0.2.21+ placeholder: E.g., 0.2.22validations:required: truediff --git a/camel/__init__.py b/camel/__init__.pyindex 77b1760a7..8cb8df7f0 100644--- a/camel/__init__.py+++ b/camel/__init__.py@@ -14,7 +14,7 @@from camel.logger import disable_logging, enable_logging, set_log_level-__version__ = '0.2.21'+__version__ = '0.2.22'__all__ = ['__version__',diff --git a/camel/models/base_model.py b/camel/models/base_model.pyindex 37e9d4061..44ae64720 100644--- a/camel/models/base_model.py+++ b/camel/models/base_model.py@@ -11,6 +11,8 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import abc+import refrom abc import ABC, abstractmethodfrom typing import Any, Dict, List, Optional, Union@@ -27,7 +29,30 @@ from camel.types import (from camel.utils import BaseTokenCounter-class BaseModelBackend(ABC):+class ModelBackendMeta(abc.ABCMeta):+ r"""Metaclass that automatically preprocesses messages in run method.++ Automatically wraps the run method of any class inheriting from+ BaseModelBackend to preprocess messages (remove <think> tags) before they+ are sent to the model.+ """++ def __new__(mcs, name, bases, namespace):+ r"""Wraps run method with preprocessing if it exists in the class."""+ if 'run' in namespace:+ original_run = namespace['run']++ def wrapped_run(+ self, messages: List[OpenAIMessage], *args, **kwargs+ ):+ messages = self.preprocess_messages(messages)+ return original_run(self, messages, *args, **kwargs)++ namespace['run'] = wrapped_run+ return super().__new__(mcs, name, bases, namespace)+++class BaseModelBackend(ABC, metaclass=ModelBackendMeta):r"""Base class for different model backends.It may be OpenAI API, a local LLM, a stub for unit tests, etc.@@ -73,6 +98,34 @@ class BaseModelBackend(ABC):"""pass+ def preprocess_messages(+ self, messages: List[OpenAIMessage]+ ) -> List[OpenAIMessage]:+ r"""Preprocess messages before sending to model API.+ Removes thinking content and other model-specific preprocessing.++ Args:+ messages (List[OpenAIMessage]): Original messages++ Returns:+ List[OpenAIMessage]: Preprocessed messages+ """+ # Remove thinking content from messages before sending to API+ # This ensures only the final response is sent, excluding+ # intermediate thought processes+ return [+ { # type: ignore[misc]+ **msg,+ 'content': re.sub(+ r'<think>.*?</think>',+ '',+ msg['content'], # type: ignore[arg-type]+ flags=re.DOTALL,+ ).strip(),+ }+ for msg in messages+ ]+@abstractmethoddef run(self,diff --git a/camel/models/deepseek_model.py b/camel/models/deepseek_model.pyindex 40375749f..56b694742 100644--- a/camel/models/deepseek_model.py+++ b/camel/models/deepseek_model.py@@ -118,8 +118,6 @@ class DeepSeekModel(BaseModelBackend):if self.model_type in [ModelType.DEEPSEEK_REASONER,]:- import re-logger.warning("You are using a DeepSeek Reasoner model, ""which has certain limitations, reference: "@@ -141,22 +139,6 @@ class DeepSeekModel(BaseModelBackend):if key in self.model_config_dict:del self.model_config_dict[key]- # Remove thinking content from messages before sending to API- # This ensures only the final response is sent, excluding- # intermediate thought processes- messages = [- { # type: ignore[misc]- **msg,- 'content': re.sub(- r'<think>.*?</think>',- '',- msg['content'], # type: ignore[arg-type]- flags=re.DOTALL,- ).strip(),- }- for msg in messages- ]-response = self._client.chat.completions.create(messages=messages,model=self.model_type,diff --git a/docs/conf.py b/docs/conf.pyindex f767aa6e3..fdf039a8f 100644--- a/docs/conf.py+++ b/docs/conf.py@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))project = 'CAMEL'copyright = '2024, CAMEL-AI.org'author = 'CAMEL-AI.org'-release = '0.2.21'+release = '0.2.22'html_favicon = ('https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'diff --git a/docs/get_started/installation.md b/docs/get_started/installation.mdindex a978ca460..c85311ec3 100644--- a/docs/get_started/installation.md+++ b/docs/get_started/installation.md@@ -60,7 +60,7 @@ conda create --name camel python=3.10conda activate camel# Clone github repo-git clone -b v0.2.21 https://github.com/camel-ai/camel.git+git clone -b v0.2.22 https://github.com/camel-ai/camel.git# Change directory into project directorycd cameldiff --git a/docs/key_modules/loaders.md b/docs/key_modules/loaders.mdindex 0a74d9614..371a70c7d 100644--- a/docs/key_modules/loaders.md+++ b/docs/key_modules/loaders.md@@ -340,14 +340,14 @@ response = jina_reader.read_content("https://docs.camel-ai.org/")print(response)``````markdown->>>Welcome to CAMEL’s documentation! — CAMEL 0.2.21 documentation+>>>Welcome to CAMEL’s documentation! — CAMEL 0.2.22 documentation===============[Skip to main content](https://docs.camel-ai.org/#main-content)Back to top Ctrl+K- [ CAMEL 0.2.21](https://docs.camel-ai.org/#)+ [ CAMEL 0.2.22](https://docs.camel-ai.org/#)Search Ctrl+Kdiff --git a/pyproject.toml b/pyproject.tomlindex 5d6d679fe..7f42d885d 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"[tool.poetry]name = "camel-ai"-version = "0.2.21"+version = "0.2.22"authors = ["CAMEL-AI.org"]description = "Communicative Agents for AI Society Study"readme = "README.md"diff --git a/test/models/test_base_model.py b/test/models/test_base_model.pynew file mode 100644index 000000000..f84b7b1e1--- /dev/null+++ b/test/models/test_base_model.py@@ -0,0 +1,89 @@+# ========= 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.models import BaseModelBackend+from camel.types import ModelType+++class TestBaseModelBackend:+ r"""Unit tests for the BaseModelBackend class."""++ def test_preprocess_messages(self):+ r"""Test message preprocessing removes thinking content correctly."""++ class DummyModel(BaseModelBackend):+ @property+ def token_counter(self):+ pass++ def run(self, messages):+ pass++ def check_model_config(self):+ pass++ model = DummyModel(ModelType.GPT_4O_MINI)++ # Test basic thinking removal+ messages = [+ {+ 'role': 'user',+ 'content': 'Hello <think>thinking about response</think> '+ 'world',+ },+ {+ 'role': 'assistant',+ 'content': '<think>Let me think...\nThinking more...</'+ 'think>Response',+ },+ ]++ processed = model.preprocess_messages(messages)+ assert len(processed) == 2+ assert processed[0]['content'] == 'Hello world'+ assert processed[1]['content'] == 'Response'++ # Test message without thinking tags+ messages = [{'role': 'user', 'content': 'plain message'}]+ processed = model.preprocess_messages(messages)+ assert processed[0]['content'] == 'plain message'++ def test_metaclass_preprocessing(self):+ r"""Test that metaclass automatically preprocesses messages in run+ method."""+ processed_messages = None++ class TestModel(BaseModelBackend):+ @property+ def token_counter(self):+ pass++ def run(self, messages):+ nonlocal processed_messages+ processed_messages = messages+ return None++ def check_model_config(self):+ pass++ model = TestModel(ModelType.GPT_4O_MINI)+ messages = [+ {'role': 'user', 'content': 'Hello <think>hi</think> world'}+ ]++ # Call run method and verify messages were preprocessed+ model.run(messages)+ assert processed_messages is not None+ assert processed_messages[0]['content'] == 'Hello world'