当前位置: 首页 > news >正文

【Spring AI】简单入门(一)

【Spring AI】

  • 1. Spring AI简介
    • 1.1 Spring AI是什么
      • 1.1.1 概述
    • 1.2 发布版本
    • 1.3 Spring AI的主要功能
  • 2. 结合DeepSeek上手使用
    • 2.1 准备工作
    • 2.2 DeepSeek开放平台创建API KEY
    • 2.3 代码

1. Spring AI简介

在当今这样一个快速发展的技术时代,人工智能(AI)已经成为各行各业的一种标配。而作为一款主流的Java应用开发框架Spring,肯定会紧跟时代的潮流,所以,推出了Spring AI框架。

1.1 Spring AI是什么

官网 https://spring.io/projects/spring-ai

1.1.1 概述

在这里插入图片描述

Spring AI 是一个面向 AI 工程的应用框架。它的目标是把 Spring 生态系统中的设计原则(例如可移植性、模块化设计等)应用到 AI 领域,并推动在 AI 应用中使用 POJO(普通 Java 对象)作为应用的构建模块。

其核心在于解决 AI 集成的基础性挑战:将你的企业数据(Data)和 API 与 AI 模型连接起来。

特性(Features)
Spring AI 提供以下功能:

  • 支持所有主要的 AI 模型供应商(Model providers),包括 Anthropic、OpenAI、Microsoft、Amazon、Google,以及 Ollama。支持的模型类型有:

    • 聊天补全(Chat Completion)
    • 嵌入(Embedding)
    • 文本生成图像(Text to Image)
    • 音频转录(Audio Transcription)
    • 文本转语音(Text to Speech)
    • 内容审核 / Moderation(Moderation)
  • 对不同 AI 提供商的 API,支持同步(synchronous)和流式(streaming)调用方式,同时也可以访问特定模型(model-specific)的特性。

  • 结构化输出(Structured Outputs) —— 将 AI 模型的输出映射为 POJOs(方便在 Java 环境中处理)

  • 支持主要的向量数据库(Vector Database)提供商(Providers),例如 Apache Cassandra、Azure Vector Search、 Chroma、Milvus、MongoDB Atlas、Neo4j、Oracle、PostgreSQL/PGVector、PineCone、Qdrant、Redis、Weaviate 等等。

  • 在向量存储提供商之间提供可移植 API(Portable API),包括一个类 SQL 的元数据过滤 API(metadata filter API)。

  • 支持工具 / 函数调用(Tools / Function Calling)——即模型可以请求执行客户端的工具或函数,以便在必要时访问实时的信息。

  • 可观测性(Observability) — 提供 AI 相关操作的可监控、可视化洞见。

  • 文档注入(Document injection)以及用于数据工程的 ETL 框架。

  • AI 模型评估(AI Model Evaluation) — 包括用于评估生成内容的工具,并帮助防止“幻觉”(hallucinated responses)的问题。

  • ChatClient API — 用于与 AI 聊天模型通信的 Fluent API,其风格类似于 Spring 的 WebClient 和 RestClient。

  • Advisors API — 封装了常用的生成式 AI 模式,负责变换语言模型(LLM)之间或发送/接收的内容,并为不同模型和用例提供可移植性。

  • 支持聊天对话记忆(Chat Conversation Memory)和检索增强生成(Retrieval Augmented Generation, RAG)等功能。

  • Spring Boot 的自动配置(Auto Configuration)和 Starter(启动器)支持所有 AI 模型和向量存储 —— 你可以用 Spring Initializr 来选择你想用的模型或向量存储。

这些功能组合起来,使你可以实现诸如 “在你的文档上做问答 (Q&A over your documentation)” 或 “与你的文档进行聊天 (Chat with your documentation)” 这样的常见用例。

1.2 发布版本

写这篇文章的时间是2025年09月20日,可以看到已经有了

  • 1.0.1 Current (当前稳定版)
  • 1.1.0-SNAPSHOT(开发中的快照版本)
  • 1.1.0-M1(预发布版本 Milestone 1)

在这里插入图片描述

