Skip to content

GenAI CLI

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

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

  • an executable database system: an Admin App, a JSON API and logic
  • the underlying 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 (ChatGPT) for data model creation, and
  • API Logic Server for project creation.

You can also use GenAI services via the GenAI WebSite. For Web/GenAI architecture, click here.

 

Overview

To use GenAI to create projects

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

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

 

Create Projects

You can use the als cli to create projects with logic. See the example provided in the Manager:

create-projects

Note:

  1. Logic files can contain derivations and constraints
  2. The system will create model attributes for derived columns.

 

Key Directories: Temp and Docs

When you create projects, the system saves prompts and responses. This provided for documentation, error recovery, and iterations (described below):

key dirs

 

Iterating Projects

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. Iterations 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. Iterations are saved in 2 different conversation-directories:

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

  • the created project's doc directory.

The --using argument can be a file, or a directory. That means you can iterate by adding files to the manager's system/genai/temp/<project> directory. See the example provided in the Manager:

iterate-cli

 

IDE Nat Language: docs/logic

As shown below, you can add Natural Language logic to existing projects. Using 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.
  • Most likely, you will maintain the logic in the logic_discovery directory.
    • When you are done, you might want to rename the docs/logic files (e.g., change the file extension) so they are not processed on future runs.

Add logic to Existing Project

 

Logic Suggestions

You can ask GenAI to suggest logic for your system. This can help you learn about rules, and can inspire your own imagination about required logic.

It's AI, so or course you will want to review the suggestions carefully.

logic Editor

Explore suggestions using the Manager:

1. Create Project, without Rules
# 1. Create Project, without Rules
als genai --project-name='genai_demo_no_logic' --using=system/genai/examples/genai_demo/genai_demo_no_logic.prompt
2. Request Rule Suggestions
# 2. Request Rule Suggestions
cd genai_demo_no_logic
als genai-logic --suggest

You can review the resultant logic suggestions in the genai_demo_no_logic project:

  • See and edit: docs/logic_suggestions/002_logic_suggestions.prompt (used in step 3, below)
    • This corresponds to the WebGenAI Logic Editor - Logic View in the WebGenAI web app
3. See the rules for the logic
# 3. See the rule code for the logic
als genai-logic --suggest --logic='*'

Important notes about suggestions and generated code:

  • --suggest --logic='*' is intended to enable you to identify logic that does not translate into proper code
  • The example above was pretty good, but sometimes the results are downright silly:
    • Just run suggest again, or
    • Repair docs/logic_suggestions/002_logic_suggestions.prompt

Also...

  • It is not advised to paste the code into logic/declare_logic.py
    • The suggested logic may result in new data model attributes
    • These are created automatically by running als genai (next step)

The logic suggestions directory (genai_demo_no_logic/docs/logic_suggestions) now contains the prompts to create a new project with the suggested logic.
When you are ready to proceed: 1. Execute the following to create a new project (iteration), with suggested logic:

4. Create a new project with the Rule Suggestions
# 4. Create a new project with the Rule Suggestions
cd ..  # important - back to manager root dir
als genai --project-name='genai_demo_with_logic' --using=genai_demo_no_logic/docs/logic_suggestions

Observe:

  1. The created project has the rule suggestions in logic/declare_logic.py
  2. A revised Data Model in database/models.py that includes attributes introduced by the logic suggestions
  3. Revised test database, initialized to reflect the derivations in the suggested logic

   

Fixup: Add Missing Attributes

Fixes project issues by updating the Data Model and Test Data. When adding rules, such as using suggestions, you may introduce new attributes. If these are missing, you will see exceptions when you start your project.

The genai-utils --fixup fixes such project issues by updating the Data Model and Test Data. For more information, see Fixup.

 

Rebuild Test Data

