Skip to content

Commit

Permalink
Add data field and image type
Browse files Browse the repository at this point in the history
Exploration
  • Loading branch information
BenderV committed Sep 20, 2024
1 parent 9a92e86 commit 1ba1250
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions autochat/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def to_base64(self):
except Exception as e:
raise ValueError(f"Failed to convert image to base64: {e}")

@classmethod
def from_bytes(cls, img_bytes: bytes):
return cls(PILImage.open(BytesIO(img_bytes)))

def to_bytes(self):
try:
buffered = BytesIO()
self.image.save(buffered, format=self.image.format)
return buffered.getvalue()
except Exception as e:
raise ValueError(f"Failed to convert image to bytes: {e}")

@property
def format(self):
return "image/" + self.image.format.lower()
Expand All @@ -51,6 +63,7 @@ def __init__(
id: typing.Optional[int] = None,
function_call_id: typing.Optional[str] = None,
image: typing.Optional[PILImage.Image] = None,
data: typing.Optional[dict] = None, # Used for function call output
) -> None:
self.role = role
self.content = content
Expand All @@ -59,6 +72,7 @@ def __init__(
self.id = id
self.function_call_id = function_call_id
self.image = Image(image) if image else None
self.data = data

def to_openai_dict(self) -> dict:
res = {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="autochat",
version="0.3.4",
version="0.3.5",
packages=find_packages(),
install_requires=["tenacity==8.3.0", "pillow==10.4.0"],
extras_require={
Expand Down

0 comments on commit 1ba1250

Please sign in to comment.