从 0 到 1 学 Spring AI 1.0 GA:Java 工程师的 AI 开发入门实战

Spring AI 是 Spring 生态系统中的新兴框架,专为 Java 开发者设计,用于轻松集成人工智能模型(如大型语言模型)。它于 1.0 GA 版本发布,标志着稳定性和生产就绪性。本教程将引导你从零开始,掌握 Spring AI 的核心用法,并构建一个简单的文本生成应用。无论你是 AI 新手还是有经验的 Java 工程师,都能通过本指南快速上手。文章结构清晰,分步讲解,确保你理解每个环节。

1. Spring AI 简介与背景

Spring AI 旨在简化 AI 模型在 Java 应用中的集成,支持多种模型提供者(如 OpenAI、Hugging Face)。它基于 Spring Boot 构建,提供熟悉的依赖注入和配置方式。1.0 GA 版本引入了关键特性,如统一的 API 接口、模型抽象层和自动配置。为什么 Java 工程师需要学习它?AI 正改变软件开发,Spring AI 让你无需深入底层细节,就能为应用添加智能功能,例如聊天机器人、内容生成或数据分析。

2. 准备工作:设置开发环境

在开始编码前,确保你的开发环境已就绪。你需要:

  • JDK 17 或更高版本(Spring AI 依赖 Java 17+)。
  • Maven 或 Gradle 作为构建工具(本教程使用 Maven)。
  • IDE 如 IntelliJ IDEA 或 Eclipse。

添加 Spring AI 依赖到 Maven 项目。打开 pom.xml 文件,添加以下内容:

<dependencies>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        <version>1.0.0</version> <!-- 使用 Spring AI 1.0 GA 版本 -->
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

运行 mvn clean install 安装依赖。现在,环境已配置好,我们可以进入核心概念。

3. Spring AI 核心概念

Spring AI 的核心是模型抽象层,主要包括:

  • 模型(Model):代表 AI 模型实例,如 OpenAI 的 GPT-4。
  • 提示(Prompt):输入给模型的请求,通常包含文本指令。
  • 输出(Output):模型的响应,可以是文本、JSON 或其他格式。

这些组件通过 Spring 的依赖注入管理。例如,定义一个提示模板:

import org.springframework.ai.prompt.PromptTemplate;

public class AiService {
    private final PromptTemplate promptTemplate;

    public AiService() {
        this.promptTemplate = new PromptTemplate("请生成一段关于{主题}的简短描述。");
    }

    public String generateText(String topic) {
        return promptTemplate.create(topic).getText();
    }
}

这里,PromptTemplate 使用占位符 {主题} 动态生成提示。Spring AI 自动处理模型调用。

4. 入门示例:构建一个简单的文本生成器

让我们实现一个“Hello World”应用:输入主题,输出 AI 生成的描述。首先,创建 Spring Boot 主类。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringAiDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringAiDemoApplication.class, args);
    }

    @Bean
    public AiService aiService() {
        return new AiService(); // 注入自定义服务
    }
}

接下来,创建一个 REST 控制器来暴露 API:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AiController {

    @Autowired
    private AiService aiService;

    @GetMapping("/generate")
    public String generateDescription(@RequestParam String topic) {
        return aiService.generateText(topic);
    }
}

运行应用后,访问 http://localhost:8080/generate?topic=科技,AI 将返回类似:“科技是推动社会进步的关键力量,涉及创新和数字化。” 的文本。这展示了 Spring AI 的基本集成。

5. 进阶实战:构建 AI 聊天机器人

现在,升级到更实用的场景:一个简单的聊天机器人。我们将使用 OpenAI 模型(需 API 密钥)。首先,配置 application.properties 文件:

spring.ai.openai.api-key=你的OpenAI密钥

然后,扩展服务类处理对话:

import org.springframework.ai.chat.ChatClient;
import org.springframework.stereotype.Service;

@Service
public class ChatService {

    private final ChatClient chatClient;

    public ChatService(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    public String chat(String message) {
        return chatClient.generate(message).getText();
    }
}

更新控制器添加聊天端点:

@RestController
public class ChatController {

    @Autowired
    private ChatService chatService;

    @GetMapping("/chat")
    public String handleChat(@RequestParam String message) {
        return chatService.chat(message);
    }
}

启动应用后,发送请求如 http://localhost:8080/chat?message=你好,介绍一下Spring AI,AI 将返回详细解释。这实现了可扩展的聊天功能,适合嵌入到 Web 或移动应用中。

6. 总结与下一步

通过本教程,你已掌握了 Spring AI 1.0 GA 的入门基础:从环境设置到核心概念,再到实际应用构建。关键收获包括:

  • 使用 Spring Boot 轻松集成 AI 模型。
  • 通过统一 API 简化提示和输出处理。
  • 实战中构建了文本生成器和聊天机器人。

Spring AI 还在进化,建议探索官方文档,尝试更多模型(如 Hugging Face)或高级特性如流式响应。作为 Java 工程师,掌握 Spring AI 将提升你的技能组合,助你在 AI 驱动的开发中领先。动手实践吧,遇到问题可查阅社区资源或继续学习!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