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

Spring AI整合聊天模型DeepSeek

参考资料:

参考视频

视频对应的资料,包括MD文件

SpringBoot搭建教程

参考demo及学习笔记


说明:

1. JDK及SpringBoot版本要求

        搭建的时候记得选用JDK17+,不用系统安装,用IDEA下载的也可以

        SpringBoot版本要求3.2.x或者3.3.x

2. DeepSeek的key

        关于申请DeepSeek这里就不赘述了,网上一搜一大堆,申请地址


搭建流程:

提示:下面只讲述怎么配置,具体含义请自行进行查阅

依赖和配置

        结合自己申请的DeepSeek的Key添加如下配置

server.port=8899
spring.application.name=SpringAiChatDemospring.ai.openai.api-key=sk-139298b9e929496290******
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat
spring.ai.openai.chat.options.temperature=0.7

        添加如下依赖及版本管理

<properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><spring-ai.version>1.0.0-M5</spring-ai.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><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>

简单的请求接口(不推荐)


import org.springframework.ai.openai.OpenAiChatModel;
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 SimpleController {@Autowiredprivate OpenAiChatModel chatModel;@GetMapping("/ai/generate")public String generate(@RequestParam(value = "message", defaultValue = "hello")String message) {String response = this.chatModel.call(message);System.out.println("response : "+response);return response;}
}

        postman测试

构造注入式(不推荐)

        构造注入式是直接从SpringBeanFactory里面拿,不推荐这种方式


/*** 构造注入示例 (不推荐)*/
@RestController
public class ConstructController {private final ChatClient chatClient;public ConstructController(ChatClient.Builder chatClientBuilder) {this.chatClient = chatClientBuilder.build();}@GetMapping("/construct/chat")public String chat(@RequestParam(value = "msg",defaultValue = "给我讲个笑话")String message) {//prompt:提示词return this.chatClient.prompt()//用户输入的信息.user(message)//请求大模型.call()//返回文本.content();}
}

Spring自定义注入(推荐)

  • 先进行ChatClient注入和预设置

        下面这个设置了默认角色,也可以不设置默认角色


@Configuration
public class AIConfig {@Beanpublic ChatClient chatClient(ChatClient.Builder builder) {return builder.defaultSystem("你将作为一名Java开发语言的专家,对于用户的使用需求作出解答").build();}}

  • 然后使用初始化好的ChatClient对象
/*** Spring 自定义注入示例*/
@RestController
public class SpringConfigController {@Autowiredprivate ChatClient chatClient;@GetMapping("/springChat")public String chat(@RequestParam(value = "msg") String message) {return chatClient.prompt().user(message).call().content();}}
  • 访问查看结果

流式响应

