SpringAI实现角色扮演(自定义人设)和Prompts模板语法-基于DeepSeek
SpringAI实现角色扮演(自定义人设)和Prompts模板语法-基于DeepSeek

上一篇文章分享了SpringAI实现上下文对话-基于DeepSeek今天这篇文章我来分享SpringAI实现角色扮演(自定义人设)和Prompts模板语法-基于DeepSeek。
完整代码在文章最后,如果觉得本篇文章对你有用,记得点赞、关注、收藏哦。你的支持是我持续更新的动力!
文章最后可以加入免费的Java技术栈沟通社群,一起探讨Java/你的产品如何与AI结合,请按照要求加入。在群中可以聊开发、系统设计、架构、行业趋势、AI等等话题
AI专栏软件环境
- IntelliJ IDEA2024.3.2.2
- Spring AI 1.0.0-SNAPSHOT
- Spring Boot 3.4.2
- Spring 6.2.2
- spring-ai-openai-spring-boot-starter
- DeepSeek
- JDK 17.0.12
我们先看本篇文章对应的项目结构,请看下图

1 项目基础配置
1.1 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>cn.itbeien.ai</groupId>
<artifactId>spring-ai-labs</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-ai-lab05</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
</dependency>
</dependencies>
</project>
1.2 deepseek配置
spring.ai.openai.api-key=sk-xxxxxx
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
# The DeepSeek API doesn't support embeddings, so we need to disable it.
spring.ai.openai.embedding.enabled=false
2 自定义人设,来与用户进行角色扮演
2.1 代码实现
package cn.itbeien.ai.springai.controller;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author itbeien
* 项目网站:https://www.itbeien.cn
* 公众号:贝恩聊架构
* 全网同名,欢迎小伙伴们关注
* Java/AI学习社群
* Copyright© 2025 itbeien
*/
@RestController
@RequestMapping("/ai")
public class Lab05Controller {
private final OpenAiChatModel chatModel;
// 自定义人设,来与用户进行角色扮演 提示词
private final static String systemPrompt = "请你扮演程序员编程助手,且只能回答与计算机科学、编程、算法或其他技术相关的问题。对于非编程类的问题,你无法提供回答";
// 历史消息列表
static List<Message> historyMessage = new ArrayList<>(List.of(new SystemMessage(systemPrompt)));
// 历史消息列表的最大长度
static int maxLen = 15;
public Lab05Controller(OpenAiChatModel chatModel) {
this.chatModel = chatModel;
}
/**
* 自定义人设,来与用户进行角色扮演
* @param prompt
* @return
*/
@GetMapping("/content")
public String content(@RequestParam("prompt") String prompt) {
// 用户输入的文本是UserMessage
historyMessage.add(new UserMessage(prompt));
// 发给AI前对历史消息对列的长度进行检查
if(historyMessage.size() > maxLen){
historyMessage = historyMessage.subList(historyMessage.size()-maxLen-1,historyMessage.size());
// 确保第一个是SystemMessage
historyMessage.add(0,new SystemMessage(systemPrompt));
}
// 获取AssistantMessage
ChatResponse chatResponse = chatModel.call(new Prompt(historyMessage));
AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
// 将AI回复的消息放到历史消息列表中
historyMessage.add(assistantMessage);
return assistantMessage.getText();
}
}
2.2 代码测试
我们按照给deepseek预先设置的角色和他进行对话,我们先问他是谁

然后提一个与编程不相关的问题

最后提一个与编程相关的问题

我们基于以上三张图中的结果,可以得知我们预先通过SystemMessage给deepseek设置的角色已经生效
3 Prompts模板语法
3.1 代码实现
/**
* Prompts模板语法
* @param language
* @return
*/
@GetMapping("/template")
public String template(@RequestParam("prompt") String language) {
// 提示词
final String template = "请问{language}是谁发明的?什么时候发布的?优缺点是什么?";
PromptTemplate promptTemplate = new PromptTemplate(template);
// 动态地将language填充进去
Prompt prompt = promptTemplate.create(Map.of("language", language));
ChatResponse chatResponse = chatModel.call(prompt);
AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
return assistantMessage.getText();
}
3.2 代码测试
我们按照预先设置好的Prompts模板语法进行提问,我们看下图,我们在输入对应language后deepseek会按照PromptTemplate设置好的模版语法内容进行提问回答



以上就是今天SpringAI实现角色扮演(自定义人设)和Prompts模板语法-基于DeepSeek全部内容,文章最后有源码下载地址
欢迎大家关注我的项目实战内容itbeien.cn,一起学习一起进步,在项目和业务中理解各种技术。

欢迎沟通交流Java/AI技术和支付业务,一起探讨大模型应用/SAAS多租户/聚合支付/预付卡系统业务、技术、系统架构、微服务、容器化。并结合大模型应用/SAAS多租户/聚合支付系统深入技术框架/微服务原理及分布式事务原理。加入我的知识星球吧

AI专栏
01IDEA&VsCode集成DeepSeek-V3 API提高编程效率
02IntelliJ IDEA集成主流 AI 编程助手及特性介绍
03Spring AI快速入门-基于DeepSeek&智谱实现聊天应用
04Spring AI中流式对话API如何使用-基于DeepSeek
SpringBoot3专栏
01SpringBoot3专栏-SpringBoot3.4.0整合Mybatis-plus和Mybatis
02SpringBoot3.4.0结合Mybatis-plus实现动态数据源
03mapstruct对象映射在Springboot3中这样用就对了
04RocketMQ5.3.1集成SpringBoot3.4.0就这样简单
05SpringBoot3.4.0整合Redisson实现分布式锁
06MySQL增量数据同步利器Canal1.1.7环境搭建流程
07SpringBoot3.4.0集成Canal1.1.7实现MySQL实时同步数据到Redis
08基于Docker-SpringBoot3.4.0集成Apache Pulsar4.0.1实现消息发布和订阅
09SpringBoot3.4.0整合消息中间件Kafka和RabbitMQ
10SpringBoot3.4.0整合ActiveMQ6.1.4
11SpringBoot3整合Spring Security6.4.2 安全认证框架实现简单身份认证
12SpringBoot3.4.1和Spring Security6.4.2实现基于内存和MySQL的用户认证
13SpringBoot3.4.1和Spring Security6.4.2结合OAuth2实现GitHub授权登录
14SpringBoot3.4.1和Spring Security6.4.2结合JWT实现用户登录
16SpringBoot3.4.1基于MySQL8和Quartz实现定时任务管理
跟着我学微服务系列
01跟着我学微服务,什么是微服务?微服务有哪些主流解决方案?
05SpringCloudAlibaba之图文搞懂微服务核心组件在企业级支付系统中的应用
06JDK17+SpringBoot3.4.0+Netty4.1.115搭建企业级支付系统POS网关
07JDK17+SpringCloud2023.0.3搭建企业级支付系统-预付卡支付交易微服务
08JDK17+Dubbo3.3.2搭建企业级支付系统-预付卡支付交易微服务
09JDK17+SpringBoot3.3.6+Netty4.1.115实现企业级支付系统POS网关签到功能
贝恩聊架构-项目实战地址
欢迎大家一起讨论学习,加我备注"JAVA"拉你进入技术讨论群,在技术学习、成长、工作的路上不迷路!加我后不要急,每天下午6点左右通过!营销号免入

4 源码地址
贝恩聊架构-SpringBoot3专栏系列文章、资料和源代码会同步到以下地址,代码和资料每周都会同步更新
该仓库地址主要用于贝恩聊架构-SpringBoot3专栏、基于企业级支付系统,学习微服务整体技术栈
