1+ """
2+ This file contains all the datatypes relevant for a chat conversation. In general this is the nomenclature that we follow:
3+ * Message: a unit of information produced by a ``role``
4+ * Thread: a group of messages is called a thread. There can be many 2 types of threads, linear and tree based.
5+ * ThreadsList: a group of linear threads is called a threads list.
6+ * Dataset: a container for grouping threads lists is called a dataset
7+
8+ Almost all the classes contain ``to_dict`` and ``from_dict`` for serialisation and deserialisation.
9+ """
10+
111# Copyright © 2023- Frello Technology Private Limited
212
313import io
2333
2434
2535class Tool :
36+ """A tool is a container for telling the LLM what it can do. This is a standard definition."""
2637
2738 class Prop :
39+ """
40+ An individual property is called a prop.
41+ """
42+
2843 def __init__ (
2944 self ,
3045 name : str ,
@@ -97,6 +112,16 @@ def from_dict(cls, x):
97112
98113
99114class Message :
115+ """
116+ A message is the unit element of information in a thread. You should avoid using directly and use the convinience
117+ aliases ``tuneapi.types.chat. human/assistant/system/...``.
118+
119+ Args:
120+ - value: this is generally a string or a list of dictionary objects for more advanced use cases
121+ - role: the role who produced this information
122+ - images: a list of PIL images or base64 strings
123+ """
124+
100125 # names that are our standards roles
101126 SYSTEM = "system"
102127 HUMAN = "human"
@@ -123,11 +148,12 @@ class Message:
123148 "function_resp" : FUNCTION_RESP ,
124149 "function-resp" : FUNCTION_RESP ,
125150 }
151+ """A map that contains the popularly known mappings to make life simpler"""
126152
127153 # start initialization here
128154 def __init__ (
129155 self ,
130- value : str | float | List [Dict [str , Any ]],
156+ value : str | List [Dict [str , Any ]],
131157 role : str ,
132158 images : List [str | Image ] = [],
133159 id : str = None ,
@@ -230,20 +256,31 @@ def to_dict(
230256
231257 @classmethod
232258 def from_dict (cls , data ):
259+ """Deserialise and construct a message from a dictionary"""
233260 return cls (
234261 value = data .get ("value" ) or data .get ("content" ),
235262 role = data .get ("from" ) or data .get ("role" ),
236- id = data .get ("id" ),
263+ id = data .get ("id" , "" ),
264+ images = data .get ("images" , []),
237265 ** data .get ("metadata" , {}),
238266 ) # type: ignore
239267
240268
241269### Aliases
242270human = partial (Message , role = Message .HUMAN )
271+ """Convinience for creating a human message"""
272+
243273system = partial (Message , role = Message .SYSTEM )
274+ """Convinience for creating a system message"""
275+
244276assistant = partial (Message , role = Message .GPT )
277+ """Convinience for creating an assistant message"""
278+
245279function_call = partial (Message , role = Message .FUNCTION_CALL )
280+ """Convinience for creating a function call message"""
281+
246282function_resp = partial (Message , role = Message .FUNCTION_RESP )
283+ """Convinience for creating a function response message"""
247284
248285
249286########################################################################################################################
@@ -255,6 +292,8 @@ def from_dict(cls, data):
255292
256293
257294class ModelInterface :
295+ """This is the generic interface implemented by all the model APIs"""
296+
258297 def set_api_token (self , token : str ) -> None :
259298 """This are used to set the API token for the model"""
260299
@@ -296,11 +335,12 @@ def stream_chat(
296335
297336class Thread :
298337 """
299- If the last Message is a "value".
338+ This is a container for a list of chat messages. This follows a similar interface to a list in python. See the methods
339+ below for more information.
300340
301341 Args:
302- chats (List[Message]) : List of chat messages
303- jl (Dict[str, Any]): Optional json- logic
342+ * chats: List of chat ``Message`` objects
343+ evals: JSON logic and
304344 """
305345
306346 def __init__ (
@@ -469,24 +509,24 @@ def _eval(self, out):
469509 evals [k ] = tu .json_logic (e , {"response" : out })
470510 return evals
471511
472- def step_streaming (self , model : ModelInterface , / , eval : bool = False ):
473- out = ""
474- for x in model .stream_chat (self ):
475- yield x
476- if isinstance (x , dict ):
477- out = x
478- else :
479- out += x
480- if eval :
481- yield self ._eval (out )
482- self .append (assistant (out ))
483-
484- def step (self , model : ModelInterface , / , eval : bool = False ):
485- out = model .chat (self )
486- self .append (assistant (out ))
487- if eval :
488- return out , self ._eval (out )
489- return out
512+ # def step_streaming(self, model: ModelInterface, /, eval: bool = False):
513+ # out = ""
514+ # for x in model.stream_chat(self):
515+ # yield x
516+ # if isinstance(x, dict):
517+ # out = x
518+ # else:
519+ # out += x
520+ # if eval:
521+ # yield self._eval(out)
522+ # self.append(assistant(out))
523+
524+ # def step(self, model: ModelInterface, /, eval: bool = False):
525+ # out = model.chat(self)
526+ # self.append(assistant(out))
527+ # if eval:
528+ # return out, self._eval(out)
529+ # return out
490530
491531
492532########################################################################################################################
@@ -699,7 +739,7 @@ def undo(self) -> "ThreadsTree":
699739
700740 def pick (self , to : Message = None , from_ : Message = None ) -> Thread :
701741 """
702- Get a thread from the Tree srtucture by telling ``to`` and ``from_`` in the tree
742+ A poerful methods to get a thread from the Tree srtucture by telling ``to`` and ``from_`` in the tree
703743 """
704744 if self .system :
705745 thread = Thread (system (self .system ))
@@ -844,7 +884,7 @@ def rollout(
844884 ):
845885 # perform a full on rollout of the tree and provide necesary callbacks. The underlying threads contain
846886 # all the necessary information to perform the rollouts
847- raise NotImplementedError ("Not implemented yet" )
887+ raise NotImplementedError ("Not implemented yet, contact developers if urgent! " )
848888
849889
850890########################################################################################################################
@@ -1053,6 +1093,9 @@ def to_hf_dict(self) -> Tuple["datasets.DatasetDict", Dict[str, List]]: # type:
10531093 }
10541094
10551095 def to_disk (self , folder : str , fmt : Optional [str ] = None ):
1096+ """
1097+ Serialise all the items of the container to a folder on the disk
1098+ """
10561099 config = {}
10571100 config ["type" ] = "tune"
10581101 config ["hf_type" ] = fmt
@@ -1063,6 +1106,9 @@ def to_disk(self, folder: str, fmt: Optional[str] = None):
10631106
10641107 @classmethod
10651108 def from_disk (cls , folder : str ):
1109+ """
1110+ Deserialise and rebuild the container from a folder on the disk
1111+ """
10661112 if not os .path .exists (folder ):
10671113 raise ValueError (f"Folder '{ folder } ' does not exist" )
10681114 if not os .path .exists (f"{ folder } /train" ):
0 commit comments