Skip to main content
Version: 10.3

GenAI rules REST API

The page describes the GenAI rules REST API. The integration with the Swagger UI allows you to easily explore and interact with API while testing API endpoints, viewing request and response details, and understanding available resources.

Prerequisites

Before running the application, complete the following steps:

  1. Install Python Interpreter (3.12.1), the core engine that executes the Python code.
  2. Clone the Git repository that contains the Python application.

Run application

To run the Python application, perform the following steps:

  1. Navigate to the project directory and change your working directory to the root of the Python application.

    cd genai-rulesbuilder
  2. Install the needed dependencies and libraries.

    pip install -r requirements.txt
  3. Once the installation is complete, run the application using the Python command:

    python .\GenAI-RulesBuilder.py

Configure key for OpenAI

To configure openai.key for your environment, follow the steps below:

  1. Log in to the Master server via SSH and navigate to the secure storage directory:

    cd /opt/workfusion/wf-sec-storage
  2. Check if the openai.key file exists. To verify whether openai.key is listed, run the following command:

    ./loader.sh automl-worker review
  3. If openai.key is not present, run the command:

    vi key
  4. Add the OpenAI key in the following format:

    openai.key =<your-key>

    Replace <your-key> with your actual API key.

  5. Verify that openai.key is successfully added:

    ./loader.sh automl-worker review
  6. Restart GenAI rules to apply the changes:

    wfmanager restart genai-rulesbuilder

Interact with API endpoints

With the help of the Swagger UI, you can view available resources, make test requests, and understand the request and response structure for each endpoint.

tip

To access the Swagger UI documentation for API, ensure the application is running. Then, open a browser and navigate to the Swagger UI URL: http://localhost:5000/apidocs.

See the endpoint parameters below:

  • Description: the endpoint returns a decision JSON response that contains a list of rules based on the input prompt.
  • HTTP method: POST
  • Path: /generateRulesWithSchema
  • Request parameters (String, required): the input string with a query.

