Guide to AI Commands
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 generates a comprehensive README.md
file that provides step-by-step instructions to achieve your goal.
It is 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, letting you review the proposed changes.
Once you are satisfied with the generated README.md file, you can apply it to your code base by using the following command:
spring guide apply <Name of the guide markdown file>
For further improvements and accuracy, you can get ChatGPT to rewrite the description by using the --rewrite option:
spring ai add <Description of functionality> --preview --rewrite
The detailed documentation in the rest of this page 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. |
Example
Consider the following 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 by using the previous command.
Prerequisite
Before using the AI commands, make sure you have a developer token from OpenAI.
To do so, 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 (usually ~/.openai
).
The file should contain the following:
OPEN_AI_API_KEY=<paste your key here>
Using ai add
The ai add
command lets you add code to your project generated by using OpenAI’s ChatGPT.
To do so, provide a brief description of the code you want to add by using the --description
option and the command will send the request to OpenAI’s API.
By default, this command modifies your code base.
The following listing shows an example:
spring ai add "JPA functionality"
This command creates a file called README-ai-jpa.md and applies the changes outlined in that file to your project.
Code generation may take 3-4 minutes. |
Previewing the Response
When you use the --preview
option, a README-ai-jpa.md
file is generated without making any changes to the project.
This lets you review the file and evaluate whether it provides an acceptable solution.
spring ai add "JPA functionality" --preview
This command creates a file called README-ai-jpa.md. Remember, it does not 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 spring guide apply README-ai-jpa.md
command.
Iterating to Get What You Want
If you examine the file called README-ai-jpa.md from the previous step, you can see that it did not include any tests. We always recommend including tests as a best practice, so the provided description should be updated.
This is the general back and forth you get 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 the desired results.
spring ai add "JPA functionality with an integration test. Include all Java code in the same package." --preview
Here is the README-ai-jpa.md file that was created by 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 lets you ask the AI model to rewrite the description, providing clearer intent and improving the generated code.
Consider the following 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 is the README-ai-jpa.md file that was created by 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 by using the following command.
spring guide apply README-ai-jpa.md