55import json
66import copy
77import random
8+ from PIL .Image import Image
89from functools import partial
910from collections .abc import Iterable
1011from typing import Dict , List , Any , Tuple , Optional , Generator , Union
@@ -128,8 +129,8 @@ def __init__(
128129 self ,
129130 value : str | float | List [Dict [str , Any ]],
130131 role : str ,
132+ images : List [str | Image ] = [],
131133 id : str = None ,
132- fn_pairs : Optional [Tuple ["Message" , "Message" ]] = None ,
133134 ** kwargs ,
134135 ):
135136 if role not in self .KNOWN_ROLES :
@@ -141,7 +142,12 @@ def __init__(
141142 self .value = value
142143 self .id = id or "msg_" + str (tu .get_snowflake ())
143144 self .metadata = kwargs
144- self .fn_pairs = fn_pairs
145+ self .images = images
146+ for i , img in enumerate (self .images ):
147+ if isinstance (img , Image ):
148+ buf = io .BytesIO ()
149+ img .save (buf , "png" )
150+ self .images [i ] = tu .to_b64 (buf .getvalue ())
145151
146152 # validations
147153 if self .role == self .FUNCTION_CALL :
@@ -164,14 +170,7 @@ def __add__(self, other: str):
164170 return Message (self .value + other , self .role )
165171
166172 def __repr__ (self ) -> str :
167- out = ""
168- if self .fn_pairs :
169- for fc , fr in self .fn_pairs :
170- out += f"[[FC] { fc } => [FR] { fr } ]"
171- if out :
172- out += " " + str (self .value )
173- else :
174- out = str (self .value )
173+ out = str (self .value )
175174 return out
176175
177176 def __getitem__ (self , x ):
@@ -190,13 +189,11 @@ def to_dict(
190189 meta : bool = False ,
191190 ):
192191 """
193- if format == ``ft`` then export to following format: ``{"from": "system/human/gpt", "value": "..."}``
194-
195- elif format == ``api`` then ``{"role": "system/user/assistant", "content": [{"type": "text", "text": {"value": "..."}]}``
196-
197- elif format == ``full`` then ``{"id": 1234421123, "role": "system/user/assistant", "content": [{"type": "text", "text": {"value": "..."}]}``
198-
199- else export to following format: ``{"role": "system/user/assistant", "content": "..."}``
192+ Serialise the Message into a dictionary of different formats:
193+ - format == ``ft`` then export to following format: ``{"from": "system/human/gpt", "value": "..."}``
194+ - format == ``api`` then ``{"role": "system/user/assistant", "content": [{"type": "text", "text": {"value": "..."}]}``. This is used with TuneAPI
195+ - format == ``full`` then ``{"id": 1234421123, "role": "system/user/assistant", "content": [{"type": "text", "text": {"value": "..."}]}``
196+ - default: ``{"role": "system/user/assistant", "content": "..."}``
200197 """
201198 role = self .role
202199
@@ -219,7 +216,7 @@ def to_dict(
219216 if ft :
220217 chat_message ["value" ] = self .value
221218 elif api :
222- chat_message ["content" ] = [{"type" : "text" , "text" : { "value" : self .value } }]
219+ chat_message ["content" ] = [{"type" : "text" , "text" : self .value }]
223220 else :
224221 chat_message ["content" ] = self .value
225222
0 commit comments