Decision rule examples

  • Decision rule:

    Example
    {
    "prompt": "alert if date is after current date and type is cash with bridge operator as AND name starts with joh and paymentType is cash",
    "schema": {
    "transaction": {
    "amount": "",
    "country": "",
    "item": "",
    "itemType": "",
    "payee": "",
    "type": ""
    },
    "firscosoft": {
    "date": "",
    "name": "",
    "paymentType": ""
    }
    }
    }
    Response
    {
    "data": {
    "rulename": "alert if date is after current date and type is cash with bridge operator as AND name starts with joh and paymentType is cash",
    "ruleDescription": "Check if the fircosoft.date is after current date and transaction.type is cash AND fircosoft.name starts with joh and fircosoft.paymentType is cash",
    "enabled": true,
    "decision": "alert",
    "narrative": "rule got triggered because the date, ${fircosoft.date} is after current date and the type, ${transaction.type} is cash AND the name, ${fircosoft.name} starts with joh and the paymentType, ${fircosoft.paymentType} is cash",
    "priority": -1,
    "ruleSet": [
    {
    "logicalOperator": "AND",
    "bridgeOperator": "AND",
    "rule": [
    {
    "lhs": {
    "value": "fircosoft.date",
    "originalValue": "fircosoft.date",
    "custom": false,
    "type": "date",
    "originalType": "date"
    },
    "operator": "after",
    "rhs": {
    "value": "currentDate",
    "originalValue": "currentDate",
    "custom": true,
    "type": "date",
    "originalType": "date"
    }
    },
    {
    "lhs": {
    "value": "transaction.type",
    "originalValue": "transaction.type",
    "custom": false,
    "type": "string",
    "originalType": "string"
    },
    "operator": "equals",
    "rhs": {
    "value": "cash",
    "originalValue": "cash",
    "custom": true,
    "type": "string",
    "originalType": "string"
    }
    }
    ]
    },
    {
    "logicalOperator": "AND",
    "bridgeOperator": "NONE",
    "rule": [
    {
    "lhs": {
    "value": "fircosoft.name",
    "originalValue": "fircosoft.name",
    "custom": false,
    "type": "string",
    "originalType": "string"
    },
    "operator": "starts with",
    "rhs": {
    "value": "joh",
    "originalValue": "joh",
    "custom": true,
    "type": "string",
    "originalType": "string"
    }
    },
    {
    "lhs": {
    "value": "fircosoft.paymentType",
    "originalValue": "fircosoft.paymentType",
    "custom": false,
    "type": "string",
    "originalType": "string"
    },
    "operator": "equals",
    "rhs": {
    "value": "cash",
    "originalValue": "cash",
    "custom": true,
    "type": "string",
    "originalType": "string"
    }
    }
    ]
    }
    ]
    },
    "status": 200,
    "error": ""
    }
  • Decision rule with expression:

    Example
    {
    "prompt": "escalate if lowercase of payee equals workfusion or square root of amount greater than 500 with bridge operator as and floor of amount greater than 400000 and uppercase of type equals CASH",
    "schema": {
    "transaction": {
    "amount": "",
    "country": "",
    "item": "",
    "itemType": "",
    "payee": "",
    "type": "",
    "name": ""
    }
    }
    }
    Response
    {
    "data": {
    "rulename": "escalate if lowercase of payee equals workfusion or square root of amount greater than 500 with bridge operator as and floor of amount greater than 400000 and uppercase of type equals CASH",
    "ruleDescription": "escalate if lowercase of transaction.payee equals workfusion or square root of transaction.amount greater than 500 with bridge operator as and floor of transaction.amount greater than 400000 and uppercase of transaction.type equals CASH",
    "enabled": true,
    "decision": "escalate",
    "narrative": "rule got triggered because the payee, ${transaction.payee} in lowercase equals workfusion or the square root of the amount, ${transaction.amount} is greater than 500 and the floor of the amount, ${transaction.amount} is greater than 400000 and the type, ${transaction.type} in uppercase equals CASH",
    "priority": -1,
    "ruleSet": [
    {
    "logicalOperator": "OR",
    "bridgeOperator": "AND",
    "rule": [
    {
    "lhs": {
    "value": "${transaction.payee}.toLowerCase()",
    "originalValue": "transaction.payee",
    "custom": false,
    "type": "string",
    "originalType": "string"
    },
    "operator": "equals",
    "rhs": {
    "value": "workfusion",
    "originalValue": "workfusion",
    "custom": true,
    "type": "string",
    "originalType": "string"
    }
    },
    {
    "lhs": {
    "value": "expr.squareRoot(${transaction.amount})",
    "originalValue": "transaction.amount",
    "custom": false,
    "type": "number",
    "originalType": "number"
    },
    "operator": "greater than",
    "rhs": {
    "value": "500",
    "originalValue": "500",
    "custom": true,
    "type": "number",
    "originalType": "number"
    }
    }
    ]
    },
    {
    "logicalOperator": "AND",
    "bridgeOperator": "NONE",
    "rule": [
    {
    "lhs": {
    "value": "expr.floor(${transaction.amount})",
    "originalValue": "transaction.amount",
    "custom": false,
    "type": "number",
    "originalType": "number"
    },
    "operator": "greater than",
    "rhs": {
    "value": "400000",
    "originalValue": "400000",
    "custom": true,
    "type": "number",
    "originalType": "number"
    }
    },
    {
    "lhs": {
    "value": "${transaction.type}.toUpperCase()",
    "originalValue": "transaction.type",
    "custom": false,
    "type": "string",
    "originalType": "string"
    },
    "operator": "equals",
    "rhs": {
    "value": "CASH",
    "originalValue": "CASH",
    "custom": true,
    "type": "string",
    "originalType": "string"
    }
    }
    ]
    }
    ]
    },
    "status": 200,
    "error": ""
    }

