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

chore: update zhipu_model and fix redundant api call (#3197)

Tao Sunba570d02c829
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index 2bc3c8c42..9e2a58bbe 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.76a5
+ placeholder: E.g., 0.2.76a6
validations:
required: true
diff --git a/camel/__init__.py b/camel/__init__.py
index 79309436f..2c808d0fa 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.76a5'
+__version__ = '0.2.76a6'
__all__ = [
'__version__',
diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.py
index 1e4c6c55b..b7cef4218 100644
--- a/camel/models/azure_openai_model.py
+++ b/camel/models/azure_openai_model.py
@@ -247,9 +247,6 @@ class AzureOpenAIModel(BaseModelBackend):
)
is_streaming = self.model_config_dict.get("stream", False)
if response_format:
- result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
- self._request_parse(messages, response_format, tools)
- )
if is_streaming:
return self._request_stream_parse(
messages, response_format, tools
@@ -308,9 +305,6 @@ class AzureOpenAIModel(BaseModelBackend):
)
is_streaming = self.model_config_dict.get("stream", False)
if response_format:
- result: Union[
- ChatCompletion, AsyncStream[ChatCompletionChunk]
- ] = await self._arequest_parse(messages, response_format, tools)
if is_streaming:
return await self._arequest_stream_parse(
messages, response_format, tools
diff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.py
index 4fd9c3b1f..14f7d8271 100644
--- a/camel/models/openai_compatible_model.py
+++ b/camel/models/openai_compatible_model.py
@@ -190,9 +190,6 @@ class OpenAICompatibleModel(BaseModelBackend):
is_streaming = self.model_config_dict.get("stream", False)
if response_format:
- result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
- self._request_parse(messages, response_format, tools)
- )
if is_streaming:
# Use streaming parse for structured output
return self._request_stream_parse(
@@ -256,9 +253,6 @@ class OpenAICompatibleModel(BaseModelBackend):
is_streaming = self.model_config_dict.get("stream", False)
if response_format:
- result: Union[
- ChatCompletion, AsyncStream[ChatCompletionChunk]
- ] = await self._arequest_parse(messages, response_format, tools)
if is_streaming:
# Use streaming parse for structured output
return await self._arequest_stream_parse(
diff --git a/camel/models/zhipuai_model.py b/camel/models/zhipuai_model.py
index cea234ee2..b7b02ad59 100644
--- a/camel/models/zhipuai_model.py
+++ b/camel/models/zhipuai_model.py
@@ -13,16 +13,26 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import Any, Dict, Optional, Union
+from typing import Any, Dict, List, Optional, Type, Union
+
+from pydantic import BaseModel
from camel.configs import ZhipuAIConfig
+from camel.logger import get_logger
+from camel.messages import OpenAIMessage
+from camel.models._utils import try_modify_message_with_format
from camel.models.openai_compatible_model import OpenAICompatibleModel
-from camel.types import ModelType
+from camel.types import (
+ ChatCompletion,
+ ModelType,
+)
from camel.utils import (
BaseTokenCounter,
api_keys_required,
)
+logger = get_logger(__name__)
+
class ZhipuAIModel(OpenAICompatibleModel):
r"""ZhipuAI API in a unified OpenAICompatibleModel interface.
@@ -85,3 +95,52 @@ class ZhipuAIModel(OpenAICompatibleModel):
max_retries=max_retries,
**kwargs,
)
+
+ def _request_parse(
+ self,
+ messages: List[OpenAIMessage],
+ response_format: Type[BaseModel],
+ tools: Optional[List[Dict[str, Any]]] = None,
+ ) -> ChatCompletion:
+ import copy
+
+ request_config = copy.deepcopy(self.model_config_dict)
+ request_config.pop("stream", None)
+ if tools is not None:
+ request_config["tools"] = tools
+
+ try_modify_message_with_format(messages[-1], response_format)
+ request_config["response_format"] = {"type": "json_object"}
+ try:
+ return self._client.beta.chat.completions.parse(
+ messages=messages,
+ model=self.model_type,
+ **request_config,
+ )
+ except Exception as e:
+ logger.error(f"Fallback attempt also failed: {e}")
+ raise
+
+ async def _arequest_parse(
+ self,
+ messages: List[OpenAIMessage],
+ response_format: Type[BaseModel],
+ tools: Optional[List[Dict[str, Any]]] = None,
+ ) -> ChatCompletion:
+ import copy
+
+ request_config = copy.deepcopy(self.model_config_dict)
+ request_config.pop("stream", None)
+ if tools is not None:
+ request_config["tools"] = tools
+ 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(
+ messages=messages,
+ model=self.model_type,
+ **request_config,
+ )
+ except Exception as e:
+ logger.error(f"Fallback attempt also failed: {e}")
+ raise
diff --git a/docs/conf.py b/docs/conf.py
index d57517499..4ddabf6b4 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.76a5'
+release = '0.2.76a6'
html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
diff --git a/pyproject.toml b/pyproject.toml
index 9d4bc7a39..f3eb78b66 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "camel-ai"
-version = "0.2.76a5"
+version = "0.2.76a6"
description = "Communicative Agents for AI Society Study"
authors = [{ name = "CAMEL-AI.org" }]
requires-python = ">=3.10,<3.13"
diff --git a/uv.lock b/uv.lock
index 8def144f2..178d555c6 100644
--- a/uv.lock
+++ b/uv.lock
@@ -650,7 +650,7 @@ wheels = [
[[package]]
name = "camel-ai"
-version = "0.2.76a5"
+version = "0.2.76a6"
source = { editable = "." }
dependencies = [
{ name = "colorama" },