Skip to content

GenAI CLI

💡 TL;DR - Create Microservice from Natural Language Prompt using CLI

API Logic Server / GenAI provides CLI commands that accepts a Natural Language prompt (e.g, CRM system), and produces:

  • a Microservice, an Admin App and a JSON API
  • a project that you can download and customize in your IDE using rules and Python
  • which can be deployed as a container using automatically generated scripts.

API Logic Server / GenAI uses:

  • GenAI services for data model creation, and
  • API Logic Server for project creation.

You can also use GenAI services via the GenAI WebSite.

 

Overview

To use GenAI to create projects

  1. Establish your virtual environment (suggestion: use the Manager)
  2. Provide a prompt in either a file (als genai), or a CLI argument (als genai-create):
Project creation with GenAI
als genai --using=system/genai/examples/genai_demo/genai_demo.prompt

# or, provide the prompt in an **quoted** argument:
als genai-create --project-name='customer_orders' --using='customer orders'

Projects are created in the currrent working directory, here, the manager.

 

Configuration

GenAI uses ChatGPT, which requires an API Key. The simplest approach is to

  1. Obtain one from here or here
  2. Authorize payments here
  3. Create an environmental variable APILOGICSERVER_CHATGPT_MODEL

 

Conversations vs. resubmit

You can review created projects by using the app, and/or reviewing the data model. Of course, it's simple to resubmit a new prompt and re-create the project.

However, this will be a completely new rendition of your idea, and it may change things you like about the project. Conversations enable you to keep what you already have, while making desired changes.

When you create a project, the API Logic Server / GenAI saves your prompt and response in a conversation-directory. Conversations are saved in 2 different conversation-directories:

  • the manager's system/genai/temp/<project> directory

  • the created project's doc directory.

You can iterate with interactive prompts, or by adding files to the manager's system/genai/temp/<project> directory.

 

Conversations - Interactive

The figure below creates and iterates a project, using the manager:

create and iterate project with interactive prompts using genai-iterate
als genai-create -project-name=conv —-using='customer orders'
als genai-iterate —-project-name=conv —using='add payments'

GenAI Automation

 

Conversations - Files

Alternatively, you can iterate projects by adding files to the Manager's temp directory:

iterate project using files (Note: genai, not genai-iterate)
als genai-create --project-name=conv --using='customer orders'
echo "add payments" | cat >> system/genai/temp/conv/conv_002.prompt
als genai --using=system/genai/temp/conv

The project name can be provided with the --project-name argument (as of release 11.02.01). If omitted, it is defaulted to the last node of the directory name, here, conv.

This will recreate the project based on the existing context. See the next section, below.

 

Customized Project Sync

In the prior section, the result was a recreated project. If you have customized the project, you can preserve your customizations as follows:

  1. Copy database/models.py and db.sqlite from the GenAI to your customized project
  2. In your customized project, use als rebuild-from-model

 

Natural Language Logic

As of release 11.2.10, you can declare Natural Language Logic when you create projects, and for existing projects.

Status: Beta. Current implementation presumes projects are running in the Manager directory.

 

Create Projects with Logic

As shown below, you can use the CLI als genai command to designate a prompt file that generates a system, including logic.

Note:

  1. Logic files can contain derivations and constraints
  2. The system will create model attributes for derived columns. Note that these can dramatically improve performance.

You can declare logic formally, or informally.

 

Formal Rules

You can use familiar dot notation in declaring rules, e.g.

Create Projects with Logic

Formal Logic - Familiar Dot Notation
Create a system with customers, orders, items and products.

Include a notes field for orders.

Use LogicBank to enforce the Check Credit requirement:
1. Customer.balance <= credit_limit
2. Customer.balance = Sum(Order.amount_total where date_shipped is null)
3. Order.amount_total = Sum(Item.amount)
4. Item.amount = quantity * unit_price
5. Store the Item.unit_price as a copy from Product.unit_price

 

Informal Rules

You can also use a more relaxed declaration. For example, you can create a system (database, API, Admin App and Logic) with the following prompt (see system/genai/examples/genai_demo/genai_demo_informal.prompt):

Prompt With Informal Logic
Create a system with customers, orders, items and products.

Include a notes field for orders.

