🐫 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
feat: support openai structured output as objecct (#1229)
Will Shang43336c9683b3
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex c26add253..78ec45685 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -1090,6 +1090,7 @@ class ChatAgent(BaseAgent):role_type=self.role_type,meta_dict=dict(),content=choice.message.content or "",+ parsed=getattr(choice.message, 'parsed', None),)# Process log probabilities and append to the message meta informationif choice.logprobs is not None:diff --git a/camel/messages/base.py b/camel/messages/base.pyindex 56da5fe8c..c9b11f4af 100644--- a/camel/messages/base.py+++ b/camel/messages/base.py@@ -15,10 +15,11 @@ import base64import ioimport refrom dataclasses import dataclass-from typing import Any, Dict, List, Literal, Optional, Tuple, Union+from typing import Any, Dict, List, Literal, Optional, Tuple, Type, Unionimport numpy as npfrom PIL import Image+from pydantic import BaseModelfrom camel.messages import (FunctionCallFormatter,@@ -51,13 +52,15 @@ class BaseMessage:for the message.content (str): The content of the message.video_bytes (Optional[bytes]): Optional bytes of a video associated- with the message. Default is None.+ with the message. (default::obj:`None`)image_list (Optional[List[Image.Image]]): Optional list of PIL Image- objects associated with the message. Default is None.+ objects associated with the message. (default::obj:`None`)image_detail (Literal["auto", "low", "high"]): Detail level of the- images associated with the message. Default is "auto".+ images associated with the message. (default::obj:`auto`)video_detail (Literal["auto", "low", "high"]): Detail level of the- videos associated with the message. Default is "low".+ videos associated with the message. (default::obj:`low`)+ parsed: Optional[Union[Type[BaseModel], dict]]: Optional object which+ is parsed from the content. (default::obj:`None`)"""role_name: str@@ -69,6 +72,7 @@ class BaseMessage:image_list: Optional[List[Image.Image]] = Noneimage_detail: Literal["auto", "low", "high"] = "auto"video_detail: Literal["auto", "low", "high"] = "low"+ parsed: Optional[Union[Type[BaseModel], dict]] = None@classmethoddef make_user_message(@@ -419,7 +423,6 @@ class BaseMessage:"text": self.content,})-if self.image_list and len(self.image_list) > 0:for image in self.image_list:if image.format is None:diff --git a/camel/models/openai_model.py b/camel/models/openai_model.pyindex 27f388566..8e8f8b1f1 100644--- a/camel/models/openai_model.py+++ b/camel/models/openai_model.py@@ -158,6 +158,7 @@ class OpenAIModel(BaseModelBackend):"role": response.choices[0].message.role,"content": response.choices[0].message.content,"tool_calls": response.choices[0].message.tool_calls,+ "parsed": response.choices[0].message.parsed,},finish_reason=response.choices[0].finish_reason,)diff --git a/examples/models/openai_structured_output_example.py b/examples/models/openai_structured_output_example.pyindex 1fc549f23..62ac81e1a 100644--- a/examples/models/openai_structured_output_example.py+++ b/examples/models/openai_structured_output_example.py@@ -25,7 +25,7 @@ class Student(BaseModel):class StudentList(BaseModel):- studentLlst: list[Student]+ studentList: list[Student]openai_model = ModelFactory.create(@@ -45,10 +45,15 @@ user_msg = """give me some student infos."""# Get response informationresponse = camel_agent.step(user_msg)print(response.msgs[0].content)+print(response.msgs[0].parsed)'''==============================================================================={"studentLlst":[{"name":"Alice Johnson","age":"20"},{"name":"Brian Smith","age":"22"},{"name":"Catherine Lee","age":"19"},{"name":"David Brown","age":"21"},{"name":"Eva White","age":"20"}]}++studentList=[Student(name='Alice Johnson', age='20'), Student(name=+'Brian Smith', age='22'), Student(name='Catherine Lee', age='19'),+Student(name='David Brown', age='21'), Student(name='Eva White', age='23')]==============================================================================='''