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

refactor: model log system include tool schema and support multi-ling… (#3866)

Wendong-Fan4be29ca8a0b1
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index 6b815f549..a7722eda3 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.90a2
+ placeholder: E.g., 0.2.90a3
validations:
required: true
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index cc0928677..cd2ecdc4c 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -268,6 +268,23 @@ Examples:
logger.debug(f"User input: {user_input}")
```
+For model request/response JSON logs during development or debugging,
+you can use:
+
+```bash
+export CAMEL_MODEL_LOG_ENABLED=true
+export CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED=true
+export CAMEL_LOG_DIR=camel_logs
+```
+
+- `CAMEL_MODEL_LOG_ENABLED`: Enables model request/response log files.
+- `CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED`: Controls whether
+ `model_config_dict` is added to `request.model_config_dict`.
+ When unset, it defaults to the same value as
+ `CAMEL_MODEL_LOG_ENABLED`.
+- `CAMEL_LOG_DIR`: Sets the output directory for those logs.
+- Logs are written in UTF-8 JSON so multilingual text stays readable.
+
### Board Item Create Workflow 🛠️
At CAMEL, we manage our project through a structured workflow that ensures efficiency and clarity in our development process. Our workflow includes stages for issue creation and pull requests (PRs), sprint planning, and reviews.
diff --git a/README.ja.md b/README.ja.md
index 1c38fe58f..1b50620d9 100644
--- a/README.ja.md
+++ b/README.ja.md
@@ -290,6 +290,24 @@ pip install camel-ai
# [https://github.com/camel-ai/camel](https://github.com/camel-ai/camel)です。
```
+4. **(任意)モデルのリクエスト/レスポンスログを有効化:**
+
+ ```bash
+ export CAMEL_MODEL_LOG_ENABLED=true
+ export CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED=true
+ export CAMEL_LOG_DIR=camel_logs
+ ```
+
+ - `CAMEL_MODEL_LOG_ENABLED`: リクエスト/レスポンスの
+ JSONログを有効化します。
+ - `CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED`: `request.model_config_dict`
+ を記録するかを制御します。未設定の場合は
+ `CAMEL_MODEL_LOG_ENABLED` と同じ値が使われます。
+ - `CAMEL_LOG_DIR`: ログの出力先ディレクトリ
+ (デフォルト: `camel_logs`)。
+ - ログはUTF-8 JSONで保存されるため、中国語・日本語・アラビア語
+ などの多言語テキストも可読なまま保持されます。
+
より詳細な手順と追加の設定オプションについては、[インストールセクション](https://github.com/camel-ai/camel/blob/master/docs/get_started/installation.md)をご確認ください。
diff --git a/README.md b/README.md
index 7066c8476..e41d88db7 100644
--- a/README.md
+++ b/README.md
@@ -290,6 +290,24 @@ This example demonstrates how to create a `ChatAgent` using the CAMEL framework
# [https://github.com/camel-ai/camel](https://github.com/camel-ai/camel).
```
+4. **(Optional) Enable model request/response logs:**
+
+ ```bash
+ export CAMEL_MODEL_LOG_ENABLED=true
+ export CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED=true
+ export CAMEL_LOG_DIR=camel_logs
+ ```
+
+ - `CAMEL_MODEL_LOG_ENABLED`: Enables request/response JSON logs.
+ - `CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED`: Controls whether
+ `model_config_dict` is logged under `request.model_config_dict`.
+ When unset, it defaults to the same value as
+ `CAMEL_MODEL_LOG_ENABLED`.
+ - `CAMEL_LOG_DIR`: Directory for generated log files
+ (default: `camel_logs`).
+ - Logs are written as UTF-8 JSON with multilingual text preserved
+ (for example Chinese, Japanese, Arabic) without Unicode escape noise.
+
For more detailed instructions and additional configuration options, check out the [installation section](https://github.com/camel-ai/camel/blob/master/docs/get_started/installation.md).
diff --git a/README.zh.md b/README.zh.md
index 61bf724b7..d72041561 100644
--- a/README.zh.md
+++ b/README.zh.md
@@ -245,6 +245,22 @@ pip install camel-ai
# [https://github.com/camel-ai/camel](https://github.com/camel-ai/camel).
```
+4. **(可选)开启模型请求/响应日志:**
+
+ ```bash
+ export CAMEL_MODEL_LOG_ENABLED=true
+ export CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED=true
+ export CAMEL_LOG_DIR=camel_logs
+ ```
+
+ - `CAMEL_MODEL_LOG_ENABLED`:开启请求/响应 JSON 日志。
+ - `CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED`:控制是否记录
+ `request.model_config_dict`。未设置时,默认跟
+ `CAMEL_MODEL_LOG_ENABLED` 保持一致。
+ - `CAMEL_LOG_DIR`:日志输出目录(默认:`camel_logs`)。
+ - 日志以 UTF-8 JSON 写入,中文、日文、阿拉伯语等多语言内容可读,
+ 不会出现大量 Unicode 转义字符。
+
有关更详细的说明和其他配置选项,请参阅[安装部分](https://github.com/camel-ai/camel/blob/master/docs/get_started/installation.md)。
运行之后,您可以访问 [docs.camel-ai.org](https://docs.camel-ai.org) 探索我们的 CAMEL 技术栈和操作手册,构建强大的多智能体系统。
diff --git a/camel/__init__.py b/camel/__init__.py
index d3e330904..517fbb365 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.90a2'
+__version__ = '0.2.90a3'
__all__ = [
'__version__',
diff --git a/camel/models/anthropic_model.py b/camel/models/anthropic_model.py
index a362a99df..e235e9e3d 100644
--- a/camel/models/anthropic_model.py
+++ b/camel/models/anthropic_model.py
@@ -781,11 +781,15 @@ class AnthropicModel(BaseModelBackend):
if is_streaming:
# Return streaming response
- stream = create_func(**request_params, stream=True)
+ stream = self._call_client(
+ create_func,
+ **request_params,
+ stream=True,
+ )
return self._wrap_anthropic_stream(stream, str(self.model_type))
else:
# Return non-streaming response
- response = create_func(**request_params)
+ response = self._call_client(create_func, **request_params)
return self._convert_anthropic_to_openai_response(
response, str(self.model_type)
)
@@ -906,13 +910,17 @@ class AnthropicModel(BaseModelBackend):
if is_streaming:
# Return streaming response
- stream = await create_func(**request_params, stream=True)
+ stream = await self._acall_client(
+ create_func,
+ **request_params,
+ stream=True,
+ )
return self._wrap_anthropic_async_stream(
stream, str(self.model_type)
)
else:
# Return non-streaming response
- response = await create_func(**request_params)
+ response = await self._acall_client(create_func, **request_params)
return self._convert_anthropic_to_openai_response(
response, str(self.model_type)
)
diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.py
index c07e097e8..79775a414 100644
--- a/camel/models/azure_openai_model.py
+++ b/camel/models/azure_openai_model.py
@@ -346,7 +346,8 @@ class AzureOpenAIModel(BaseModelBackend):
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
request_config = self._prepare_request_config(tools)
- return self._client.chat.completions.create(
+ return self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=str(self.model_type),
**request_config,
@@ -359,7 +360,8 @@ class AzureOpenAIModel(BaseModelBackend):
) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
request_config = self._prepare_request_config(tools)
- return await self._async_client.chat.completions.create(
+ return await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=str(self.model_type),
**request_config,
@@ -377,7 +379,8 @@ class AzureOpenAIModel(BaseModelBackend):
# with structured response
request_config.pop("stream", None)
- return self._client.beta.chat.completions.parse(
+ return self._call_client(
+ self._client.beta.chat.completions.parse,
messages=messages,
model=str(self.model_type),
**request_config,
@@ -395,7 +398,8 @@ class AzureOpenAIModel(BaseModelBackend):
# with structured response
request_config.pop("stream", None)
- return await self._async_client.beta.chat.completions.parse(
+ return await self._acall_client(
+ self._async_client.beta.chat.completions.parse,
messages=messages,
model=str(self.model_type),
**request_config,
@@ -416,7 +420,8 @@ class AzureOpenAIModel(BaseModelBackend):
request_config.pop("stream", None)
# Use the beta streaming API for structured outputs
- return self._client.beta.chat.completions.stream(
+ return self._call_client(
+ self._client.beta.chat.completions.stream,
messages=messages,
model=str(self.model_type),
response_format=response_format,
@@ -438,7 +443,8 @@ class AzureOpenAIModel(BaseModelBackend):
request_config.pop("stream", None)
# Use the beta streaming API for structured outputs
- return self._async_client.beta.chat.completions.stream(
+ return self._call_client(
+ self._async_client.beta.chat.completions.stream,
messages=messages,
model=str(self.model_type),
response_format=response_format,
diff --git a/camel/models/base_model.py b/camel/models/base_model.py
index 3571da210..27990e51d 100644
--- a/camel/models/base_model.py
+++ b/camel/models/base_model.py
@@ -15,6 +15,7 @@ import abc
import inspect
import os
from abc import ABC, abstractmethod
+from contextvars import ContextVar
from typing import (
Any,
AsyncGenerator,
@@ -99,7 +100,7 @@ class _StreamLogger:
from datetime import datetime
try:
- with open(self._log_path, "r+") as f:
+ with open(self._log_path, "r+", encoding="utf-8") as f:
data = json.load(f)
data["response_timestamp"] = datetime.now().isoformat()
data["response"] = {
@@ -111,7 +112,7 @@ class _StreamLogger:
"streaming": True,
}
f.seek(0)
- json.dump(data, f, indent=4)
+ json.dump(data, f, indent=4, ensure_ascii=False, default=str)
f.truncate()
except Exception:
pass
@@ -283,11 +284,27 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
self._max_retries = max_retries
self._extract_thinking_from_response = extract_thinking_from_response
# Initialize logging configuration
- self._log_enabled = (
- os.environ.get("CAMEL_MODEL_LOG_ENABLED", "False").lower()
- == "true"
- )
+ self._log_enabled = os.environ.get(
+ "CAMEL_MODEL_LOG_ENABLED", "False"
+ ).strip().lower() in {"1", "true", "yes", "on"}
+ default_model_config_log = "True" if self._log_enabled else "False"
+ self._log_model_config_dict_enabled = os.environ.get(
+ "CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED",
+ default_model_config_log,
+ ).strip().lower() in {"1", "true", "yes", "on"}
self._log_dir = os.environ.get("CAMEL_LOG_DIR", "camel_logs")
+ self._log_path_context: ContextVar[Optional[str]] = ContextVar(
+ f"camel_model_log_path_{id(self)}",
+ default=None,
+ )
+ self._log_client_call_seen_context: ContextVar[bool] = ContextVar(
+ f"camel_model_log_client_call_seen_{id(self)}",
+ default=False,
+ )
+ self._log_request_synced_context: ContextVar[bool] = ContextVar(
+ f"camel_model_log_request_synced_{id(self)}",
+ default=False,
+ )
@property
@abstractmethod
@@ -330,6 +347,133 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
return request_config
+ def _resolve_tools(
+ self,
+ tools: Optional[List[Dict[str, Any]]],
+ ) -> Optional[List[Dict[str, Any]]]:
+ r"""Resolve tools for the current request."""
+ if tools is None:
+ default_tools = self.model_config_dict.get("tools", None)
+ return default_tools if isinstance(default_tools, list) else None
+ if not tools:
+ return None
+ return tools
+
+ def _normalize_client_call_kwargs(
+ self,
+ call: Any,
+ args: Any,
+ kwargs: Dict[str, Any],
+ ) -> Dict[str, Any]:
+ r"""Normalize client call arguments into a keyword dictionary."""
+ raw_payload = dict(kwargs)
+ if args:
+ raw_payload["__camel_client_args__"] = list(args)
+
+ if not args:
+ return kwargs
+ try:
+ signature = inspect.signature(call)
+ bound = signature.bind_partial(*args, **kwargs)
+ normalized_kwargs = dict(bound.arguments)
+ normalized_kwargs.pop("self", None)
+ return normalized_kwargs
+ except Exception:
+ return raw_payload
+
+ def _mark_request_log_client_call_seen(self) -> None:
+ r"""Mark that a model client call happened during this run."""
+ if not self._log_enabled:
+ return
+ if self._log_path_context.get() is None:
+ return
+ self._log_client_call_seen_context.set(True)
+
+ def _call_client(self, call: Any, *args: Any, **kwargs: Any) -> Any:
+ r"""Invoke a sync client call and sync request logs with payload."""
+ self._mark_request_log_client_call_seen()
+ normalized_kwargs = self._normalize_client_call_kwargs(
+ call, args, kwargs
+ )
+ if self._should_sync_request_log(normalized_kwargs):
+ self._sync_request_log_with_client_kwargs(normalized_kwargs)
+ return call(*args, **kwargs)
+
+ async def _acall_client(self, call: Any, *args: Any, **kwargs: Any) -> Any:
+ r"""Invoke an async client call and sync request logs with payload."""
+ self._mark_request_log_client_call_seen()
+ normalized_kwargs = self._normalize_client_call_kwargs(
+ call, args, kwargs
+ )
+ if self._should_sync_request_log(normalized_kwargs):
+ self._sync_request_log_with_client_kwargs(normalized_kwargs)
+ return await call(*args, **kwargs)
+
+ def _should_sync_request_log(self, kwargs: Dict[str, Any]) -> bool:
+ r"""Check whether a client call should sync request logs."""
+ return bool(kwargs)
+
+ def _extract_model_config_from_client_kwargs(
+ self, kwargs: Dict[str, Any]
+ ) -> Dict[str, Any]:
+ r"""Extract model config payload from client call kwargs."""
+ import copy
+
+ model_config_dict: Dict[str, Any] = {}
+ for key, value in kwargs.items():
+ if key in {"messages", "model"}:
+ continue
+ try:
+ model_config_dict[key] = copy.deepcopy(value)
+ except Exception:
+ model_config_dict[key] = value
+ return model_config_dict
+
+ def _sync_request_log_with_client_kwargs(
+ self, kwargs: Dict[str, Any]
+ ) -> None:
+ r"""Sync request log with final payload passed to the model client."""
+ if not self._log_enabled:
+ return
+
+ log_path = self._log_path_context.get()
+ if not log_path:
+ return
+
+ import copy
+ import json
+
+ try:
+ with open(log_path, "r+", encoding="utf-8") as f:
+ log_data = json.load(f)
+ request_entry: Dict[str, Any] = log_data.get("request", {})
+ messages = kwargs.get("messages")
+ if isinstance(messages, list):
+ try:
+ logged_messages = copy.deepcopy(messages)
+ except Exception:
+ logged_messages = messages
+ request_entry["messages"] = logged_messages
+ if self._log_model_config_dict_enabled:
+ request_entry["model_config_dict"] = (
+ self._extract_model_config_from_client_kwargs(kwargs)
+ )
+ log_data["request"] = request_entry
+ f.seek(0)
+ json.dump(
+ log_data,
+ f,
+ indent=4,
+ ensure_ascii=False,
+ default=str,
+ )
+ f.truncate()
+ self._log_request_synced_context.set(True)
+ except Exception as exc:
+ logger.warning(
+ "Failed to sync request log with client payload: %s", exc
+ )
+
def preprocess_messages(
self, messages: List[OpenAIMessage]
) -> List[OpenAIMessage]:
@@ -511,11 +655,17 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
return response
- def _log_request(self, messages: List[OpenAIMessage]) -> Optional[str]:
+ def _log_request(
+ self,
+ messages: List[OpenAIMessage],
+ model_config_dict: Optional[Dict[str, Any]] = None,
+ ) -> Optional[str]:
r"""Log the request messages to a JSON file if logging is enabled.
Args:
messages (List[OpenAIMessage]): The messages to log.
+ model_config_dict (Optional[Dict[str, Any]]): The effective
+ model configuration used for this request.
Returns:
Optional[str]: The path to the log file if logging is enabled,
@@ -546,15 +696,22 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
log_file_path = os.path.join(log_subdir, f"conv_{timestamp}.json")
- log_entry = {
+ request_entry: Dict[str, Any] = {"messages": messages}
+ if (
+ self._log_model_config_dict_enabled
+ and model_config_dict is not None
+ ):
+ request_entry["model_config_dict"] = model_config_dict
+
+ log_entry: Dict[str, Any] = {
"request_timestamp": datetime.now().isoformat(),
"model": str(self.model_type),
"agent_id": agent_id,
- "request": {"messages": messages},
+ "request": request_entry,
}
- with open(log_file_path, "w") as f:
- json.dump(log_entry, f, indent=4)
+ with open(log_file_path, "w", encoding="utf-8") as f:
+ json.dump(log_entry, f, indent=4, ensure_ascii=False, default=str)
return log_file_path
@@ -571,7 +728,7 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
import json
from datetime import datetime
- with open(log_path, "r+") as f:
+ with open(log_path, "r+", encoding="utf-8") as f:
log_data = json.load(f)
log_data["response_timestamp"] = datetime.now().isoformat()
@@ -585,7 +742,7 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
log_data["response"] = str(response)
f.seek(0)
- json.dump(log_data, f, indent=4)
+ json.dump(log_data, f, indent=4, ensure_ascii=False, default=str)
f.truncate()
def _log_and_trace(self) -> None:
@@ -697,31 +854,49 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
`ChatCompletionStreamManager[BaseModel]` in the structured
stream mode.
"""
+ tools = self._resolve_tools(tools)
# Log the request if logging is enabled
log_path = self._log_request(messages)
- # None -> use default tools
- if tools is None:
- tools = self.model_config_dict.get("tools", None)
- # Empty -> use no tools
- elif not tools:
- tools = None
-
logger.info("Running model: %s", self.model_type)
logger.info("Messages: %s", messages)
logger.info("Response format: %s", response_format)
logger.info("Tools: %s", tools)
- result = self._run(messages, response_format, tools)
+ final_log_path = log_path
+ log_path_token = self._log_path_context.set(log_path)
+ log_client_call_seen_token = self._log_client_call_seen_context.set(
+ False
+ )
+ log_request_synced_token = self._log_request_synced_context.set(False)
+ try:
+ result = self._run(messages, response_format, tools)
+ final_log_path = self._log_path_context.get() or final_log_path
+ finally:
+ if (
+ self._log_enabled
+ and self._log_model_config_dict_enabled
+ and self._log_client_call_seen_context.get()
+ and not self._log_request_synced_context.get()
+ ):
+ logger.warning(
+ "Model client call detected, but request payload was not "
+ "captured in logs."
+ )
+ self._log_path_context.reset(log_path_token)
+ self._log_client_call_seen_context.reset(
+ log_client_call_seen_token
+ )
+ self._log_request_synced_context.reset(log_request_synced_token)
logger.info("Result: %s", result)
# For streaming responses, wrap with logging; otherwise log immediately
if isinstance(result, Stream) or inspect.isgenerator(result):
return _SyncStreamWrapper( # type: ignore[return-value]
- result, log_path, self._log_enabled
+ result, final_log_path, self._log_enabled
)
- if log_path:
- self._log_response(log_path, result)
+ if final_log_path:
+ self._log_response(final_log_path, result)
return result
@observe()
@@ -754,29 +929,49 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
`AsyncChatCompletionStreamManager[BaseModel]` in the structured
stream mode.
"""
+ tools = self._resolve_tools(tools)
# Log the request if logging is enabled
log_path = self._log_request(messages)
- if tools is None:
- tools = self.model_config_dict.get("tools", None)
- elif not tools:
- tools = None
-
logger.info("Running model: %s", self.model_type)
logger.info("Messages: %s", messages)
logger.info("Response format: %s", response_format)
logger.info("Tools: %s", tools)
- result = await self._arun(messages, response_format, tools)
+ final_log_path = log_path
+ log_path_token = self._log_path_context.set(log_path)
+ log_client_call_seen_token = self._log_client_call_seen_context.set(
+ False
+ )
+ log_request_synced_token = self._log_request_synced_context.set(False)
+ try:
+ result = await self._arun(messages, response_format, tools)
+ final_log_path = self._log_path_context.get() or final_log_path
+ finally:
+ if (
+ self._log_enabled
+ and self._log_model_config_dict_enabled
+ and self._log_client_call_seen_context.get()
+ and not self._log_request_synced_context.get()
+ ):
+ logger.warning(
+ "Model client call detected, but request payload was not "
+ "captured in logs."
+ )
+ self._log_path_context.reset(log_path_token)
+ self._log_client_call_seen_context.reset(
+ log_client_call_seen_token
+ )
+ self._log_request_synced_context.reset(log_request_synced_token)
logger.info("Result: %s", result)
# For streaming responses, wrap with logging; otherwise log immediately
if isinstance(result, AsyncStream) or inspect.isasyncgen(result):
return _AsyncStreamWrapper( # type: ignore[return-value]
- result, log_path, self._log_enabled
+ result, final_log_path, self._log_enabled
)
- if log_path:
- self._log_response(log_path, result)
+ if final_log_path:
+ self._log_response(final_log_path, result)
return result
def count_tokens_from_messages(self, messages: List[OpenAIMessage]) -> int:
diff --git a/camel/models/cohere_model.py b/camel/models/cohere_model.py
index b638ad63b..3f976f2b7 100644
--- a/camel/models/cohere_model.py
+++ b/camel/models/cohere_model.py
@@ -324,7 +324,8 @@ class CohereModel(BaseModelBackend):
cohere_messages = self._to_cohere_chatmessage(messages)
try:
- response = self._client.chat(
+ response = self._call_client(
+ self._client.chat,
messages=cohere_messages,
model=self.model_type,
**request_config,
@@ -393,7 +394,8 @@ class CohereModel(BaseModelBackend):
cohere_messages = self._to_cohere_chatmessage(messages)
try:
- response = await self._async_client.chat(
+ response = await self._acall_client(
+ self._async_client.chat,
messages=cohere_messages,
model=self.model_type,
**request_config,
diff --git a/camel/models/deepseek_model.py b/camel/models/deepseek_model.py
index b937e0b0b..7b268b702 100644
--- a/camel/models/deepseek_model.py
+++ b/camel/models/deepseek_model.py
@@ -187,7 +187,8 @@ class DeepSeekModel(OpenAICompatibleModel):
messages, response_format, tools
)
- response = self._client.chat.completions.create(
+ response = self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -218,7 +219,8 @@ class DeepSeekModel(OpenAICompatibleModel):
request_config = self._prepare_request(
messages, response_format, tools
)
- response = await self._async_client.chat.completions.create(
+ response = await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
diff --git a/camel/models/function_gemma_model.py b/camel/models/function_gemma_model.py
index fc83479d6..52a2902f6 100644
--- a/camel/models/function_gemma_model.py
+++ b/camel/models/function_gemma_model.py
@@ -728,7 +728,7 @@ class FunctionGemmaModel(BaseModelBackend):
]
try:
- response = self._client.post(url, json=data)
+ response = self._call_client(self._client.post, url, json=data)
response.raise_for_status()
result = response.json()
return result.get("response", "")
@@ -784,7 +784,9 @@ class FunctionGemmaModel(BaseModelBackend):
]
try:
- response = await self._async_client.post(url, json=data)
+ response = await self._acall_client(
+ self._async_client.post, url, json=data
+ )
response.raise_for_status()
result = response.json()
return result.get("response", "")
diff --git a/camel/models/gemini_model.py b/camel/models/gemini_model.py
index e4a2d60e1..8a24f2fa1 100644
--- a/camel/models/gemini_model.py
+++ b/camel/models/gemini_model.py
@@ -583,7 +583,8 @@ class GeminiModel(OpenAICompatibleModel):
request_config["tools"] = tools
- response = self._client.chat.completions.create(
+ response = self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -640,7 +641,8 @@ class GeminiModel(OpenAICompatibleModel):
request_config["tools"] = tools
- response = await self._async_client.chat.completions.create(
+ response = await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.py
index 3c4047d0b..e8a5ecc86 100644
--- a/camel/models/mistral_model.py
+++ b/camel/models/mistral_model.py
@@ -283,7 +283,8 @@ class MistralModel(BaseModelBackend):
)
mistral_messages = self._to_mistral_chatmessage(messages)
- response = self._client.chat.complete(
+ response = self._call_client(
+ self._client.chat.complete,
messages=mistral_messages,
model=self.model_type,
**request_config,
@@ -346,7 +347,8 @@ class MistralModel(BaseModelBackend):
)
mistral_messages = self._to_mistral_chatmessage(messages)
- response = self._client.chat.complete(
+ response = self._call_client(
+ self._client.chat.complete,
messages=mistral_messages,
model=self.model_type,
**request_config,
diff --git a/camel/models/moonshot_model.py b/camel/models/moonshot_model.py
index 85d7eb649..dc87d32c3 100644
--- a/camel/models/moonshot_model.py
+++ b/camel/models/moonshot_model.py
@@ -267,7 +267,8 @@ class MoonshotModel(InterleavedThinkingMixin, OpenAICompatibleModel):
messages, response_format, tools
)
- return self._client.chat.completions.create(
+ return self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -302,7 +303,8 @@ class MoonshotModel(InterleavedThinkingMixin, OpenAICompatibleModel):
messages, response_format, tools
)
- return await self._async_client.chat.completions.create(
+ return await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
diff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.py
index 7bc05f8d4..3cddfe1e5 100644
--- a/camel/models/openai_compatible_model.py
+++ b/camel/models/openai_compatible_model.py
@@ -281,7 +281,8 @@ class OpenAICompatibleModel(BaseModelBackend):
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
request_config = self._prepare_request_config(tools)
- return self._client.chat.completions.create(
+ return self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -294,7 +295,8 @@ class OpenAICompatibleModel(BaseModelBackend):
) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
request_config = self._prepare_request_config(tools)
- return await self._async_client.chat.completions.create(
+ return await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -313,7 +315,8 @@ class OpenAICompatibleModel(BaseModelBackend):
request_config.pop("stream", None)
try:
- return self._client.beta.chat.completions.parse(
+ return self._call_client(
+ self._client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -326,7 +329,8 @@ class OpenAICompatibleModel(BaseModelBackend):
try_modify_message_with_format(messages[-1], response_format)
request_config["response_format"] = {"type": "json_object"}
try:
- return self._client.beta.chat.completions.parse(
+ return self._call_client(
+ self._client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -348,7 +352,8 @@ class OpenAICompatibleModel(BaseModelBackend):
request_config.pop("stream", None)
try:
- return await self._async_client.beta.chat.completions.parse(
+ return await self._acall_client(
+ self._async_client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -361,7 +366,8 @@ class OpenAICompatibleModel(BaseModelBackend):
try_modify_message_with_format(messages[-1], response_format)
request_config["response_format"] = {"type": "json_object"}
try:
- return await self._async_client.beta.chat.completions.parse(
+ return await self._acall_client(
+ self._async_client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -385,7 +391,8 @@ class OpenAICompatibleModel(BaseModelBackend):
request_config.pop("stream", None)
# Use the beta streaming API for structured outputs
- return self._client.beta.chat.completions.stream(
+ return self._call_client(
+ self._client.beta.chat.completions.stream,
messages=messages,
model=self.model_type,
response_format=response_format,
@@ -407,7 +414,8 @@ class OpenAICompatibleModel(BaseModelBackend):
request_config.pop("stream", None)
# Use the beta streaming API for structured outputs
- return self._async_client.beta.chat.completions.stream(
+ return self._call_client(
+ self._async_client.beta.chat.completions.stream,
messages=messages,
model=self.model_type,
response_format=response_format,
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index 5c24039a1..9d84a2b84 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -384,7 +384,8 @@ class OpenAIModel(BaseModelBackend):
request_config = self._prepare_request_config(tools)
request_config = self._sanitize_config(request_config)
- return self._client.chat.completions.create(
+ return self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -398,7 +399,8 @@ class OpenAIModel(BaseModelBackend):
request_config = self._prepare_request_config(tools)
request_config = self._sanitize_config(request_config)
- return await self._async_client.chat.completions.create(
+ return await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -417,7 +419,8 @@ class OpenAIModel(BaseModelBackend):
request_config.pop("stream", None)
request_config = self._sanitize_config(request_config)
- return self._client.beta.chat.completions.parse(
+ return self._call_client(
+ self._client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -436,7 +439,8 @@ class OpenAIModel(BaseModelBackend):
request_config.pop("stream", None)
request_config = self._sanitize_config(request_config)
- return await self._async_client.beta.chat.completions.parse(
+ return await self._acall_client(
+ self._async_client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -458,7 +462,8 @@ class OpenAIModel(BaseModelBackend):
request_config = self._sanitize_config(request_config)
# Use the beta streaming API for structured outputs
- return self._client.beta.chat.completions.stream(
+ return self._call_client(
+ self._client.beta.chat.completions.stream,
messages=messages,
model=self.model_type,
response_format=response_format,
@@ -482,7 +487,8 @@ class OpenAIModel(BaseModelBackend):
request_config = self._sanitize_config(request_config)
# Use the beta streaming API for structured outputs
- return self._async_client.beta.chat.completions.stream(
+ return self._call_client(
+ self._async_client.beta.chat.completions.stream,
messages=messages,
model=self.model_type,
response_format=response_format,
diff --git a/camel/models/reka_model.py b/camel/models/reka_model.py
index a6d7af82c..5e7a0fd1f 100644
--- a/camel/models/reka_model.py
+++ b/camel/models/reka_model.py
@@ -242,7 +242,8 @@ class RekaModel(BaseModelBackend):
reka_messages = self._convert_openai_to_reka_messages(messages)
- response = await self._async_client.chat.create(
+ response = await self._acall_client(
+ self._async_client.chat.create,
messages=reka_messages,
model=self.model_type,
**self.model_config_dict,
@@ -300,7 +301,8 @@ class RekaModel(BaseModelBackend):
reka_messages = self._convert_openai_to_reka_messages(messages)
- response = self._client.chat.create(
+ response = self._call_client(
+ self._client.chat.create,
messages=reka_messages,
model=self.model_type,
**self.model_config_dict,
diff --git a/camel/models/samba_model.py b/camel/models/samba_model.py
index d15c05c07..b37b5088c 100644
--- a/camel/models/samba_model.py
+++ b/camel/models/samba_model.py
@@ -270,7 +270,8 @@ class SambaModel(BaseModelBackend):
# Handle SambaNova's Cloud API
if self._url == "https://api.sambanova.ai/v1":
request_config = self._prepare_request_config(tools)
- response = self._client.chat.completions.create(
+ response = self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -322,7 +323,8 @@ class SambaModel(BaseModelBackend):
# Handle SambaNova's Cloud API
if self._url == "https://api.sambanova.ai/v1":
request_config = self._prepare_request_config(tools)
- response = self._client.chat.completions.create(
+ response = self._call_client(
+ self._client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -402,7 +404,8 @@ class SambaModel(BaseModelBackend):
try:
# Send the request and handle the response
with httpx.Client() as client:
- response = client.post(
+ response = self._call_client(
+ client.post,
self._url, # type: ignore[arg-type]
headers=headers,
json=data,
@@ -504,7 +507,8 @@ class SambaModel(BaseModelBackend):
# Handle SambaNova's Cloud API
if self._url == "https://api.sambanova.ai/v1":
request_config = self._prepare_request_config(tools)
- response = await self._async_client.chat.completions.create(
+ response = await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -556,7 +560,8 @@ class SambaModel(BaseModelBackend):
# Handle SambaNova's Cloud API
if self._url == "https://api.sambanova.ai/v1":
request_config = self._prepare_request_config(tools)
- response = await self._async_client.chat.completions.create(
+ response = await self._acall_client(
+ self._async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**request_config,
@@ -636,7 +641,8 @@ class SambaModel(BaseModelBackend):
try:
# Send the request and handle the response
with httpx.Client() as client:
- response = client.post(
+ response = self._call_client(
+ client.post,
self._url, # type: ignore[arg-type]
headers=headers,
json=data,
diff --git a/camel/models/sglang_model.py b/camel/models/sglang_model.py
index f8bea6757..42fd0215c 100644
--- a/camel/models/sglang_model.py
+++ b/camel/models/sglang_model.py
@@ -286,7 +286,8 @@ class SGLangModel(BaseModelBackend):
if tools is not None:
extra_params["tools"] = tools
- response = await async_client.chat.completions.create(
+ response = await self._acall_client(
+ async_client.chat.completions.create,
messages=messages,
model=self.model_type,
**extra_params,
@@ -349,7 +350,8 @@ class SGLangModel(BaseModelBackend):
if tools is not None:
extra_params["tools"] = tools
- response = client.chat.completions.create(
+ response = self._call_client(
+ client.chat.completions.create,
messages=messages,
model=self.model_type,
**extra_params,
diff --git a/camel/models/zhipuai_model.py b/camel/models/zhipuai_model.py
index e7b5667c6..20c2f9609 100644
--- a/camel/models/zhipuai_model.py
+++ b/camel/models/zhipuai_model.py
@@ -125,7 +125,8 @@ class ZhipuAIModel(InterleavedThinkingMixin, OpenAICompatibleModel):
try_modify_message_with_format(messages[-1], response_format)
request_config["response_format"] = {"type": "json_object"}
try:
- return self._client.beta.chat.completions.parse(
+ return self._call_client(
+ self._client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
@@ -150,7 +151,8 @@ class ZhipuAIModel(InterleavedThinkingMixin, OpenAICompatibleModel):
try_modify_message_with_format(messages[-1], response_format)
request_config["response_format"] = {"type": "json_object"}
try:
- return await self._async_client.beta.chat.completions.parse(
+ return await self._acall_client(
+ self._async_client.beta.chat.completions.parse,
messages=messages,
model=self.model_type,
**request_config,
diff --git a/docs/conf.py b/docs/conf.py
index 227b53ca0..2482026ea 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.90a2'
+release = '0.2.90a3'
html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
diff --git a/pyproject.toml b/pyproject.toml
index 38313988a..5b66d4eb7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "camel-ai"
-version = "0.2.90a2"
+version = "0.2.90a3"
description = "Communicative Agents for AI Society Study"
authors = [{ name = "CAMEL-AI.org" }]
requires-python = ">=3.10,<3.15"
diff --git a/test/models/test_base_model.py b/test/models/test_base_model.py
index d93379b6c..12e75e1c6 100644
--- a/test/models/test_base_model.py
+++ b/test/models/test_base_model.py
@@ -13,6 +13,7 @@
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
+import json
import uuid
import pytest
@@ -26,6 +27,494 @@ from camel.types import ModelType
class TestBaseModelBackend:
r"""Unit tests for the BaseModelBackend class."""
+ def test_log_request_records_model_config_with_env_flag(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test logging model_config_dict is controlled by env vars."""
+
+ class DummyModel(BaseModelBackend):
+ @property
+ def token_counter(self):
+ pass
+
+ def run(self, messages):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ pass
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ pass
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model_config_dict = {
+ "temperature": 0.2,
+ "system_prompt": "你好,世界",
+ "greeting": "こんにちは",
+ }
+ messages = [{"role": "user", "content": "请帮我写一首诗"}]
+ model = DummyModel(
+ ModelType.GPT_4O_MINI, model_config_dict=model_config_dict
+ )
+
+ log_path = model._log_request(messages, model_config_dict)
+ assert log_path is not None
+
+ with open(log_path, "r", encoding="utf-8") as f:
+ log_text = f.read()
+
+ log_data = json.loads(log_text)
+ assert log_data["request"]["messages"] == messages
+ assert log_data["request"]["model_config_dict"] == model_config_dict
+ assert "你好,世界" in log_text
+ assert "请帮我写一首诗" in log_text
+ assert r"\u4f60\u597d" not in log_text
+
+ def test_log_request_skips_model_config_when_disabled(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test model_config_dict is omitted when env flag is disabled."""
+
+ class DummyModel(BaseModelBackend):
+ @property
+ def token_counter(self):
+ pass
+
+ def run(self, messages):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ pass
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ pass
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "false")
+
+ model = DummyModel(
+ ModelType.GPT_4O_MINI,
+ model_config_dict={"temperature": 0.1, "lang": "中文"},
+ )
+ log_path = model._log_request(
+ [{"role": "user", "content": "test message"}],
+ model.model_config_dict,
+ )
+ assert log_path is not None
+
+ with open(log_path, "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert "model_config_dict" not in log_data["request"]
+
+ def test_log_request_model_config_enabled_by_default_when_log_on(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test model_config logging defaults to enabled with log enabled."""
+
+ class DummyModel(BaseModelBackend):
+ @property
+ def token_counter(self):
+ pass
+
+ def run(self, messages):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ pass
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ pass
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.delenv(
+ "CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", raising=False
+ )
+
+ model = DummyModel(
+ ModelType.GPT_4O_MINI, model_config_dict={"temperature": 0.3}
+ )
+ log_path = model._log_request(
+ [{"role": "user", "content": "test message"}],
+ model.model_config_dict,
+ )
+ assert log_path is not None
+
+ with open(log_path, "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert log_data["request"]["model_config_dict"] == {"temperature": 0.3}
+
+ def test_log_response_preserves_multilingual_text(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test response logging keeps multilingual text readable."""
+
+ class DummyModel(BaseModelBackend):
+ @property
+ def token_counter(self):
+ pass
+
+ def run(self, messages):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ pass
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ pass
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model = DummyModel(
+ ModelType.GPT_4O_MINI, model_config_dict={"lang": "多语言"}
+ )
+ log_path = model._log_request(
+ [{"role": "user", "content": "你好,مرحبا,こんにちは"}]
+ )
+ assert log_path is not None
+
+ model._log_response(
+ log_path,
+ {
+ "content": "回复:你好,مرحبا,こんにちは",
+ "extra": "Русский текст",
+ },
+ )
+
+ with open(log_path, "r", encoding="utf-8") as f:
+ log_text = f.read()
+
+ assert "回复:你好,مرحبا,こんにちは" in log_text
+ assert "Русский текст" in log_text
+ assert r"\u56de\u590d" not in log_text
+
+ def test_run_logs_final_client_payload_model_config_dict(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test run logs the final payload passed to the model client."""
+
+ class FakeCompletions:
+ def create(self, **kwargs):
+ return {"status": "ok", "request_keys": sorted(kwargs)}
+
+ class FakeChat:
+ def __init__(self):
+ self.completions = FakeCompletions()
+
+ class FakeClient:
+ def __init__(self):
+ self.chat = FakeChat()
+
+ class DummyModel(BaseModelBackend):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._client = FakeClient()
+
+ @property
+ def token_counter(self):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ return self._call_client(
+ self._client.chat.completions.create,
+ messages=messages,
+ model=self.model_type,
+ tools=tools,
+ temperature=0.9,
+ )
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ return {"status": "ok", "tool_count": len(tools or [])}
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model = DummyModel(
+ ModelType.GPT_4O_MINI,
+ model_config_dict={
+ "temperature": 0.1,
+ "tools": [
+ {
+ "type": "function",
+ "function": {"name": "default_tool"},
+ }
+ ],
+ },
+ )
+ runtime_tools = [
+ {
+ "type": "function",
+ "function": {
+ "name": "weather_lookup",
+ "description": "查询天气信息",
+ },
+ }
+ ]
+
+ model.run(
+ messages=[{"role": "user", "content": "帮我查北京天气"}],
+ tools=runtime_tools,
+ )
+
+ log_files = list(tmp_path.rglob("conv_*.json"))
+ assert len(log_files) == 1
+
+ with open(log_files[0], "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert (
+ log_data["request"]["model_config_dict"]["tools"] == runtime_tools
+ )
+ assert log_data["request"]["model_config_dict"]["temperature"] == 0.9
+
+ @pytest.mark.asyncio
+ async def test_arun_logs_final_client_payload_model_config_dict(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test arun logs final payload passed to the async client."""
+
+ class FakeAsyncCompletions:
+ async def create(self, **kwargs):
+ return {"status": "ok", "request_keys": sorted(kwargs)}
+
+ class FakeAsyncChat:
+ def __init__(self):
+ self.completions = FakeAsyncCompletions()
+
+ class FakeAsyncClient:
+ def __init__(self):
+ self.chat = FakeAsyncChat()
+
+ class DummyModel(BaseModelBackend):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._async_client = FakeAsyncClient()
+
+ @property
+ def token_counter(self):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ return {"status": "unused"}
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ return await self._acall_client(
+ self._async_client.chat.completions.create,
+ messages=messages,
+ model=self.model_type,
+ tools=tools,
+ temperature=0.6,
+ )
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model = DummyModel(
+ ModelType.GPT_4O_MINI,
+ model_config_dict={"temperature": 0.1},
+ )
+ runtime_tools = [
+ {
+ "type": "function",
+ "function": {"name": "search"},
+ }
+ ]
+
+ await model.arun(
+ messages=[{"role": "user", "content": "test async"}],
+ tools=runtime_tools,
+ )
+
+ log_files = list(tmp_path.rglob("conv_*.json"))
+ assert len(log_files) == 1
+
+ with open(log_files[0], "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert (
+ log_data["request"]["model_config_dict"]["tools"] == runtime_tools
+ )
+ assert log_data["request"]["model_config_dict"]["temperature"] == 0.6
+
+ def test_run_logs_payload_from_positional_client_args(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test positional client args are normalized into request logs."""
+
+ class FakeCompletions:
+ def create(
+ self,
+ messages,
+ model,
+ tools=None,
+ temperature=None,
+ ):
+ return {"status": "ok", "model": str(model)}
+
+ class FakeChat:
+ def __init__(self):
+ self.completions = FakeCompletions()
+
+ class FakeClient:
+ def __init__(self):
+ self.chat = FakeChat()
+
+ class DummyModel(BaseModelBackend):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._client = FakeClient()
+
+ @property
+ def token_counter(self):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ return self._call_client(
+ self._client.chat.completions.create,
+ messages,
+ self.model_type,
+ tools,
+ 0.7,
+ )
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ return {"status": "unused"}
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model = DummyModel(ModelType.GPT_4O_MINI, model_config_dict={})
+ runtime_tools = [
+ {
+ "type": "function",
+ "function": {"name": "positional_tool"},
+ }
+ ]
+
+ model.run(
+ messages=[{"role": "user", "content": "positional call"}],
+ tools=runtime_tools,
+ )
+
+ log_files = list(tmp_path.rglob("conv_*.json"))
+ assert len(log_files) == 1
+
+ with open(log_files[0], "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert (
+ log_data["request"]["model_config_dict"]["tools"] == runtime_tools
+ )
+ assert log_data["request"]["model_config_dict"]["temperature"] == 0.7
+
+ def test_run_logs_json_payload_when_client_call_has_no_messages(
+ self, monkeypatch, tmp_path
+ ):
+ r"""Test non-chat client payloads are still captured in logs."""
+
+ class FakeClient:
+ def post(self, url, json=None):
+ return {"status": "ok", "url": url, "json": json}
+
+ class DummyModel(BaseModelBackend):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._client = FakeClient()
+
+ @property
+ def token_counter(self):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ return self._call_client(
+ self._client.post,
+ "https://example.org/infer",
+ json={
+ "model": str(self.model_type),
+ "prompt": "hello",
+ "temperature": 0.4,
+ },
+ )
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ return {"status": "unused"}
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model = DummyModel(ModelType.GPT_4O_MINI, model_config_dict={})
+ model.run(messages=[{"role": "user", "content": "ping"}])
+
+ log_files = list(tmp_path.rglob("conv_*.json"))
+ assert len(log_files) == 1
+
+ with open(log_files[0], "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert log_data["request"]["messages"][0]["content"] == "ping"
+ assert (
+ log_data["request"]["model_config_dict"]["url"]
+ == "https://example.org/infer"
+ )
+ assert (
+ log_data["request"]["model_config_dict"]["json"]["temperature"]
+ == 0.4
+ )
+
+ def test_run_warns_when_client_call_payload_is_empty(
+ self, monkeypatch, tmp_path, caplog
+ ):
+ r"""Test empty client payloads do not silently fallback."""
+
+ class FakeClient:
+ def ping(self):
+ return {"status": "ok"}
+
+ class DummyModel(BaseModelBackend):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._client = FakeClient()
+
+ @property
+ def token_counter(self):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ return self._call_client(self._client.ping)
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ return {"status": "unused"}
+
+ monkeypatch.setenv("CAMEL_MODEL_LOG_ENABLED", "true")
+ monkeypatch.setenv("CAMEL_LOG_DIR", str(tmp_path))
+ monkeypatch.setenv("CAMEL_MODEL_LOG_MODEL_CONFIG_ENABLED", "true")
+
+ model = DummyModel(ModelType.GPT_4O_MINI, model_config_dict={})
+ with caplog.at_level("WARNING"):
+ model.run(messages=[{"role": "user", "content": "ping"}])
+
+ assert "request payload was not captured in logs" in caplog.text
+
+ log_files = list(tmp_path.rglob("conv_*.json"))
+ assert len(log_files) == 1
+
+ with open(log_files[0], "r", encoding="utf-8") as f:
+ log_data = json.load(f)
+
+ assert "model_config_dict" not in log_data["request"]
+
def test_preprocess_messages(self):
r"""Test message preprocessing removes thinking content correctly."""
diff --git a/uv.lock b/uv.lock
index 8614d736c..ee6eab981 100644
--- a/uv.lock
+++ b/uv.lock
@@ -886,7 +886,7 @@ wheels = [
[[package]]
name = "camel-ai"
-version = "0.2.90a2"
+version = "0.2.90a3"
source = { editable = "." }
dependencies = [
{ name = "astor" },