Transformation rule examples

  • Transformation rule:

    Example
    {
    "prompt": "replace $ with \"\" in amount when country is U.S and date is before current date with bridge operator as OR country is U.K and paymentType is Cash",
    "schema": {
    "fircosoft": {
    "addressList": "",
    "paymentType": ""
    },
    "transaction": {
    "amount": "",
    "country": "",
    "countryCode": "",
    "date": "",
    "address": ""
    }
    }
    }
    Response
    {
    "data": {
    "rulename": "replace $ with \"\" in amount when country is U.S and date is before current date with bridge operator as OR country is U.K and paymentType is Cash",
    "ruleDescription": "Remove currency symbol $ from transaction.amount when transaction.country is U.S and transaction.date is before current date OR transaction.country is U.K and fircosoft.paymentType is Cash",
    "enabled": true,
    "priority": -1,
    "ruleSet": [
    {
    "logicalOperator": "AND",
    "bridgeOperator": "OR",
    "rule": [
    {
    "lhs": {
    "value": "transaction.country",
    "originalValue": "transaction.country",
    "type": "string",
    "originalType": "string",
    "custom": false
    },
    "operator": "equals",
    "rhs": {
    "value": "U.S",
    "originalValue": "U.S",
    "type": "string",
    "originalType": "string",
    "custom": true
    }
    },
    {
    "lhs": {
    "value": "transaction.date",
    "originalValue": "transaction.date",
    "type": "date",
    "originalType": "date",
    "custom": false
    },
    "operator": "before",
    "rhs": {
    "value": "current date",
    "originalValue": "current date",
    "type": "date",
    "originalType": "date",
    "custom": true
    }
    }
    ]
    },
    {
    "logicalOperator": "AND",
    "bridgeOperator": "NONE",
    "rule": [
    {
    "lhs": {
    "value": "transaction.country",
    "originalValue": "transaction.country",
    "type": "string",
    "originalType": "string",
    "custom": false
    },
    "operator": "equals",
    "rhs": {
    "value": "U.K",
    "originalValue": "U.K",
    "type": "string",
    "originalType": "string",
    "custom": true
    }
    },
    {
    "lhs": {
    "value": "fircosoft.paymentType",
    "originalValue": "fircosoft.paymentType",
    "type": "string",
    "originalType": "string",
    "custom": false
    },
    "operator": "equals",
    "rhs": {
    "value": "Cash",
    "originalValue": "Cash",
    "type": "string",
    "originalType": "string",
    "custom": true
    }
    }
    ]
    }
    ],
    "thenRule": [
    {
    "lhs": {
    "value": "transaction.amount",
    "originalValue": "transaction.amount",
    "type": "string",
    "originalType": "string",
    "custom": false
    },
    "operator": "replace",
    "rhs": {
    "srcValue": {
    "value": "$",
    "originalValue": "$",
    "type": "string",
    "originalType": "string",
    "custom": true
    },
    "tgtValue": {
    "value": "",
    "originalValue": "",
    "type": "string",
    "originalType": "string",
    "custom": true
    }
    }
    }
    ]
    },
    "status": 200,
    "error": ""
    }
  • Transformation rule with expression:

    Example
    {
    "prompt": "set type to fraud when floor of amount greater than 5000",
    "schema": {
    "transaction": {
    "amount": "",
    "date": "",
    "item": "",
    "itemtype": "",
    "payee": "",
    "type": ""
    }
    }
    }
    Response
    {
    "data": {
    "rulename": "set type to fraud when floor of amount greater than 5000",
    "ruleDescription": "Set transaction.type to 'fraud' when floor of transaction.amount is greater than 5000",
    "enabled": true,
    "priority": -1,
    "ruleSet": [
    {
    "logicalOperator": "NONE",
    "bridgeOperator": "NONE",
    "rule": [
    {
    "lhs": {
    "value": "expr.floor(${transaction.amount})",
    "originalValue": "expr.floor(${transaction.amount})",
    "type": "number",
    "originalType": "number",
    "custom": true
    },
    "operator": "greater than",
    "rhs": {
    "value": "5000",
    "originalValue": "5000",
    "type": "number",
    "originalType": "number",
    "custom": true
    }
    }
    ]
    }
    ],
    "thenRule": [
    {
    "lhs": {
    "value": "transaction.type",
    "originalValue": "transaction.type",
    "type": "string",
    "originalType": "string",
    "custom": false
    },
    "operator": "set",
    "rhs": {
    "srcValue": {
    "value": "NONE",
    "originalValue": "NONE",
    "type": "NONE",
    "custom": "NONE"
    },
    "tgtValue": {
    "value": "fraud",
    "originalValue": "fraud",
    "type": "string",
    "custom": true
    }
    }
    }
    ]
    },
    "status": 200,
    "error": ""
    }

Test API

You can use the Swagger UI to test the API endpoints directly from the web interface. To test an endpoint, perform the following steps:

  1. Open the Swagger UI in your browser.
  2. Navigate to the endpoint you want to test.
  3. Click the Try it out button.
  4. Provide values for any required parameters.
  5. Click Execute to send a request.
  6. Review a response with the results.

Build dependencies

To build a wheel or egg and all dependencies for a Python project, run the following command:

python setup.py bdist_wheel
pip wheel

Install project

To install the project, run the following command:

pip install -r GenAI_RulesBuilder-1.0.0-py3-none-any.whl