Remote model classes
OpenAIModel #
OpenAIModel(
name,
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
tokenizer=None,
api_key=None,
base_url=None,
overhead_per_msg=None,
token_estimation_factor=None,
create_tokenizer=False,
other_init_kwargs={}
)
Access an OpenAI model.
Supports constrained JSON output, via the OpenAI API tools mechanism.
Ref
https://platform.openai.com/docs/api-reference/chat/create
Create an OpenAI remote model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Model name to resolve into an existing model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. 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 (shared for input and output). None for model's default. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for model's default. |
None
|
tokenizer |
Optional[Tokenizer]
|
An external initialized tokenizer to use instead of the created from the GGUF file. Defaults to None. |
None
|
api_key |
Optional[str]
|
OpenAI API key. Defaults to None, which will use env variable OPENAI_API_KEY. |
None
|
base_url |
Optional[str]
|
Base location for API access. Defaults to None, which will use env variable OPENAI_BASE_URL or a default. |
None
|
overhead_per_msg |
Optional[int]
|
Overhead tokens to account for when calculating token length. None for model's default. |
None
|
token_estimation_factor |
Optional[float]
|
Used when no tokenizer is available. Multiplication factor to estimate token usage: multiplies total text length to obtain token length. |
None
|
create_tokenizer |
bool
|
When no tokenizer is passed, should try to create one? |
False
|
other_init_kwargs |
dict
|
Extra args for OpenAI.OpenAI() initialization. Defaults to {}. |
{}
|
Raises:
Type | Description |
---|---|
ImportError
|
If OpenAI API is not installed. |
NameError
|
If model name was not found or there's an API or authentication problem. |
Source code in sibila/openai.py
71 72 73 74 75 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 |
|
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. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/openai.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.
If a json_schema is provided in genconf, we use its string's token_len as upper bound for the extra prompt tokens.
From https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
More info on calculating function_call (and tools?) tokens:
https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/24
https://gist.github.com/CGamesPlay/dd4f108f27e2eec145eedf5c717318f5
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_or_text |
Union[Thread, str]
|
For token length calculation. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Estimated number of tokens used. |
Source code in sibila/openai.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
known_models
classmethod
#
List of model names that can be used. Some of the models are not chat models and cannot be used, for example embedding models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_key |
Optional[str]
|
Requires OpenAI API key, passed as this arg or set in env variable OPENAI_API_KEY. |
None
|
Returns:
Type | Description |
---|---|
Union[list[str], None]
|
Returns a list of known models. |
Source code in sibila/openai.py
AnthropicModel #
AnthropicModel(
name,
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
api_key=None,
token_estimation_factor=None,
anthropic_init_kwargs={}
)
Access an Anthropic model. Supports constrained JSON output, via the Anthropic API function calling mechanism.
Ref
https://docs.anthropic.com/claude/docs/intro-to-claude
Create an Anthropic remote model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Model name to resolve into an existing model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. 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 (shared for input and output). None for model's default. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for model's default. |
None
|
api_key |
Optional[str]
|
Anthropic API key. Defaults to None, which will use env variable ANTHROPIC_API_KEY. |
None
|
token_estimation_factor |
Optional[float]
|
Multiplication factor to estimate token usage: multiplies total text length to obtain token length. |
None
|
anthropic_init_kwargs |
dict
|
Extra args for Anthropic() initialization. Defaults to {}. |
{}
|
Raises:
Type | Description |
---|---|
ImportError
|
If Anthropic API is not installed. |
NameError
|
If model name was not found or there's an API or authentication problem. |
Source code in sibila/anthropic.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 |
|
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. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/anthropic.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]
|
For token length calculation. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Estimated number of tokens occupied. |
Source code in sibila/anthropic.py
tokenizer
instance-attribute
#
tokenizer = tokenizer
Tokenizer used to encode text. Some remote models don't have tokenizer and token length is estimated
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
FireworksModel #
FireworksModel(
name,
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
tokenizer=None,
api_key=None,
base_url=None,
token_estimation_factor=None,
other_init_kwargs={}
)
Access a Fireworks AI model with the OpenAI API. Supports constrained JSON output, via the response_format JSON Schema mechanism.
Ref
https://readme.fireworks.ai/docs/structured-response-formatting
https://readme.fireworks.ai/reference/createchatcompletion
Create a Fireworks AI remote model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Model name to resolve into an existing model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. 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 (shared for input and output). None for model's default. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for model's default. |
None
|
tokenizer |
Optional[Tokenizer]
|
An external initialized tokenizer to use instead of the created from the GGUF file. Defaults to None. |
None
|
api_key |
Optional[str]
|
API key. Defaults to None, which will use env variable FIREWORKS_API_KEY. |
None
|
base_url |
Optional[str]
|
Base location for API access. Defaults to None, which will use env variable FIREWORKS_BASE_URL or a default. |
None
|
token_estimation_factor |
Optional[float]
|
Used when no tokenizer is available. Multiplication factor to estimate token usage: multiplies total text length to obtain token length. |
None
|
other_init_kwargs |
dict
|
Extra args for OpenAI.OpenAI() initialization. Defaults to {}. |
{}
|
Raises:
Type | Description |
---|---|
ImportError
|
If OpenAI API is not installed. |
NameError
|
If model name was not found or there's an API or authentication problem. |
Source code in sibila/schema_format_openai.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 to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/openai.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.
If a json_schema is provided in genconf, we use its string's token_len as upper bound for the extra prompt tokens.
From https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
More info on calculating function_call (and tools?) tokens:
https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/24
https://gist.github.com/CGamesPlay/dd4f108f27e2eec145eedf5c717318f5
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_or_text |
Union[Thread, str]
|
For token length calculation. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Estimated number of tokens used. |
Source code in sibila/openai.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
known_models
classmethod
#
List of model names that can be used. Some of the models are not chat models and cannot be used, for example embedding models.
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/schema_format_openai.py
GroqModel #
GroqModel(
name,
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
tokenizer=None,
api_key=None,
base_url=None,
token_estimation_factor=None,
other_init_kwargs={}
)
Access a Groq model with the OpenAI API. Supports constrained JSON output, via the response_format JSON Schema mechanism.
Ref
https://console.groq.com/docs/tool-use
https://github.com/groq/groq-api-cookbook/blob/main/parallel-tool-use/parallel-tool-use.ipynb
Create a Groq remote model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Model name to resolve into an existing model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. 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 (shared for input and output). None for model's default. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for model's default. |
None
|
tokenizer |
Optional[Tokenizer]
|
An external initialized tokenizer to use instead of the created from the GGUF file. Defaults to None. |
None
|
api_key |
Optional[str]
|
API key. Defaults to None, which will use env variable GROQ_API_KEY. |
None
|
base_url |
Optional[str]
|
Base location for API access. Defaults to None, which will use env variable GROQ_BASE_URL or a default. |
None
|
token_estimation_factor |
Optional[float]
|
Used when no tokenizer is available. Multiplication factor to estimate token usage: multiplies total text length to obtain token length. |
None
|
other_init_kwargs |
dict
|
Extra args for OpenAI.OpenAI() initialization. Defaults to {}. |
{}
|
Raises:
Type | Description |
---|---|
ImportError
|
If OpenAI API is not installed. |
NameError
|
If model name was not found or there's an API or authentication problem. |
Source code in sibila/schema_format_openai.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 to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/openai.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.
If a json_schema is provided in genconf, we use its string's token_len as upper bound for the extra prompt tokens.
From https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
More info on calculating function_call (and tools?) tokens:
https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/24
https://gist.github.com/CGamesPlay/dd4f108f27e2eec145eedf5c717318f5
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_or_text |
Union[Thread, str]
|
For token length calculation. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Estimated number of tokens used. |
Source code in sibila/openai.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
known_models
classmethod
#
List of model names that can be used. Some of the models are not chat models and cannot be used, for example embedding models.
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/schema_format_openai.py
MistralModel #
MistralModel(
name,
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
api_key=None,
token_estimation_factor=None,
mistral_init_kwargs={}
)
Access a Mistral AI model. Supports constrained JSON output, via the Mistral API function calling mechanism.
Ref
https://docs.mistral.ai/guides/function-calling/
Create a Mistral AI remote model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Model name to resolve into an existing model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. 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 (shared for input and output). None for model's default. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for model's default. |
None
|
api_key |
Optional[str]
|
Mistral API key. Defaults to None, which will use env variable MISTRAL_API_KEY. |
None
|
token_estimation_factor |
Optional[float]
|
Multiplication factor to estimate token usage: multiplies total text length to obtain token length. |
None
|
mistral_init_kwargs |
dict
|
Extra args for mistral.MistralClient() initialization. Defaults to {}. |
{}
|
Raises:
Type | Description |
---|---|
ImportError
|
If Mistral API is not installed. |
NameError
|
If model name was not found or there's an API or authentication problem. |
Source code in sibila/mistral.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 |
|
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. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/mistral.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]
|
For token length calculation. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Estimated number of tokens occupied. |
Source code in sibila/mistral.py
tokenizer
instance-attribute
#
tokenizer = tokenizer
Tokenizer used to encode text. Some remote models don't have tokenizer and token length is estimated
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/mistral.py
TogetherModel #
TogetherModel(
name,
*,
genconf=None,
schemaconf=None,
ctx_len=None,
max_tokens_limit=None,
tokenizer=None,
api_key=None,
base_url=None,
token_estimation_factor=None,
other_init_kwargs={}
)
Access a together.ai model with the OpenAI API. Supports constrained JSON output, via the response_format JSON Schema mechanism.
Ref
https://docs.together.ai/docs/json-mode
https://docs.together.ai/reference/chat-completions
Create a together.ai remote model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Model name to resolve into an existing model. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. 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 (shared for input and output). None for model's default. |
None
|
max_tokens_limit |
Optional[int]
|
Maximum output tokens limit. None for model's default. |
None
|
tokenizer |
Optional[Tokenizer]
|
An external initialized tokenizer to use instead of the created from the GGUF file. Defaults to None. |
None
|
api_key |
Optional[str]
|
API key. Defaults to None, which will use env variable TOGETHER_API_KEY. |
None
|
base_url |
Optional[str]
|
Base location for API access. Defaults to None, which will use env variable TOGETHER_BASE_URL or a default. |
None
|
token_estimation_factor |
Optional[float]
|
Used when no tokenizer is available. Multiplication factor to estimate token usage: multiplies total text length to obtain token length. |
None
|
other_init_kwargs |
dict
|
Extra args for OpenAI.OpenAI() initialization. Defaults to {}. |
{}
|
Raises:
Type | Description |
---|---|
ImportError
|
If OpenAI API is not installed. |
NameError
|
If model name was not found or there's an API or authentication problem. |
Source code in sibila/schema_format_openai.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 to use as model input. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If unable to generate. |
Returns:
Type | Description |
---|---|
GenOut
|
A GenOut object with result, generated text, etc. |
GenOut
|
The output text is in GenOut.text. |
Source code in sibila/openai.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.
If a json_schema is provided in genconf, we use its string's token_len as upper bound for the extra prompt tokens.
From https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
More info on calculating function_call (and tools?) tokens:
https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/24
https://gist.github.com/CGamesPlay/dd4f108f27e2eec145eedf5c717318f5
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_or_text |
Union[Thread, str]
|
For token length calculation. |
required |
genconf |
Optional[GenConf]
|
Model generation configuration. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
int
|
Estimated number of tokens used. |
Source code in sibila/openai.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
known_models
classmethod
#
List of model names that can be used. Some of the models are not chat models and cannot be used, for example embedding models.
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. |