afnio.cognitive.functional#
Functions
|
Implements an addition operation for |
|
Implements a chat completion operation using the specified language model within the |
|
Evaluates predictions deterministically using a user-defined evaluation function within the |
|
Evaluates predictions using exact matching within the |
|
Implements an evaluation of a model prediction using a language model (LM) as the judge within the |
|
Implements a split operation for |
|
Implements a summation operation for a list of |
- afnio.cognitive.functional.add(x, y)[source]#
Implements an addition operation for
Variableinstances within theafnioframework, supporting automatic differentiation.The
Addfunction supports both scalar and list.datafields:Scalars: Adds numerical values (
int,float) or concatenates strings.Lists: Performs element-wise addition of corresponding elements from the lists. Lists must be of the same length.
It automatically handles type-based operations:
For numerical data (
int,float), it performs arithmetic addition.For strings, it concatenates the values.
Mixed types (e.g., string and number) are converted appropriately before performing the addition.
This operation also tracks
Variabledependencies, enabling automatic gradient computation through backpropagation.Example with scalar inputs:
>>> x = Variable(data="abc", role="first input", requires_grad=True) >>> y = Variable(data="def", role="second input", requires_grad=False) >>> result = F.add(x, y) >>> result.data 'abcdef' >>> result.role 'first input and second input' >>> result.requires_grad True >>> g = Variable(data="MY_FEEDBACK", role="add gradient") >>> result.backward(g) >>> x.grad.data 'Here is the combined feedback we got for this specific first input and other variables: MY_FEEDBACK' >>> x.grad.role 'feedback to first input'
Example with batched inputs:
>>> x = Variable(data=[1, 2, 3], role="first input", requires_grad=True) >>> y = Variable(data=[4, 5, 6], role="second input", requires_grad=False) >>> result = F.add(x, y) >>> result.data [5, 7, 9] >>> result.role 'first input and second input' >>> result.requires_grad True
- afnio.cognitive.functional.chat_completion(forward_model_client, messages, inputs=None, **completion_args)[source]#
Implements a chat completion operation using the specified language model within the
afnioframework, supporting automatic differentiation.Features:#
- Mini-Batching: Processes multiple input dictionaries simultaneously to improve
throughput.
- Asynchronous Execution: Both the forward and backward passes are optimized to
run asynchronous calls for each mini-batch, reducing latency.
- Gradient Computation: Supports automatic differentiation for all
Variables in
messagesandinputsarguments, maintaining the order of gradients.
- Gradient Computation: Supports automatic differentiation for all
The
ChatCompletionfunction generates aVariableresponses by passing a composite prompt, built from a list ofmessagesand optionalinputs, to theforward_model_client. Each message is a dictionary with a ‘role’ (e.g., ‘system’, ‘user’) and a list ofVariableobjects as ‘content’.inputsis a dictionary containing strings, list of strings orVariable``s providing dynamic values to fill placeholders within message templates. If ``inputscontain lists of strings orVariable``s which ``.datafield is a list, the response’s.datafield will be a list, corresponding to the batched results. Otherwise, the.datafield will be a scalar string. Additional behavior, such as temperature or token limits, can be customized throughcompletion_args.Example with scalar inputs:
>>> system = Variable( ... "You are a helpful assistant.", ... role="system instruction", ... requires_grad=True ... ) >>> user = Variable("Translate 'Hello' to {language}.", role="user query") >>> messages = [ ... {"role": "system", "content": [system]}, ... {"role": "user", "content": [user]}, ... ] >>> inputs = {"language": Variable("Italian", role="language")} >>> response = F.chat_completion( ... model_client, ... messages, ... inputs=inputs, ... temperature=0.7 ... ) >>> print(response.data) 'Ciao' 'Hola' >>> feedback = Variable("Use only capital letters.", role="feedback") >>> response.backward(feedback) >>> system.grad[0].data 'The system instruction should enforce the use of capital letters only.'
Example with batched inputs:
>>> system = Variable( ... "You are a helpful assistant.", ... role="system instruction", ... requires_grad=True ... ) >>> user = Variable("Translate 'Hello' to {language}.", role="user query") >>> messages = [ ... {"role": "system", "content": [system]}, ... {"role": "user", "content": [user]}, ... ] >>> inputs = { ... "language": [ ... Variable("Italian", role="language"), ... Variable("Spanish", role="language") ... ] ... } >>> response = F.chat_completion( ... model_client, ... messages, ... inputs=inputs, ... temperature=0.7 ... ) >>> print(response.data) ['Ciao', 'Hola']
- afnio.cognitive.functional.deterministic_evaluator(prediction, target, eval_fn, eval_fn_purpose, success_fn, reduction_fn, reduction_fn_purpose)[source]#
Evaluates predictions deterministically using a user-defined evaluation function within the
afnioframework, supporting automatic differentiation.The
DeterministicEvaluatorfunction computes ascoreand anexplanationbased on thepredictionandtargetinputs using a user-defined evaluation function (eval_fn). The evaluation function’s purpose is described byeval_fn_purpose. Outputs include a numerical or textual score and a textual explanation, both wrapped asVariableobjects.The
predictionis aVariable. Thetargetcan be a string, a list of strings, or aVariable. EachVariablepassed as an input argument can have either a scalar or a list .data field, supporting both individual samples and batch processing. For batch processing, the lengths ofpredictionandtargetmust match.The
success_fnparameter is a user-defined function that returnsTruewhen all predictions evaluated byeval_fnare considered successful, andFalseotherwise. Ifsuccess_fnreturnsTrue, thebackwardpass will skip gradient calculations and directly return an empty gradient, optimizing computational time.The
reduction_fnparameter specifies the aggregation function to use for scores across a batch of predictions and targets. When specified, the reduction function’s purpose is described usingreduction_fn_purpose. If aggregation is not desired, setreduction_fnandreduction_fn_purposetoNone.Example with scalar inputs:
>>> prediction = Variable( ... data="green", ... role="color prediction", ... requires_grad=True ... ) >>> target = "red" >>> def exact_match_fn(p: str, t: str) -> int: ... return 1 if p == t else 0 >>> score, explanation = F.deterministic_evaluator( ... prediction, ... target, ... exact_match_fn, ... "exact match", ... ) >>> score.data 0 >>> explanation.data 'The evaluation function, designed for 'exact match', compared the <DATA> field of the predicted variable ('green') with the <DATA> field of the target variable ('red'), resulting in a score: 0.' >>> explanation.backward() >>> prediction.grad[0].data 'Reassess the criteria that led to the initial prediction of 'green'.'
Example with batched inputs:
>>> prediction = Variable( ... data=["green", "blue"], ... role="color prediction", ... requires_grad=True ... ) >>> target = ["red", "blue"] >>> def exact_match_fn(p: str, t: str) -> int: ... return 1 if p == t else 0 >>> score, explanation = F.deterministic_evaluator( ... prediction, ... target, ... exact_match_fn, ... "exact match", ... reduction_fn=sum, ... reduction_fn_purpose="summation" ... ) >>> score.data 1 >>> explanation.data 'The evaluation function, designed for 'exact match', compared the <DATA> fields of the predicted variable and the target variable across all samples in the batch, generating individual scores for each pair. These scores were then aggregated using the reduction function 'summation', resulting in a final aggregated score: 1.' >>> explanation.backward() >>> prediction.grad[0].data 'Reassess the criteria that led to the initial prediction of 'green'.'
- afnio.cognitive.functional.exact_match_evaluator(prediction, target, reduction_fn=<built-in function sum>, reduction_fn_purpose='summation')[source]#
Evaluates predictions using exact matching within the
afnioframework, supporting automatic differentiation.The
ExactMatchEvaluatorfunction computes ascoreand anexplanationby comparing thedatafields of apredictionand atargetfor an exact match. For each sample:A score of
1is assigned for an exact match.A score of
0is assigned otherwise.
The
predictionis aVariable. Thetargetcan be a string, a list of strings, or aVariable. EachVariablepassed as an input argument can have either a scalar or a list.datafield, supporting both individual samples and batch processing. For batch processing, the lengths ofpredictionandtargetmust match.If batched inputs are provided, the scores can be aggregated using an optional
reduction_fn, such assum. The purpose of the reduction is described usingreduction_fn_purpose. If aggregation is not desired, setreduction_fnandreduction_fn_purposetoNone.Example with scalar inputs:
>>> prediction = Variable( ... data="green", ... role="color prediction", ... requires_grad=True ... ) >>> target = "red", >>> score, explanation = F.exact_match_evaluator(prediction, target) >>> score.data 0 >>> explanation.data 'The evaluation function, designed for 'exact match', compared the <DATA> field of the predicted variable ('green') with the <DATA> field of the target variable ('red'), resulting in a score: 0.' >>> explanation.backward() >>> prediction.grad[0].data 'Reassess the criteria that led to the initial prediction of 'green'.'
Example with batched inputs:
>>> prediction = Variable( ... data=["green", "blue"], ... role="color prediction", ... requires_grad=True ... ) >>> target = ["red", "blue"] >>> score, explanation = F.exact_match_evaluator(prediction, target) >>> score.data 1 >>> explanation.data 'The evaluation function, designed for 'exact match', compared the <DATA> fields of the predicted variable and the target variable across all samples in the batch, generating individual scores for each pair. These scores were then aggregated using the reduction function 'summation', resulting in a final aggregated score: 1.' >>> explanation.backward() >>> prediction.grad[0].data 'Reassess the criteria that led to the initial prediction of 'green'.'
- afnio.cognitive.functional.lm_judge_evaluator(forward_model_client, messages, prediction, target=None, inputs=None, success_fn=None, reduction_fn=<built-in function sum>, reduction_fn_purpose='summation', eval_mode=True, **completion_args)[source]#
Implements an evaluation of a model prediction using a language model (LM) as the judge within the
afnioframework, supporting automatic differentiation.This function returns a
scoreand anexplanation, both asVariableobjects, by comparing apredictionagainst atarget(when present) using a composite prompt. The prompt is constructed from a list ofmessagesand optionalinputs, which can dynamically populate placeholders in the message templates. The evaluation process leverages the specifiedforward_model_clientto perform the LM-based assessment.The
predictionis aVariable. Thetargetcan be a string, a list of strings, or aVariable. Similarly, theinputsdictionary can include strings, lists of strings, orVariable``s. Each ``Variablepassed as an input argument can have either a scalar or a list.datafield, supporting both individual samples and batch processing. For batch processing, the lengths ofprediction,target, and any batchedinputsmust match.The
success_fnparameter is a user-defined function that returnsTruewhen all predictions evaluated by the LM as Judge are considered successful, andFalseotherwise. Ifsuccess_fnreturnsTrue, thebackwardpass will skip gradient calculations and directly return an empty gradient, optimizing computational time.If you are processing a batch of predictions and targets, you can use the
reduction_fnto aggregate individual scores (e.g., usingsumto compute a total score). Thereduction_fn_purposeparameter is a brief description of the aggregation’s purpose (e.g., “summation”). If you don’t want any aggregation, set bothreduction_fnandreduction_fn_purposetoNone.The function operates in two modes controlled by
eval_mode:``eval_mode=True`` (default) – Computes gradients for
predictiononly. Use it for direct feedback on predictions.``eval_mode=False`` – Computes gradients for
messagesandinputs. Use it to optimize the evaluator or align with human evaluation datasets.
Additional model parameters, such as temperature, max tokens, or seed values, can be passed through
completion_argsto customize the LLM’s behavior.Example with scalar inputs:
>>> task = Variable( ... "Evaluate if the translation is accurate.", ... role="evaluation task", ... requires_grad=True ... ) >>> format = Variable( ... "Provide 'score' (true/false) and 'explanation' in JSON.", ... role="output format" ... ) >>> user = Variable( ... "<PREDICTION>{prediction}</PREDICTION><TARGET>{target}</TARGET>", ... role="user query" ... ) >>> prediction = Variable( ... "Hola Mundo", ... role="translated text", ... requires_grad=True ... ) >>> target = Variable("Ciao Mondo", role="expected output") >>> messages = [ ... {"role": "system", "content": [task, format]}, ... {"role": "user", "content": [user]} ... ] >>> score, explanation = F.lm_judge_evaluator( ... model, ... messages, ... prediction, ... target, ... temperature=0.5, ... ) >>> score.data False >>> explanation.data 'The translated text is in Spanish, but the expected is in Italian.' >>> explanation.backward() >>> prediction.grad[0].data 'The translated text should be in Italian.'
Example with batched inputs:
>>> task = Variable( ... "Evaluate if the translation is accurate.", ... role="evaluation task", ... requires_grad=True ... ) >>> format = Variable( ... "Provide 'score' (true/false) and 'explanation' in JSON.", ... role="output format" ... ) >>> user = Variable( ... "<PREDICTION>{prediction}</PREDICTION><TARGET>{target}</TARGET>", ... role="user query" ... ) >>> prediction = Variable( ... data=["Hola Mundo", "Salve a tutti"], ... role="translated text", ... requires_grad=True, ... ) >>> target = ["Ciao Mondo", "Salve a tutti"] >>> score, explanation = F.lm_judge_evaluator( ... model, ... messages, ... prediction, ... target, ... reduction_fn=sum, ... reduction_fn_purpose="summation", ... ) >>> score.data 1 >>> explanation.data 'The evaluation function, designed using an LM as the judge, compared the <DATA> fields of the predicted variable and the target variable across all samples in the batch. These scores were then aggregated using the reduction function 'summation', resulting in a final aggregated score: 1.' >>> explanation.backward() >>> prediction.grad[0].data 'The translated text should be in Italian.'
- afnio.cognitive.functional.split(x, sep=None, maxsplit=-1)[source]#
Implements a split operation for
Variableinstances within theafnioframework, supporting automatic differentiation.The
Splitfunction divides the.dataof the inputVariableinto multiple parts using a specified delimitersep. Ifmaxsplitis specified, the split operation is limited to a maximum number of splits. It handles both scalar and list.datafields:Scalars: The scalar
.data(a single string) is split into substrings based on the specifiedsepandmaxsplitparameters.Lists: Each element of the list
.data(strings) is split individually. If splits of varying lengths occur, shorter splits are automatically padded with empty strings to ensure consistent dimensions.
During backpropagation, feedback is collected and aggregated across all split parts. The combined feedback is propagated back to the original input
Variable, allowing for the proper computation of gradients.Example with scalar inputs:
>>> x = Variable(data="afnio is great!", role="sentence", requires_grad=True) >>> result = Split.apply(x, sep=" ", maxsplit=1) >>> [var.data for var in result] ['afnio', 'is great!'] >>> result[0].role 'split part 0 of sentence' >>> g_1 = Variable(data="MY_FIRST_FEEDBACK", role="gradient") >>> g_2 = Variable(data="MY_SECOND_FEEDBACK", role="gradient") >>> result[0].backward(g_1, retain_graph=True) >>> result[1].backward(g_2) >>> x.grad[0].data 'Here is the combined feedback we got for this specific sentence and other variables: <ITEM>MY_FIRST_FEEDBACK</ITEM><ITEM></ITEM>' >>> x.grad[0].role 'feedback to sentence' >>> x.grad[1].data 'Here is the combined feedback we got for this specific sentence and other variables: <ITEM></ITEM><ITEM>MY_SECOND_FEEDBACK</ITEM>' >>> x.grad[1].role 'feedback to sentence'
Example with batched inputs:
>>> x = Variable( ... data=["afnio is great!", "Deep learning"], ... role="sentences", ... requires_grad=True ... ) >>> result = Split.apply(x, sep=" ", maxsplit=2) >>> [var.data for var in result] [['afnio', 'Deep'], ['is', 'learning'], ['great!', '']] >>> g = Variable(data="MY_FEEDBACK", role="gradient") >>> result[1].backward(g) >>> x.grad[0].data 'Here is the combined feedback we got for this specific sentences and other variables: <ITEM></ITEM><ITEM>MY_FEEDBACK</ITEM><ITEM></ITEM>' >>> x.grad[0].role 'feedback to sentences'
- afnio.cognitive.functional.sum(x)[source]#
Implements a summation operation for a list of
Variableinstances within theafnioframework, supporting automatic differentiation.The
Sumfunction aggregates the.data,.role, and.requires_gradattributes of all inputVariableinstances into a singleVariable. It supports both scalar and list.datafields:Scalars: Computes the arithmetic sum for numerical data (
int,float) or concatenates all string values, wrapping each in<ITEM></ITEM>tags.Lists: Aggregates the corresponding elements of the lists. For numerical data, it sums the corresponding elements. For string data, it concatenates them, wrapping each element in
<ITEM></ITEM>tags.
During backpropagation, the function distributes the gradient to all input
Variableinstances that require gradients.Example with scalar inputs:
>>> x = Variable(data="abc", role="first input", requires_grad=True) >>> y = Variable(data="def", role="second input", requires_grad=False) >>> result = F.sum([x, y]) >>> result.data '<ITEM>abc</ITEM><ITEM>def</ITEM>' >>> result.role 'first input and second input' >>> result.requires_grad True >>> g = Variable(data="MY_FEEDBACK", role="add gradient") >>> result.backward(g) >>> x.grad.data 'Here is the combined feedback we got for this specific first input and other variables: MY_FEEDBACK' >>> x.grad.role 'feedback to first input'
Example with batched inputs:
>>> x = Variable(data=[1, 2, 3.5], role="first input", requires_grad=True) >>> y = Variable(data=[4, 5, 6], role="second input", requires_grad=False) >>> result = F.sum([x, y]) >>> result.data [5, 7, 9.5] >>> result.role 'first input and second input' >>> result.requires_grad True