【Spring AI】03. 开始

发布于:2024-04-30 ⋅ 阅读:(25) ⋅ 点赞:(0)

开始(Getting Started)


本节提供了如何开始使用 Spring AI 的起点。您应根据需要按照以下各节中的步骤进行操作。

Spring CLI

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

重要的是要了解“Spring CLI”与“Spring Boot CLI”是一个不同的项目,每个项目都有自己的一组功能。

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

  1. 下载最新的 Spring CLI RELEASE 版本并按照 安装说明 进行操作。
  2. 要创建基于 OpenAI 的简单应用程序,请使用以下命令:
    spring boot new --from ai --name myai
    
  3. 请参阅生成README.md的文件 获取 OpenAI API 密钥和运行第一个 AI 应用程序的指导。

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

spring boot add ai

Spring CLI 允许用户定义自己的项目目录,这些目录定义您可以创建哪些项目或将其添加到现有代码库中。

Spring Initializr

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

添加 Milestone 和 Snapshot Repositories

如果希望手动添加依赖项代码段,请按照以下部分中的说明进行操作。

要使用 Milestone 和 Snapshot 版本,您需要在构建文件中添加对 Spring Milestone 和/或 Snapshot Repositories 的引用。

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

<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,请根据需要添加以下存储库定义:

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 版本决定了使用的依赖项版本。它还可确保默认使用受支持和测试的依赖项版本,除非您选择替代它们。

如果您是 Maven 用户,则可以通过将以下内容添加到 pom.xml 文件中来使用 BOM:

<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+)native 支持来使用 Spring AI BOM,使用 Maven BOM 声明依赖项约束。这是通过将“platform”依赖项添加到 Gradle 构建脚本的依赖项部分来实现的。如下面的代码片段所示,可以为一个或多个spring-ai模块(例如spring-ai-openai)的Starter依赖项进行无版本声明。

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'
}

为特定组件添加依赖

文档中 04章节 的每个部分显示了需要添加到项目生成系统中的依赖项,这里不再一一列举。

示例项目

您可以在 GitHub 上克隆这些项目以开始使用。

OpenAI

  • github.com/rd-1-2022/ai-openai-helloworld

Azure OpenAI

  • github.com/rd-1-2022/ai-azure-openai-helloworld
  • github.com/Azure-Samples/spring-ai-azure-workshop

网站公告

今日签到

点亮在社区的每一天
去签到