Guide to AI Commands

OpenAI 的 ChatGPT 等大型语言模型提供了一种使用 AI 生成代码的强大解决方案。ChatGPT 不仅接受过 Java 代码的训练,还接受过 Spring 开源生态系统中各种项目的训练。这使得 Spring CLI 能够使用专门针对特定用例的功能来增强应用程序,这是传统教程无法提供的。

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.

使用一个简单的命令,您可以描述所需的特性,ChatGPT 就会生成一个全面的 README.md 文件,其中提供了实现目标的分步说明。它就像为您的项目需求创建自定义教程一样。

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.

CLI 还可以将生成的指令自动应用于您的代码库。

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

改进生成的解决方案通常需要迭代修改说明。spring ai add 命令的 --preview 选项在不修改您的项目的情况下生成 README.md 文件,让您查看建议的更改。

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.

在对生成的 README.md 文件感到满意后,你可以使用以下命令应用到代码库中:

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>

为了进一步改进并提高准确性,你可以让 ChatGPT 使用 --rewrite 选项来重写描述:

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.

随着我们不断探索人工智能技术的潜力,我们预期将进一步细化结果,以迎合 Spring 项目的特定版本。

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

这里使用上一个命令创建的 README-ai-jpa.md 文件。

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

Prerequisite

在使用 AI 命令之前,请确保你拥有来自 OpenAI 的开发者令牌。

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

为此,在 OpenAI Signup 创建一个帐户,并在 API Keys 中生成令牌。将密钥保存在你的主目录(通常为 ~/.openai)下的名为 .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

“ai add”命令可以让你将使用 OpenAI 的 ChatGPT 生成的代码添加到你的项目。

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

为此,请使用 “--description” 选项提供你想要添加代码的简要说明,并且该命令将把请求发送到 OpenAI 的 API。

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 showss an example:

spring ai add "JPA functionality"

此命令会创建一个名为 README-ai-jpa.md 的文件,并将该文件概述的更改应用到你的项目。

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

代码生成可能需要 3-4 分钟。

Code generation may take 3-4 minutes.

Previewing the Response

当你使用 “--preview” 选项时,将生成一个 README-ai-jpa.md 文件,而不会对项目进行任何更改。这可让你审阅文件并评估它是否提供了一个可以接受的解决方案。

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

此命令会创建一个名为 README-ai-jpa.md 的文件。请记住,它不会修改你的项目,但它提供了一个机会来审查内容并确定其是否适合你的特定需求。

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.

如果生成的说明符合你的需求,则可以使用“spring guide apply README-ai-jpa.md”命令将更改应用到你的代码库。

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

如果你检查上一步中称作 README-ai-jpa.md的文件,你可以看到它没有任何测试。我们总建议将测试作为最佳实践包括进去,所以应更新提供的说明。

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.

这是当你使用 “ai add” 命令时,收到的常规反复信息。

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

这是使用上一个命令创建的 README-ai-jpa.md文件。

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

Rewriting the Project Description Automatically

为了从 AI 模型中获得更好的结果,你可以选择让 AI 模型重写项目说明。有些时候,开发人员会使用简写或简洁的风格撰写说明,这可能不会产生最佳结果。使用 “--rewrite” 选项可以让你要求 AI 模型重写说明,从而提供明确的意图并改进生成的代码。

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 ...

这是使用上一个命令创建的 README-ai-jpa.md文件。

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

Applying the README

在您对生成的文件 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