        上述使用的是非流式,流式响应就是每生成一个字段就返回,此处不再赘述。

        

/*** Spring 流式响应*/
@RestController
public class StreamController {@Autowiredprivate ChatClient chatClient;@GetMapping(value = "/chat/stream",produces="text/html;charset=UTF-8")public Flux<String> chatStream(@RequestParam(value = "msg") String message) {return chatClient.prompt().user(message).stream().content();}}

基于chatModel的简单对话(不推荐)

@RestController
public class ChatModelController {@Autowiredprivate ChatModel chatModel;@GetMapping("/simpleChat")public String chat(@RequestParam("msg")String msg) {return chatModel.call(msg);}@GetMapping("/openai")public String openai(@RequestParam("msg")String msg) {ChatResponse call = chatModel.call(new Prompt(msg,OpenAiChatOptions.builder()//可以更换成其他大模型,如Anthropic3ChatOptions亚马逊.model("deepseek-chat").temperature(0.8).build()));return call.getResult().getOutput().getContent();}
}

基于ChatModel使用提示语模板


@RestController
public class ChatModelTemplateController {@Autowiredprivate ChatModel chatModel;@GetMapping("/prompt")public String prompt(@RequestParam("name")String name,@RequestParam("voice")String voice){String userText= """给我推荐北京的至少三种美食""";UserMessage userMessage = new UserMessage(userText);String systemText= """你是一个美食咨询助手,可以帮助人们查询美食信息。你的名字是{name},你应该用你的名字和{voice}的饮食习惯回复用户的请求。""";SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);//替换占位符Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "voice", voice));Prompt prompt = new Prompt(List.of(userMessage, systemMessage));List<Generation> results = chatModel.call(prompt).getResults();return results.stream().map(x->x.getOutput().getContent()).collect(Collectors.joining(""));}}


SpringAI自动调用自定义方法---对AI结果进行处理(函数调用):

        例如,用SpringAI自动对算术运算的语句进行解析,并且输出结果

  • 首先进行自定义方法的编写和注入

@Configuration
public class CalculatorService {public record AddOperation(int a, int b) {}public record MulOperation(int m, int n) {}@Bean@Description("加法运算")public Function<AddOperation, Integer> addOperation() {return request -> {System.out.println("调用执行加法运算");return request.a + request.b;};}@Bean@Description("乘法运算")public Function<MulOperation, Integer> mulOperation() {return request -> {System.out.println("调用执行乘法运算");return request.m * request.n;};}
}
  • 然后调用使用

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CalculatorController {@Autowiredprivate ChatModel chatModel;@GetMapping(value = "/calculator", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)public String ragJsonText(@RequestParam(value = "userMessage") String userMessage) {return ChatClient.builder(chatModel).build().prompt().system("""您是算术计算器的代理。您能够支持加法运算、乘法运算等操作,其余功能将在后续版本中添加,如果用户问的问题不支持请告知详情。在提供加法运算、乘法运算等操作之前,您必须从用户处获取如下信息:两个数字,运算类型。请调用自定义函数执行加法运算、乘法运算。请讲中文。""").user(userMessage).functions("addOperation", "mulOperation").call().content();}
}
  • 测试

  • 说明

程序自动调用,自定义的方法,并返回,关键在于提示语的设计,需要反复进行测试才行


SpringAI调用本地RAG数据进行问答:

       SpringAI的Rag功能见Spring Ai Alibaba的文章。


SpringAI的其它功能

        SpringAI的其他功能(如:图像、音视频)见Spring Ai Alibaba的文章

http://www.dtcms.com/a/424146.html

相关文章:

  • 做国外网站做什么内容好无锡市城市建设规划局网站
  • 软装设计网站推荐湖南畅想网站建设
  • 手机电影网站怎么做wordpress邮箱登录
  • 圆桌对话 | “内卷”与“出海”:中国智能网联汽车的破局之路与未来生态构想
  • 网站建设开头wordpress go
  • 门户子网站建设申请微页制作平台网站建设
  • linux拷贝命令
  • 遥感目标检测数据集汇总,覆盖城市问题/工业安全/农业健康/室内场景……
  • LeetCode 刷题【92. 反转链表 II】
  • Spring Boot 异步处理框架核心源码解析及实现原理
  • 三只松鼠网站怎样做p2p网站开发多少钱
  • C++(day4)
  • GESP2025年9月C++三级编程题日历制作题解参考
  • 网站建设与管理实训心得怎么写2023热点新闻事件
  • 做网站很火的APP来个网站吧好人一生平安百度贴吧
  • 花未全开月未圆:布隆过滤器的留白美学
  • 网站安全措施优秀个人网页
  • Flutter完整开发指南 | FlutterDart – The Complete Guide
  • 营销型企业网站系统模板下载建设网站前期准备工作
  • Android车机系统上电获取历史定位信息异常分析
  • 建设银行征信中心官方网站企业网站建设与推广多少钱
  • 如何利用开源库和安全芯片设计fido令牌
  • 全国信息网查询平台知名seo公司
  • 加强网站信息内容建设的意见最新网站制作公司哪个好
  • 橙域名网站网站开发天津网站开发
  • 学校网站推广方案凡科网h5
  • 网站建设xiu021网站域名备案注册证书查询
  • 建设网站政策风险百度推广优化师是什么
  • 深入理解MySQL_1 MySQL系统架构
  • C++之友元函数与前向引用