Getting Started

本部分提供了有关如何开始使用 Spring AI 的切入点。

This section offers jumping off points for how to get started using Spring AI.

你应根据自己的需要遵循以下每个部分中的步骤。

You should follow the steps in each of the following section according to your needs.

Spring CLI

Spring CLI简化了直接从终端创建新应用程序的过程。对于熟悉 JavaScript 生态系统的用户来说,就像’create-react-app' 命令,Spring CLI 提供了 `spring boot new`命令来创建基于 Spring 的项目。Spring CLI 还提供将外部代码库集成到当前项目的功能,以及许多其他生产力功能。

The Spring CLI, simplifies creating new applications directly from your terminal. Like the 'create-react-app' command for those familiar with the JavaScript ecosystem, Spring CLI provides a spring boot new command to create Spring-based projects. Spring CLI also offers features to integrate external code bases into your current project, and many other productivity features.

重要的是要理解,“Spring CLI”是一个独立的项目,与“Spring Boot CLI”不同,它们各自有一组独特的功能。

It is important to understand that the "Spring CLI" is a distinct project from the "Spring Boot CLI", each with its own set of functionalities.

要开始创建 Spring AI 应用程序,请按照以下步骤操作:

To begin creating a Spring AI application, follow these steps:

  1. Download the latest Spring CLI Release and follow the installation instructions.

  2. To create a simple OpenAI-based application, use the command:[source, shell]

spring boot new --from ai --name myai
  1. Consult the generated README.md file for guidance on obtaining an OpenAI API Key and running your first AI application.

若要将同一个简单 AI 应用程序添加到一个*现有的*项目,请执行:

To add the same simple AI application to an existing project, execute:

spring boot add ai
  1. Spring CLI 允许用户定义他们自己的 project catalogs,这些定义可以用来定义哪些项目可以创建或添加到你现有的代码库。

Spring CLI allows users to define their own project catalogs that define which projects you can create or add to your existing code base.

Spring Initializr

进入 start.spring.io,选择要在新应用程序中使用的 AI 模型和向量存储。

Head on over to start.spring.io and select the AI Models and Vector Stores that you want to use in your new applications.

Add Milestone and Snapshot Repositories

如果你希望手工添加依赖项代码段,请遵循以下部分中的说明。

If you prefer to add the dependency snippets by hand, follow the directions in the following sections.

要使用里程碑和快照版本,你需要在你的构建文件中添加对 Spring 里程碑和/或快照存储库的引用。

To use the Milestone and Snapshot version, you need to add references to the Spring Milestone and/or Snapshot repositories in your build file.

对于 Maven,根据需要添加以下存储库定义:

For Maven, add the following repository definitions as needed:

  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
  </repositories>

对于 Gradle,根据需要添加以下存储库定义:

For Gradle, add the following repository definitions as needed:

repositories {
  mavenCentral()
  maven { url 'https://repo.spring.io/milestone' }
  maven { url 'https://repo.spring.io/snapshot' }
}

Dependency Management

Spring AI 物料清单 (BOM) 声明了 Spring AI 给定版本使用的所有依赖项的推荐版本。在你应用程序的构建脚本中使用 BOM 无需你自己指定和维护依赖项版本。相反,你使用的 BOM 版本决定了所利用的依赖项版本。它还确保了你默认情况下使用依赖项的受支持和经过测试的版本,除非你选择覆盖它们。

The Spring AI Bill of Materials (BOM) declares the recommended versions of all the dependencies used by a given release of Spring AI. Using the BOM from your application’s build script avoids the need for you to specify and maintain the dependency versions yourself. Instead, the version of the BOM you’re using determines the utilized dependency versions. It also ensures that you’re using supported and tested versions of the dependencies by default, unless you choose to override them.

如果你是一个 Maven 用户,你可以通过将以下内容添加到你的 pom.xml 文件中来使用 BOM -

If you’re a Maven user, you can use the BOM by adding the following to your pom.xml file -

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>0.8.1-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Gradle 用户还可以通过使用 Gradle (5.0+) 固有支持来声明依赖项约束(使用 Maven BOM)来使用 Spring AI BOM。这是通过向你的 Gradle 构建脚本的依赖项部分添加一个 'platform' 依赖项处理程序方法来实现的。如下面的代码段所示,这之后可以紧跟着对你想要使用的模块 starter 依赖项的无版本声明,例如:spring-ai-openai。

Gradle users can also use the Spring AI BOM by leveraging Gradle (5.0+) native support for declaring dependency constraints using a Maven BOM. This is implemented by adding a 'platform' dependency handler method to the dependencies section of your Gradle build script. As shown in the snippet below this can then be followed by version-less declarations of the Starter Dependencies for the one or more spring-ai modules you wish to use, e.g. spring-ai-openai.

dependencies {
  implementation platform("org.springframework.ai:spring-ai-bom:0.8.1-SNAPSHOT")
  // Replace the following with the starter dependencies of specific modules you wish to use
  implementation 'org.springframework.ai:spring-ai-openai'
}

Add dependencies for specific components

文档中以下每个部分都显示了你需要将哪些依赖项添加到你的项目构建系统中。

Each of the following sections in the documentation shows which dependencies you need to add to your project build system.

Chat Models

Vector Databases

Sample Projects

你可以克隆这些项目,然后在 GitHub 上开始使用。

You can clone these projects on GitHub to get started.