The following is provided to fix project issues by rebuilding the database to conform to the derivation rules. This procedure is available in the Manager README (see Explore Creating Projects > Rebuild the test data).

  1. Create genai_demo:
    als genai --using=system/genai/examples/genai_demo/genai_demo.prompt --project-name=genai_demo
    
  2. Rebuild:
    cd genai_demo
    als genai-utils --rebuild-test-data
    

 

Fixup

To Fix it:

1. Run FixUp to add missing attributes to the fixup response data model
# 1. Run FixUp to add missing attributes to the data model
cd genai_demo_fixup_required
als genai-utils --fixup

Finally, use the created fixup files (genai_demo_fixup_required/docs/fixup) to rebuild the project:

2. Rebuild the project from the fixup response data model
# 2. Rebuild the project from the fixup response data model
cd ../
als genai --repaired-response=genai_demo_fixup_required/docs/fixup/response_fixup.json --project-name=fixed_project

  The created project may still report some attributes as missing.
(ChatGPT seems to often miss attributes mentioned in sum/count where clauses.) To fix:

  1. Note the missing attributes(s) from the log
  2. Add them to docs/003_suggest.prompt
  3. Rebuild the project: als genai --project-name='genai_demo_with_logic' --using=genai_demo_no_logic/docs

Internal Note: this sequence available in the run configs (f1/f2).

 

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

 

Export

You can export your project from WebGenAI, either from the Browser or from GitHub:

export

This enables you to verify all aspects of project operation, and extend GenAI functionality in your local IDE.

 

Open in your IDE

Once have exported and expanded the tar file:

exported

  1. Optionally, copy the project folder to your manager folder (where you installed API Logic Server).
    • This is not required, but it keeps your projects organized, and provide quick access to the Samples that illustrate typical customizations.
  2. Setup your virtual environment - see Virtual Environment.

    • You can create a new venv, but it's simplest just to use the one used by the Manager.

    • Note: in some configurations of VSCode (e.g., mac multiple projects in the same window), it may not allow you to specify your virtual environment. You can fix this:

      • Set the proper virtual environment in .vscode/settings.json: "python.defaultInterpreterPath": "~/dev/ApiLogicServer/ApiLogicServer-dev/build_and_test/ApiLogicServer/venv/bin/python"
      • Rename the project
  3. Verify your default value settings, as shown below (typically True)

  4. You should then be able to open and run the exported project in your IDE. In the terminal window of your IDE:
    • `cd
    • code .

defaulting

 

wg_rules and IDE rules

The system is designed to support concurrent ongoing Multi-Team Development from WebGenAI, and from traditional development. For more information, see Import / Merge WebGenai.

To simplify the file mechanics during merge, WebGenAI rules are stored separately from rules created in the IDE:

Logic Source Stored Source of Truth - Manage In
WebGenAI Rules logic/wg_rules The WebGenAI system. Import / merge projects into local dev environment using Import / Merge WebGenAI
IDE Rules logic/declare_logic.py, and (optionally) as files in logic/logic_discovery IDE / Source control

wg-rules

 

Multi read-only wg_rules files

Consider that WebGenAI and IDEs are quite different environments. For example, IDEs support code completion and highlight errors, while WebGenAI uses Natural Language (which does not have 'syntax errors').

Without an IDE, WebGenAI users still require diagnostics about which rules fail to properly compile. This requires the system to break each rule into a separate file, as shown above.

Since the source of truth is the WebGenAI system, you should not alter the wg_rules. You can and should use these files to:

  • Review and verify the logic created by WebGenAI
  • Debug the logic using the debugger and the logic log; see Debugging Logic.

 

active_rules_export.py

The separate wg_rules files are gathered into a single active_rules_export.py file. This simplifies verification / debugging (see above).

This is a different execution path for IDE execution vs. WebGenAI execution. You can force the WebGenAI execution path using the environment variables shown above. This is normally not necessary.

 

Multi-Team Development

Since the logic is in separate directories, the system is able to support ongoing development in both WebGenAI and your IDE.

Please see Import / Merge GenAI.



Appendices

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.

 

GenAI 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