|
| 1 | +--- |
| 2 | +title: One Day Projects |
| 3 | +Description: Create a project instantly using AI and API/Logic Automation. Customize later with Python, Rules, and Flask, which is open-source |
| 4 | +Version: 1.1 |
| 5 | +--- |
| 6 | +<style> |
| 7 | + .md-typeset h1, |
| 8 | + .md-content__button { |
| 9 | + display: none; |
| 10 | + } |
| 11 | +</style> |
| 12 | + |
| 13 | +# AI Sample |
| 14 | + |
| 15 | +Here's how to use AI and API Logic Server to create complete running systems in minutes: |
| 16 | + |
| 17 | +1. Use **ChatGPT for *Schema Automation:*** create a database schema from natural language |
| 18 | +2. Use **API Logic Server *Microservice Automation*:** create working software *with 1 command:* |
| 19 | + * ***App Automation:*** a multi-page, multi-table admin app |
| 20 | + * ***API Automation:*** a JSON:API - crud for each table, with filtering, sorting, optimistic locking and pagination |
| 21 | +3. **Customize** the project with *your IDE*: |
| 22 | + * ***Logic Automation* using rules:** declare spreadsheet-like rules in Python for multi-table derivations and constraints - **40X more concise** than code |
| 23 | + * Use Python and standard libraries (Flask, SQLAlchemy), and debug in your IDE |
| 24 | +4. **Iterate** your project: |
| 25 | + * Revise your database design and logic |
| 26 | + * **Integrate** with B2B partners and internal systems |
| 27 | + |
| 28 | + |
| 29 | +[](https://youtu.be/-7aZPWz849I "Microservice Automation"){:target="_blank" rel="noopener"} |
| 30 | + |
| 31 | +This process **leverages your existing IT infrastructure:** your IDE, GitHub, the cloud, your database… open source. Let's see how. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## 1. AI: Schema Automation |
| 38 | + |
| 39 | +You can use an existing database, or create a new one with ChapGPT or your database tools. |
| 40 | + |
| 41 | +Use ChatGPT to generate SQL commands for database creation: |
| 42 | + |
| 43 | +!!! pied-piper "Create database schemas from ChatGPT -- provide this prompt" |
| 44 | + |
| 45 | + Create a sqlite database for customers, orders, items and product |
| 46 | + |
| 47 | + Hints: use autonum keys, allow nulls, Decimal types, foreign keys, no check constraints. |
| 48 | + |
| 49 | + Include a notes field for orders. |
| 50 | + |
| 51 | + Create a few rows of only customer and product data. |
| 52 | + |
| 53 | + Use Logic Bank to enforce the Check Credit requirement: |
| 54 | + |
| 55 | + 1. Customer.Balance <= CreditLimit |
| 56 | + 2. Customer.Balance = Sum(Order.AmountTotal where date shipped is null) |
| 57 | + 3. Order.AmountTotal = Sum(Items.Amount) |
| 58 | + 4. Items.Amount = Quantity * UnitPrice |
| 59 | + 5. Store the Items.UnitPrice as a copy from Product.UnitPrice |
| 60 | + |
| 61 | + |
| 62 | +This creates standard SQL, [like this](https://github.com/ApiLogicServer/ApiLogicServer-src/blob/main/api_logic_server_cli/prototypes/sample_ai/database/chatgpt/sample_ai.sql){:target="_blank" rel="noopener"}. Copy the generated SQL commands into a file, say, `sample-ai.sql`. (As always with AI, eyeball the result - for example, you may need to remove a command like "CREATE DATABASE store.db;"). |
| 63 | + |
| 64 | +Then, create the database: |
| 65 | + |
| 66 | +```bash |
| 67 | +sqlite3 sample_ai.sqlite < sample_ai.sql |
| 68 | +``` |
| 69 | + |
| 70 | +> You may not have the sqlite cli; you can proceed to step 2 and the system will use a pre-installed database. |
| 71 | +
|
| 72 | + |
| 73 | + |
| 74 | +## 2. API Logic Server: Create |
| 75 | + |
| 76 | +Given a database, API Logic Server creates an executable, customizable project with the following single command: |
| 77 | + |
| 78 | +```bash |
| 79 | +$ ApiLogicServer create --project_name=sample_ai --db_url=sqlite:///sample_ai.sqlite |
| 80 | +``` |
| 81 | + |
| 82 | +This creates a project you can open with your IDE, such as VSCode (see below). The project is now ready to run - press F5. It includes: |
| 83 | + |
| 84 | +* a self-serve **API** ready for UI developers, and |
| 85 | +* an **Admin app** ready for Business User Collaboration |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +### a. App Automation |
| 92 | + |
| 93 | +App Automation means that `ApiLogicServer create` creates a multi-page, multi-table Admin App -- automatically. This React-Admin app does *not* consist of hundreds of lines of complex html and javascript - it's a simple yaml file that's easy to customize. |
| 94 | + |
| 95 | +> Ready for business user collaboration, back-office data maintenance - Day 1. |
| 96 | +
|
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +### b. API Automation |
| 102 | + |
| 103 | +API Automation means that `ApiLogicServer create` creates a JSON:API -- automatically. Your API supports related data access, pagination, optimistic locking, filtering, and sorting. |
| 104 | + |
| 105 | +> It would take days to months to create such an API using frameworks. |
| 106 | +
|
| 107 | +UI App Developers can create custom apps immediately, using swagger to design their API call, and copying the URI into their JavaScript code. APIs are thus ***self-serve:*** no server coding is required. |
| 108 | + |
| 109 | +> Custom App Dev is unblocked - Day 1. |
| 110 | +
|
| 111 | + |
| 112 | + |
| 113 | +!!! pied-piper ":bulb: Key Take Away -- Microservice Automation" |
| 114 | + |
| 115 | + Microservice Automation means that With 1 command, we have a running API and Admin App. |
| 116 | + |
| 117 | + * With a **framework**, you are ready to **code** |
| 118 | + * With **automation**, you are ready to **run** |
| 119 | + |
| 120 | + * UI Developers unblocked |
| 121 | + * Ad Hoc Integration |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +## 3. Customize |
| 126 | + |
| 127 | +So, we have working software, in minutes. It's running, but we really can't *deploy* it until we have logic and security. Which brings us to customization. |
| 128 | + |
| 129 | +Projects are designed for customization, using standards: Python, frameworks (e.g., Flask, SQLAlchemy), and your IDE for code editing and debugging. Not only Python *code*, but also ***Rules***. |
| 130 | + |
| 131 | +To explore, let's customize this project. To speed things up, instead of the normal procedure of declaring rules in your IDE, follow this procedure: |
| 132 | + |
| 133 | +1. Stop the Server |
| 134 | + |
| 135 | +2. Execute the following in your IDE terminal window: |
| 136 | + |
| 137 | +```bash |
| 138 | +ApiLogicServer sample-ai |
| 139 | +ApiLogicServer add-auth --db_url=auth |
| 140 | +``` |
| 141 | + |
| 142 | +This applies customized logic and security, which we examine below. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +### a. Logic Automation |
| 147 | + |
| 148 | +Logic Automation means that you can ***declare spreadsheet-like rules*** using Python. Such logic maintains database integrity with multi-table derivations and constraints, and security. Rules are 40X more concise than traditional code, and can be extended with Python. |
| 149 | + |
| 150 | +Below we implement the **Check Credit** requirement - see the comments at top. Their implementation follows: 5 rules, instead of 200 lines of Python. |
| 151 | + |
| 152 | +1. Use the Admin App to add an Item for 1000 Widgets, observe how the constraint prevents the transaction |
| 153 | + |
| 154 | +Rules are an executable design. Note they map exactly to our natural language design: |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +**1. Debugging** |
| 161 | + |
| 162 | +The screenshot above shows our logic declarations, and how we debug them: |
| 163 | + |
| 164 | +1. Execution is paused at a **breakpoint** in the debugger, where we can examine state, and execute step by step. |
| 165 | + |
| 166 | +2. Note the **logging** for inserting an `Item`. Each line represents a rule firing, and shows the complete state of the row. |
| 167 | +<br><br> |
| 168 | + |
| 169 | +**2. Chaining - Multi-Table Transaction Automation** |
| 170 | + |
| 171 | +Note that it's a `Multi-Table Transaction`, as indicated by the log indentation. This is because - like a spreadsheet - **rules automatically chain, *including across tables.*** |
| 172 | +<br><br> |
| 173 | + |
| 174 | +**3. 40X More Concise** |
| 175 | + |
| 176 | +The 5 spreadsheet-like rules represent the same logic as 200 lines of code, [shown here](https://github.com/valhuber/LogicBank/wiki/by-code){:target="_blank" rel="noopener"}. That's a remarkable 40X decrease in the backend *half* of the system. |
| 177 | +<br><br> |
| 178 | + |
| 179 | +**4. Automatic Re-use** |
| 180 | + |
| 181 | +The logic above, perhaps conceived for Place order, applies automatically to all transactions: deleting an order, changing items, moving an order to a new customer, etc. This reduces code, and promotes quality (no missed corner cases). |
| 182 | +<br><br> |
| 183 | + |
| 184 | +**5. Automatic Optimizations** |
| 185 | + |
| 186 | +SQL overhead is minimized by pruning, and by elimination of expensive aggregate queries. These can result in orders of magnitude impact. This is because the rule engine is not a Rete algorithm, but highly optimized for transaction processing, and integrated with the SQLAlchemy ORM (Object Relational Manager). |
| 187 | +<br><br> |
| 188 | + |
| 189 | +**6. Transparent** |
| 190 | + |
| 191 | +Rules are an executable design. Note they map exactly to our natural language design (shown in comments) - readable by business users. |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +### b. Security Automation |
| 196 | + |
| 197 | +Security Automation means you activate security, and declare grants (using Python) to control row access for user roles. |
| 198 | + |
| 199 | +Security requires login to use the Admin App and Swagger. Security also provide **row-level authorization** - here, we ensure that less active accounts are hidden if we login as user s1.p: |
| 200 | + |
| 201 | +```python |
| 202 | +Grant( on_entity = models.Customer, |
| 203 | + to_role = Roles.sales, |
| 204 | + filter = lambda : models.Customer.CreditLimit > 3000, |
| 205 | + filter_debug = "CreditLimit > 3000") |
| 206 | +``` |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +## 4. Iterate: Rules + Python |
| 211 | + |
| 212 | +So we have completed our 1 day project. We can deploy it, as [described here](Tutorial-Deployment.md){:target="_blank" rel="noopener"}, for *agile collaboration* with business users. |
| 213 | + |
| 214 | +Which leads to *agile iterations.* Automation helps here too: not only are spreadsheet-like rules 40X more concise, they meaningfully simplify iterations and maintenance. Let’s explore this with two changes: |
| 215 | + |
| 216 | +!!! pied-piper "Green Discounts" |
| 217 | + |
| 218 | + Give a 10% discount for carbon-neutral products for 10 items or more. |
| 219 | + |
| 220 | +And: |
| 221 | + |
| 222 | +!!! pied-piper "Application Integration" |
| 223 | + |
| 224 | + 1. Provide read access for internal applications. |
| 225 | + |
| 226 | + 2. Enable B2B partners to place orders with a custom API. |
| 227 | + |
| 228 | + 3. Send new Orders to Shipping using a Kafka message. |
| 229 | + |
| 230 | + |
| 231 | +As above, we speed things up with the following procedure: |
| 232 | + |
| 233 | +1. Stop the Server |
| 234 | + |
| 235 | +2. Execute the following in your IDE terminal window: |
| 236 | + |
| 237 | +```bash |
| 238 | +ApiLogicServer sample-ai-iteration |
| 239 | +ApiLogicServer rebuild-from-database --project_name=. --db_url=sqlite:///database/db.sqlite |
| 240 | +``` |
| 241 | + |
| 242 | +This revises your database to add the new Product.CarbonNeutral column, and installs some new code we'll explore below. |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +**Iterate Logic - Add Python** |
| 247 | + |
| 248 | +Here is our revised logic to apply the discount, and send the Kafka message: |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | +We can also **extend our API** for our new B2BOrder endpoint, using standard Python and Flask as shown below. The code includes the swagger example, so we can now test our endpoint: |
| 253 | + |
| 254 | +1. Use Swagger (**ServicesEndPoint > POST /ServicesEndPoint/OrderB2B)** |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | +Note: Kafka is not activated in this example. To explore a running Tutorial for application integration with running Kafka, [click here](Sample-Integration.md){:target="_blank" rel="noopener"}. |
| 259 | + |
| 260 | + |
| 261 | +This illustrates some significant aspects of logic. |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | +#### a. Maintenance Automation |
| 266 | + |
| 267 | +Along with perhaps documentation, one of the tasks programmers most loathe is maintenance. That’s because it’s not about writing code, but archaeology - deciphering code someone else wrote, just so you can add 4 or 5 lines that’ll hopefully be called and function correctly. |
| 268 | + |
| 269 | +Logic Automation changes that, with ***Maintenance Automation,*** which means: |
| 270 | + |
| 271 | +* Rules *automatically order* their execution (and pruning) based on system-discovered dependencies |
| 272 | +* Rules are *automatically reused* for all relevant transactions |
| 273 | + |
| 274 | +So, to alter logic, you just *“drop a new rule in the bucket”,* and the system will ensure it’s called in the proper order, and *re-used* over all the relevant Use Cases. |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | +#### b. Extensibility: With Python |
| 279 | + |
| 280 | +In the first case, we needed to do some if/else testing, and it was more convenient to add a dash of Python. While this is pretty simple *Python as a 4GL*, you have full power of object-oriented Python and its many libraries. |
| 281 | + |
| 282 | +For example, our extended API leverages Flask and open source libraries for Kafka messages. |
| 283 | + |
| 284 | + |
| 285 | + |
| 286 | +#### c. Rebuild: Logic Preserved |
| 287 | + |
| 288 | +Note we rebuilt the project from our altered database (`ApiLogicServer rebuild-from-database`), without losing customizations. |
| 289 | + |
| 290 | + |
| 291 | + |
| 292 | +## Summary |
| 293 | + |
| 294 | + |
| 295 | + |
| 296 | +In minutes, you've used ChatGPT and API Logic Server to convert an idea into working software. It required only 5 rules, and 20 lines of Python. The process was simple: |
| 297 | + |
| 298 | +* **Created the Schema** with `ChatGPT` |
| 299 | + |
| 300 | +* **Created the Project** with `ApiLogicServer` |
| 301 | + * A **Self-Serve API** to unblock UI Developers -- Day 1 |
| 302 | + * An **Admin App** for Business User Collaboration -- Day 1 |
| 303 | + |
| 304 | +* **Customized** the project |
| 305 | + * With Rules -- 40X more concise than code |
| 306 | + |
| 307 | +* **Iterated** the project in your IDE to implement new requirements |
| 308 | + * Rules, *with Python* for complete flexibility |
| 309 | + * Prior customizations are preserved |
| 310 | + |
| 311 | +It all works with standard tooling: Python, your IDE, and container-based deployment. |
0 commit comments