springAI学习 一

一、Spring AI 概述

什么是Spring AI?

  • Spring生态的AI集成框架

  • 统一API访问不同AI服务(OpenAI、Azure OpenAI、Anthropic等)

  • 支持多种AI功能:聊天、文生图、嵌入、向量存储等

Spring AI 是一个用于 AI 工程的应用框架。 其目标是将 Spring 生态系统设计原则应用于 AI 领域,如可移植性和模块化设计,并推广将 POJO 作为应用构建模块到人工智能领域的应用。

二、学习资源

官方资源

  1. 文档spring.io/projects/spring-ai

  2. 参考文档:Spring AI Reference Documentation

  3. 示例项目GitHub - spring-ai/spring-ai-samples

实践建议

  1. 从OpenAI开始:先用OpenAI API熟悉基本概念

  2. 尝试本地模型:使用Ollama运行本地LLM

  3. 实现RAG应用:构建文档问答系统

  4. 集成到现有项目:为已有Spring Boot项目添加AI能力

三、核心概念

1. AI Models

  • 聊天模型:ChatClient、ChatModel

  • 嵌入模型:EmbeddingClient、EmbeddingModel

  • 图像模型:ImageClient、ImageModel

  • 语音模型:AudioClient、AudioModel

2. Prompt Engineering

java 复制代码
PromptTemplate template = new PromptTemplate("请用{style}风格解释:{topic}");
Prompt prompt = template.create(Map.of("style", "简单易懂", "topic", "机器学习"))

3. 输出解析

java 复制代码
@Parser(description = "天气预报响应")
public class WeatherResponse {
    private String city;
    private double temperature;
    private String condition;
}

四、快速开始

1. 添加依赖

XML 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>0.8.1</version>
</dependency>

2. 基础配置

XML 复制代码
spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      chat:
        model: gpt-3.5-turbo
        # model: gpt-4.1-mini

3. 基本使用示例

java 复制代码
@Service
public class ChatService {
    private final ChatClient chatClient;
    
    public String chat(String message) {
        return chatClient.call(message);
    }
    
    // 流式响应
    public Flux<String> streamChat(String message) {
        return chatClient.stream(message)
            .map(ChatResponse::getOutput);
    }
}

四、核心功能详解

1. 聊天功能

java 复制代码
// 带上下文对话
List<Message> messages = new ArrayList<>();
messages.add(new UserMessage("你好"));
messages.add(new AssistantMessage("你好!有什么可以帮助你?"));
messages.add(new UserMessage("Java是什么?"));

ChatResponse response = chatClient.call(new Prompt(messages));
复制代码

2. 函数调用(Function Calling)

java 复制代码
@Bean
public FunctionCallback weatherFunction() {
    return FunctionCallbackWrapper.builder(new WeatherService())
        .withName("getWeather")
        .withDescription("获取城市天气")
        .build();
}
复制代码

3. RAG(检索增强生成)

java 复制代码
// 1. 创建向量存储
VectorStore vectorStore = new InMemoryVectorStore(embeddingModel);

// 2. 添加文档
vectorStore.add(List.of(
    new Document("Spring AI是Spring官方AI框架", Map.of("source", "docs"))
));

// 3. 检索相关文档
List<Document> docs = vectorStore.similaritySearch(request);

// 4. 增强提示
String enhancedPrompt = "基于以下信息:" + docs + "\n问题:" + question;
复制代码

4. 多模态支持

java 复制代码
// 图像生成
ImagePrompt prompt = new ImagePrompt("一只可爱的猫");
ImageResponse response = imageClient.call(prompt);

// 语音转文字
AudioPrompt audioPrompt = new AudioPrompt(audioFile);
AudioTranscription transcription = audioClient.call(audioPrompt);

五、支持的AI服务

主要Provider:

  • OpenAI:GPT、DALL-E

  • Azure OpenAI:Azure上的OpenAI服务

  • Anthropic:Claude模型

  • Google Vertex AI:Gemini模型

  • Hugging Face:开源模型

  • 本地模型:Ollama、LM Studio等




相关推荐
失败才是人生常态6 小时前
并发编程场景题学习
学习
菜鸟‍6 小时前
【论文学习】Co-Seg:互提示引导的组织与细胞核分割协同学习
人工智能·学习·算法
老华带你飞6 小时前
出行旅游安排|基于springboot出行旅游安排系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring·旅游
YJlio6 小时前
Active Directory 工具学习笔记(10.14):第十章·实战脚本包——AdExplorer/AdInsight/AdRestore 一键化落地
服务器·笔记·学习
东华万里6 小时前
第十四篇 操作符详讲
c语言·学习·大学生专区
m0_740043736 小时前
SpringBoot快速入门01- Spring 的 IOC/DI、AOP,
spring boot·后端·spring
nwsuaf_huasir6 小时前
深度学习2-pyTorch学习-张量基本操作
pytorch·深度学习·学习
d111111111d6 小时前
江协科技-PID基本原理-(学习笔记)-主页有所有STM32外设的笔记基本都是万字起步。
笔记·科技·stm32·单片机·嵌入式硬件·学习
微尘hjx6 小时前
【目标检测软件 01】YOLO识别软件功能与操作指南
人工智能·测试工具·yolo·目标检测·计算机视觉·ai·pyqt