Creating your First Algorithm Component
In this example, you will be building an algorithm component that takes in a feature value from the user and prints out that value in a generated report.
There are three objectives in this algorithm component example:
- Modify the input schema for the algorithm to receive user input
- Modify the output schema and and write code to return the expected output
- Modify the testing codes
Generating the algorithm component project
Algorithms are stored under the my_plugin/algorithms folder. From your terminal, use ai-verify-plugin ga
to generate an algorithm component template for your new algorithm.
# Navigate to the plugin project directory
cd my_plugin
# Generate the algorithm template
ai-verify-plugin ga my_algorithm --name "My Algorithm" --description "This algorithm returns the value of the feature name selected by the user."
Yay! You have generated an algorithm component project to create your first algorithm. Verify that the directory algorithms/my_algorithm
exists in your current directory.
You should see the files generated for the algorithm component under the directory. For more information on the files generated, see Understanding your algorithm project.
Check the Algorithm Meta Data
Open the file my_algorithm.meta.json
under the algorithms/my_algorithm folder and check that the properties are set correctly as shown below:
{
"cid": "my_algorithm",
"name": "My Algorithm",
"modelType": [
"classification"
],
"version": "0.1.0",
"author": "Example Author",
"description": "This algorithm returns the value of the feature name selected by the user.",
"tags": [
"My Algorithm",
"classification"
],
"requireGroundTruth": true,
"requiredFiles": [
"AUTHORS.rst",
"CHANGELOG.md",
"input.schema.json",
"LICENSE",
"output.schema.json",
"my_algorithm.meta.json",
"my_algorithm.py",
"README.md",
"requirements.txt",
"syntax_checker.py"
]
}
Modifying input schema
First, modify input.schema.json
to request an input called feature_name
from the user when the user uses this algorithm. Notice the highlighted lines that requires a feature_name
field, and the properties of the feature_name
is also defined.
Modifying algorithm
Modify my_algorithm.py
to receive and return the data of the requested feature_name
.
Tip
All codes generated has been annotated with TODO:
for users to quickly navigate to areas that require code modification.
Next, update the generate
method to retrieve the return the values of the selected feature_name
in a given sample data file.
Lastly, update the output.schema.json
to return the expected results. This file will be validated against the output to ensure that the results (see line 180 in the previous code snippet) adhere to the output schema.
In this algorithm, the expected output will be stored in a list (or array) named my_expected_results
. There must be at least 10 items in the list, and the items must have the type number
(as shown in the highlighted lines).
Run the test
First, update __main__.py
with your sample data, model and ground truth files required for your algorithm to run.
In this algorithm, we have updated the data_path
, model_path
, ground_truth_path
to the path of the files. The ground_truth
has also been set to default. We have also updated the plugin_argument_values
with required arguments to ensure that the input validation passes. This is typically an input parameter from the dataset. For the mock data, we will be using gender as the plugin argument value.
If you have been following every step from Installing AI Verify Developer Tools to Guided Example, you will need to update core_modules_path
as shown below. If you decide to structure the files differently, ensure that core_modules_path
is an absolute/relative path pointing to your test-engine-core-modules folder.
Note
Ground truth is optional so if your algorithm does not require ground truth, ground_truth_path
and ground_truth
can be left as an empty string ""
.
Next, run ai-verify-plugin testa
to test your algorithm.
If the test passes (no error messages in terminal), you have successfully completed the creation of the algorithm component. At this stage, you can either deploy your algorithm component as a standalone plugin, or continue to work on other components (eg. another algorithm, widget, input block etc) before packaging it as a single plugin.
If the test fails, refer to the troubleshooting guide for help.