1.3 Spring AI的主要功能

  • 第一、 对主流 AI 大模型供应商提供了支持,比如:OpenAI、DeepSeek、Microsoft、Ollama、Amazon、Google HuggingFace等。
  • 第二、 支持AI大模型类型包括:聊天、文本到图像、文本到声音等。
  • 第三、 支持主流的Embedding Models(嵌入模型)和向量数据库,比如:Azure Vector Search、Chroma、Milvus、Neo4j、Redis、PineCone、PostgreSQL/PGVector 等。
  • 第四、 把 AI 大模型输出映射到简单的 Java 对象(POJOs)上。
  • 第五、 支持了函数调用(Function calling)功能。
  • 第六、 为数据工程提供 ETL(数据抽取、转换和加载)框架。
  • 第七、 支持 Spring Boot 自动配置和快速启动,便于运行 AI 模型和管理向量库。

2. 结合DeepSeek上手使用

2.1 准备工作

DeepSeek 是一款由深度求索所开发的 AI 人工智能大模型,其基于深度学习和多模态数据融合技术,采用先进的 Transformer 架构和跨模态协同算法,可实现对复杂文档和图像的自动化解析与结构化信息提取。

依托于最新推出的“深度思考”模式(R1),这款AI大模型在极低成本下实现了与国际顶尖模型ChatGPT-o1相媲美的性能表现,其中文理解与输出能力更是远超ChatGPT、Claude等顶尖模型。再加上极具竞争力的API定价和全面开源的策略,让这款AI大模型成功在国际上火爆出圈

如果说AI是一个广泛的概念,那么DeepSeek就是是AI领域中的一个具体产品。

DeepSeek的特点‌:

  • 成本‌:DeepSeek致力于降低AI应用的成本。通过采用先进的技术和独特的模型架构,DeepSeek在保持高性能的同时,显著降低了推理和训练的成本。
  • 性能‌:DeepSeek在性能上表现出色。它使用强化学习技术训练,推理过程中包含大量反思与验证,能够处理更加复杂的数据和任务。在一些benchmark测试中,其性能与OpenAI的模型相当,但推理成本远低于同类产品。
  • 功能‌:DeepSeek擅长处理数学、编程和复杂逻辑推理等任务。它的推理能力源于深度思考特性,推理长度与准确率呈正相关。此外,DeepSeek还支持多模态信息处理,能够应对更加多样化的应用场景。
  • 应用领域‌:DeepSeek在多个领域展现出巨大的应用潜力。无论是在医疗、教育、交通等传统领域,还是在智能制造、智慧城市等新兴领域,DeepSeek都有望发挥重要作用。

综上所述,AI是一个广泛的概念,涵盖了人工智能领域的所有技术和应用。而DeepSeek则是AI领域中的一个具体产品,它在成本、性能、功能和应用领域等方面都有着独特的特点和优势。两者之间的关系可以理解为:DeepSeek是AI领域中的一个具体实现和优秀代表。

如何使用Java集成DeepSeek:

DeepSeek 作为一款卓越的国产 AI 模型,越来越多的公司考虑在自己的应用中集成。对于 Java 应用来说,我们可以借助 Spring AI 集成 DeepSeek,非常简单方便!

2.2 DeepSeek开放平台创建API KEY

DeepSeek官网 https://www.deepseek.com/
接口文档 https://api-docs.deepseek.com/zh-cn/

在官网上创建api key
在这里插入图片描述
使用curl调用

