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

fix: O1 models not support system roles (#1923)

Raye8d284422dd9
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index 34f558790..9e01ffb2b 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -104,7 +104,7 @@ class OpenAIModel(BaseModelBackend):
)
def _sanitize_config(self, config_dict: Dict[str, Any]) -> Dict[str, Any]:
- """Sanitize the model configuration for O1 models."""
+ r"""Sanitize the model configuration for O1 models."""
if self.model_type in [
ModelType.O1,
@@ -113,7 +113,7 @@ class OpenAIModel(BaseModelBackend):
ModelType.O3_MINI,
]:
warnings.warn(
- "Warning: You are using an O1 model (O1_MINI or O1_PREVIEW), "
+ "Warning: You are using an reasoning model (O1 or O3), "
"which has certain limitations, reference: "
"`https://platform.openai.com/docs/guides/reasoning`.",
UserWarning,
@@ -125,6 +125,52 @@ class OpenAIModel(BaseModelBackend):
}
return config_dict
+ def _adapt_messages_for_o1_models(
+ self, messages: List[OpenAIMessage]
+ ) -> List[OpenAIMessage]:
+ r"""Adjust message roles to comply with O1 model requirements by
+ converting 'system' or 'developer' to 'user' role.
+
+ Args:
+ messages (List[OpenAIMessage]): Message list with the chat history
+ in OpenAI API format.
+
+ Returns:
+ processed_messages (List[OpenAIMessage]): Return a new list of
+ messages to avoid mutating input.
+ """
+
+ # Define supported O1 model types as a class constant would be better
+ O1_MODEL_TYPES = {ModelType.O1_MINI, ModelType.O1_PREVIEW}
+
+ if self.model_type not in O1_MODEL_TYPES:
+ return messages.copy()
+
+ # Issue warning only once using class state
+ if not hasattr(self, "_o1_warning_issued"):
+ warnings.warn(
+ "O1 models (O1_MINI/O1_PREVIEW) have role limitations: "
+ "System or Developer messages will be converted to user role."
+ "Reference: https://community.openai.com/t/"
+ "developer-role-not-accepted-for-o1-o1-mini-o3-mini/1110750/7",
+ UserWarning,
+ stacklevel=2,
+ )
+ self._o1_warning_issued = True
+
+ # Create new message list to avoid mutating input
+ processed_messages = []
+ for message in messages:
+ processed_message = message.copy()
+ if (
+ processed_message["role"] == "system"
+ or processed_message["role"] == "developer"
+ ):
+ processed_message["role"] = "user" # type: ignore[arg-type]
+ processed_messages.append(processed_message)
+
+ return processed_messages
+
@property
def token_counter(self) -> BaseTokenCounter:
r"""Initialize the token counter for the model backend.
@@ -158,6 +204,7 @@ class OpenAIModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[ChatCompletionChunk]` in the stream mode.
"""
+ messages = self._adapt_messages_for_o1_models(messages)
response_format = response_format or self.model_config_dict.get(
"response_format", None
)