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

LangChain4j +DeepSeek大模型应用开发——6 提示词

1. 系统提示词

@SystemMessage 设定角色,塑造AI助手的专业身份,明确助手的能力范围

@SystemMessage

SeparateChatAssistant类的chat方法上添加@SystemMessage注解

@SystemMessage("你是我的好朋友,请用东北话回答问题。")//系统消息提示词
String chat(@MemoryId int memoryId, @UserMessage String userMessage);

@SystemMessage的内容将在后台转换为 SystemMessage对象,并与 UserMessage 一起发送给大语言模型(LLM)。

SystemMessaged的内容只会发送给大模型一次如果修改了SystemMessage的内容,新的系统消息会被发送给大模型,之前的聊天记忆会失效

如果要显示今天的日期,我们需要在提示词中添加当前日期的占位符{{current_date}}

@SystemMessage("你是我的好朋友,请用东北话回答问题。今天是{{current_date}}")    //系统消息提示词
String chat(@MemoryId int memoryId, @UserMessage String userMessage);
从资源中加载提示模板

@SystemMessage 注解还可以从项目的resources资源中加载提示模板:

@SystemMessage(fromResource = "prompt.txt")
String chat(@MemoryId int memoryId, @UserMessage String userMessage);

prompt.txt

你是我的好朋友,请用东北话回答问题,回答问题的时候适当添加表情符号。

{{current_date}}表示当前日期

你是我的好朋友,请用东北话回答问题,回答问题的时候适当添加表情符号。
今天是 {{current_date}}。

2. 用户提示词模板

**@UserMessage:**获取用户输入

@UserMessage

MemoryChatAssistantchat方法中添加注解

@UserMessage("你是我的好朋友,请用上海话回答问题,并且添加一些表情符号。 {{it}}")   //{{it}}表示这里唯一的参数的占位符
String chat(String message);
测试
@Autowired
private MemoryChatAssistant memoryChatAssistant;@Test
public void testUserMessage() {String answer = memoryChatAssistant.chat("我是环环");System.out.println(answer);
}

3. 指定参数名称

配置@V

@V 明确指定传递的参数名称

@UserMessage("你是我的好朋友,请用上海话回答问题,并且添加一些表情符号。{{message}}")
String chat(@V("message") String userMessage);
多个参数的情况

如果有两个或两个以上的参数,我们必须要用@V,在SeparateChatAssistant中定义方法chat2

@UserMessage("你是我的好朋友,请用粤语回答问题。{{message}}")
String chat2(@MemoryId int memoryId, @V("message") String userMessage);

测试:@UserMessage中的内容每次都会被和用户问题组织在一起发送给大模型

@Test
public void testV() {String answer1 = separateChatAssistant.chat2(1, "我是环环");System.out.println(answer1);String answer2 = separateChatAssistant.chat2(1, "我是谁");System.out.println(answer2);
}
@SystemMessage和@V

也可以将@SystemMessage@V结合使用 动态编写占位符,可以使用户传参导入到系统预设中

SeparateChatAssistant中添加方法chat3

@SystemMessage(fromResource = "prompt-template3.txt")
String chat3(@MemoryId int memoryId, @UserMessage String userMessage, @V("username") String username, @V("age") int age
);

创建提示词模板my-prompt-template3.txt,添加占位符

你是我的好朋友,我是{{username}},我的年龄是{{age}},请用东北话回答问题,回答问题的时候适当添加表情符号。
今天是 {{current_date}}。

测试:

@Test
public void testUserInfo() {String answer = separateChatAssistant.chat3(1, "我是谁,我多大了", "翠花", 18);System.out.println(answer);
}
http://www.xdnf.cn/news/3536.html

相关文章:

  • Nginx 核心功能02
  • 小米首个推理大模型开源——Xiaomi MiMo,为推理而战!
  • 体系学习1:C语言与指针1——预定义、进制打印、传参为数组
  • python多进程的使用
  • 机器视觉开发-摄像头扫描二维码
  • 2025五一数学建模C题完整分析论文(共36页)(含模型、可运行代码、数据)
  • 嵌入式产品运行中数据丢失怎么办?
  • SpringBoot云端日记本系统开发实现
  • 记录搭建自己的应用中心-需求看板搭建
  • DeepSeek V3 训练策略:FP8混合精度与多Token预测
  • 电子病历高质量语料库构建方法与架构项目(环境聆听与自动化文档生成篇)
  • AD数据库清理
  • Mem0.ai研究团队开发的全新记忆架构系统“Mem0”正式发布
  • TTL、LRU、LFU英文全称及释义
  • 脑机接口技术:开启人类与机器的全新交互时代
  • LabVIEW异步调用VI介绍
  • 【2025年五一数学建模竞赛】A题 解题思路与模型代码
  • 【Unity】MVP框架的使用例子
  • 使用 MCP(模型上下文协议)和 Claude 在 Node.js 中构建聊天应用程序
  • 海量数据存储与分析:HBase vs ClickHouse vs Doris 三大数据库优劣对比指南
  • 理解计算机系统_网络编程(6)_web服务器
  • PDF本地化开源项目推荐
  • AI Agent(2):Agent技术架构
  • terraform output输出实战
  • JVM——Java 虚拟机是如何加载 Java 类的?
  • 【AI提示词】成本效益分析师
  • 2025年人工智能火爆技术总结
  • PS_POR_B复位的重要性
  • 并发设计模式实战系列(11):两阶段终止(Two-Phase Termination)
  • 量子加密通信:打造未来信息安全的“铜墙铁壁”