(base) ➜  ~ curl https://api.deepseek.com/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer sk-8c780d3293d94b799d7b8000972xxxxx" \-d '{"model": "deepseek-chat","messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello!"}],"stream": false}'
{"id": "3ec31844-c3da-466b-b286-xxx0b90bcf83","object": "chat.completion","created": 1758340902,"model": "deepseek-chat","choices": [{"index": 0,"message": {},"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 12,"completion_tokens": 11,"total_tokens": 23,"prompt_tokens_details": {},"prompt_cache_hit_tokens": 0,"prompt_cache_miss_tokens": 12},"system_fingerprint": "fp_08f168e49b_xxxxxx_fp8_kvcache"
}

2.3 代码

新建一个Maven项目
在这里插入图片描述

其中父类的pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.beijing</groupId><artifactId>SpringAi</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><!-- 子模块 --><modules><module>springai_hello</module></modules><properties><spring-boot.version>3.3.8</spring-boot.version><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-ai.version>1.0.1</spring-ai.version></properties><!-- 版本管理 --><dependencyManagement><dependencies><!-- Spring Boot 依赖版本管理 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- Spring AI 依赖版本管理 --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring-ai.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- 公共依赖(子模块都会继承) --><dependencies><!-- Web Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai</artifactId></dependency><!-- Test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

子类springai_hello
application.yml

server:port: 18888spring:application:name: springai_helloai:openai:api-key: sk-8cxxxd3293d94b799d7b8000972xxxxbase-url: https://api.deepseek.comchat:options:model: deepseek-chattemperature: 0.7

temperature参数用于控制生成文本的多样性。具体来说:
‌值越高‌,生成的文本越多样化,但也可能包含更多的随机性和不可预测的内容。 ‌值越低‌,生成的文本越接近于确定性的结果,即生成的文本会更加一致和可预测。

子类的pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.beijing</groupId><artifactId>SpringAi</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>springai_hello</artifactId><properties><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-openai</artifactId></dependency><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.5.0</version></dependency></dependencies></project>

在这里插入图片描述
配置文件
ChatModelConfig

package com.config;import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ChatModelConfig {/*** 配置 ChatClient(用于对话调用)*/@Beanpublic ChatClient chatClient(OpenAiChatModel openAiChatModel) {return ChatClient.builder(openAiChatModel).build();}
}

Controller

package com.controller;import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ChatController {@Resourceprivate ChatClient chatClient;@GetMapping("/hello")public String generate(@RequestParam(value = "message") String message) {String call = chatClient.prompt(message).call().content();return call;}
}
http://www.dtcms.com/a/391968.html

相关文章:

  • Java中接口入参验证
  • 【高并发内存池——项目】central cache 讲解
  • vue3 <el-image 的:src=“event.fileName[0]“ 长度为 “0“ 的元组类型 “[]“ 在索引 “0“ 处没有元素。
  • 问题记录: 跨服务接口调用日期类型字段格式转换问题
  • 亚马逊关键词按什么角度筛选?从人工摸索到智能化系统的全面升级
  • C语言基础【19】:指针6
  • 正则表达式【阿里版】
  • 使用云端GPU训练Lerobot
  • RNA-seq分析之基因ID转换
  • [视图功能9] 图表联动与多维度分析:打造协同动态的数据洞察仪表盘
  • Python基础 6》数据类型_列表(List)
  • 40、大模型工程平台全景对比 - 技术选型指南
  • BEVformer训练nusenes-mini数据集
  • 《Unity3D NavMeshAgent与Rigidbody移动同步问题的技术拆解》
  • Psy Protocol 技术核心解读
  • PS练习3:使用变形将图片放到实际场景中
  • 在排序数组中查找元素的第一个和最后一个位置
  • 一条命令在ubuntu安装vscode
  • 【开题答辩全过程】以 ASP.NET抗疫物资管理系统为例,包含答辩的问题和答案
  • 探饭 - 字节跳动推出的AI美食推荐助手
  • ZCC5515_耐压9.5V ,超低静态功耗5uA,完全替代CS5515
  • 端脑云AI生图体验:从提示词到精美肖像
  • 临界处有一条看不见的河
  • JavaWeb--day8-- Mybatis(正式)
  • 基于WSL BES2710编译环境搭建方法
  • 模块化设计逻辑:太阳镜气流单元 / 耳机可拆卸结构的装配精度与兼容性分析
  • 半监督学习实战:如何用少量标注数据获得媲美全监督学习的性能?
  • 作业3(初学CSS)
  • CSS基础(总结)
  • 【信创云架构 PACS系统】全网首发-基于JDK17+Vue3全新开发的信创国产化系统