Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support openai structured output as objecct #1229

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ def handle_batch_response(
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 information
if choice.logprobs is not None:
Expand Down
15 changes: 9 additions & 6 deletions camel/messages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import io
import re
from dataclasses import dataclass
from typing import Any, Dict, List, Literal, Optional, Tuple, Union
from typing import Any, Dict, List, Literal, Optional, Tuple, Type, Union

import numpy as np
from PIL import Image
from pydantic import BaseModel

from camel.messages import (
FunctionCallFormatter,
Expand Down Expand Up @@ -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
Expand All @@ -69,6 +72,7 @@ class BaseMessage:
image_list: Optional[List[Image.Image]] = None
image_detail: Literal["auto", "low", "high"] = "auto"
video_detail: Literal["auto", "low", "high"] = "low"
parsed: Optional[Union[Type[BaseModel], dict]] = None

@classmethod
def make_user_message(
Expand Down Expand Up @@ -419,7 +423,6 @@ def to_openai_user_message(self) -> OpenAIUserMessage:
"text": self.content,
}
)

if self.image_list and len(self.image_list) > 0:
for image in self.image_list:
if image.format is None:
Expand Down
1 change: 1 addition & 0 deletions camel/models/openai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def _to_chat_completion(
"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,
)
Expand Down
7 changes: 6 additions & 1 deletion examples/models/openai_structured_output_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Student(BaseModel):


class StudentList(BaseModel):
studentLlst: list[Student]
studentList: list[Student]


openai_model = ModelFactory.create(
Expand All @@ -45,10 +45,15 @@ class StudentList(BaseModel):
# Get response information
response = 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')]
===============================================================================
'''
Loading