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

feat: Sharegpt conversion (#1144)

CaelumF333c9d9f823b
diff --git a/camel/messages/__init__.py b/camel/messages/__init__.py
index 870626024..b0779f153 100644
--- a/camel/messages/__init__.py
+++ b/camel/messages/__init__.py
@@ -19,12 +19,24 @@ from camel.types import (
ChatCompletionUserMessageParam,
)
+from .conversion import (
+ HermesFunctionFormatter,
+ ShareGPTMessage,
+)
+from .conversion.models import (
+ ShareGPTConversation,
+)
+from .conversion.sharegpt.function_call_formatter import (
+ FunctionCallFormatter,
+)
+
OpenAISystemMessage = ChatCompletionSystemMessageParam
OpenAIAssistantMessage = ChatCompletionAssistantMessageParam
OpenAIUserMessage = ChatCompletionUserMessageParam
OpenAIFunctionMessage = ChatCompletionFunctionMessageParam
OpenAIMessage = ChatCompletionMessageParam
+
from .base import BaseMessage # noqa: E402
from .func_message import FunctionCallingMessage # noqa: E402
@@ -34,6 +46,10 @@ __all__ = [
'OpenAIUserMessage',
'OpenAIFunctionMessage',
'OpenAIMessage',
+ 'FunctionCallFormatter',
+ 'HermesFunctionFormatter',
+ 'ShareGPTConversation',
+ 'ShareGPTMessage',
'BaseMessage',
'FunctionCallingMessage',
]
diff --git a/camel/messages/base.py b/camel/messages/base.py
index 19017edb2..e2ec9dd14 100644
--- a/camel/messages/base.py
+++ b/camel/messages/base.py
@@ -13,6 +13,7 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import base64
import io
+import re
from dataclasses import dataclass
from typing import Any, Dict, List, Literal, Optional, Tuple, Union
@@ -20,11 +21,14 @@ import numpy as np
from PIL import Image
from camel.messages import (
+ FunctionCallFormatter,
+ HermesFunctionFormatter,
OpenAIAssistantMessage,
OpenAIMessage,
OpenAISystemMessage,
OpenAIUserMessage,
)
+from camel.messages.conversion import ShareGPTMessage
from camel.prompts import CodePrompt, TextPrompt
from camel.types import (
OpenAIBackendRole,
@@ -271,6 +275,106 @@ class BaseMessage:
return text_prompts, code_prompts
+ @classmethod
+ def from_sharegpt(
+ cls,
+ message: ShareGPTMessage,
+ function_format: Optional[FunctionCallFormatter[Any, Any]] = None,
+ role_mapping=None,
+ ) -> "BaseMessage":
+ r"""Convert ShareGPT message to BaseMessage or FunctionCallingMessage.
+ Note tool calls and responses have an 'assistant' role in CAMEL
+
+ Args:
+ message (ShareGPTMessage): ShareGPT message to convert.
+ function_format (FunctionCallFormatter, optional): Function call
+ formatter to use. (default: :obj:`HermesFunctionFormatter()`.
+ role_mapping (Dict[str, List[str, RoleType]], optional): Role
+ mapping to use. Defaults to a CAMEL specific mapping.
+
+ Returns:
+ BaseMessage: Converted message.
+ """
+ from camel.messages import FunctionCallingMessage
+
+ if role_mapping is None:
+ role_mapping = {
+ "system": ["system", RoleType.USER],
+ "human": ["user", RoleType.USER],
+ "gpt": ["assistant", RoleType.ASSISTANT],
+ "tool": ["assistant", RoleType.ASSISTANT],
+ }
+ role_name, role_type = role_mapping[message.from_]
+
+ if function_format is None:
+ function_format = HermesFunctionFormatter()
+
+ # Check if this is a function-related message
+ if message.from_ == "gpt":
+ func_info = function_format.extract_tool_calls(message.value)
+ if (
+ func_info and len(func_info) == 1
+ ): # TODO: Handle multiple tool calls
+ # Including cleaned content is useful to
+ # remind consumers of non-considered content
+ clean_content = re.sub(
+ r"<tool_call>.*?</tool_call>",
+ "",
+ message.value,
+ flags=re.DOTALL,
+ ).strip()
+
+ return FunctionCallingMessage(
+ role_name=role_name,
+ role_type=role_type,
+ meta_dict=None,
+ content=clean_content,
+ func_name=func_info[0].__dict__["name"],
+ args=func_info[0].__dict__["arguments"],
+ )
+ elif message.from_ == "tool":
+ func_r_info = function_format.extract_tool_response(message.value)
+ if func_r_info:
+ return FunctionCallingMessage(
+ role_name=role_name,
+ role_type=role_type,
+ meta_dict=None,
+ content="",
+ func_name=func_r_info.__dict__["name"],
+ result=func_r_info.__dict__["content"],
+ )
+
+ # Regular message
+ return cls(
+ role_name=role_name,
+ role_type=role_type,
+ meta_dict=None,
+ content=message.value,
+ )
+
+ def to_sharegpt(
+ self,
+ function_format: Optional[FunctionCallFormatter] = None,
+ ) -> ShareGPTMessage:
+ r"""Convert BaseMessage to ShareGPT message
+
+ Args:
+ function_format (FunctionCallFormatter): Function call formatter
+ to use. Defaults to Hermes.
+ """
+
+ if function_format is None:
+ function_format = HermesFunctionFormatter()
+
+ # Convert role type to ShareGPT 'from' field
+ if self.role_type == RoleType.USER:
+ from_ = "system" if self.role_name == "system" else "human"
+ else: # RoleType.ASSISTANT
+ from_ = "gpt"
+
+ # Function conversion code in FunctionCallingMessage
+ return ShareGPTMessage(from_=from_, value=self.content) # type: ignore[call-arg]
+
def to_openai_message(
self,
role_at_backend: OpenAIBackendRole,
diff --git a/camel/messages/conversion/__init__.py b/camel/messages/conversion/__init__.py
new file mode 100644
index 000000000..1a04972f5
--- /dev/null
+++ b/camel/messages/conversion/__init__.py
@@ -0,0 +1,29 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from .models import (
+ ShareGPTConversation,
+ ShareGPTMessage,
+ ToolCall,
+ ToolResponse,
+)
+from .sharegpt import HermesFunctionFormatter
+
+__all__ = [
+ 'ShareGPTMessage',
+ 'ShareGPTConversation',
+ 'HermesFunctionFormatter',
+ 'ToolCall',
+ 'ToolResponse',
+]
diff --git a/camel/messages/conversion/models.py b/camel/messages/conversion/models.py
new file mode 100644
index 000000000..6f468c747
--- /dev/null
+++ b/camel/messages/conversion/models.py
@@ -0,0 +1,178 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+import json
+from typing import Any, Dict, List, Literal
+
+from pydantic import (
+ BaseModel,
+ Field,
+ RootModel,
+ field_validator,
+ model_validator,
+)
+
+
+class ShareGPTMessage(BaseModel):
+ r"""A single message in ShareGPT format with enhanced validation"""
+
+ from_: Literal["human", "gpt", "system", "tool"] = Field(
+ alias="from", description="The role of the message sender"
+ )
+ value: str = Field(
+ min_length=0,
+ max_length=100000,
+ description="The content of the message",
+ )
+
+ model_config = {
+ "populate_by_name": True,
+ "extra": "forbid",
+ "json_schema_extra": {
+ "examples": [
+ {"from": "human", "value": "What's the weather like today?"}
+ ]
+ },
+ }
+
+
+class ShareGPTConversation(RootModel):
+ r"""A full conversation in ShareGPT format with validation"""
+
+ root: List[ShareGPTMessage]
+
+ @model_validator(mode='after')
+ def validate_conversation_flow(self) -> 'ShareGPTConversation':
+ r"""Validate the conversation follows logical message order"""
+ messages = self.root
+
+ if not messages:
+ raise ValueError("Conversation cannot be empty")
+
+ if messages[0].from_ not in ("system", "human"):
+ raise ValueError(
+ "Conversation must start with either system or human message"
+ )
+
+ # Validate message sequence
+ for i in range(1, len(messages)):
+ curr, prev = messages[i], messages[i - 1]
+
+ if curr.from_ == "tool":
+ if prev.from_ != "gpt" or "<tool_call>" not in prev.value:
+ raise ValueError(
+ f"Tool response at position {i} "
+ f"must follow an gpt message with a tool call"
+ )
+
+ if curr.from_ == "gpt" and prev.from_ not in (
+ "human",
+ "tool",
+ ):
+ raise ValueError(
+ f"Assistant message at position {i} "
+ f"must follow a human or tool message"
+ )
+
+ return self
+
+ def model_dump(self, **kwargs):
+ return self.root
+
+ def __iter__(self):
+ return iter(self.root)
+
+
+class ToolCall(BaseModel):
+ r"""Represents a single tool/function call with validation"""
+
+ name: str = Field(
+ min_length=1,
+ max_length=256,
+ description="The name of the tool to call",
+ )
+ arguments: Dict[str, Any] = Field(
+ description="The arguments to pass to the tool"
+ )
+
+ @field_validator('arguments')
+ @classmethod
+ def validate_arguments(cls, v: Dict[str, Any]) -> Dict[str, Any]:
+ r"""Validate argument structure and content"""
+
+ # Try to serialize arguments to ensure they're JSON-compatible
+ try:
+ json.dumps(v)
+ except (TypeError, ValueError):
+ raise ValueError("Arguments must be JSON-serializable")
+
+ return v
+
+ model_config = {
+ "extra": "forbid",
+ "json_schema_extra": {
+ "examples": [
+ {
+ "name": "get_weather",
+ "arguments": {"city": "London", "units": "celsius"},
+ }
+ ]
+ },
+ }
+
+
+class ToolResponse(BaseModel):
+ r"""Represents a tool/function response with validation. This is a
+ base class and default implementation for tool responses, for the purpose
+ of converting between different formats.
+ """
+
+ name: str = Field(
+ min_length=1,
+ max_length=256,
+ description="The name of the tool that was called",
+ )
+ content: Any = Field(
+ description="The response content from the tool."
+ " Must be JSON serializable literal or object"
+ )
+
+ @field_validator('content')
+ @classmethod
+ def validate_content(cls, v: Dict[str, Any]) -> Dict[str, Any]:
+ r"""Validate response content structure"""
+
+ # Ensure content is JSON-serializable
+ try:
+ json.dumps(v)
+ except (TypeError, ValueError):
+ raise ValueError("Response content must be JSON-serializable")
+
+ return v
+
+ model_config = {
+ "extra": "forbid",
+ "json_schema_extra": {
+ "examples": [
+ {
+ "name": "get_weather",
+ "content": {
+ "temperature": 20,
+ "conditions": "sunny",
+ "humidity": 65,
+ },
+ }
+ ]
+ },
+ }
diff --git a/camel/messages/conversion/sharegpt/__init__.py b/camel/messages/conversion/sharegpt/__init__.py
new file mode 100644
index 000000000..aa950c4c6
--- /dev/null
+++ b/camel/messages/conversion/sharegpt/__init__.py
@@ -0,0 +1,20 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+
+from .hermes import HermesFunctionFormatter
+
+__all__ = [
+ 'HermesFunctionFormatter',
+]
diff --git a/camel/messages/conversion/sharegpt/function_call_formatter.py b/camel/messages/conversion/sharegpt/function_call_formatter.py
new file mode 100644
index 000000000..555f11008
--- /dev/null
+++ b/camel/messages/conversion/sharegpt/function_call_formatter.py
@@ -0,0 +1,49 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+from abc import ABC, abstractmethod
+from typing import Any, Dict, Generic, List, Optional, TypeVar
+
+from camel.messages.conversion import (
+ ToolCall,
+ ToolResponse,
+)
+
+CallT = TypeVar('CallT', bound=ToolCall, covariant=True)
+ResponseT = TypeVar('ResponseT', bound=ToolResponse, covariant=True)
+
+
+class FunctionCallFormatter(ABC, Generic[CallT, ResponseT]):
+ r"""Abstract base class for function calling formats"""
+
+ @abstractmethod
+ def extract_tool_calls(self, message: str) -> List[CallT]:
+ r"""Extract function call info from a message string"""
+ pass
+
+ @abstractmethod
+ def extract_tool_response(self, message: str) -> Optional[ResponseT]:
+ r"""Extract function response info from a message string"""
+ pass
+
+ @abstractmethod
+ def format_tool_call(
+ self, content: str, func_name: str, args: Dict[str, Any]
+ ) -> str:
+ r"""Format a function call into a message string"""
+ pass
+
+ @abstractmethod
+ def format_tool_response(self, func_name: str, result: Any) -> str:
+ r"""Format a function response into a message string"""
+ pass
diff --git a/camel/messages/conversion/sharegpt/hermes/__init__.py b/camel/messages/conversion/sharegpt/hermes/__init__.py
new file mode 100644
index 000000000..0be41e33c
--- /dev/null
+++ b/camel/messages/conversion/sharegpt/hermes/__init__.py
@@ -0,0 +1,19 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from .hermes_function_formatter import HermesFunctionFormatter
+
+__all__ = [
+ 'HermesFunctionFormatter',
+]
diff --git a/camel/messages/conversion/sharegpt/hermes/hermes_function_formatter.py b/camel/messages/conversion/sharegpt/hermes/hermes_function_formatter.py
new file mode 100644
index 000000000..8a243c49a
--- /dev/null
+++ b/camel/messages/conversion/sharegpt/hermes/hermes_function_formatter.py
@@ -0,0 +1,128 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+import json
+import re
+from typing import Any, Dict, List, Optional
+
+from camel.messages.conversion import (
+ ToolCall,
+ ToolResponse,
+)
+from camel.messages.conversion.sharegpt.function_call_formatter import (
+ FunctionCallFormatter,
+)
+
+
+class HermesToolResponse(ToolResponse):
+ r"""Represents a single tool/function call with validation"""
+
+ pass
+
+
+class HermesToolCall(ToolCall):
+ r"""Represents a single tool/function call with validation"""
+
+ pass
+
+
+class HermesFunctionFormatter(
+ FunctionCallFormatter[HermesToolCall, HermesToolResponse]
+):
+ r"""Hermes-style function calling format implementation with validation"""
+
+ def extract_tool_calls(self, message: str) -> List[HermesToolCall]:
+ r"""Extracts all tool calls from the provided message string.
+
+ Args:
+ message (str): The input message string containing potential tool
+ calls.
+
+ Returns:
+ List[HermesToolCall]: A list of parsed HermesToolCall objects.
+ """
+ tool_calls = []
+ pattern = r"<tool_call>\s*({.*?})\s*</tool_call>"
+ matches = re.finditer(pattern, message, re.DOTALL)
+
+ for match in matches:
+ try:
+ call_dict = json.loads(match.group(1).replace("'", '"'))
+ tool_calls.append(HermesToolCall.model_validate(call_dict))
+ except Exception as e:
+ print(f"Warning: Failed to parse tool call: {e}")
+ continue
+
+ return tool_calls
+
+ def extract_tool_response(
+ self, message: str
+ ) -> Optional[HermesToolResponse]:
+ r"""Extracts a single tool response from the provided message string.
+
+ Args:
+ message (str): The input message string containing a potential
+ tool response.
+
+ Returns:
+ Optional[HermesToolResponse]: A parsed HermesToolResponse object,
+ or None if no valid response is found.
+ """
+ pattern = r"<tool_response>\s*({.*?})\s*</tool_response>"
+ match = re.search(pattern, message, re.DOTALL)
+
+ if match:
+ try:
+ response_json = match.group(1)
+ response_dict = json.loads(response_json.replace("'", '"'))
+ return HermesToolResponse.model_validate(response_dict)
+ except Exception as e:
+ print(f"Warning: Failed to parse tool response: {e}")
+ return None
+ return None
+
+ def format_tool_call(
+ self, content: str, func_name: str, args: Dict[str, Any]
+ ) -> str:
+ r"""Formats a tool call message with the given content, function name,
+ and arguments.
+
+ Args:
+ content (str): The content or message to be included in the tool
+ call.
+ func_name (str): The name of the function being called.
+ args (Dict[str, Any]): A dictionary of arguments to be passed to
+ the function.
+
+ Returns:
+ str: A formatted string representing the tool call in Hermes
+ format.
+ """
+ tool_call_dict = {"name": func_name, "arguments": args}
+ return f"{content}\n<tool_call>\n{tool_call_dict}\n</tool_call>"
+
+ def format_tool_response(self, func_name: str, result: Any) -> str:
+ r"""Formats a tool response message with the given function name and
+ result.
+
+ Args:
+ func_name (str): The name of the function whose result is being
+ returned.
+ result (Any): The result to be included in the tool response.
+
+ Returns:
+ str: A formatted string representing the tool response in Hermes
+ format.
+ """
+ response_dict = {"name": func_name, "content": result}
+ return f"<tool_response>\n{response_dict}\n</tool_response>"
diff --git a/camel/messages/func_message.py b/camel/messages/func_message.py
index 29081faec..2c99c923a 100644
--- a/camel/messages/func_message.py
+++ b/camel/messages/func_message.py
@@ -16,10 +16,19 @@ from typing import Any, Dict, Optional
from camel.messages import (
BaseMessage,
+ HermesFunctionFormatter,
OpenAIAssistantMessage,
OpenAIFunctionMessage,
OpenAIMessage,
)
+from camel.messages.conversion import (
+ ShareGPTMessage,
+ ToolCall,
+ ToolResponse,
+)
+from camel.messages.conversion.sharegpt.function_call_formatter import (
+ FunctionCallFormatter,
+)
from camel.types import OpenAIBackendRole
@@ -61,6 +70,43 @@ class FunctionCallingMessage(BaseMessage):
else:
raise ValueError(f"Unsupported role: {role_at_backend}.")
+ def to_sharegpt(
+ self,
+ function_format: Optional[
+ FunctionCallFormatter[ToolCall, ToolResponse]
+ ] = None,
+ ) -> ShareGPTMessage:
+ r"""Convert FunctionCallingMessage to ShareGPT message.
+
+ Args:
+ function_format (FunctionCallFormatter[ToolCall, ToolResponse],
+ optional): The function formatter to use. Defaults to None.
+ """
+
+ if function_format is None:
+ function_format = HermesFunctionFormatter()
+ # The role of the message is an unreliable indicator of whether
+ # it is a function call or response, so use result
+ if self.result is None:
+ # This is a function call
+ # TODO: split the incoming types to be more specific
+ # and remove the type ignores
+ content = function_format.format_tool_call(
+ self.content or "", # type: ignore[arg-type]
+ self.func_name, # type: ignore[arg-type]
+ self.args, # type: ignore[arg-type]
+ )
+ return ShareGPTMessage(from_="gpt", value=content) # type: ignore[call-arg]
+ else:
+ # This is a function response
+ # TODO: Allow for more flexible setting of tool role,
+ # optionally to be the same as assistant messages
+ content = function_format.format_tool_response(
+ self.func_name, # type: ignore[arg-type]
+ self.result, # type: ignore[arg-type]
+ )
+ return ShareGPTMessage(from_="tool", value=content) # type: ignore[call-arg]
+
def to_openai_assistant_message(self) -> OpenAIAssistantMessage:
r"""Converts the message to an :obj:`OpenAIAssistantMessage` object.
diff --git a/examples/conversion.py b/examples/conversion.py
new file mode 100644
index 000000000..dc1cf3f35
--- /dev/null
+++ b/examples/conversion.py
@@ -0,0 +1,95 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+from typing import List
+
+from camel.messages import BaseMessage, HermesFunctionFormatter
+from camel.messages.conversion import (
+ ShareGPTConversation,
+ ShareGPTMessage,
+)
+from camel.messages.func_message import FunctionCallingMessage
+
+
+def sharegpt_to_camel_messages(
+ conversation: ShareGPTConversation,
+) -> List[BaseMessage]:
+ r"""Convert ShareGPT conversation to list of CAMEL messages"""
+ return [
+ BaseMessage.from_sharegpt(msg, HermesFunctionFormatter())
+ for msg in conversation
+ ]
+
+
+def camel_messages_to_sharegpt(
+ messages: List[BaseMessage],
+) -> ShareGPTConversation:
+ r"""Convert list of CAMEL messages to ShareGPT conversation"""
+ sharegpt_messages = [
+ msg.to_sharegpt(HermesFunctionFormatter()) for msg in messages
+ ]
+ return ShareGPTConversation.model_validate(sharegpt_messages)
+
+
+# Example usage
+if __name__ == "__main__":
+ # Create a sample ShareGPT conversation
+ sharegpt_conv = ShareGPTConversation.model_validate(
+ [
+ ShareGPTMessage(
+ from_="system", value="You are a helpful assistant."
+ ),
+ ShareGPTMessage(from_="human", value="What's Tesla's P/E ratio?"),
+ ShareGPTMessage(
+ from_="gpt",
+ value="Let me check Tesla's stock fundamentals.\n"
+ "<tool_call>\n{'name': 'get_stock_fundamentals',"
+ " 'arguments': {'symbol': 'TSLA'}}\n</tool_call>",
+ ),
+ ShareGPTMessage(
+ from_="tool",
+ value='''<tool_response>
+{"name": "get_stock_fundamentals", "content":
+{"symbol": "TSLA", "company_name": "Tesla, Inc.",
+"sector": "Consumer Cyclical", "pe_ratio": 49.604652}}
+</tool_response>''',
+ ),
+ ShareGPTMessage(
+ from_="gpt",
+ value="Tesla (TSLA) currently has a P/E ratio of 49.60.",
+ ),
+ ]
+ )
+
+ # Convert to CAMEL messages
+ camel_messages = sharegpt_to_camel_messages(sharegpt_conv)
+
+ print("\nCAMEL Messages:")
+ for msg in camel_messages:
+ print(f"Role: {msg.role_name}")
+ print(f"Content: {msg.content}")
+ if isinstance(msg, FunctionCallingMessage):
+ print(f"Function Name: {msg.func_name}")
+ if msg.args:
+ print(f"Arguments: {msg.args}")
+ if msg.result:
+ print(f"Result: {msg.result}")
+ print()
+
+ # Convert back to ShareGPT
+ converted_back = camel_messages_to_sharegpt(camel_messages)
+ print("\nConverted back to ShareGPT:")
+ for msg in converted_back:
+ print(f"From: {msg.from_}")
+ print(f"Value: {msg.value}")
+ print()
diff --git a/test/messages/test_func_message.py b/test/messages/test_func_message.py
index 2ae05b97b..21ee46658 100644
--- a/test/messages/test_func_message.py
+++ b/test/messages/test_func_message.py
@@ -11,17 +11,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from typing import Any, Dict
+from typing import Any, Dict, List
import pytest
-from camel.messages import FunctionCallingMessage
-from camel.types import RoleType
+from camel.memories import ContextRecord
+from camel.messages import (
+ BaseMessage,
+ FunctionCallingMessage,
+ HermesFunctionFormatter,
+)
+from camel.models import ModelFactory
+from camel.societies import RolePlaying
+from camel.toolkits import MathToolkit
+from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
@pytest.fixture
-def assistant_func_message() -> FunctionCallingMessage:
- role_name = "test_assistant"
+def assistant_func_call_message() -> FunctionCallingMessage:
+ role_name = "assistant"
role_type = RoleType.ASSISTANT
meta_dict = None
content = "test function message"
@@ -37,29 +45,28 @@ def assistant_func_message() -> FunctionCallingMessage:
@pytest.fixture
-def function_func_message() -> FunctionCallingMessage:
- role_name = "test_function"
+def function_result_message() -> FunctionCallingMessage:
+ role_name = "function"
role_type = RoleType.ASSISTANT
meta_dict = None
- content = "test function message"
return FunctionCallingMessage(
role_name=role_name,
role_type=role_type,
meta_dict=meta_dict,
- content=content,
+ content="",
func_name="add",
result=3,
)
def test_assistant_func_message(
- assistant_func_message: FunctionCallingMessage,
+ assistant_func_call_message: FunctionCallingMessage,
):
content = "test function message"
- assert assistant_func_message.func_name == "add"
- assert assistant_func_message.args == {"a": "1", "b": "2"}
+ assert assistant_func_call_message.func_name == "add"
+ assert assistant_func_call_message.args == {"a": "1", "b": "2"}
msg_dict: Dict[str, Any]
msg_dict = {
@@ -70,12 +77,16 @@ def test_assistant_func_message(
"arguments": str({"a": "1", "b": "2"}),
},
}
- assert assistant_func_message.to_openai_assistant_message() == msg_dict
+ assert (
+ assistant_func_call_message.to_openai_assistant_message() == msg_dict
+ )
-def test_function_func_message(function_func_message: FunctionCallingMessage):
- assert function_func_message.func_name == "add"
- assert function_func_message.result == 3
+def test_function_func_message(
+ function_result_message: FunctionCallingMessage,
+):
+ assert function_result_message.func_name == "add"
+ assert function_result_message.result == 3
result_content = {"result": {str(3)}}
msg_dict: Dict[str, str] = {
@@ -83,11 +94,11 @@ def test_function_func_message(function_func_message: FunctionCallingMessage):
"name": "add",
"content": f'{result_content}',
}
- assert function_func_message.to_openai_function_message() == msg_dict
+ assert function_result_message.to_openai_function_message() == msg_dict
def test_assistant_func_message_to_openai_function_message(
- assistant_func_message: FunctionCallingMessage,
+ assistant_func_call_message: FunctionCallingMessage,
):
expected_msg_dict: Dict[str, str] = {
"role": "function",
@@ -96,13 +107,88 @@ def test_assistant_func_message_to_openai_function_message(
}
assert (
- assistant_func_message.to_openai_function_message()
+ assistant_func_call_message.to_openai_function_message()
== expected_msg_dict
)
+def test_roleplay_conversion_with_tools():
+ tools = MathToolkit().get_tools()
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4O_MINI,
+ )
+
+ role_playing = RolePlaying(
+ assistant_role_name="assistant",
+ assistant_agent_kwargs=dict(
+ model=model,
+ tools=tools,
+ ),
+ user_role_name="user",
+ user_agent_kwargs=dict(model=model),
+ task_prompt="Perform the task",
+ task_specify_agent_kwargs=dict(model=model),
+ task_type=TaskType.AI_SOCIETY,
+ )
+ input_msg = role_playing.init_chat("What is 2 + 4?")
+ [assistant, _] = role_playing.step(input_msg)
+ role_playing.step(assistant.msg)
+
+ records: List[ContextRecord] = (
+ role_playing.assistant_agent.memory.retrieve()
+ )
+ original_messages = []
+ sharegpt_msgs = []
+
+ for record in records:
+ message = record.memory_record.message
+ # Remove meta_dict to avoid comparison issues
+ message.meta_dict = None
+ original_messages.append(message)
+ sharegpt_msgs.append(message.to_sharegpt())
+
+ converted_back = []
+ for msg in sharegpt_msgs:
+ converted_back.append(
+ BaseMessage.from_sharegpt(
+ msg, function_format=HermesFunctionFormatter()
+ )
+ )
+
+ assert converted_back == original_messages
+
+
+def test_convert_function_call_and_response_to_from_sharegpt_hermes(
+ assistant_func_call_message: FunctionCallingMessage,
+ function_result_message: FunctionCallingMessage,
+):
+ sharegpt_function_call = assistant_func_call_message.to_sharegpt()
+
+ # Check the function call contains a hermes function call
+ # TODO: Consider using code from https://github.com/NousResearch/Hermes-Function-Calling/blob/main/validator.py # noqa: E501
+ # and adjacent files
+ assert "<tool_call>" in sharegpt_function_call.value
+
+ # Test it converts back
+ reconverted_function_call = BaseMessage.from_sharegpt(
+ sharegpt_function_call
+ )
+ assert assistant_func_call_message == reconverted_function_call
+
+ sharegpt_function_result = function_result_message.to_sharegpt()
+ reconverted_function_result = BaseMessage.from_sharegpt(
+ sharegpt_function_result
+ )
+
+ # Set reference function call to take on CAMEL function result role
+ function_result_message.role_name = "assistant"
+ assert function_result_message == reconverted_function_result
+
+
def test_function_func_message_to_openai_assistant_message(
- function_func_message: FunctionCallingMessage,
+ function_result_message: FunctionCallingMessage,
):
with pytest.raises(
ValueError,
@@ -111,4 +197,4 @@ def test_function_func_message_to_openai_assistant_message(
" due to missing function name or arguments."
),
):
- function_func_message.to_openai_assistant_message()
+ function_result_message.to_openai_assistant_message()
diff --git a/test/messages/test_message_base.py b/test/messages/test_message_base.py
index 288e93ec7..437992a7d 100644
--- a/test/messages/test_message_base.py
+++ b/test/messages/test_message_base.py
@@ -11,11 +11,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+from typing import List
+
import pytest
+from camel.memories import ContextRecord
from camel.messages import BaseMessage
+from camel.models import ModelFactory
from camel.prompts import CodePrompt, TextPrompt
-from camel.types import OpenAIBackendRole, RoleType
+from camel.societies import RolePlaying
+from camel.types import (
+ ModelPlatformType,
+ ModelType,
+ OpenAIBackendRole,
+ RoleType,
+ TaskType,
+)
@pytest.fixture
@@ -125,3 +136,45 @@ def test_base_message():
**(meta_dict or {}),
"content": content,
}
+
+
+def test_roleplay_sharegpt_conversion():
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4O_MINI,
+ )
+
+ role_playing = RolePlaying(
+ assistant_role_name="assistant",
+ assistant_agent_kwargs=dict(
+ model=model,
+ tools=[],
+ ),
+ user_role_name="user",
+ user_agent_kwargs=dict(model=model),
+ task_prompt="Perform the task",
+ task_specify_agent_kwargs=dict(model=model),
+ task_type=TaskType.AI_SOCIETY,
+ )
+ input_msg = role_playing.init_chat()
+ role_playing.step(input_msg)
+
+ records: List[ContextRecord] = (
+ role_playing.assistant_agent.memory.retrieve()
+ )
+ original_messages = []
+ sharegpt_msgs = []
+
+ for record in records:
+ message = record.memory_record.message
+ # Remove meta_dict to avoid comparison issues
+ message.meta_dict = None
+ original_messages.append(message)
+ sharegpt_msgs.append(message.to_sharegpt())
+
+ converted_back = []
+ for msg in sharegpt_msgs:
+ converted_back.append(BaseMessage.from_sharegpt(msg))
+
+ assert converted_back == original_messages