Use LogicBank to enforce the Check Credit requirement:
    1. The Customer's balance is less than the credit limit
    2. The Customer's balance is the sum of the Order amount_total where date_shipped is null
    3. The Order's amount_total is the sum of the Item amount
    4. The Item amount is the quantity * unit_price
    5. The Item unit_price is copied from the Product unit_price

 

Multi-rule Logic

You can even declare logic that is transformed into 2 rules:

Prompt with Multi-Rule Logic
System for Departments and Employees.

LogicBank:
1. Sum of employee salaries cannot exceed department budget

This creates a running system: a database, an API, an Admin App, and logic. From the Manager:

CLI Command to Create a microservice, with logic
als genai --using=system/genai/examples/emp_depts/emp_dept.prompt

The logic is non-trivial:

  1. A Department.total_salaries is created
  2. Two rules are created in logic/declare_logic.py
Logic Creates 2 Rules
    # Logic from GenAI: (or, use your IDE w/ code completion)

    # Aggregate the total salaries of employees for each department.
    Rule.sum(derive=Department.total_salaries, as_sum_of=Employee.salary)

    # Ensure the sum of employee salaries does not exceed the department budget.
    Rule.constraint(validate=Department, as_condition=lambda row: row.total_salaries <= row.budget, error_msg="Total salaries ({row.total_salaries}) exceed the department budget ({row.budget})")

    # End Logic from GenAI

 

Cardinality Patterns

Logic GenAI training has enabled the following:

Products have Notices, with severity 0-5.

Raise and error if product is orderable == True and there are any severity 5 Notices, or more than 3 Notices.

Notes:

  1. Note the use of a "qualified any", resulting in a count with a where condition:
Logic Recognizes "qualified any"
    # Logic from GenAI: (or, use your IDE w/ code completion)

    # Derive product notice count from related notices.
    Rule.count(derive=Product.notice_count, as_count_of=Notice)

    # Derive count of severity 5 notices for products.
    Rule.count(derive=Product.class_5_notice_count, as_count_of=Notice, where=lambda row: row.severity == 5)

    # Ensure product is not orderable if conditions on notices are met.
    Rule.constraint(validate=Product,
    as_condition=lambda row: not (row.orderable and (row.class_5_notice_count > 0 or row.notice_count > 3)),
    error_msg="Orderable product contains severity 5 or excessive notices.")

    # End Logic from GenAI

 

Add Logic to Existing Projects

As shown below, in an existing project located under the Manager:

  1. Create a prompt such as docs/logic/check_credit.prompt

    • Create logic files in docs/logic
    • Use a descriptive name to denote the purpose of the logic
    • Your docs/logic can contain multiple files; only .prompt files are processed
  2. In the terminal window:

Create logic from docs/logic prompt files
cd <project root>
als genai-logic
  1. Your logic is created in logic/logic_discovery

Notes:

  • See the notes above for creating new projects with logic
  • Unlike new projects, columns are not created automatically for derived attributes. You can create these as described in data model changes.

Add logic to Existing Project

 


Appendices

GenAI and API Logic Server

GenAI is part of API Logic Server. As shown below:

  • API Logic Server can create microservices (Admin Apps, APIs, and Logic) from databases.

  • It can also create microservices from database models (als create --from-model=my_model.py)

  • GenAI

    • Use ChatGPT APIs, and creates a model file from the response
    • Submit that to als create --from-model=my_model.py

GenAI Automation

Error Recovery

AI results are not consistent, so you may sometimes need to correct errors and resume. This requires a bit of background about genai processing.

 

GemAI Processing

genai processing is shown below (internal steps denoted in grey):

  1. You create your .prompt file, and invoke als genai --using=your.prompt. genai then creates your project as follows:

    a. Submits your prompt to the ChatGPT API

    b. Writes the response to file, so you can correct and retry if anything goes wrong

    c. Extracts model.py from the response

    d. Invokes als create-from-model, which creates the database and your project

  2. Your created project is opened in your IDE, ready to execute and customize.

    a. Review Sample-Genai.md, Explore Customizations.

GenAI Automation

 

Recovery options

AI somtimes fails - here's how to recover after you correct the response or the model file.

From the Model File

You can find the models file at system/genai/temp/model.py. You can correct the model file, and then run:

als create --project-name=genai_demo --from-model=system/genai/temp/create_db_models.py --db-url=sqlite
From the Response

Or, correct the chatgpt response, and

als genai --using=genai_demo.prompt --repaired-response=system/genai/temp/chatgpt_retry.response