🐫 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
feat: Update function call result message format (#1436)
ZackYulebfdc89fe4086
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex 5abe0cf91..29961f89b 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -94,11 +94,13 @@ class FunctionCallingRecord(BaseModel):args (Dict[str, Any]): The dictionary of arguments passed tothe function.result (Any): The execution result of calling this function.+ tool_call_id (str): The ID of the tool call, if available."""func_name: strargs: Dict[str, Any]result: Any+ tool_call_id: strdef __str__(self) -> str:r"""Overridden version of the string function.@@ -109,7 +111,7 @@ class FunctionCallingRecord(BaseModel):return (f"Function Execution: {self.func_name}\n"f"\tArgs: {self.args}\n"- f"\tResult: {self.result}"+ f"\tResult: {self.result}\n")def as_dict(self) -> dict[str, Any]:@@ -1384,6 +1386,7 @@ class ChatAgent(BaseAgent):tool = self.tool_dict[func_name]result = tool(**args)+ tool_call_id = choice.message.tool_calls[0].idassist_msg = FunctionCallingMessage(role_name=self.role_name,@@ -1392,6 +1395,7 @@ class ChatAgent(BaseAgent):content="",func_name=func_name,args=args,+ tool_call_id=tool_call_id,)func_msg = FunctionCallingMessage(role_name=self.role_name,@@ -1400,11 +1404,15 @@ class ChatAgent(BaseAgent):content="",func_name=func_name,result=result,+ tool_call_id=tool_call_id,)# Record information about this function callfunc_record = FunctionCallingRecord(- func_name=func_name, args=args, result=result+ func_name=func_name,+ args=args,+ result=result,+ tool_call_id=tool_call_id,)return assist_msg, func_msg, func_record@@ -1448,6 +1456,7 @@ class ChatAgent(BaseAgent):args = json.loads(choice.message.tool_calls[0].function.arguments)tool = self.tool_dict[func_name]result = await tool(**args)+ tool_call_id = choice.message.tool_calls[0].idassist_msg = FunctionCallingMessage(role_name=self.role_name,@@ -1456,6 +1465,7 @@ class ChatAgent(BaseAgent):content="",func_name=func_name,args=args,+ tool_call_id=tool_call_id,)func_msg = FunctionCallingMessage(role_name=self.role_name,@@ -1464,11 +1474,15 @@ class ChatAgent(BaseAgent):content="",func_name=func_name,result=result,+ tool_call_id=tool_call_id,)# Record information about this function callfunc_record = FunctionCallingRecord(- func_name=func_name, args=args, result=result+ func_name=func_name,+ args=args,+ result=result,+ tool_call_id=tool_call_id,)return assist_msg, func_msg, func_recorddiff --git a/camel/data_collector/base.py b/camel/data_collector/base.pyindex f9a919a4c..d511762c7 100644--- a/camel/data_collector/base.py+++ b/camel/data_collector/base.py@@ -27,7 +27,7 @@ class CollectorData:self,id: UUID,name: str,- role: Literal["user", "assistant", "system", "function"],+ role: Literal["user", "assistant", "system", "tool"],message: Optional[str] = None,function_call: Optional[Dict[str, Any]] = None,) -> None:@@ -52,7 +52,7 @@ class CollectorData:ValueError: If neither message nor function call is provided."""- if role not in ["user", "assistant", "system", "function"]:+ if role not in ["user", "assistant", "system", "tool"]:raise ValueError(f"Role {role} not supported")if role == "system" and function_call:raise ValueError("System role cannot have function call")@@ -82,7 +82,7 @@ class CollectorData:name=name,role=context["role"],message=context["content"],- function_call=context.get("function_call", None),+ function_call=context.get("tool_calls", None),)@@ -98,7 +98,7 @@ class BaseDataCollector(ABC):def step(self,- role: Literal["user", "assistant", "system", "function"],+ role: Literal["user", "assistant", "system", "tool"],name: Optional[str] = None,message: Optional[str] = None,function_call: Optional[Dict[str, Any]] = None,@@ -106,7 +106,7 @@ class BaseDataCollector(ABC):r"""Record a message.Args:- role (Literal["user", "assistant", "system", "function"]):+ role (Literal["user", "assistant", "system", "tool"]):The role of the message.name (Optional[str], optional): The name of the agent.(default: :obj:`None`)diff --git a/camel/data_collector/sharegpt_collector.py b/camel/data_collector/sharegpt_collector.pyindex ff6b5ce99..8a5452142 100644--- a/camel/data_collector/sharegpt_collector.py+++ b/camel/data_collector/sharegpt_collector.py@@ -131,7 +131,7 @@ class ShareGPTDataCollector(BaseDataCollector):conversations.append({"from": "gpt", "value": message.message})- elif role == "function":+ elif role == "function" or role == "tool":conversations.append({"from": "observation",@@ -182,7 +182,7 @@ class ShareGPTDataCollector(BaseDataCollector):if message.function_call:context.append(prefix + json.dumps(message.function_call))- elif role == "function":+ elif role == "function" or role == "tool":context.append(prefix + json.dumps(message.message)) # type: ignore[attr-defined]else:context.append(prefix + str(message.message))diff --git a/camel/messages/__init__.py b/camel/messages/__init__.pyindex 3a1c560da..831178ad3 100644--- a/camel/messages/__init__.py+++ b/camel/messages/__init__.py@@ -11,11 +11,13 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from typing import Union+from camel.types import (ChatCompletionAssistantMessageParam,- ChatCompletionFunctionMessageParam,ChatCompletionMessageParam,ChatCompletionSystemMessageParam,+ ChatCompletionToolMessageParam,ChatCompletionUserMessageParam,)@@ -32,9 +34,13 @@ from .conversion.sharegpt.function_call_formatter import ()OpenAISystemMessage = ChatCompletionSystemMessageParam-OpenAIAssistantMessage = ChatCompletionAssistantMessageParam+OpenAIAssistantMessage = Union[+ ChatCompletionAssistantMessageParam,+ ChatCompletionToolMessageParam,+]OpenAIUserMessage = ChatCompletionUserMessageParam-OpenAIFunctionMessage = ChatCompletionFunctionMessageParam+OpenAIToolMessageParam = ChatCompletionToolMessageParam+OpenAIMessage = ChatCompletionMessageParam@@ -45,7 +51,7 @@ __all__ = ['OpenAISystemMessage','OpenAIAssistantMessage','OpenAIUserMessage',- 'OpenAIFunctionMessage',+ 'OpenAIToolMessageParam','OpenAIMessage','FunctionCallFormatter','HermesFunctionFormatter',diff --git a/camel/messages/conversion/conversation_models.py b/camel/messages/conversion/conversation_models.pyindex 28dbea5c6..67e87e3cb 100644--- a/camel/messages/conversion/conversation_models.py+++ b/camel/messages/conversion/conversation_models.py@@ -69,6 +69,11 @@ class ShareGPTConversation(RootModel):for i in range(1, len(messages)):curr, prev = messages[i], messages[i - 1]+ print("@@@@")+ print(curr)+ print(prev)+ print("@@@@")+if curr.from_ == "tool":if prev.from_ != "gpt" or "<tool_call>" not in prev.value:raise ValueError(diff --git a/camel/messages/func_message.py b/camel/messages/func_message.pyindex f69250d5d..2e10f25d4 100644--- a/camel/messages/func_message.py+++ b/camel/messages/func_message.py@@ -11,6 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import jsonfrom dataclasses import dataclassfrom typing import Any, Dict, Optional@@ -18,8 +19,8 @@ from camel.messages import (BaseMessage,HermesFunctionFormatter,OpenAIAssistantMessage,- OpenAIFunctionMessage,OpenAIMessage,+ OpenAIToolMessageParam,)from camel.messages.conversion import (ShareGPTMessage,@@ -44,11 +45,14 @@ class FunctionCallingMessage(BaseMessage):function. (default: :obj:`None`)result (Optional[Any]): The result of function execution.(default: :obj:`None`)+ tool_call_id (Optional[str]): The ID of the tool call, if available.+ (default: :obj:`None`)"""func_name: Optional[str] = Noneargs: Optional[Dict] = Noneresult: Optional[Any] = None+ tool_call_id: Optional[str] = Nonedef to_openai_message(self,@@ -66,7 +70,7 @@ class FunctionCallingMessage(BaseMessage):if role_at_backend == OpenAIBackendRole.ASSISTANT:return self.to_openai_assistant_message()elif role_at_backend == OpenAIBackendRole.FUNCTION:- return self.to_openai_function_message()+ return self.to_openai_tool_message()else:raise ValueError(f"Unsupported role: {role_at_backend}.")@@ -120,24 +124,29 @@ class FunctionCallingMessage(BaseMessage):" due to missing function name or arguments.")- msg_dict: OpenAIAssistantMessage = {+ return {"role": "assistant",- "content": self.content,- "function_call": {- "name": self.func_name,- "arguments": str(self.args),- },+ "content": self.content or "",+ "tool_calls": [+ {+ "id": self.tool_call_id or "",+ "type": "function",+ "function": {+ "name": self.func_name,+ "arguments": json.dumps(self.args),+ },+ }+ ],}- return msg_dict-- def to_openai_function_message(self) -> OpenAIFunctionMessage:- r"""Converts the message to an :obj:`OpenAIMessage` object- with the role being "function".+ def to_openai_tool_message(self) -> OpenAIToolMessageParam:+ r"""Converts the message to an :obj:`OpenAIToolMessageParam` object+ with the role being "tool".Returns:- OpenAIMessage: The converted :obj:`OpenAIMessage` object- with its role being "function".+ OpenAIToolMessageParam: The converted+ :obj:`OpenAIToolMessageParam` object with its role being+ "tool"."""if not self.func_name:raise ValueError(@@ -145,11 +154,10 @@ class FunctionCallingMessage(BaseMessage):" due to missing function name.")- result_content = {"result": {str(self.result)}}- msg_dict: OpenAIFunctionMessage = {- "role": "function",- "name": self.func_name,- "content": f'{result_content}',- }+ result_content = json.dumps(self.result)- return msg_dict+ return {+ "role": "tool",+ "content": result_content,+ "tool_call_id": self.tool_call_id or "",+ }diff --git a/camel/models/cohere_model.py b/camel/models/cohere_model.pyindex 53757ce20..a9deea220 100644--- a/camel/models/cohere_model.py+++ b/camel/models/cohere_model.py@@ -228,6 +228,14 @@ class CohereModel(BaseModelBackend):cohere_messages = self._to_cohere_chatmessage(messages)+ # Removing 'strict': True from the dictionary for+ # cohere client+ if self.model_config_dict.get('tools') is not None:+ for tool in self.model_config_dict.get('tools', []):+ function_dict = tool.get('function', {})+ if 'strict' in function_dict:+ del function_dict['strict']+try:response = self._client.chat(messages=cohere_messages,diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.pyindex 76d5fdb13..e248e099e 100644--- a/camel/models/mistral_model.py+++ b/camel/models/mistral_model.py@@ -147,18 +147,25 @@ class MistralModel(BaseModelBackend):new_messages = []for msg in messages:tool_id = uuid.uuid4().hex[:9]- tool_call_id = uuid.uuid4().hex[:9]+ tool_call_id = msg.get("tool_call_id") or uuid.uuid4().hex[:9]role = msg.get("role")- function_call = msg.get("function_call")+ tool_calls = msg.get("tool_calls")content = msg.get("content")mistral_function_call = None- if function_call:- mistral_function_call = FunctionCall(- name=function_call.get("name"), # type: ignore[attr-defined]- arguments=function_call.get("arguments"), # type: ignore[attr-defined]+ if tool_calls:+ # Ensure tool_calls is treated as a list+ tool_calls_list = (+ tool_calls+ if isinstance(tool_calls, list)+ else [tool_calls])+ for tool_call in tool_calls_list:+ mistral_function_call = FunctionCall(+ name=tool_call["function"].get("name"), # type: ignore[attr-defined]+ arguments=tool_call["function"].get("arguments"), # type: ignore[attr-defined]+ )tool_calls = Noneif mistral_function_call:@@ -178,7 +185,7 @@ class MistralModel(BaseModelBackend):new_messages.append(ToolMessage(content=content, # type: ignore[arg-type]- tool_call_id=tool_call_id,+ tool_call_id=tool_call_id, # type: ignore[arg-type]name=msg.get("name"), # type: ignore[arg-type]))diff --git a/camel/types/__init__.py b/camel/types/__init__.pyindex 3904dc261..1948c2cb4 100644--- a/camel/types/__init__.py+++ b/camel/types/__init__.py@@ -33,10 +33,11 @@ from .openai_types import (ChatCompletion,ChatCompletionAssistantMessageParam,ChatCompletionChunk,- ChatCompletionFunctionMessageParam,ChatCompletionMessage,ChatCompletionMessageParam,+ ChatCompletionMessageToolCall,ChatCompletionSystemMessageParam,+ ChatCompletionToolMessageParam,ChatCompletionUserMessageParam,Choice,CompletionUsage,@@ -62,7 +63,8 @@ __all__ = ['ChatCompletionSystemMessageParam','ChatCompletionUserMessageParam','ChatCompletionAssistantMessageParam',- 'ChatCompletionFunctionMessageParam',+ 'ChatCompletionToolMessageParam',+ 'ChatCompletionMessageToolCall','CompletionUsage','OpenAIImageType','OpenAIVisionDetailType',diff --git a/camel/types/enums.py b/camel/types/enums.pyindex d11c2dbef..035192557 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -167,7 +167,15 @@ class ModelType(UnifiedModelType, Enum):@propertydef support_native_tool_calling(self) -> bool:return any(- [self.is_openai, self.is_gemini, self.is_mistral, self.is_qwen]+ [+ self.is_openai,+ self.is_gemini,+ self.is_mistral,+ self.is_qwen,+ self.is_deepseek,+ self.is_cohere,+ self.is_internlm,+ ])@propertydiff --git a/camel/types/openai_types.py b/camel/types/openai_types.pyindex e0c56b8d8..66449bdfb 100644--- a/camel/types/openai_types.py+++ b/camel/types/openai_types.py@@ -16,10 +16,10 @@ from openai.types.chat.chat_completion import ChatCompletion, Choicefrom openai.types.chat.chat_completion_assistant_message_param import (ChatCompletionAssistantMessageParam,)-from openai.types.chat.chat_completion_chunk import ChatCompletionChunk-from openai.types.chat.chat_completion_function_message_param import (- ChatCompletionFunctionMessageParam,+from openai.types.chat.chat_completion_tool_message_param import (+ ChatCompletionToolMessageParam,)+from openai.types.chat.chat_completion_chunk import ChatCompletionChunkfrom openai.types.chat.chat_completion_message import ChatCompletionMessagefrom openai.types.chat.chat_completion_message_param import (ChatCompletionMessageParam,@@ -33,6 +33,7 @@ from openai.types.chat.chat_completion_user_message_param import (from openai.types.completion_usage import CompletionUsagefrom openai.types.chat import ParsedChatCompletionfrom openai._types import NOT_GIVEN, NotGiven+from openai.types.chat import ChatCompletionMessageToolCallChoice = ChoiceChatCompletion = ChatCompletion@@ -42,7 +43,8 @@ ChatCompletionMessageParam = ChatCompletionMessageParamChatCompletionSystemMessageParam = ChatCompletionSystemMessageParamChatCompletionUserMessageParam = ChatCompletionUserMessageParamChatCompletionAssistantMessageParam = ChatCompletionAssistantMessageParam-ChatCompletionFunctionMessageParam = ChatCompletionFunctionMessageParam+ChatCompletionToolMessageParam = ChatCompletionToolMessageParam+ChatCompletionMessageToolCall = ChatCompletionMessageToolCallCompletionUsage = CompletionUsageNOT_GIVEN = NOT_GIVENNotGiven = NotGivendiff --git a/examples/data_collector/alpaca_collector.py b/examples/data_collector/alpaca_collector.pyindex cbaedad83..f408b88e1 100644--- a/examples/data_collector/alpaca_collector.py+++ b/examples/data_collector/alpaca_collector.py@@ -62,7 +62,8 @@ print(collector.convert())# ruff: noqa: E501"""-{'instruction': 'You are a helpful assistant', 'input': 'When is the release date of the video game Portal?', 'output': 'The video game "Portal" was released on October 10, 2007, as part of the game bundle "The Orange Box," which also included "Half-Life 2" and its episodes. It was later released as a standalone game on April 9, 2008, for PC and Xbox 360.'}-{'instruction': 'You are a helpful assistant', 'input': 'When is the release date of the video game Portal?', 'output': "The video game Portal was released on October 10, 2007, as part of the game bundle 'The Orange Box.' It was later released as a standalone game on April 9, 2008, for PC and Xbox 360."}-{'instruction': 'You are a helpful assistant', 'input': 'When is the release date of the video game Portal?', 'output': 'The video game "Portal" was released on October 10, 2007, as part of the game bundle "The Orange Box," which also included "Half-Life 2" and its episodes. It was later released as a standalone game on April 9, 2008, for PC and Xbox 360.'}+{'instruction': 'You are a helpful assistantWhen is the release date of the video game Portal?', 'input': '', 'output': 'The video game "Portal" was released on October 10, 2007. It was developed by Valve Corporation and is part of the game bundle known as "The Orange Box," which also included "Half-Life 2" and its episodes.'}+2025-01-19 19:26:09,140 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"+{'instruction': 'You are a helpful assistant When is the release date of the video game Portal?', 'input': '', 'output': 'The video game "Portal" was released on October 10, 2007. It was developed by Valve Corporation and is part of the game bundle known as "The Orange Box," which also included "Half-Life 2" and its episodes.'}+{'instruction': 'You are a helpful assistantWhen is the release date of the video game Portal?', 'input': '', 'output': 'The video game "Portal" was released on October 10, 2007. It was developed by"""diff --git a/examples/structured_response/structure_response_prompt_engineering.py b/examples/structured_response/structure_response_prompt_engineering.pyindex 2621db7e4..2458ccef8 100644--- a/examples/structured_response/structure_response_prompt_engineering.py+++ b/examples/structured_response/structure_response_prompt_engineering.py@@ -33,7 +33,7 @@ class StudentList(BaseModel):# Define Qwen modelqwen_model = ModelFactory.create(model_platform=ModelPlatformType.QWEN,- model_type=ModelType.QWEN_PLUS,+ model_type=ModelType.QWEN_TURBO,model_config_dict=QwenConfig().as_dict(),)diff --git a/test/data_collectors/test_sharegpt_collector.py b/test/data_collectors/test_sharegpt_collector.pyindex c4d4e9acb..fda17560d 100644--- a/test/data_collectors/test_sharegpt_collector.py+++ b/test/data_collectors/test_sharegpt_collector.py@@ -55,7 +55,7 @@ def test_sharegpt_converter():_ = agent.step(usr_msg)resp = collector.convert()assert resp["system"] == "You are a helpful assistant"- assert len(resp["conversations"]) == 4+ assert len(resp["conversations"]) in {3, 4}def test_sharegpt_llm_converter():diff --git a/test/messages/test_func_message.py b/test/messages/test_func_message.pyindex 850104254..fb6b17b0b 100644--- a/test/messages/test_func_message.py+++ b/test/messages/test_func_message.py@@ -11,7 +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. =========-from typing import Any, Dict, List+import json+from typing import Dict, Listimport pytest@@ -41,6 +42,7 @@ def assistant_func_call_message() -> FunctionCallingMessage:content=content,func_name="add",args={"a": "1", "b": "2"},+ tool_call_id=None,)@@ -57,6 +59,7 @@ def function_result_message() -> FunctionCallingMessage:content="",func_name="add",result=3,+ tool_call_id=None,)@@ -68,17 +71,15 @@ def test_assistant_func_message(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 = {- "role": "assistant",- "content": content,- "function_call": {- "name": "add",- "arguments": str({"a": "1", "b": "2"}),- },- }- assert (- assistant_func_call_message.to_openai_assistant_message() == msg_dict+ result = assistant_func_call_message.to_openai_assistant_message()+ assert result["role"] == "assistant"+ assert result["content"] == content+ assert len(result["tool_calls"]) == 1 # type: ignore[arg-type]+ tool_call = result["tool_calls"][0] # type: ignore[index]+ assert tool_call["type"] == "function"+ assert tool_call["function"]["name"] == "add"+ assert tool_call["function"]["arguments"] == json.dumps(+ {"a": "1", "b": "2"})@@ -88,26 +89,25 @@ def test_function_func_message(assert function_result_message.func_name == "add"assert function_result_message.result == 3- result_content = {"result": {str(3)}}msg_dict: Dict[str, str] = {- "role": "function",- "name": "add",- "content": f'{result_content}',+ "role": "tool",+ "content": json.dumps(3),+ "tool_call_id": "",}- assert function_result_message.to_openai_function_message() == msg_dict+ assert function_result_message.to_openai_tool_message() == msg_dict-def test_assistant_func_message_to_openai_function_message(+def test_assistant_func_message_to_openai_tool_message(assistant_func_call_message: FunctionCallingMessage,):expected_msg_dict: Dict[str, str] = {- "role": "function",- "name": "add",- "content": "{'result': {'None'}}",+ "role": "tool",+ "content": json.dumps(None),+ "tool_call_id": "",}assert (- assistant_func_call_message.to_openai_function_message()+ assistant_func_call_message.to_openai_tool_message()== expected_msg_dict)@@ -146,16 +146,21 @@ def test_roleplay_conversion_with_tools():message = record.memory_record.message# Remove meta_dict to avoid comparison issuesmessage.meta_dict = None+ # Clear tool_call_id for function messages+ if isinstance(message, FunctionCallingMessage):+ message.tool_call_id = ""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()- )+ message = BaseMessage.from_sharegpt(+ msg, function_format=HermesFunctionFormatter())+ # Clear tool_call_id for function messages+ if isinstance(message, FunctionCallingMessage):+ message.tool_call_id = ""+ converted_back.append(message)assert converted_back == original_messages