Threads and messages
Thread #
A sequence of messages alternating between IN ("user" role) and OUT ("assistant" role).
Stores a special initial INST information (known as "system" role in ChatML) providing instructions to the model. Some models don't use system instructions - in those cases it's prepended to first IN message.
Messages are kept in a strict IN,OUT,IN,OUT,... order. To enforce this, if two IN messages are added, the second just appends to the text of the first or to its image list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
Optional[Union[Self, list, Msg, dict, tuple, str]]
|
Optionally initialize from a Thread, list[Msg], list[ChatML format dict], list[tuple], list[str], Msg, ChatML format dict, tuple or str. |
None
|
inst |
str
|
Instructions text. If inst arg is not set and t is a Thread, its inst will be used. |
''
|
join_sep |
str
|
Separator used when message text needs to be joined. Defaults to "\n". |
'\n'
|
Raises:
Type | Description |
---|---|
TypeError
|
On invalid args passed. |
Source code in sibila/thread.py
inst
instance-attribute
#
System instructions in an Msg of kind INST, defaults to empty text.
init_INST_IN #
Initialize Thread with instructions and an IN message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inst_text |
str
|
Instructions text. |
required |
in_text |
str
|
Text for IN message. |
required |
in_images |
Optional[Union[list, str, dict]]
|
An array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with "url" key and others. If url arg is not a valid URL, it will be loaded and converted to a data: URL. |
None
|
Source code in sibila/thread.py
add_IN #
Appends an IN message to Thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_text |
str
|
Text for IN message. |
required |
in_images |
Optional[Union[list, str, dict]]
|
An array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with "url" key and others. If url arg is not a valid URL, it will be loaded and converted to a data: URL. |
None
|
Source code in sibila/thread.py
add_OUT #
Appends an OUT message to Thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_text |
str
|
Text for OUT message. |
required |
out_images |
Optional[Union[list, str, dict]]
|
An array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with "url" key and others. If url arg is not a valid URL, it will be loaded and converted to a data: URL. |
None
|
Source code in sibila/thread.py
add_OUT_IN #
Appends an OUT message followed by an IN message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_text |
str
|
Text for OUT message. |
required |
in_text |
str
|
Text for IN message. |
required |
out_images |
Optional[Union[list, str, dict]]
|
An array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with "url" key and others. If url arg is not a valid URL, it will be loaded and converted to a data: URL. |
None
|
in_images |
Optional[Union[list, str, dict]]
|
Optional list of IN message images. |
None
|
Source code in sibila/thread.py
make_INST_IN
staticmethod
#
Return an initialized Thread with instructions and an IN message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inst_text |
str
|
Instructions text. |
required |
in_text |
str
|
Text for IN message. |
required |
in_images |
Optional[Union[list, str, dict]]
|
An array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with "url" key and others. If url arg is not a valid URL, it will be loaded and converted to a data: URL. |
None
|
Source code in sibila/thread.py
make_IN
staticmethod
#
Return an initialized Thread with an IN message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_text |
str
|
Text for IN message. |
required |
in_images |
Optional[Union[list, str, dict]]
|
An array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with "url" key and others. If url arg is not a valid URL, it will be loaded and converted to a data: URL. |
None
|
Source code in sibila/thread.py
clone #
clear #
load #
Load this Thread from a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path of file to load. |
required |
clear |
bool
|
Should thread be cleared of messages, including INST? If not will concatenate with existing ones. |
required |
Source code in sibila/thread.py
save #
Serialize this Thread to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path of file to save into. |
required |
Source code in sibila/thread.py
from_dict
staticmethod
#
Deserialize a Thread from a dict.
Source code in sibila/thread.py
as_dict #
Serialize this Thread to a dict.
Source code in sibila/thread.py
as_chatml #
Returns Thread as a list of ChatML messages.
Returns:
Type | Description |
---|---|
list[dict]
|
A list of ChatML dict elements with "role" and "content" keys. |
Source code in sibila/thread.py
Trim #
trim #
Trim context by selectively removing older messages until thread fits max_token_len.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trim_flags |
Trim
|
Flags to guide selection of which messages to remove. |
required |
max_token_len |
int
|
Cut messages until size is lower than this number. Defaults to None. |
required |
thread_token_len_fn |
Callable
|
A function that returns token count for a passed Thread. |
required |
Example of a thread_token_len_fn that counts 1 char = 1 token: def thread_token_len_fn(thread: Thread) -> int: total = len(thread.inst.text) for msg in thread: total += len(msg.text) if msg.images: total += len(str(msg.images)) return total
Returns:
Type | Description |
---|---|
int
|
Trimming result: 1=trimmed messages to max_token_len, 0: no trimming was needed, -1: Unable to trim to max_token_len. |
Source code in sibila/thread.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
|
add #
Add a message to Thread.
Accepts any of these argument combinations
t=Msg, ChatML format dict, tuple or str --or-- t=kind, text[, images]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
Union[Msg, dict, tuple, str, Kind]
|
One of Msg, ChatML format dict, tuple or str, or Msg.Kind. |
required |
text |
Optional[str]
|
Message text, only if t=Msg.Kind. |
None
|
images |
Optional[Union[list, str, dict]]
|
only if t=Msg.Kind or t=str-> an array (or its first element) of either an str (a file path, will be loaded and converted to a data: URL) or a dict with keys "url" and any other keys like "detail". If url arg is not a valid URL, it will be loaded and converted to a data URL. |
None
|
Source code in sibila/thread.py
concat #
Concatenate to current Thread: another Thread, list[Msg], list[ChatML format dict], list[str], Msg, ChatML format dict or str.
if last message in self is the same kind of first in t, their text, images, etc will be joined.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
Union[Self, list, Msg, dict, tuple, str]
|
A Thread, list[Msg], list[ChatML format dict], list[str], Msg, ChatML format dict or str. |
required |
Source code in sibila/thread.py
get_iter #
Return an iterator that can be used to cycle over messages. include_set_inst: If inst message is set, include it before all others.
Source code in sibila/thread.py
next_kind
property
#
Get kind of next new message that can be added to thread .
Returns:
Type | Description |
---|---|
Kind
|
Kind of last message or Msg.Kind.IN if empty. |
has_text_lower #
join_sep
instance-attribute
#
join_sep = join_sep
Separator used when message text needs to be joined. Defaults to '\n'
__add__ #
Msg
dataclass
#
Kind #
images
class-attribute
instance-attribute
#
List of images in message. An entry must have a 'url' key, but any other keys can be added. Key 'url' key must be a remote url (https,http) or a 'data:' base64-encoded url.
make_IN
staticmethod
#
make_OUT
staticmethod
#
make_INST
staticmethod
#
clone #
from_dict
staticmethod
#
as_dict #
from_chatml
staticmethod
#
Source code in sibila/thread.py
as_chatml #
Returns message in a ChatML dict.
Returns:
Type | Description |
---|---|
dict
|
A ChatML dict with "role" and "content" keys. |