Skip to content

Commit ba570d0

Browse files
authored
chore: update zhipu_model and fix redundant api call (#3197)
1 parent 16f6c98 commit ba570d0

File tree

8 files changed

+66
-19
lines changed

8 files changed

+66
-19
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ body:
2626
attributes:
2727
label: What version of camel are you using?
2828
description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.
29-
placeholder: E.g., 0.2.76a5
29+
placeholder: E.g., 0.2.76a6
3030
validations:
3131
required: true
3232

camel/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from camel.logger import disable_logging, enable_logging, set_log_level
1616

17-
__version__ = '0.2.76a5'
17+
__version__ = '0.2.76a6'
1818

1919
__all__ = [
2020
'__version__',

camel/models/azure_openai_model.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,6 @@ def _run(
247247
)
248248
is_streaming = self.model_config_dict.get("stream", False)
249249
if response_format:
250-
result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
251-
self._request_parse(messages, response_format, tools)
252-
)
253250
if is_streaming:
254251
return self._request_stream_parse(
255252
messages, response_format, tools
@@ -308,9 +305,6 @@ async def _arun(
308305
)
309306
is_streaming = self.model_config_dict.get("stream", False)
310307
if response_format:
311-
result: Union[
312-
ChatCompletion, AsyncStream[ChatCompletionChunk]
313-
] = await self._arequest_parse(messages, response_format, tools)
314308
if is_streaming:
315309
return await self._arequest_stream_parse(
316310
messages, response_format, tools

camel/models/openai_compatible_model.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@ def _run(
190190
is_streaming = self.model_config_dict.get("stream", False)
191191

192192
if response_format:
193-
result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
194-
self._request_parse(messages, response_format, tools)
195-
)
196193
if is_streaming:
197194
# Use streaming parse for structured output
198195
return self._request_stream_parse(
@@ -256,9 +253,6 @@ async def _arun(
256253
is_streaming = self.model_config_dict.get("stream", False)
257254

258255
if response_format:
259-
result: Union[
260-
ChatCompletion, AsyncStream[ChatCompletionChunk]
261-
] = await self._arequest_parse(messages, response_format, tools)
262256
if is_streaming:
263257
# Use streaming parse for structured output
264258
return await self._arequest_stream_parse(

camel/models/zhipuai_model.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414

1515
import os
16-
from typing import Any, Dict, Optional, Union
16+
from typing import Any, Dict, List, Optional, Type, Union
17+
18+
from pydantic import BaseModel
1719

1820
from camel.configs import ZhipuAIConfig
21+
from camel.logger import get_logger
22+
from camel.messages import OpenAIMessage
23+
from camel.models._utils import try_modify_message_with_format
1924
from camel.models.openai_compatible_model import OpenAICompatibleModel
20-
from camel.types import ModelType
25+
from camel.types import (
26+
ChatCompletion,
27+
ModelType,
28+
)
2129
from camel.utils import (
2230
BaseTokenCounter,
2331
api_keys_required,
2432
)
2533

34+
logger = get_logger(__name__)
35+
2636

2737
class ZhipuAIModel(OpenAICompatibleModel):
2838
r"""ZhipuAI API in a unified OpenAICompatibleModel interface.
@@ -85,3 +95,52 @@ def __init__(
8595
max_retries=max_retries,
8696
**kwargs,
8797
)
98+
99+
def _request_parse(
100+
self,
101+
messages: List[OpenAIMessage],
102+
response_format: Type[BaseModel],
103+
tools: Optional[List[Dict[str, Any]]] = None,
104+
) -> ChatCompletion:
105+
import copy
106+
107+
request_config = copy.deepcopy(self.model_config_dict)
108+
request_config.pop("stream", None)
109+
if tools is not None:
110+
request_config["tools"] = tools
111+
112+
try_modify_message_with_format(messages[-1], response_format)
113+
request_config["response_format"] = {"type": "json_object"}
114+
try:
115+
return self._client.beta.chat.completions.parse(
116+
messages=messages,
117+
model=self.model_type,
118+
**request_config,
119+
)
120+
except Exception as e:
121+
logger.error(f"Fallback attempt also failed: {e}")
122+
raise
123+
124+
async def _arequest_parse(
125+
self,
126+
messages: List[OpenAIMessage],
127+
response_format: Type[BaseModel],
128+
tools: Optional[List[Dict[str, Any]]] = None,
129+
) -> ChatCompletion:
130+
import copy
131+
132+
request_config = copy.deepcopy(self.model_config_dict)
133+
request_config.pop("stream", None)
134+
if tools is not None:
135+
request_config["tools"] = tools
136+
try_modify_message_with_format(messages[-1], response_format)
137+
request_config["response_format"] = {"type": "json_object"}
138+
try:
139+
return await self._async_client.beta.chat.completions.parse(
140+
messages=messages,
141+
model=self.model_type,
142+
**request_config,
143+
)
144+
except Exception as e:
145+
logger.error(f"Fallback attempt also failed: {e}")
146+
raise

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
project = 'CAMEL'
2828
copyright = '2024, CAMEL-AI.org'
2929
author = 'CAMEL-AI.org'
30-
release = '0.2.76a5'
30+
release = '0.2.76a6'
3131

3232
html_favicon = (
3333
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "camel-ai"
7-
version = "0.2.76a5"
7+
version = "0.2.76a6"
88
description = "Communicative Agents for AI Society Study"
99
authors = [{ name = "CAMEL-AI.org" }]
1010
requires-python = ">=3.10,<3.13"

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)