🐫 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
chore: fix litellm_functioncall (#2799)
Tao Sun0dc53c6e82db
diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.pyindex 03266c63a..863822249 100644--- a/camel/models/litellm_model.py+++ b/camel/models/litellm_model.py@@ -12,6 +12,7 @@# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os+import uuidfrom typing import Any, Dict, List, Optional, Type, Unionfrom pydantic import BaseModel@@ -98,23 +99,47 @@ class LiteLLMModel(BaseModelBackend):Returns:ChatCompletion: The response object in OpenAI's format."""- return ChatCompletion.construct(- id=response.id,- choices=[++ converted_choices = []+ for choice in response.choices:+ # Build the assistant message dict+ msg_dict: Dict[str, Any] = {+ "role": choice.message.role,+ "content": choice.message.content,+ }++ if getattr(choice.message, "tool_calls", None):+ msg_dict["tool_calls"] = choice.message.tool_calls++ elif getattr(choice.message, "function_call", None):+ func_call = choice.message.function_call+ msg_dict["tool_calls"] = [+ {+ "id": f"call_{uuid.uuid4().hex[:24]}",+ "type": "function",+ "function": {+ "name": getattr(func_call, "name", None),+ "arguments": getattr(func_call, "arguments", "{}"),+ },+ }+ ]++ converted_choices.append({- "index": response.choices[0].index,- "message": {- "role": response.choices[0].message.role,- "content": response.choices[0].message.content,- },- "finish_reason": response.choices[0].finish_reason,+ "index": choice.index,+ "message": msg_dict,+ "finish_reason": choice.finish_reason,}- ],- created=response.created,- model=response.model,- object=response.object,- system_fingerprint=response.system_fingerprint,- usage=response.usage,+ )++ return ChatCompletion.construct(+ id=response.id,+ choices=converted_choices,+ created=getattr(response, "created", None),+ model=getattr(response, "model", None),+ object=getattr(response, "object", None),+ system_fingerprint=getattr(response, "system_fingerprint", None),+ usage=getattr(response, "usage", None),)@property@@ -148,6 +173,13 @@ class LiteLLMModel(BaseModelBackend):Returns:ChatCompletion"""++ request_config = self.model_config_dict.copy()+ if tools:+ request_config['tools'] = tools+ if response_format:+ request_config['response_format'] = response_format+update_current_observation(input={"messages": messages,@@ -176,7 +208,7 @@ class LiteLLMModel(BaseModelBackend):base_url=self._url,model=self.model_type,messages=messages,- **self.model_config_dict,+ **request_config,**self.kwargs,)response = self._convert_response_from_litellm_to_openai(response)