🐫 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: enhance Azure and OpenAI models with response format and tools (#1815)
samanhappyb6ebe9e41b0a
diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.pyindex 19a483151..a960750a5 100644--- a/camel/models/azure_openai_model.py+++ b/camel/models/azure_openai_model.py@@ -128,18 +128,23 @@ class AzureOpenAIModel(BaseModelBackend):Args:messages (List[OpenAIMessage]): Message list with the chat historyin 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."""- response = self._client.chat.completions.create(- messages=messages,- model=self.azure_deployment_name, # type:ignore[arg-type]- **self.model_config_dict,+ response_format = response_format or self.model_config_dict.get(+ "response_format", None)- return response+ if response_format:+ return self._request_parse(messages, response_format, tools)+ else:+ return self._request_chat_completion(messages, tools)async def _arun(self,@@ -152,18 +157,93 @@ class AzureOpenAIModel(BaseModelBackend):Args:messages (List[OpenAIMessage]): Message list with the chat historyin 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."""- response = await self._async_client.chat.completions.create(+ response_format = response_format or self.model_config_dict.get(+ "response_format", None+ )+ if response_format:+ return await self._arequest_parse(messages, response_format, tools)+ else:+ return await self._arequest_chat_completion(messages, tools)++ def _request_chat_completion(+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ request_config = self.model_config_dict.copy()++ if tools:+ request_config["tools"] = tools++ return self._client.chat.completions.create(+ messages=messages,+ model=self.azure_deployment_name, # type:ignore[arg-type]+ **request_config,+ )++ async def _arequest_chat_completion(+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:+ request_config = self.model_config_dict.copy()++ if tools:+ request_config["tools"] = tools++ return await self._async_client.chat.completions.create(+ messages=messages,+ model=self.azure_deployment_name, # type:ignore[arg-type]+ **request_config,+ )++ def _request_parse(+ self,+ messages: List[OpenAIMessage],+ response_format: Type[BaseModel],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ request_config = self.model_config_dict.copy()++ request_config["response_format"] = response_format+ request_config.pop("stream", None)+ if tools is not None:+ request_config["tools"] = tools++ return self._client.beta.chat.completions.parse(+ messages=messages,+ model=self.azure_deployment_name, # type:ignore[arg-type]+ **request_config,+ )++ async def _arequest_parse(+ self,+ messages: List[OpenAIMessage],+ response_format: Type[BaseModel],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ request_config = self.model_config_dict.copy()++ request_config["response_format"] = response_format+ request_config.pop("stream", None)+ if tools is not None:+ request_config["tools"] = tools++ return await self._async_client.beta.chat.completions.parse(messages=messages,model=self.azure_deployment_name, # type:ignore[arg-type]- **self.model_config_dict,+ **request_config,)- return responsedef check_model_config(self):r"""Check whether the model configuration contains anydiff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.pyindex d77cded87..95b81b047 100644--- a/camel/models/openai_compatible_model.py+++ b/camel/models/openai_compatible_model.py@@ -86,18 +86,23 @@ class OpenAICompatibleModel(BaseModelBackend):Args:messages (List[OpenAIMessage]): Message list with the chat historyin 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."""- response = self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **self.model_config_dict,+ response_format = response_format or self.model_config_dict.get(+ "response_format", None)- return response+ if response_format:+ return self._request_parse(messages, response_format, tools)+ else:+ return self._request_chat_completion(messages, tools)async def _arun(self,@@ -110,18 +115,93 @@ class OpenAICompatibleModel(BaseModelBackend):Args:messages (List[OpenAIMessage]): Message list with the chat historyin 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."""- response = await self._async_client.chat.completions.create(+ response_format = response_format or self.model_config_dict.get(+ "response_format", None+ )+ if response_format:+ return await self._arequest_parse(messages, response_format, tools)+ else:+ return await self._arequest_chat_completion(messages, tools)++ def _request_chat_completion(+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ request_config = self.model_config_dict.copy()++ if tools:+ request_config["tools"] = tools++ return self._client.chat.completions.create(+ messages=messages,+ model=self.model_type,+ **request_config,+ )++ async def _arequest_chat_completion(+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:+ request_config = self.model_config_dict.copy()++ if tools:+ request_config["tools"] = tools++ return await self._async_client.chat.completions.create(+ messages=messages,+ model=self.model_type,+ **request_config,+ )++ def _request_parse(+ self,+ messages: List[OpenAIMessage],+ response_format: Type[BaseModel],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ request_config = self.model_config_dict.copy()++ request_config["response_format"] = response_format+ request_config.pop("stream", None)+ if tools is not None:+ request_config["tools"] = tools++ return self._client.beta.chat.completions.parse(+ messages=messages,+ model=self.model_type,+ **request_config,+ )++ async def _arequest_parse(+ self,+ messages: List[OpenAIMessage],+ response_format: Type[BaseModel],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ request_config = self.model_config_dict.copy()++ request_config["response_format"] = response_format+ request_config.pop("stream", None)+ if tools is not None:+ request_config["tools"] = tools++ return await self._async_client.beta.chat.completions.parse(messages=messages,model=self.model_type,- **self.model_config_dict,+ **request_config,)- return response@propertydef token_counter(self) -> BaseTokenCounter: