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

feat: Enhance OpenAICompatibleModel (#2221)

Tao Sun66ebc9edb8ba
diff --git a/camel/models/groq_model.py b/camel/models/groq_model.py
index 9790c3eea..dca8fb482 100644
--- a/camel/models/groq_model.py
+++ b/camel/models/groq_model.py
@@ -12,20 +12,11 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import Any, Dict, List, Optional, Type, Union
-
-from openai import AsyncStream, Stream
-from pydantic import BaseModel
+from typing import Any, Dict, Optional, Union
from camel.configs import GROQ_API_PARAMS, GroqConfig
-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 (
- ChatCompletion,
- ChatCompletionChunk,
- ModelType,
-)
+from camel.types import ModelType
from camel.utils import (
BaseTokenCounter,
api_keys_required,
@@ -82,87 +73,6 @@ class GroqModel(OpenAICompatibleModel):
timeout=timeout,
)
- def _prepare_request(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Dict[str, Any]:
- request_config = self.model_config_dict.copy()
- if tools:
- request_config["tools"] = tools
- elif response_format:
- try_modify_message_with_format(messages[-1], response_format)
- request_config["response_format"] = {"type": "json_object"}
-
- return request_config
-
- def _run(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- r"""Runs inference of Groq chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- response_format (Optional[Type[BaseModel]]): The format of the
- response.
- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
- use for the request.
-
- Returns:
- Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `Stream[ChatCompletionChunk]` in the stream mode.
- """
- request_config = self._prepare_request(
- messages, response_format, tools
- )
-
- response = self._client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
-
- return response
-
- async def _arun(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- r"""Runs inference of Groq chat completion asynchronously.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- response_format (Optional[Type[BaseModel]]): The format of the
- response.
- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
- use for the request.
-
- Returns:
- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `AsyncStream[ChatCompletionChunk]` in the stream mode.
- """
- request_config = self._prepare_request(
- messages, response_format, tools
- )
-
- response = await self._async_client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
-
- return response
-
def check_model_config(self):
r"""Check whether the model configuration contains any unexpected
arguments to Groq API. But Groq API does not have any additional
diff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.py
index 338e4ea3e..7ee4ccc39 100644
--- a/camel/models/openai_compatible_model.py
+++ b/camel/models/openai_compatible_model.py
@@ -13,12 +13,15 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
+from json import JSONDecodeError
from typing import Any, Dict, List, Optional, Type, Union
from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream
-from pydantic import BaseModel
+from pydantic import BaseModel, ValidationError
+from camel.logger import get_logger
from camel.messages import OpenAIMessage
+from camel.models._utils import try_modify_message_with_format
from camel.models.base_model import BaseModelBackend
from camel.types import (
ChatCompletion,
@@ -30,6 +33,8 @@ from camel.utils import (
OpenAITokenCounter,
)
+logger = get_logger(__name__)
+
class OpenAICompatibleModel(BaseModelBackend):
r"""Constructor for model backend supporting OpenAI compatibility.
@@ -187,11 +192,28 @@ class OpenAICompatibleModel(BaseModelBackend):
if tools is not None:
request_config["tools"] = tools
- return self._client.beta.chat.completions.parse(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
+ try:
+ return self._client.beta.chat.completions.parse(
+ messages=messages,
+ model=self.model_type,
+ **request_config,
+ )
+ except (ValidationError, JSONDecodeError) as e:
+ logger.warning(
+ f"Format validation error: {e}. "
+ f"Attempting fallback with JSON format."
+ )
+ 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,
@@ -209,11 +231,28 @@ class OpenAICompatibleModel(BaseModelBackend):
if tools is not None:
request_config["tools"] = tools
- return await self._async_client.beta.chat.completions.parse(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
+ try:
+ return await self._async_client.beta.chat.completions.parse(
+ messages=messages,
+ model=self.model_type,
+ **request_config,
+ )
+ except (ValidationError, JSONDecodeError) as e:
+ logger.warning(
+ f"Format validation error: {e}. "
+ f"Attempting fallback with JSON format."
+ )
+ 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
@property
def token_counter(self) -> BaseTokenCounter:
diff --git a/camel/models/openrouter_model.py b/camel/models/openrouter_model.py
index 50afe46b3..7b5a1cce8 100644
--- a/camel/models/openrouter_model.py
+++ b/camel/models/openrouter_model.py
@@ -12,20 +12,11 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import Any, Dict, List, Optional, Type, Union
-
-from openai import AsyncStream, Stream
-from pydantic import BaseModel
+from typing import Any, Dict, Optional, Union
from camel.configs import OPENROUTER_API_PARAMS, OpenRouterConfig
-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 (
- ChatCompletion,
- ChatCompletionChunk,
- ModelType,
-)
+from camel.types import ModelType
from camel.utils import (
BaseTokenCounter,
api_keys_required,
@@ -83,87 +74,6 @@ class OpenRouterModel(OpenAICompatibleModel):
timeout=timeout,
)
- def _prepare_request(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Dict[str, Any]:
- request_config = self.model_config_dict.copy()
- if tools:
- request_config["tools"] = tools
- elif response_format:
- try_modify_message_with_format(messages[-1], response_format)
- request_config["response_format"] = {"type": "json_object"}
-
- return request_config
-
- def _run(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- r"""Runs inference of OpenAI chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- response_format (Optional[Type[BaseModel]]): The format of the
- response.
- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
- use for the request.
-
- Returns:
- Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `Stream[ChatCompletionChunk]` in the stream mode.
- """
- request_config = self._prepare_request(
- messages, response_format, tools
- )
-
- response = self._client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
-
- return response
-
- async def _arun(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- r"""Runs inference of OpenRouter chat completion asynchronously.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- response_format (Optional[Type[BaseModel]]): The format of the
- response.
- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
- use for the request.
-
- Returns:
- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `AsyncStream[ChatCompletionChunk]` in the stream mode.
- """
- request_config = self._prepare_request(
- messages, response_format, tools
- )
-
- response = await self._async_client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
-
- return response
-
def check_model_config(self):
r"""Check whether the model configuration contains any unexpected
arguments to OpenRouter API. But OpenRouter API does not have any
diff --git a/camel/models/qwen_model.py b/camel/models/qwen_model.py
index aae80107d..b0e2bfd0b 100644
--- a/camel/models/qwen_model.py
+++ b/camel/models/qwen_model.py
@@ -13,20 +13,11 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import Any, Dict, List, Optional, Type, Union
-
-from openai import AsyncStream, Stream
-from pydantic import BaseModel
+from typing import Any, Dict, Optional, Union
from camel.configs import QWEN_API_PARAMS, QwenConfig
-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 (
- ChatCompletion,
- ChatCompletionChunk,
- ModelType,
-)
+from camel.types import ModelType
from camel.utils import (
BaseTokenCounter,
api_keys_required,
@@ -88,77 +79,6 @@ class QwenModel(OpenAICompatibleModel):
timeout=timeout,
)
- async def _arun(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- r"""Runs inference of Qwen chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
-
- Returns:
- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `AsyncStream[ChatCompletionChunk]` in the stream mode.
- """
- request_config = self._prepare_request(
- messages, response_format, tools
- )
-
- response = await self._async_client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
- return response
-
- def _run(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- r"""Runs inference of Qwen chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
-
- Returns:
- Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `Stream[ChatCompletionChunk]` in the stream mode.
- """
- request_config = self._prepare_request(
- messages, response_format, tools
- )
-
- response = self._client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
- return response
-
- def _prepare_request(
- self,
- messages: List[OpenAIMessage],
- response_format: Optional[Type[BaseModel]] = None,
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Dict[str, Any]:
- request_config = self.model_config_dict.copy()
- if tools:
- request_config["tools"] = tools
- elif response_format:
- try_modify_message_with_format(messages[-1], response_format)
- request_config["response_format"] = {"type": "json_object"}
-
- return request_config
-
def check_model_config(self):
r"""Check whether the model configuration contains any
unexpected arguments to Qwen API.
diff --git a/examples/models/openai_compatibility_model_examples/zhipu_response_format.py b/examples/models/openai_compatibility_model_examples/zhipu_response_format.py
new file mode 100644
index 000000000..8de48f0d8
--- /dev/null
+++ b/examples/models/openai_compatibility_model_examples/zhipu_response_format.py
@@ -0,0 +1,57 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+from pydantic import BaseModel
+
+from camel.agents import ChatAgent
+from camel.models import ModelFactory
+from camel.types import ModelPlatformType
+
+
+class ResponseFormat(BaseModel):
+ question: str
+ reasoning: str
+ answer: str
+
+
+# Take calling model from DashScope as an example
+# Refer: https://dashscope.console.aliyun.com/overview
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,
+ model_type="glm-4",
+ api_key="fb75bxxxxx",
+ url="https://open.bigmodel.cn/api/paas/v4/",
+ model_config_dict={"temperature": 0.4},
+)
+
+assistant_sys_msg = "You are a helpful assistant."
+
+agent = ChatAgent(assistant_sys_msg, model=model, token_limit=4096)
+
+user_msg = """Say hi to CAMEL AI, one open-source community
+ dedicated to the study of autonomous and communicative agents."""
+
+assistant_response = agent.step(user_msg, response_format=ResponseFormat)
+print(assistant_response.msg.content)
+
+# ruff: noqa: E501
+"""
+===============================================================================
+{
+ "question": "Say hi to CAMEL AI, one open-source community dedicated to the study of autonomous and communicative agents.",
+ "reasoning": "This is a greeting and recognition of CAMEL AI's focus on autonomous and communicative agents.",
+ "answer": "Hello CAMEL AI!"
+}
+===============================================================================
+"""