最新版本SpringAI接入DeepSeek大模型,并集成Mybatis

当时集成这个环境依赖冲突,搞了好久,分享一下依赖配置

XML 复制代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>spring.ai.example</groupId>
	<artifactId>spring-ai-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-ai-demo</name>
	<description>Spring AI , getting started example, using Open AI</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
		<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>

		<!--引入 MybatisPlus 依赖-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
			<version>3.5.5</version>
		</dependency>
		<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.30</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.83</version> <!-- 确保使用最新版本 -->
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.redisson</groupId>
			<artifactId>redisson-spring-boot-starter</artifactId>
			<version>3.18.0</version>
		</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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

在spring配置文件加上

XML 复制代码
spring.ai.openai.api-key=自己的api-key
spring.ai.openai.base-url=https://api.lkeap.cloud.tencent.com/v1
spring.ai.openai.chat.options.model=deepseek-r1
java 复制代码
@RestController()
@RequestMapping("/ai")
public class AIController {

    private final OpenAiChatModel openAiChatModel;
    private final ChatClient chatClient;

    public AIController(OpenAiChatModel openAiChatModel) {
        this.openAiChatModel = openAiChatModel;
        this.chatClient = ChatClient
                .builder(openAiChatModel)
                .defaultAdvisors(new LoggingAdvisor()).build();
    }

    /**
     * 同步方式实现聊天功能
     *
     * @param prompt 提示词
     * @return 聊天结果
     */
    @GetMapping("/chat")
    public String chat(@RequestParam String prompt) {
        return openAiChatModel.call(prompt);
    }


    /***
     * 流式方式实现聊天功能
     * @param prompt 提示词
     * @return 聊天结果流
     */
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(@RequestParam String prompt) {
        return openAiChatModel.stream(prompt);
    }
}

通过postman进行测试

http://localhost:8080/ai/chat?prompt=你是谁?

返回结果说明调用成功!

相关推荐
代码不停9 小时前
MyBatis操作数据库
数据库·mybatis
wno70410 小时前
Spring Boot中JdbcTemplate与Mybatis使用PostgreSQL
spring boot·postgresql·mybatis
lzhdim1 天前
100条常用SQL语句
数据库·sql·mysql·mybatis
景熙55231 天前
2.什么是业务后台系统
java·spring·tomcat·mybatis·jdbc
2601_962885722 天前
AkShare 和 Tushare 哪个好?区别、场景与选型
mybatis
_Evan_Yao3 天前
Redis 缓存穿透、击穿、雪崩,一次讲清!
java·spring·mybatis
paopaokaka_luck3 天前
宠物门店经营管理系统(销售与库存联动、进货自动记账、 退换与退款关联、员工个人业绩、ECharts 经营分析、滞销识别与客户分层、宠物档案年龄计算)
前端·javascript·spring boot·mysql·echarts·mybatis·1024程序员节
2301_32241428043 天前
springboot农产品交易平台的设计与实现08055-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·spring boot·redis·mysql·tomcat·maven·mybatis
乐茵lin3 天前
Redis 持久化机制详解:RDB 与 AOF
redis·bootstrap·mybatis
bksczm4 天前
Redis数据结构之string
java·redis·mybatis