🐫 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: moonshot access (#3292)
Puzhen Zhanga007f7abf5e1
diff --git a/camel/models/moonshot_model.py b/camel/models/moonshot_model.pyindex ba27e7f47..a6b2bda4c 100644--- a/camel/models/moonshot_model.py+++ b/camel/models/moonshot_model.py@@ -12,6 +12,7 @@# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import copyimport osfrom typing import Any, Dict, List, Optional, Type, Union@@ -19,6 +20,7 @@ from openai import AsyncStreamfrom pydantic import BaseModelfrom camel.configs import MoonshotConfig+from camel.logger import get_loggerfrom camel.messages import OpenAIMessagefrom camel.models._utils import try_modify_message_with_formatfrom camel.models.openai_compatible_model import OpenAICompatibleModel@@ -34,6 +36,8 @@ from camel.utils import (update_langfuse_trace,)+logger = get_logger(__name__)+if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":try:from langfuse.decorators import observe@@ -84,7 +88,7 @@ class MoonshotModel(OpenAICompatibleModel):model_type: Union[ModelType, str],model_config_dict: Optional[Dict[str, Any]] = None,api_key: Optional[str] = None,- url: Optional[str] = "https://api.moonshot.ai/v1",+ url: Optional[str] = None,token_counter: Optional[BaseTokenCounter] = None,timeout: Optional[float] = None,max_retries: int = 3,@@ -93,7 +97,12 @@ class MoonshotModel(OpenAICompatibleModel):if model_config_dict is None:model_config_dict = MoonshotConfig().as_dict()api_key = api_key or os.environ.get("MOONSHOT_API_KEY")- url = url or os.environ.get("MOONSHOT_API_BASE_URL")+ # Preserve default URL if not provided+ if url is None:+ url = (+ os.environ.get("MOONSHOT_API_BASE_URL")+ or "https://api.moonshot.ai/v1"+ )timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(model_type=model_type,@@ -125,12 +134,12 @@ class MoonshotModel(OpenAICompatibleModel):Returns:Dict[str, Any]: The prepared request configuration."""- import copy-request_config = copy.deepcopy(self.model_config_dict)if tools:- request_config["tools"] = tools+ # Clean tools to remove null types (Moonshot API incompatibility)+ cleaned_tools = self._clean_tool_schemas(tools)+ request_config["tools"] = cleaned_toolselif response_format:# Use the same approach as DeepSeek for structured outputtry_modify_message_with_format(messages[-1], response_format)@@ -138,6 +147,94 @@ class MoonshotModel(OpenAICompatibleModel):return request_config+ def _clean_tool_schemas(+ self, tools: List[Dict[str, Any]]+ ) -> List[Dict[str, Any]]:+ r"""Clean tool schemas to remove null types for Moonshot compatibility.++ Moonshot API doesn't accept {"type": "null"} in anyOf schemas.+ This method removes null type definitions from parameters.++ Args:+ tools (List[Dict[str, Any]]): Original tool schemas.++ Returns:+ List[Dict[str, Any]]: Cleaned tool schemas.+ """++ def remove_null_from_schema(schema: Any) -> Any:+ """Recursively remove null types from schema."""+ if isinstance(schema, dict):+ # Create a copy to avoid modifying the original+ result = {}++ for key, value in schema.items():+ if key == 'type' and isinstance(value, list):+ # Handle type arrays like ["string", "null"]+ filtered_types = [t for t in value if t != 'null']+ if len(filtered_types) == 1:+ # Single type remains, convert to string+ result[key] = filtered_types[0]+ elif len(filtered_types) > 1:+ # Multiple types remain, keep as array+ result[key] = filtered_types+ else:+ # All were null, use string as fallback+ logger.warning(+ "All types in tool schema type array "+ "were null, falling back to 'string' "+ "type for Moonshot API compatibility. "+ "Original tool schema may need review."+ )+ result[key] = 'string'+ elif key == 'anyOf':+ # Handle anyOf with null types+ filtered = [+ item+ for item in value+ if not (+ isinstance(item, dict)+ and item.get('type') == 'null'+ )+ ]+ if len(filtered) == 1:+ # If only one type remains, flatten it+ return remove_null_from_schema(filtered[0])+ elif len(filtered) > 1:+ result[key] = [+ remove_null_from_schema(item)+ for item in filtered+ ]+ else:+ # All were null, return string type as fallback+ logger.warning(+ "All types in tool schema anyOf were null, "+ "falling back to 'string' type for "+ "Moonshot API compatibility. Original "+ "tool schema may need review."+ )+ return {"type": "string"}+ else:+ # Recursively process other values+ result[key] = remove_null_from_schema(value)++ return result+ elif isinstance(schema, list):+ return [remove_null_from_schema(item) for item in schema]+ else:+ return schema++ cleaned_tools = copy.deepcopy(tools)+ for tool in cleaned_tools:+ if 'function' in tool and 'parameters' in tool['function']:+ params = tool['function']['parameters']+ if 'properties' in params:+ params['properties'] = remove_null_from_schema(+ params['properties']+ )++ return cleaned_tools+@observe()async def _arun(self,