Decision Models API
DecisionModel
Bases: Module
A decision model API wrapper.
Decision models (System One models) evaluate a state against typed questions and return calibrated probabilities instead of generated text. They are fast and cheap, and their answers are always one of the options you gave, which makes them a good fit for routing, classification and grading. They do not reason step by step, generate text or read images.
A decision model is called like a LanguageModel, with chat messages and
the target output schema, and the request is inferred from them: the
chat messages are sent as the state, so the system message (instructions
and few-shot examples) is part of the context the questions are answered
in, and each field of the output schema is one question, asked with the
field's description. The output follows the schema. The field type sets
the question type:
bool: a yes/no (noul) question. The field isTruewhen the probability of yes is at least 0.5.- A string enum (
LiteralorEnum): pick one option, up to 255. The field is the most probable option. - A score (
synalinks.Rating,synalinks.Score...): rate along its scale. The field is the value of the scale nearest to the probability-weighted score. score_schema(...): rate along 2 to 10 described levels, answered with{"score", "legend", "probabilities", "confidence"}, wherescoreis the probability-weighted level index.
Decision models do not generate: any other field (a free-form str, a
number, a list...) raises an UnsupportedSchemaError. Use a
LanguageModel for those, or check_schema() to check a schema first.
Answers are validated before they are returned: a missing answer, a choice outside the options or a probability outside [0, 1] fails the call.
Refer to what the messages hold in backticks, e.g.
"Does `message` ask for a refund?". A description holding a JSON
object is sent as structured instructions. All questions of a call see the
same state and are answered independently and in parallel, so ask several
at once rather than making several calls.
The modules that can use a decision model take it as their
decision_model (never as their language_model): Generator,
Decision, MultiDecision, Branch, SelfCritique and RubricsAsJudge
(and the rubric rewards). Decision, MultiDecision, SelfCritique and
RubricsAsJudge switch to a data model made of such questions (without
thinking or critique) when given one. Set a default with
synalinks.set_default_decision_model(...): these modules then use it
instead of the default language model, unless given a language_model.
Using TypeSafe models
import synalinks
import os
from typing import Literal
os.environ["TYPESAFE_API_KEY"] = "your-api-key"
decision_model = synalinks.DecisionModel(
model="typesafe/jev-latest",
)
messages = synalinks.ChatMessages(
messages=[
synalinks.ChatMessage(
role="system",
content="You triage the support tickets of an online shop.",
),
synalinks.ChatMessage(
role="user",
content="I was charged twice. Please fix this ASAP.",
),
]
)
class Triage(synalinks.DataModel):
is_billing: bool = synalinks.Field(
description="Is the ticket about billing?",
)
team: Literal["billing", "technical"] = synalinks.Field(
description="Which team should handle the ticket?",
)
triage = await decision_model(messages, schema=Triage.get_schema())
print(triage.get("is_billing"), triage.get("team"))
urgency = await decision_model(
messages,
schema={
"type": "object",
"properties": {
"urgency": synalinks.decision_models.score_schema(
"How urgent is the ticket?",
["Can wait", "This week", "Today"],
),
},
},
)
print(urgency.get("urgency")["probabilities"])
Routing with a Branch
A decision model picks the branch: the question is asked as is, over the labels, without step by step reasoning.
import synalinks
import asyncio
class Query(synalinks.DataModel):
query: str = synalinks.Field(
description="The user query",
)
class Answer(synalinks.DataModel):
answer: str = synalinks.Field(
description="The correct answer",
)
class AnswerWithThinking(synalinks.DataModel):
thinking: str = synalinks.Field(
description="Your step by step thinking",
)
answer: str = synalinks.Field(
description="The correct answer",
)
async def main():
language_model = synalinks.LanguageModel(
model="ollama/mistral",
)
decision_model = synalinks.DecisionModel(
model="typesafe/jev-latest",
)
x0 = synalinks.Input(data_model=Query)
(x1, x2) = await synalinks.Branch(
question="What is the difficulty level of the above query?",
labels=["easy", "difficult"],
branches=[
synalinks.Generator(
data_model=Answer,
language_model=language_model,
),
synalinks.Generator(
data_model=AnswerWithThinking,
language_model=language_model,
),
],
decision_model=decision_model,
)(x0)
x3 = x1 | x2
program = synalinks.Program(
inputs=x0,
outputs=x3,
name="conditional_reasoning",
description="Think step by step only when the query needs it",
)
if __name__ == "__main__":
asyncio.run(main())
Grading with rubrics (compile + fit)
A decision model grades every rubric in a single call, which makes it a fast and cheap reward to train a program with.
import synalinks
import asyncio
async def main():
language_model = synalinks.LanguageModel(
model="ollama/mistral",
)
decision_model = synalinks.DecisionModel(
model="typesafe/jev-latest",
)
x0 = synalinks.Input(
data_model=synalinks.datasets.gsm8k.get_input_data_model(),
)
x1 = await synalinks.Generator(
data_model=synalinks.datasets.gsm8k.get_output_data_model(),
language_model=language_model,
)(x0)
program = synalinks.Program(
inputs=x0,
outputs=x1,
)
program.compile(
reward=synalinks.rewards.RubricsAsJudge(
decision_model=decision_model,
rubrics=[
{
"name": "correct",
"description": "The answer matches the reference.",
"weight": 2.0,
},
{
"name": "sound_reasoning",
"description": "Every step of the thinking is valid.",
},
],
),
optimizer=synalinks.optimizers.RandomFewShot(),
)
(x_train, y_train), (x_test, y_test) = synalinks.datasets.gsm8k.load_data()
history = await program.fit(
x=x_train,
y=y_train,
validation_data=(x_test, y_test),
epochs=2,
batch_size=32,
)
if __name__ == "__main__":
asyncio.run(main())
The API key is read from TYPESAFE_API_KEY on every call (it is never
stored in the config). Without it, a call fails like any failed call: it
warns and returns None, or asks the fallback model. Set
TYPESAFE_BASE_URL (or api_base) to use another endpoint.
Note: Use an .env file and .gitignore to keep your API keys out
of the code and out of any config file you push to a repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The model to use, prefixed by its provider
(e.g. |
None
|
api_base
|
str
|
Optional. The endpoint to use. |
None
|
timeout
|
float
|
Optional. The timeout in seconds of each HTTP request (default to 30). |
30.0
|
retry
|
int
|
Optional. The number of attempts (default to 5). |
5
|
retry_max_wait
|
int
|
Optional. Max seconds to wait between retries when a
rate-limit |
60
|
fallback
|
DecisionModel
|
Optional. The decision model to fallback to if anything is wrong. |
None
|
cache_dir
|
str
|
Optional. Directory for a persistent on-disk cache. When set, every successful response is saved as a JSON file keyed by the full request (model, state and questions), and identical requests are answered from disk. (Default to None, disabled). |
None
|
cost_per_token
|
float
|
Optional. USD per input token, overriding the built-in price table (e.g. for a custom plan). |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
hooks
|
list
|
Optional. Hooks to attach to this module's calls. |
None
|
Source code in synalinks/src/modules/decision_models/decision_model.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 | |
call(messages, schema=None, tools=None, tool_schemas=None, streaming=False, **kwargs)
async
Answer the questions inferred from schema about the chat messages.
Same interface as LanguageModel.call(), so a decision model can be
used wherever a language model is (e.g. in a Generator), as long as
the schema only asks questions it can answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
ChatMessages
|
The chat messages to evaluate, sent as the
|
required |
schema
|
dict
|
The output JSON schema. Each field is one question (see the class docstring). |
None
|
tools
|
list
|
Not supported: decision models do not call tools. |
None
|
tool_schemas
|
list
|
Not supported: decision models do not call tools. |
None
|
streaming
|
bool
|
Ignored: the answers are not generated. |
False
|
**kwargs
|
keyword arguments
|
Ignored sampling arguments
(e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
JsonDataModel
|
The answers, following |
Raises:
| Type | Description |
|---|---|
UnsupportedSchemaError
|
If the schema asks for something a decision model cannot answer. |
ValueError
|
If |
Source code in synalinks/src/modules/decision_models/decision_model.py
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 | |
check_schema(schema)
Check that a decision model can answer an output schema.
Every field must be a question a decision model answers: a bool, a
string enum (Literal or Enum), a score (synalinks.Rating,
synalinks.Score...) or a score_schema object, each with a
description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict
|
The output JSON schema to check. |
required |
Raises:
| Type | Description |
|---|---|
UnsupportedSchemaError
|
If a field cannot be answered by a decision model. |
Source code in synalinks/src/modules/decision_models/decision_model.py
supported_providers()
classmethod
Returns the supported decision model provider prefixes.
These are the values accepted before the / in model, e.g.
"typesafe" in "typesafe/jev-latest".
Returns:
| Type | Description |
|---|---|
list
|
The sorted list of supported provider prefixes. |
Source code in synalinks/src/modules/decision_models/decision_model.py
UnsupportedSchemaError
Bases: ValueError
Raised when a schema asks for more than a decision model can answer.
Decision models only answer typed questions (yes/no, choice, score): a
field they would have to generate, such as a free-form string, is not
supported. Use a LanguageModel for such schemas.
Source code in synalinks/src/modules/decision_models/decision_model.py
choice_schema(instructions, options)
Return the schema of a field answered by a choice question.
The answer is {"choice", "probabilities", "confidence"}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instructions
|
str | dict
|
The question, used as the field description. |
required |
options
|
list | dict
|
The options, or a dict mapping each option to
its description (or |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema of the field. |
Source code in synalinks/src/modules/decision_models/decision_model.py
confidence_schema()
Return the JSON schema of a confidence field.
current_call_usage()
Return the usage of the decision model call in the current task.
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in synalinks/src/modules/decision_models/decision_model.py
noul_schema(instructions)
Return the schema of a field answered by a yes/no (noul) question.
The answer is {"noul": p}, the probability that the answer is yes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instructions
|
str | dict
|
The question, used as the field description. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema of the field. |
Source code in synalinks/src/modules/decision_models/decision_model.py
outputs_from_answers(questions, plain, answers)
Shape validated answers into the values of the output schema.
Source code in synalinks/src/modules/decision_models/decision_model.py
probabilities_schema(keys, description)
Return the JSON schema of a probability per key (option, label or level).
Source code in synalinks/src/modules/decision_models/decision_model.py
probability_schema(title, description=None)
Return the JSON schema of a probability: a number in [0, 1].
Source code in synalinks/src/modules/decision_models/decision_model.py
questions_from_schema(schema)
Infer the decision model questions from an output schema.
Each top-level field is one question, keyed by its name and asked with its
description. The question type follows the field type:
boolean: a noul question, answered withTruewhen p >= 0.5.- A numeric
enum, such assynalinks.Scoreorsynalinks.Rating: a score question over the scale's values, answered with the value nearest to the probability-weighted score. - A string
enum(LiteralorEnum): a choice question over the values, answered with the most probable one. - A
score_schemaobject: a score question over its levels, answered with the full answer (score, probabilities, confidence). - A
noul_schemaorchoice_schemaobject: the yes/no and choice questionsMultiDecisionandDecisionask internally, answered with the full answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict
|
The output JSON schema. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
The dict of questions, and the fields answered with a plain
value ( |
Raises:
| Type | Description |
|---|---|
UnsupportedSchemaError
|
If a field cannot be answered by a decision model. |
Source code in synalinks/src/modules/decision_models/decision_model.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
score_schema(instructions, levels)
Return the schema of a field answered by a score question.
The answer is {"score", "legend", "probabilities", "confidence"}, where
score is the probability-weighted level index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instructions
|
str | dict
|
The question, used as the field description. |
required |
levels
|
list
|
The ordered level descriptions (2 to 10). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema of the field. |
Source code in synalinks/src/modules/decision_models/decision_model.py
validate_answers(questions, answers)
Check the answers against their questions and normalize them.
Every question must have an answer of its type, choices must be one of the options, and probabilities, nouls and confidences must be in [0, 1] (values off by float noise are clipped).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
questions
|
dict
|
The questions that were asked. |
required |
answers
|
dict
|
The answers returned by the API. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The answers, restricted to the asked questions and clipped. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an answer is missing or malformed. |
Source code in synalinks/src/modules/decision_models/decision_model.py
validate_questions(questions)
Check a question map against the API limits before sending it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
questions
|
dict
|
A map of question ID to question dict, each with a
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a question is malformed. |