🐫 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: mistral and sambanova model tool calling (#3862)
Wendong-Fan5a6e084cb0d8
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex 1d9886053..6b815f549 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.90a1+ placeholder: E.g., 0.2.90a2validations:required: truediff --git a/camel/__init__.py b/camel/__init__.pyindex 08783f95a..d3e330904 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.90a1'+__version__ = '0.2.90a2'__all__ = ['__version__',diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.pyindex 066133d20..3c4047d0b 100644--- a/camel/models/mistral_model.py+++ b/camel/models/mistral_model.py@@ -176,8 +176,6 @@ class MistralModel(BaseModelBackend):self,messages: List[OpenAIMessage],) -> List["Messages"]:- import uuid-from mistralai.models import (AssistantMessage,FunctionCall,@@ -187,16 +185,13 @@ class MistralModel(BaseModelBackend):UserMessage,)- new_messages = []+ new_messages: List["Messages"] = []for msg in messages:- tool_id = uuid.uuid4().hex[:9]- tool_call_id = msg.get("tool_call_id") or uuid.uuid4().hex[:9]-role = msg.get("role")tool_calls = msg.get("tool_calls")content = msg.get("content")- mistral_function_call = None+ mistral_tool_calls = Noneif tool_calls:# Ensure tool_calls is treated as a listtool_calls_list = (@@ -204,23 +199,29 @@ class MistralModel(BaseModelBackend):if isinstance(tool_calls, list)else [tool_calls])+ mistral_tool_calls = []for tool_call in tool_calls_list:- mistral_function_call = FunctionCall(+ 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 = None- if mistral_function_call:- tool_calls = [- ToolCall(function=mistral_function_call, id=tool_id)- ]+ # Preserve the original tool call id to keep tool result+ # ordering valid across turns.+ mistral_tool_calls.append(+ ToolCall(+ function=function_call,+ id=tool_call.get("id"), # type: ignore[attr-defined]+ )+ )if role == "user":new_messages.append(UserMessage(content=content)) # type: ignore[arg-type]elif role == "assistant":new_messages.append(- AssistantMessage(content=content, tool_calls=tool_calls) # type: ignore[arg-type]+ AssistantMessage(+ content=content, # type: ignore[arg-type]+ tool_calls=mistral_tool_calls,+ ))elif role == "system":new_messages.append(SystemMessage(content=content)) # type: ignore[arg-type]@@ -228,7 +229,7 @@ class MistralModel(BaseModelBackend):new_messages.append(ToolMessage(content=content, # type: ignore[arg-type]- tool_call_id=tool_call_id, # type: ignore[arg-type]+ tool_call_id=msg.get("tool_call_id"), # type: ignore[arg-type]name=msg.get("name"), # type: ignore[arg-type]))diff --git a/camel/models/samba_model.py b/camel/models/samba_model.pyindex 93d7003c0..d15c05c07 100644--- a/camel/models/samba_model.py+++ b/camel/models/samba_model.py@@ -201,12 +201,10 @@ class SambaModel(BaseModelBackend):self._log_and_trace()- if "tools" in self.model_config_dict:- del self.model_config_dict["tools"]if self.model_config_dict.get("stream") is True:- return await self._arun_streaming(messages)+ return await self._arun_streaming(messages, tools)else:- response = await self._arun_non_streaming(messages)+ response = await self._arun_non_streaming(messages, tools)update_current_observation(usage=response.usage,)@@ -240,19 +238,19 @@ class SambaModel(BaseModelBackend):)self._log_and_trace()- if "tools" in self.model_config_dict:- del self.model_config_dict["tools"]if self.model_config_dict.get("stream") is True:- return self._run_streaming(messages)+ return self._run_streaming(messages, tools)else:- response = self._run_non_streaming(messages)+ response = self._run_non_streaming(messages, tools)update_current_observation(usage=response.usage,)return responsedef _run_streaming(- self, messages: List[OpenAIMessage]+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,) -> Stream[ChatCompletionChunk]:r"""Handles streaming inference with SambaNova's API.@@ -271,10 +269,11 @@ class SambaModel(BaseModelBackend):"""# Handle SambaNova's Cloud APIif self._url == "https://api.sambanova.ai/v1":+ request_config = self._prepare_request_config(tools)response = self._client.chat.completions.create(messages=messages,model=self.model_type,- **self.model_config_dict,+ **request_config,)# Add AgentOps LLM Event tracking@@ -301,7 +300,9 @@ class SambaModel(BaseModelBackend):raise RuntimeError(f"Unknown URL: {self._url}")def _run_non_streaming(- self, messages: List[OpenAIMessage]+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,) -> ChatCompletion:r"""Handles non-streaming inference with SambaNova's API.@@ -320,10 +321,11 @@ class SambaModel(BaseModelBackend):"""# Handle SambaNova's Cloud APIif self._url == "https://api.sambanova.ai/v1":+ request_config = self._prepare_request_config(tools)response = self._client.chat.completions.create(messages=messages,model=self.model_type,- **self.model_config_dict,+ **request_config,)# Add AgentOps LLM Event tracking@@ -344,6 +346,7 @@ class SambaModel(BaseModelBackend):# Handle SambaNova's Sambaverse APIelse:+ request_config = self._prepare_request_config(tools)headers = {"Content-Type": "application/json","key": str(self._api_key),@@ -362,14 +365,12 @@ class SambaModel(BaseModelBackend):"do_sample": {"type": "bool", "value": "true"},"max_tokens_to_generate": {"type": "int",- "value": str(self.model_config_dict.get("max_tokens")),+ "value": str(request_config.get("max_tokens")),},"process_prompt": {"type": "bool", "value": "true"},"repetition_penalty": {"type": "float",- "value": str(- self.model_config_dict.get("repetition_penalty")- ),+ "value": str(request_config.get("repetition_penalty")),},"return_token_count_only": {"type": "bool",@@ -381,21 +382,19 @@ class SambaModel(BaseModelBackend):},"stop_sequences": {"type": "str",- "value": self.model_config_dict.get("stop_sequences"),+ "value": request_config.get("stop_sequences"),},"temperature": {"type": "float",- "value": str(- self.model_config_dict.get("temperature")- ),+ "value": str(request_config.get("temperature")),},"top_k": {"type": "int",- "value": str(self.model_config_dict.get("top_k")),+ "value": str(request_config.get("top_k")),},"top_p": {"type": "float",- "value": str(self.model_config_dict.get("top_p")),+ "value": str(request_config.get("top_p")),},},}@@ -483,7 +482,9 @@ class SambaModel(BaseModelBackend):return self.model_config_dict.get('stream', False)async def _arun_streaming(- self, messages: List[OpenAIMessage]+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,) -> AsyncStream[ChatCompletionChunk]:r"""Handles streaming inference with SambaNova's API.@@ -502,10 +503,11 @@ class SambaModel(BaseModelBackend):"""# Handle SambaNova's Cloud APIif self._url == "https://api.sambanova.ai/v1":+ request_config = self._prepare_request_config(tools)response = await self._async_client.chat.completions.create(messages=messages,model=self.model_type,- **self.model_config_dict,+ **request_config,)# Add AgentOps LLM Event tracking@@ -532,7 +534,9 @@ class SambaModel(BaseModelBackend):raise RuntimeError(f"Unknown URL: {self._url}")async def _arun_non_streaming(- self, messages: List[OpenAIMessage]+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,) -> ChatCompletion:r"""Handles non-streaming inference with SambaNova's API.@@ -551,10 +555,11 @@ class SambaModel(BaseModelBackend):"""# Handle SambaNova's Cloud APIif self._url == "https://api.sambanova.ai/v1":+ request_config = self._prepare_request_config(tools)response = await self._async_client.chat.completions.create(messages=messages,model=self.model_type,- **self.model_config_dict,+ **request_config,)# Add AgentOps LLM Event tracking@@ -575,6 +580,7 @@ class SambaModel(BaseModelBackend):# Handle SambaNova's Sambaverse APIelse:+ request_config = self._prepare_request_config(tools)headers = {"Content-Type": "application/json","key": str(self._api_key),@@ -593,14 +599,12 @@ class SambaModel(BaseModelBackend):"do_sample": {"type": "bool", "value": "true"},"max_tokens_to_generate": {"type": "int",- "value": str(self.model_config_dict.get("max_tokens")),+ "value": str(request_config.get("max_tokens")),},"process_prompt": {"type": "bool", "value": "true"},"repetition_penalty": {"type": "float",- "value": str(- self.model_config_dict.get("repetition_penalty")- ),+ "value": str(request_config.get("repetition_penalty")),},"return_token_count_only": {"type": "bool",@@ -612,21 +616,19 @@ class SambaModel(BaseModelBackend):},"stop_sequences": {"type": "str",- "value": self.model_config_dict.get("stop_sequences"),+ "value": request_config.get("stop_sequences"),},"temperature": {"type": "float",- "value": str(- self.model_config_dict.get("temperature")- ),+ "value": str(request_config.get("temperature")),},"top_k": {"type": "int",- "value": str(self.model_config_dict.get("top_k")),+ "value": str(request_config.get("top_k")),},"top_p": {"type": "float",- "value": str(self.model_config_dict.get("top_p")),+ "value": str(request_config.get("top_p")),},},}diff --git a/docs/conf.py b/docs/conf.pyindex a1e054296..227b53ca0 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.90a1'+release = '0.2.90a2'html_favicon = ('https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'diff --git a/pyproject.toml b/pyproject.tomlindex 00d2909e2..38313988a 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "camel-ai"-version = "0.2.90a1"+version = "0.2.90a2"description = "Communicative Agents for AI Society Study"authors = [{ name = "CAMEL-AI.org" }]requires-python = ">=3.10,<3.15"@@ -316,6 +316,7 @@ eigent = ["google-auth-oauthlib==1.2.1","httplib2>=0.31.0","av<16",+ "mistralai>=1.1.0,<2",]all = ["numpy>=1.2,<=2.2",diff --git a/test/models/test_mistral_model.py b/test/models/test_mistral_model.pyindex b210cf53c..562fa9b3c 100644--- a/test/models/test_mistral_model.py+++ b/test/models/test_mistral_model.py@@ -46,3 +46,62 @@ def test_mistral_model(model_type):assert isinstance(model.token_counter, OpenAITokenCounter)assert isinstance(model.model_type.value_for_tiktoken, str)assert isinstance(model.model_type.token_limit, int)++[email protected]_backend+def test_to_mistral_chatmessage_preserves_tool_call_ids(monkeypatch):+ monkeypatch.setenv("MISTRAL_API_KEY", "test_key")+ model = MistralModel(ModelType.MISTRAL_LARGE, MistralConfig().as_dict())++ tool_call_id_1 = "call_tool_1"+ tool_call_id_2 = "call_tool_2"+ messages = [+ {"role": "user", "content": "Please run two calculations."},+ {+ "role": "assistant",+ "content": "",+ "tool_calls": [+ {+ "id": tool_call_id_1,+ "type": "function",+ "function": {+ "name": "add",+ "arguments": '{"a": 2, "b": 3}',+ },+ },+ {+ "id": tool_call_id_2,+ "type": "function",+ "function": {+ "name": "multiply",+ "arguments": '{"a": 4, "b": 5}',+ },+ },+ ],+ },+ {+ "role": "tool",+ "tool_call_id": tool_call_id_1,+ "content": "5",+ "name": "add",+ },+ {+ "role": "tool",+ "tool_call_id": tool_call_id_2,+ "content": "20",+ "name": "multiply",+ },+ ]++ mistral_messages = model._to_mistral_chatmessage(messages)++ assistant_message = mistral_messages[1]+ assert assistant_message.tool_calls is not None+ assert len(assistant_message.tool_calls) == 2+ assert assistant_message.tool_calls[0].id == tool_call_id_1+ assert assistant_message.tool_calls[1].id == tool_call_id_2++ first_tool_message = mistral_messages[2]+ second_tool_message = mistral_messages[3]+ assert first_tool_message.tool_call_id == tool_call_id_1+ assert second_tool_message.tool_call_id == tool_call_id_2diff --git a/test/models/test_samba_model.py b/test/models/test_samba_model.pyindex f205971aa..bae1a52b2 100644--- a/test/models/test_samba_model.py+++ b/test/models/test_samba_model.py@@ -12,6 +12,8 @@# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+from unittest.mock import MagicMock+import pytestfrom camel.configs import (@@ -34,3 +36,89 @@ def test_samba_model(model_type: ModelType):assert model.model_type == model_typeassert model.model_config_dict == SambaCloudAPIConfig().as_dict()assert isinstance(model.token_counter, OpenAITokenCounter)+++def _mock_chat_completion_response() -> MagicMock:+ response = MagicMock()+ response.id = "test-id"+ response.choices = [MagicMock()]+ response.choices[0].message.content = "Hello"+ response.usage = MagicMock()+ response.usage.prompt_tokens = 10+ response.usage.completion_tokens = 5+ response.usage.total_tokens = 15+ return response+++def test_samba_model_run_passes_tools_to_cloud_api(monkeypatch):+ monkeypatch.setenv("SAMBA_API_KEY", "test_key")+ mock_client = MagicMock()+ mock_client.chat.completions.create.return_value = (+ _mock_chat_completion_response()+ )++ model = SambaModel(+ model_type="llama3-70b",+ client=mock_client,+ async_client=MagicMock(),+ )++ tools = [+ {+ "type": "function",+ "function": {+ "name": "get_weather",+ "description": "Get weather by city",+ "parameters": {+ "type": "object",+ "properties": {"city": {"type": "string"}},+ "required": ["city"],+ },+ },+ }+ ]+ messages = [{"role": "user", "content": "what's the weather?"}]++ model.run(messages=messages, tools=tools)++ call_kwargs = mock_client.chat.completions.create.call_args.kwargs+ assert call_kwargs["tools"] == tools+++def test_samba_model_run_keeps_tools_in_model_config_dict(monkeypatch):+ monkeypatch.setenv("SAMBA_API_KEY", "test_key")+ mock_client = MagicMock()+ mock_client.chat.completions.create.return_value = (+ _mock_chat_completion_response()+ )++ tools = [+ {+ "type": "function",+ "function": {+ "name": "echo",+ "description": "Echo the input",+ "parameters": {+ "type": "object",+ "properties": {"text": {"type": "string"}},+ "required": ["text"],+ },+ },+ }+ ]+ model_config_dict = SambaCloudAPIConfig().as_dict()+ model_config_dict["tools"] = tools++ model = SambaModel(+ model_type="llama3-70b",+ model_config_dict=model_config_dict,+ client=mock_client,+ async_client=MagicMock(),+ )+ messages = [{"role": "user", "content": "echo hello"}]++ model.run(messages=messages)++ assert model.model_config_dict["tools"] == tools+ call_kwargs = mock_client.chat.completions.create.call_args.kwargs+ assert call_kwargs["tools"] == toolsdiff --git a/uv.lock b/uv.lockindex 02cf12ab8..8614d736c 100644--- a/uv.lock+++ b/uv.lock@@ -886,7 +886,7 @@ wheels = [[[package]]name = "camel-ai"-version = "0.2.90a1"+version = "0.2.90a2"source = { editable = "." }dependencies = [{ name = "astor" },@@ -1182,6 +1182,7 @@ eigent = [{ name = "markitdown", extra = ["all"], marker = "python_full_version < '3.13'" },{ name = "mcp-server-fetch" },{ name = "mcp-simple-arxiv" },+ { name = "mistralai" },{ name = "numpy", version = "1.26.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.13'" },{ name = "numpy", version = "2.2.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.13'" },{ name = "onnxruntime" },@@ -1528,6 +1529,7 @@ requires-dist = [{ name = "microsandbox", marker = "extra == 'all'", specifier = ">=0.1.8" },{ name = "microsandbox", marker = "extra == 'dev-tools'", specifier = ">=0.1.8" },{ name = "mistralai", marker = "extra == 'all'", specifier = ">=1.1.0,<2" },+ { name = "mistralai", marker = "extra == 'eigent'", specifier = ">=1.1.0,<2" },{ name = "mistralai", marker = "extra == 'model-platforms'", specifier = ">=1.1.0,<2" },{ name = "mock", marker = "extra == 'all'", specifier = ">=5,<6" },{ name = "mock", marker = "extra == 'dev'", specifier = ">=5,<6" },@@ -7451,7 +7453,7 @@ name = "pexpect"version = "4.9.0"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "ptyprocess" },+ { name = "ptyprocess", marker = "sys_platform != 'win32'" },]sdist = { url = "https://files.pythonhosted.org/packages/42/92/cc564bf6381ff43ce1f4d06852fc19a2f11d180f23dc32d9588bee2f149d/pexpect-4.9.0.tar.gz", hash = "sha256:ee7d41123f3c9911050ea2c2dac107568dc43b2d3b0c7557a33212c398ead30f", size = 166450, upload-time = "2023-11-25T09:07:26.339Z" }wheels = [@@ -8424,7 +8426,7 @@ name = "pyobjc-framework-cocoa"version = "12.1"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "pyobjc-core", marker = "python_full_version >= '3.13' or sys_platform == 'darwin'" },+ { name = "pyobjc-core", marker = "(python_full_version >= '3.13' and sys_platform != 'win32') or sys_platform == 'darwin'" },]sdist = { url = "https://files.pythonhosted.org/packages/02/a3/16ca9a15e77c061a9250afbae2eae26f2e1579eb8ca9462ae2d2c71e1169/pyobjc_framework_cocoa-12.1.tar.gz", hash = "sha256:5556c87db95711b985d5efdaaf01c917ddd41d148b1e52a0c66b1a2e2c5c1640", size = 2772191, upload-time = "2025-11-14T10:13:02.069Z" }wheels = [@@ -8442,8 +8444,8 @@ name = "pyobjc-framework-quartz"version = "12.1"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "pyobjc-core", marker = "python_full_version >= '3.13' or sys_platform == 'darwin'" },- { name = "pyobjc-framework-cocoa", marker = "python_full_version >= '3.13' or sys_platform == 'darwin'" },+ { name = "pyobjc-core", marker = "(python_full_version >= '3.13' and sys_platform != 'win32') or sys_platform == 'darwin'" },+ { name = "pyobjc-framework-cocoa", marker = "(python_full_version >= '3.13' and sys_platform != 'win32') or sys_platform == 'darwin'" },]sdist = { url = "https://files.pythonhosted.org/packages/94/18/cc59f3d4355c9456fc945eae7fe8797003c4da99212dd531ad1b0de8a0c6/pyobjc_framework_quartz-12.1.tar.gz", hash = "sha256:27f782f3513ac88ec9b6c82d9767eef95a5cf4175ce88a1e5a65875fee799608", size = 3159099, upload-time = "2025-11-14T10:21:24.31Z" }wheels = [