For the latest stable version, please use Spring Cli 0.8.1!

Guide to AI Commands

Introduction

Large Language Models such at OpenAI’s ChatGPT offer a powerful solution for generating code using AI. ChatGPT is trained not only on Java code, but also on various projects within in the Spring open-source ecosystem. This enables the Spring CLI to enhance applications with functionality tailored to specific use cases beyond what traditional tutorials can offer.

Using a simple command, you can describe the desired functionality, and ChatGPT will generate a comprehensive README.md file that provides step-by-step instructions to achieve your goal. It’s like creating a customized tutorial for your project’s needs.

The CLI is also able to automatically apply the generated instructions to your code base,

To get started, use the following command:

spring ai add <Describe what functionality you want to add here> --preview

Refining the generated solution often requires iterations of modifying the description. The --preview option of the spring ai add command generates the README.md file without modifying your project, allowing you to review the proposed changes.

Once you are satisfied with the generated README.md file, you can apply it to your code base using the following command:

spring guide apply <Name of the guide markdown file>

For further improvements and accuracy, you can ChatGPT to rewrite the description using the --rewrite option:

spring ai add <Description of functionality> --preview --rewrite

The detailed documentation below provides comprehensive information on each command.

As we continue to explore the potential of AI technology, we anticipate refining the results even further to cater to specific versions of Spring projects. Stay tuned for updates!

Example

spring ai add "JPA functionality with an integration test.  Include all Java code in the same package." --preview

Here the README-ai-jpa.md file that was created using the previous command.

Prerequisite

Before using the AI commands, make sure you have a developer token from OpenAI.

Create an account at OpenAI Signup and generate the token at API Keys. Save the secret key in a file named .openai under your home directory (~/.openai).

The file should contain the following:

OPEN_AI_API_KEY=<paste your key here>

Using ai add

The ai add command allows you to add code to your project generated using OpenAI’s ChatGPT.

Provide a brief description of the code you want to add, using the --description option and the command will send the request to OpenAI’s API.

By default, this command modifies your code base.

Example:

spring ai add "JPA functionality"

This command creates the file README-ai-jpa.md and applies the changes outlined in that file to your project.

NOTE: Code generation may take 3-4 minutes.

Previewing the response

When using the --preview option, a README-ai-jpa.md file is generated without making any changes to the project. This allows you to review the file and evaluate whether it provides an acceptable solution.

spring ai add "JPA functionality" --preview

This command creates the file README-ai-jpa.md. Remember, it doesn’t modify your project, but it provides the opportunity to review the content and determine its suitability for your specific requirements.

If the generated instructions meet your needs, you can apply the changes to your code base by using the command spring guide apply README-ai-jpa.md.

Iterating to get what you want

If you examine the file README-ai-jpa.md from the previous step, you will notice that it did not include any tests. It is always recommended to include tests as a best practice, so the provided description should be updated.

This is the general back and forth you will have when working with the ai add command.

For example, in the previous case, the code that was created was missing an integration test. Running the command with the following description, gave me the desired results.

spring ai add "JPA functionality with an integration test.  Include all Java code in the same package." --preview

Here the README-ai-jpa.md file that was created using the previous command.

Rewriting the project description automatically

To obtain better results rom the AI model, you have the option to let the AI model rewrite the project description. Sometimes, developers write descriptions in a shorthand or terse style that may not produce optimal results. Using the --rewrite option allows you to ask the AI model to rewrite the description, providing clearer intent and improving generated code.

Example:

spring ai add "JPA functionality" --preview --rewrite

The description has been rewritten to be: Please provide instructions for creating a Spring Java application that utilizes JPA functionality.

Generating code.  This will take a few minutes ...

Here the README-ai-jpa.md file that was created using the previous command.

Applying the README

Once you are satisfied with the generated README file, apply the changes described in it to your code base using the following command.

spring guide apply README-ai-jpa.md