Models factory
Models #
Model and template format directory that unifies (and simplifies) model access and configuration.
This env variable is checked and used during initialization
SIBILA_MODELS: ';'-delimited list of folders where to find: models.json, formats.json and model files.
= Models Directory =
Useful to create models from resource names like "llamacpp:openchat" or "openai:gpt-4". This makes it simple to change a model, store model settings, to compare model outputs, etc.
User can add new entries from script or with JSON filenames, via the add() call. New directory entries with the same name are merged into existing ones for each added config.
Uses file "sibila/res/base_models.json" for the initial defaults, which the user can augment by calling setup() with own config files or directly adding model config with set_model().
An example of a model directory JSON config file:
{
# "llamacpp" is a provider, you can then create models with names
# like "provider:model_name", for ex: "llamacpp:openchat"
"llamacpp": {
"_default": { # place here default args for all llamacpp: models.
"genconf": {"temperature": 0.0}
# each model entry below can then override as needed
},
"openchat": { # a model definition
"name": "openchat-3.5-1210.Q4_K_M.gguf",
"format": "openchat" # chat template format used by this model
},
"phi2": {
"name": "phi-2.Q4_K_M.gguf", # model filename
"format": "phi2",
"genconf": {"temperature": 2.0} # a hot-headed model
},
"oc": "openchat"
# this is a link: "oc" forwards to the "openchat" entry
},
# The "openai" provider. A model can be created with name: "openai:gpt-4"
"openai": {
"_default": {}, # default settings for all OpenAI models
"gpt-3.5": {
"name": "gpt-3.5-turbo-1106" # OpenAI's model name
},
"gpt-4": {
"name": "gpt-4-1106-preview"
},
},
# "alias" entry is not a provider but a way to have simpler alias names.
# For example you can use "alias:develop" or even simpler, just "develop" to create the model:
"alias": {
"develop": "llamacpp:openchat",
"production": "openai:gpt-3.5"
}
}
Rules for entry inheritance/overriding
Entries in the '_default' key of each provider will serve as defaults for models of that provider. Model entries in base_models_dir (automatically loaded from 'res/base_models.json') are overridden by any entries of the same name loaded from a local 'models.json' file with Models.setup(). Here, overridden means local keys of the same name replace base keys (as a dict.update()). However '_default' entries only apply separately to either base_models_dir or 'local models.json', as in a lexical scope.
= Format Directory =
Detects chat templates from model name/filename or uses from metadata if possible.
This directory can be setup from a JSON file or by calling set_format().
Any new entries with the same name replace previous ones on each new call.
Initializes from file "sibila/res/base_formats.json".
Example of a "formats.json" file:
{
"chatml": {
# template is a Jinja template for this model
"template": "{% for message in messages %}..."
},
"openchat": {
"match": "openchat", # a regexp to match model name or filename
"template": "{{ bos_token }}..."
},
"phi": {
"match": "phi",
"template": "..."
},
"phi2": "phi",
# this is a link: "phi2" -> "phi"
}
Jinja2 templates receive a standard ChatML messages list (created from a Thread) and must deal with the following:
-
In models that don't use a system message, template must take care of prepending it to first user message.
-
The add_generation_prompt template variable is always set as True.
setup
classmethod
#
Initialize models and formats directory from given model files folder and/or contained configuration files. Path can start with "~/" current account's home directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Optional[Union[str, list[str]]]
|
Path to a folder or to "models.json" or "formats.json" configuration files. Defaults to None which tries to initialize from defaults and env variable. |
None
|
clear |
bool
|
Set to clear existing directories before loading from path arg. |
False
|
add_cwd |
bool
|
Add current working directory to search path. |
True
|
load_from_env |
bool
|
Load from SIBILA_MODELS env variable? |
True
|
Source code in sibila/models.py
create
classmethod
#
Create a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_name |
str
|
Resource name in the format: provider:model_name, for example "llamacpp:openchat". |
required |
genconf |
Optional[GenConf]
|
Optional model generation configuration. Overrides set_genconf() value and any directory defaults. Defaults to None. |
None
|
ctx_len |
Optional[int]
|
Maximum context length to be used. Overrides directory defaults. Defaults to None. |
None
|
resolved_create_args |
Optional[dict]
|
Pass an empty dict to be filled by this method with the resolved args used in model creation. Defaults to None. |
None
|
over_args |
Union[Any]
|
Model-specific creation args, which will override default args set in model directory. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Model |
Model
|
the initialized model. |
Source code in sibila/models.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
|
add_models_search_path
classmethod
#
set_genconf
classmethod
#
Set the GenConf to use as default for model creation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
genconf |
GenConf
|
Model generation configuration. |
required |
list_models
classmethod
#
List format entries matching query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_query |
str
|
Case-insensitive substring to match model names. Empty string for all. |
required |
providers |
list[str]
|
Filter by these exact provider names. Empty list for all. |
required |
include_base |
bool
|
Also list fused values from base_models_dir. |
required |
resolved_values |
bool
|
Return resolved entries or raw ones. |
required |
Returns:
Type | Description |
---|---|
dict
|
A dict where keys are model res_names and values are respective entries. |
Source code in sibila/models.py
get_model_entry
classmethod
#
Get a resolved model entry. Resolved means following any links.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_name |
str
|
Resource name in the format: provider:model_name, for example "llamacpp:openchat". |
required |
Returns:
Type | Description |
---|---|
Union[tuple[str, dict], None]
|
Resolved entry (res_name,dict) or None if not found. |
Source code in sibila/models.py
has_model_entry
classmethod
#
set_model
classmethod
#
Add model configuration for given res_name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_name |
str
|
A name in the form "provider:model_name", for example "openai:gtp-4". |
required |
model_name |
str
|
Model name or filename identifier. |
required |
format_name |
Optional[str]
|
Format name used by model. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Base GenConf to use when creating model. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If unknown provider. |
Source code in sibila/models.py
update_model
classmethod
#
update model fields
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_name |
str
|
A name in the form "provider:model_name", for example "openai:gtp-4". |
required |
model_name |
Optional[str]
|
Model name or filename identifier. Defaults to None. |
None
|
format_name |
Optional[str]
|
Format name used by model. Use "" to delete. Defaults to None. |
None
|
genconf |
Union[GenConf, str, None]
|
Base GenConf to use when creating model. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If unknown provider. |
Source code in sibila/models.py
set_model_link
classmethod
#
Create a model link into another model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_name |
str
|
A name in the form "provider:model_name", for example "openai:gtp-4". |
required |
link_name |
str
|
Name of model this entry links to. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If unknown provider. |
Source code in sibila/models.py
delete_model
classmethod
#
Delete a model entry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_name |
str
|
Model entry in the form "provider:name". |
required |
Source code in sibila/models.py
save_models
classmethod
#
Source code in sibila/models.py
list_formats
classmethod
#
List format entries matching query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_query |
str
|
Case-insensitive substring to match format names. Empty string for all. |
required |
include_base |
bool
|
Also list base_formats_dir. |
required |
resolved_values |
bool
|
Return resolved entries or raw ones. |
required |
Returns:
Type | Description |
---|---|
dict
|
A dict where keys are format names and values are respective entries. |
Source code in sibila/models.py
get_format_entry
classmethod
#
has_format_entry
classmethod
#
get_format_template
classmethod
#
match_format_entry
classmethod
#
Search the formats registry, based on model name or filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name or filename of model. |
required |
Returns:
Type | Description |
---|---|
Union[tuple[str, dict], None]
|
Tuple (name, format_entry) where name is a resolved name. Or None if none found. |
Source code in sibila/models.py
match_format_template
classmethod
#
Search the formats registry, based on model name or filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name or filename of model. |
required |
Returns:
Type | Description |
---|---|
Union[str, None]
|
Format template or None if none found. |
Source code in sibila/models.py
set_format
classmethod
#
Add a format entry to the format directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Format entry name. |
required |
template |
str
|
The Chat template format in Jinja2 format |
required |
match |
Optional[str]
|
Regex that matches names/filenames that use this format. Default is None. |
None
|
Source code in sibila/models.py
set_format_link
classmethod
#
Add a format link entry to the format directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Format entry name. |
required |
link_name |
str
|
Name of format that this entry links to. |
required |
Source code in sibila/models.py
delete_format
classmethod
#
Delete a format entry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Format entry name. |
required |
Source code in sibila/models.py
save_formats
classmethod
#
Source code in sibila/models.py
info
classmethod
#
Return information about current setup.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose |
bool
|
If False, formats directory values are abbreviated. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
str
|
Textual information about the current setup. |
Source code in sibila/models.py
clear
classmethod
#
Clear directories. Members base_models_dir and base_formats_dir and genconf are not cleared.