Local model classes
LlamaCppModel #
LlamaCppModel(
path,
format=None,
format_search_order=[
"name",
"meta_template",
"folder_json",
],
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
tokenizer=None,
n_gpu_layers=-1,
main_gpu=0,
n_batch=512,
seed=4294967295,
verbose=False,
**llamacpp_kwargs
)
Use local GGUF format models via llama.cpp engine.
Supports grammar-constrained JSON output following a JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
File path to the GGUF file. |
required |
format |
Optional[str]
|
Chat template format to use with model. Leave as None for auto-detection. |
None
|
format_search_order |
list[str]
|
Search order for auto-detecting format, "name" searches in the filename, "meta_template" looks in the model's metadata, "folder_json" looks for configs in file's folder. Defaults to ["name","meta_template", "folder_json"]. |
['name', 'meta_template', 'folder_json']
|
genconf |
Optional[GenConf]
|
Default generation configuration, which can be used in gen() and related. Defaults to None. |
None
|
schemaconf |
Optional[JSchemaConf]
|
Default configuration for JSON schema validation, used if generation call doesn't supply one. Defaults to None. |
None
|
ctx_len |
Optional[int]
|
Maximum context length to be used. Use 0 for maximum possible size, which may raise an out of memory error. None will use a default from the 'llamacpp' provider's '_default' entry at 'res/base_models.json'. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for no limit. |
None
|
tokenizer |
Optional[Tokenizer]
|
An external initialized tokenizer to use instead of the created from the GGUF file. Defaults to None. |
None
|
n_gpu_layers |
int
|
Number of model layers to run in a GPU. Defaults to -1 for all. |
-1
|
main_gpu |
int
|
Index of the GPU to use. Defaults to 0. |
0
|
n_batch |
int
|
Prompt processing batch size. Defaults to 512. |
512
|
seed |
int
|
Random number generation seed, for non zero temperature inference. Defaults to 4294967295. |
4294967295
|
verbose |
bool
|
Emit (very) verbose llama.cpp output. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
ImportError
|
If llama-cpp-python is not installed. |
ValueError
|
For arguments or settings problems. |
NameError
|
If the model was not found or the file is corrupt. |
AttributeError
|
If a suitable template format was not found. |
MemoryError
|
If an out of memory situation arises. |
Source code in sibila/llamacpp.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
close #
Close model, release resources like memory or net connections.
Source code in sibila/llamacpp.py
extract #
Type-constrained generation: an instance of the given type will be initialized with the model's output. The following target types are accepted:
-
prim_type:
- bool
- int
- float
- str
-
enums:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
-
datetime/date/time
-
a list in the form:
- list[type]
For example list[int]. The list can be annotated: Annotated[list[T], "List desc"] And/or the list item type can be annotated: list[Annotated[T, "Item desc"]]
-
dataclass with fields of the above supported types (or dataclass).
-
Pydantic BaseModel
All types can be Annotated[T, "Desc"], for example: count: int Can be annotated as: count: Annotated[int, "How many units?"]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A value of target arg type instantiated with the model's output. |
Source code in sibila/model.py
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 |
|
classify #
Returns a classification from one of the given enumeration values The following ways to specify the valid labels are accepted:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
One of the given labels, as classified by the model. |
Source code in sibila/model.py
json #
JSON/JSON-schema constrained generation, returning a Python dict of values, constrained or not by a JSON schema. Raises GenError if unable to get a valid/schema-validated JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
json_schema |
Union[dict, str, None]
|
A JSON schema describing the dict fields that will be output. None means no schema (free JSON output). |
None
|
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
massage_schema |
bool
|
Simplify schema. Defaults to True. |
True
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid JSON schema output error. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
dict
|
A dict from model's JSON response, following genconf.jsonschema, if provided. |
Source code in sibila/model.py
dataclass #
Constrained generation after a dataclass definition, resulting in an object initialized with the model's response. Raises GenError if unable to get a valid response that follows the dataclass definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A dataclass definition. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
An object of class cls (derived from dataclass) initialized from the constrained JSON output. |
Source code in sibila/model.py
pydantic #
Constrained generation after a Pydantic BaseModel-derived class definition. Results in an object initialized with the model response. Raises GenError if unable to get a valid dict that follows the BaseModel class definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A class derived from a Pydantic BaseModel class. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid BaseModel object. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A Pydantic object of class cls (derived from BaseModel) initialized from the constrained JSON output. |
Source code in sibila/model.py
call #
Text generation from a Thread or plain text, used by the other model generation methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
ok_length_is_error |
bool
|
Should a result of GenRes.OK_LENGTH be considered an error and raise? |
False
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. This can be a model error, or an invalid JSON output error. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
str
|
Text generated by model. |
Source code in sibila/model.py
__call__ #
Text generation from a Thread or plain text, used by the other model generation methods. Same as call().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
ok_length_is_error |
bool
|
Should a result of GenRes.OK_LENGTH be considered an error and raise? |
False
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. This can be a model error, or an invalid JSON output error. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
str
|
Text generated by model. |
Source code in sibila/model.py
extract_async
async
#
Async type-constrained generation: an instance of the given type will be initialized with the model's output. The following target types are accepted:
-
prim_type:
- bool
- int
- float
- str
-
enums:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
-
datetime/date/time
-
a list in the form:
- list[type]
For example list[int]. The list can be annotated: Annotated[list[T], "List desc"] And/or the list item type can be annotated: list[Annotated[T, "Item desc"]]
-
dataclass with fields of the above supported types (or dataclass).
-
Pydantic BaseModel
All types can be Annotated[T, "Desc"], for example: count: int Can be annotated as: count: Annotated[int, "How many units?"]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A value of target arg type instantiated with the model's output. |
Source code in sibila/model.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
|
classify_async
async
#
Returns a classification from one of the given enumeration values The following ways to specify the valid labels are accepted:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
One of the given labels, as classified by the model. |
Source code in sibila/model.py
json_async
async
#
json_async(
query,
*,
json_schema=None,
inst=None,
genconf=None,
massage_schema=True,
schemaconf=None
)
JSON/JSON-schema constrained generation, returning a Python dict of values, constrained or not by a JSON schema. Raises GenError if unable to get a valid/schema-validated JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
json_schema |
Union[dict, str, None]
|
A JSON schema describing the dict fields that will be output. None means no schema (free JSON output). |
None
|
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
massage_schema |
bool
|
Simplify schema. Defaults to True. |
True
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid JSON schema output error. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
dict
|
A dict from model's JSON response, following genconf.jsonschema, if provided. |
Source code in sibila/model.py
dataclass_async
async
#
Async constrained generation after a dataclass definition, resulting in an object initialized with the model's response. Raises GenError if unable to get a valid response that follows the dataclass definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A dataclass definition. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
An object of class cls (derived from dataclass) initialized from the constrained JSON output. |
Source code in sibila/model.py
pydantic_async
async
#
Async constrained generation after a Pydantic BaseModel-derived class definition. Results in an object initialized with the model response. Raises GenError if unable to get a valid dict that follows the BaseModel class definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A class derived from a Pydantic BaseModel class. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid BaseModel object. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A Pydantic object of class cls (derived from BaseModel) initialized from the constrained JSON output. |
Source code in sibila/model.py
call_async
async
#
Text generation from a Thread or plain text, used by the other model generation methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
ok_length_is_error |
bool
|
Should a result of GenRes.OK_LENGTH be considered an error and raise? |
False
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. This can be a model error, or an invalid JSON output error. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
str
|
Text generated by model. |
Source code in sibila/model.py
gen #
Text generation from a Thread, used by the other model generation methods. Doesn't raise an exception if an error occurs, always returns GenOut.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread |
Thread
|
The Thread object to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If trying to generate from an empty prompt. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
Source code in sibila/model.py
gen_json #
JSON/JSON-schema constrained generation, returning a Python dict of values, conditioned or not by a JSON schema. Doesn't raise an exception if an error occurs, always returns GenOut.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread |
Thread
|
The Thread to use as model input. |
required |
json_schema |
Union[dict, str, None]
|
A JSON schema describing the dict fields that will be output. None means no schema (free JSON output). |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
massage_schema |
bool
|
Simplify schema. Defaults to True. |
True
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. The output dict is in GenOut.dic. |
Source code in sibila/model.py
gen_dataclass #
Constrained generation after a dataclass definition. An initialized dataclass object is returned in the "value" field of the returned dict. Doesn't raise an exception if an error occurs, always returns GenOut containing the created object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A dataclass definition. |
required |
thread |
Thread
|
The Thread object to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. The initialized dataclass object is in GenOut.value. |
Source code in sibila/model.py
gen_pydantic #
Constrained generation after a Pydantic BaseModel-derived class definition. An initialized Pydantic BaseModel object is returned in the "value" field of the returned dict. Doesn't raise an exception if an error occurs, always returns GenOut containing the created object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A class derived from a Pydantic BaseModel class. |
required |
thread |
Thread
|
The Thread to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
TypeError
|
When cls is not a Pydantic BaseClass. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. The initialized Pydantic BaseModel-derived object is in GenOut.value. |
Source code in sibila/model.py
token_len #
Calculate or estimate the token length for a Thread or a plain text string. In some cases where it's not possible to calculate the exact token count, this function should give a conservative (upper bound) estimate. It's up to the implementation whether to account for side information like JSON Schema, but it must reflect the model's context token accounting. Thread or text must be the final text which will passed to model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_or_text |
Union[Thread, str]
|
Final thread or text to be passed to model. |
required |
Returns:
Type | Description |
---|---|
int
|
Number of tokens used. |
Source code in sibila/llamacpp.py
known_models
classmethod
#
If the model can only use a fixed set of models, return their names. Otherwise, return None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_key |
Optional[str]
|
If the model provider requires an API key, pass it here or set it in the respective env variable. |
None
|
Returns:
Type | Description |
---|---|
Union[list[str], None]
|
Returns a list of known models or None if unable to fetch it. |
Source code in sibila/model.py
desc #
get_metadata #
Returns model metadata.
Source code in sibila/llamacpp.py
Model #
Model is an abstract base class for common LLM model functionality. Many of the useful methods like extract() or json() are implemented here.
It should not be instantiated directly, instead LlamaCppModel, OpenAIModel, etc, all derive from this class.
Initializer for base model type, shared by actual model classes like LlamaCpp, OpenAI, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_local_model |
bool
|
Is the model running locally? |
required |
genconf |
Union[GenConf, None]
|
Default generation configuration options, used if generation call doesn't supply one. |
required |
schemaconf |
Union[JSchemaConf, None]
|
Default configuration for JSON schema validation, used if generation call doesn't supply one. |
required |
tokenizer |
Union[Tokenizer, None]
|
Tokenizer used to encode text (even for message-based models). |
required |
Source code in sibila/model.py
close
abstractmethod
#
extract #
Type-constrained generation: an instance of the given type will be initialized with the model's output. The following target types are accepted:
-
prim_type:
- bool
- int
- float
- str
-
enums:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
-
datetime/date/time
-
a list in the form:
- list[type]
For example list[int]. The list can be annotated: Annotated[list[T], "List desc"] And/or the list item type can be annotated: list[Annotated[T, "Item desc"]]
-
dataclass with fields of the above supported types (or dataclass).
-
Pydantic BaseModel
All types can be Annotated[T, "Desc"], for example: count: int Can be annotated as: count: Annotated[int, "How many units?"]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A value of target arg type instantiated with the model's output. |
Source code in sibila/model.py
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 |
|
classify #
Returns a classification from one of the given enumeration values The following ways to specify the valid labels are accepted:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
One of the given labels, as classified by the model. |
Source code in sibila/model.py
json #
JSON/JSON-schema constrained generation, returning a Python dict of values, constrained or not by a JSON schema. Raises GenError if unable to get a valid/schema-validated JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
json_schema |
Union[dict, str, None]
|
A JSON schema describing the dict fields that will be output. None means no schema (free JSON output). |
None
|
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
massage_schema |
bool
|
Simplify schema. Defaults to True. |
True
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid JSON schema output error. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
dict
|
A dict from model's JSON response, following genconf.jsonschema, if provided. |
Source code in sibila/model.py
dataclass #
Constrained generation after a dataclass definition, resulting in an object initialized with the model's response. Raises GenError if unable to get a valid response that follows the dataclass definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A dataclass definition. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
An object of class cls (derived from dataclass) initialized from the constrained JSON output. |
Source code in sibila/model.py
pydantic #
Constrained generation after a Pydantic BaseModel-derived class definition. Results in an object initialized with the model response. Raises GenError if unable to get a valid dict that follows the BaseModel class definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A class derived from a Pydantic BaseModel class. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid BaseModel object. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A Pydantic object of class cls (derived from BaseModel) initialized from the constrained JSON output. |
Source code in sibila/model.py
call #
Text generation from a Thread or plain text, used by the other model generation methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
ok_length_is_error |
bool
|
Should a result of GenRes.OK_LENGTH be considered an error and raise? |
False
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. This can be a model error, or an invalid JSON output error. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
str
|
Text generated by model. |
Source code in sibila/model.py
__call__ #
Text generation from a Thread or plain text, used by the other model generation methods. Same as call().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
ok_length_is_error |
bool
|
Should a result of GenRes.OK_LENGTH be considered an error and raise? |
False
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. This can be a model error, or an invalid JSON output error. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
str
|
Text generated by model. |
Source code in sibila/model.py
extract_async
async
#
Async type-constrained generation: an instance of the given type will be initialized with the model's output. The following target types are accepted:
-
prim_type:
- bool
- int
- float
- str
-
enums:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
-
datetime/date/time
-
a list in the form:
- list[type]
For example list[int]. The list can be annotated: Annotated[list[T], "List desc"] And/or the list item type can be annotated: list[Annotated[T, "Item desc"]]
-
dataclass with fields of the above supported types (or dataclass).
-
Pydantic BaseModel
All types can be Annotated[T, "Desc"], for example: count: int Can be annotated as: count: Annotated[int, "How many units?"]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A value of target arg type instantiated with the model's output. |
Source code in sibila/model.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
|
classify_async
async
#
Returns a classification from one of the given enumeration values The following ways to specify the valid labels are accepted:
- [1, 2, 3] or ["a","b"] - all items of the same prim_type
- Literal['year', 'name'] - all items of the same prim_type
- Enum, EnumInt, EnumStr, (Enum, int),... - all items of the same prim_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
Any
|
One of the above types. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
One of the given labels, as classified by the model. |
Source code in sibila/model.py
json_async
async
#
json_async(
query,
*,
json_schema=None,
inst=None,
genconf=None,
massage_schema=True,
schemaconf=None
)
JSON/JSON-schema constrained generation, returning a Python dict of values, constrained or not by a JSON schema. Raises GenError if unable to get a valid/schema-validated JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
json_schema |
Union[dict, str, None]
|
A JSON schema describing the dict fields that will be output. None means no schema (free JSON output). |
None
|
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
massage_schema |
bool
|
Simplify schema. Defaults to True. |
True
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid JSON schema output error. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
dict
|
A dict from model's JSON response, following genconf.jsonschema, if provided. |
Source code in sibila/model.py
dataclass_async
async
#
Async constrained generation after a dataclass definition, resulting in an object initialized with the model's response. Raises GenError if unable to get a valid response that follows the dataclass definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A dataclass definition. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example invalid object initialization. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
An object of class cls (derived from dataclass) initialized from the constrained JSON output. |
Source code in sibila/model.py
pydantic_async
async
#
Async constrained generation after a Pydantic BaseModel-derived class definition. Results in an object initialized with the model response. Raises GenError if unable to get a valid dict that follows the BaseModel class definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A class derived from a Pydantic BaseModel class. |
required |
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred, for example an invalid BaseModel object. See GenError. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
Any
|
A Pydantic object of class cls (derived from BaseModel) initialized from the constrained JSON output. |
Source code in sibila/model.py
call_async
async
#
Text generation from a Thread or plain text, used by the other model generation methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Union[Thread, Msg, tuple, str]
|
A Thread or a single IN message given as Msg, list, tuple or str. List and tuple should contain the same args as for creating Msg. |
required |
inst |
Optional[str]
|
Instruction message for model. Will override Thread's inst, if set. Defaults to None. |
None
|
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
ok_length_is_error |
bool
|
Should a result of GenRes.OK_LENGTH be considered an error and raise? |
False
|
Raises:
Type | Description |
---|---|
GenError
|
If an error occurred. This can be a model error, or an invalid JSON output error. |
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
str
|
Text generated by model. |
Source code in sibila/model.py
gen #
Text generation from a Thread, used by the other model generation methods. Doesn't raise an exception if an error occurs, always returns GenOut.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread |
Thread
|
The Thread to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
NotImplementedError
|
If method was not defined by a derived class. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/model.py
gen_json #
JSON/JSON-schema constrained generation, returning a Python dict of values, conditioned or not by a JSON schema. Doesn't raise an exception if an error occurs, always returns GenOut.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread |
Thread
|
The Thread to use as model input. |
required |
json_schema |
Union[dict, str, None]
|
A JSON schema describing the dict fields that will be output. None means no schema (free JSON output). |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
massage_schema |
bool
|
Simplify schema. Defaults to True. |
True
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. The output dict is in GenOut.dic. |
Source code in sibila/model.py
gen_dataclass #
Constrained generation after a dataclass definition. An initialized dataclass object is returned in the "value" field of the returned dict. Doesn't raise an exception if an error occurs, always returns GenOut containing the created object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A dataclass definition. |
required |
thread |
Thread
|
The Thread object to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. The initialized dataclass object is in GenOut.value. |
Source code in sibila/model.py
gen_pydantic #
Constrained generation after a Pydantic BaseModel-derived class definition. An initialized Pydantic BaseModel object is returned in the "value" field of the returned dict. Doesn't raise an exception if an error occurs, always returns GenOut containing the created object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Any
|
A class derived from a Pydantic BaseModel class. |
required |
thread |
Thread
|
The Thread to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None, which uses model's default. |
None
|
schemaconf |
Optional[JSchemaConf]
|
JSchemaConf object that controls schema simplification. Defaults to None, which uses model's default. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
TypeError
|
When cls is not a Pydantic BaseClass. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. The initialized Pydantic BaseModel-derived object is in GenOut.value. |
Source code in sibila/model.py
token_len
abstractmethod
#
Calculate or estimate the token length for a Thread or a plain text string. In some cases where it's not possible to calculate the exact token count, this function should give a conservative (upper bound) estimate. It's up to the implementation whether to account for side information like JSON Schema, but it must reflect the model's context token accounting. Thread or text must be the final text which will passed to model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_or_text |
Union[Thread, str]
|
Final thread or text to be passed to model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Number of tokens occupied. |
Source code in sibila/model.py
tokenizer
instance-attribute
#
tokenizer = tokenizer
Tokenizer used to encode text. Some remote models don't have tokenizer and token length is estimated
ctx_len
instance-attribute
#
Maximum context token length, including input and model output. There can be a limit for output tokens in the max_tokens_limit.
maybe_image_input
instance-attribute
#
Does the model support images as input? A value of False is definitive, a value of True is actually a maybe, as some providers don't give this information. Check the model specs to be certain.
known_models
classmethod
#
If the model can only use a fixed set of models, return their names. Otherwise, return None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_key |
Optional[str]
|
If the model provider requires an API key, pass it here or set it in the respective env variable. |
None
|
Returns:
Type | Description |
---|---|
Union[list[str], None]
|
Returns a list of known models or None if unable to fetch it. |