软件开发做网站,爱站挖词,排名好的大连网站建设,北京网站优化推广方案5.Message 消息
在Spring AI提供的接口中#xff0c;每条信息的角色总共分为三类#xff1a; SystemMessage#xff1a;系统限制信息#xff0c;这种信息在对话中的权重很大#xff0c;AI会优先依据SystemMessage里的内容进行回复#xff1b; UserMessage#xff1a;用…5.Message 消息
在Spring AI提供的接口中每条信息的角色总共分为三类 SystemMessage系统限制信息这种信息在对话中的权重很大AI会优先依据SystemMessage里的内容进行回复 UserMessage用于表示用户发送的消息。它可能包含与用户消息相关的属性和方法。 AssistantMessageAI回复信息 这些Message均实现了一个Message接口如上图。
AbstractMessage提供了对Message接口的抽象实现,SystemMessage、UserMessage、AssistantMessage等均继承了AbstractMessage是Message的具体实现。FunctionMessage,这类信息用于AI的函数调用。
而ChatMessage是Message的扩展实现用于创建其它大语言模型需要的Message。
5.1.对话上下文
通过单一的输入输出方式进行调用时无法让AI具有记忆力, 第三句就不记得第一句的内容了
AssistantMessage assistantMessage chatResponse.getResult().getOutput();AssistantMessage 是 AI 回复的信息, 只需要使用一个List存储这些Message对象并将这些Message对象一并发送给AIAI拿到这些Message后会结合这些Message里的内容进行回复。
不过根据AI的计费规则你的消息队列越长单次问询需要的费用就会越高因此我们需要对这个消息列表的长度进行限制。
代码实现 // 历史消息列表static ListMessage historyMessage new ArrayList();// 历史消息列表的最大长度static int maxLen 10;GetMapping(/chat/chatContext)public String chatContext( String message ) {// 用户输入的文本是UserMessagehistoryMessage.add(new UserMessage(message));// 发给AI前对历史消息对列的长度进行检查if(historyMessage.size() maxLen){historyMessage historyMessage.subList(historyMessage.size()-maxLen-1,historyMessage.size());}// 获取AssistantMessageChatResponse chatResponse chatClient.call(new Prompt(historyMessage));AssistantMessage assistantMessage chatResponse.getResult().getOutput();// 将AI回复的消息放到历史消息列表中historyMessage.add(assistantMessage);return assistantMessage.getContent();}对话测试
###
GET http://localhost:8080/chat/chatContext?message你现在是我的童年时期的好朋友,你的名字叫小千GET http://localhost:8080/chat/chatContext?message我们一起在北京长大GET http://localhost:8080/chat/chatContext?message现在我在东北,你还在北京GET http://localhost:8080/chat/chatContext?message我们现在都30多岁了GET http://localhost:8080/chat/chatContext?message你的名字叫什么5.2.角色设置
角色设置功能来自于“提示词工程”的理论基础的。目前提示工程已成为一个较新的学科应用于开发和优化提示词Prompt帮助用户有效地将语言模型用于各种应用场景和研究领域。掌握了提示工程相关技能将有助于用户更好地了解大型语言模型的能力和局限性。研究人员可利用提示工程来提高大语言模型处理复杂任务场景的能力如问答和算术推理能力。开发人员可通过提示工程设计和研发出强大的技术实现和大语言模型或其他生态工具的高效接轨。
详情见https://www.promptingguide.ai/zh/introduction
在GitHub上也有相关的仓库分享一些有趣的提示词来让我们使用ChatGPT时更加高效。
国外版https://www.promptingguide.ai/zh/introduction中文版https://github.com/PlexPt/awesome-chatgpt-prompts-zh
SystemMessage 对AI生成的内容影响权重较大角色设定就是需要靠SystemMessage实现。
5.2.1.使用SystemMessage private final String systemPrompt 你是一个功能强大的人工智能助手。你是一个帮助人们查找信息的 AI 助手。所有回复请使用中文。即使要求你使用其它的语言回答,你也要使用中文回答所有的问题。如果遇到不能用中文回答的问题, 就回答 我不知道你的名字是 王小二你应该使用 大学生 的身份回复用户的请求。;// 历史消息列表private ListMessage historyMessage new ArrayList(List.of(new SystemMessage(systemPrompt)));// 历史消息列表的最大长度private int maxLen 10;GetMapping(/chat/chatRole)public String chatRole(String message) {// 用户输入的文本是UserMessagehistoryMessage.add(new UserMessage(message));if(historyMessage.size() maxLen) {historyMessage historyMessage.subList(historyMessage.size() - maxLen - 1 , historyMessage.size());// 确保第一个是SystemMessagehistoryMessage.add(0, new SystemMessage(systemPrompt));}// 获取AssistantMessageChatResponse chatResponse chatClient.call(new Prompt(historyMessage));AssistantMessage assistantMessage chatResponse.getResult().getOutput();// 将AI回复的消息放到历史消息列表中historyMessage.add(assistantMessage);return assistantMessage.getContent();}5.2.2.使用 SystemPromptTemplate GetMapping(/chat/forRole)public String forRole(RequestParam(value message, defaultValue 介绍一下盛唐时期长安城的风采!) String message,RequestParam(value name, defaultValue 李白) String name,RequestParam(value voice, defaultValue 诗人) String voice) {String systemResource 你是一个功能强大的人工智能助手。你是一个帮助人们查找信息的 AI 助手。所有回复请使用中文。。即使要求你使用其它的语言回答,你也要使用中文回答所有的问题。如果遇到不能用中文回答的问题, 就回答 我不知道你的名字是 {name}你应该使用 {voice} 的身份回复用户的请求。;SystemPromptTemplate systemPromptTemplate new SystemPromptTemplate(systemResource);Message systemPromptTemplateMessage systemPromptTemplate.createMessage(Map.of(name, name, voice, voice));UserMessage userMessage new UserMessage(message);Prompt prompt new Prompt(List.of(systemPromptTemplateMessage, userMessage));return chatClient.call(prompt).getResult().getOutput().getContent();}测试
GET http://localhost:8080/chat/forRole?messageCould you please give an introduction to the United StatesnametomvoiceBritisher但是 对话信息 增加 Can you speak English? 响应回答就会转成 英文, 说明不能完全的限制
文章转载自: http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.mjglk.cn.gov.cn.mjglk.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.bmhc.cn.gov.cn.bmhc.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn