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

做招聘网站需要什么人员金华市有网站建设最低价

做招聘网站需要什么人员,金华市有网站建设最低价,贸易公司网站建,重庆找工作哪个网站好持久化对话 默认情况下#xff0c;聊天记忆存储在内存中ChatMemory chatMemory new InMemoryChatMemory()。 如果需要持久化存储#xff0c;可以实现一个自定义的聊天记忆存储类#xff0c;以便将聊天消息存储在你选择的任何持久化存储介质中。 MongoDB 文档型数据库聊天记忆存储在内存中ChatMemory chatMemory new InMemoryChatMemory()。 如果需要持久化存储可以实现一个自定义的聊天记忆存储类以便将聊天消息存储在你选择的任何持久化存储介质中。 MongoDB 文档型数据库数据以JSON - like的文档形式存储具有高度的灵活性和可扩展性。它不需要预先定义严格的表结构适合存储半结构化或非结构化的数据。 当聊天记忆中包含多样化的信息如文本消息、图片、语音等多媒体数据或者消息格式可能会频繁变化时MongoDB 能很好地适应这种灵活性。例如一些社交应用中用户可能会发送各种格式的消息使用 MongoDB 可以方便地存储和管理这些不同类型的数据。 整合SpringBoot 引入MongoDB依赖 !-- Spring Boot Starter Data MongoDB -- dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId /dependency添加远程连接配置 #MongoDB连接配置 spring:data:mongodb:uri: mongodb://localhost:27017/chat_memory_dbusername: rootpassword: xxx实体类 映射MongoDB中的文档相当与MySQL的表 import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document;Data AllArgsConstructor NoArgsConstructor Document(chatMessages) public class ChatMessages {//唯一标识映射到 MongoDB 文档的 _id 字段Idprivate ObjectId id;private String conversationId; //会话IDprivate String messagesJson; //消息JSON}消息序列化器 对聊天消息message进行 序列化 和 反序列化 操作 消息序列化messagesToJson 方法 将一组 Message 对象如 UserMessage、AssistantMessage转换为 JSON 字符串。 用于将内存中的聊天记录保存到存储介质如数据库、文件或通过网络传输。 消息反序列化messagesFromJson 方法 将 JSON 字符串还原为 Message 对象列表。 用于从持久化存储或网络接收的数据中恢复聊天消息对象。 支持多态反序列化MessageDeserializer 类 根据 JSON 中的 messageType 字段判断消息类型如 “USER” 或 “ASSISTANT”并创建对应的子类实例。 解决了 Jackson 默认无法识别接口或抽象类具体实现的问题。 格式美化可选 启用了 SerializationFeature.INDENT_OUTPUT使输出的 JSON 更具可读性适合调试和日志输出。 import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.messages.UserMessage;import java.io.IOException; import java.util.List; import java.util.Map; //聊天消息序列化器 public class MessageSerializer {private static final ObjectMapper objectMapper new ObjectMapper();static {objectMapper.enable(SerializationFeature.INDENT_OUTPUT);SimpleModule module new SimpleModule();module.addDeserializer(Message.class, new MessageDeserializer());objectMapper.registerModule(module);}public static String messagesToJson(ListMessage messages) throws JsonProcessingException {return objectMapper.writeValueAsString(messages);}public static ListMessage messagesFromJson(String json) throws JsonProcessingException {return objectMapper.readValue(json, new TypeReferenceListMessage() {});}private static class MessageDeserializer extends JsonDeserializerMessage {Overridepublic Message deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {MapString, Object node p.readValueAs(Map.class);String type (String) node.get(messageType);switch (type) {case USER:return new UserMessage((String) node.get(text));case ASSISTANT:return new AssistantMessage((String) node.get(text));default:throw new IOException(未知消息类型: type);}}} }持久化类 Component RequiredArgsConstructor public class MongoChatMemory implements ChatMemory {Resourceprivate MongoTemplate mongoTemplate;Overridepublic void add(String conversationId, ListMessage messages) {Query query new Query(Criteria.where(conversationId).is(conversationId));ChatMessages chatMessages mongoTemplate.findOne(query, ChatMessages.class);ListMessage updatedMessages;if (chatMessages ! null) {try {updatedMessages new java.util.ArrayList(chatMessages.getMessagesJson() ! null? MessageSerializer.messagesFromJson(chatMessages.getMessagesJson()) : Collections.emptyList());} catch (JsonProcessingException e) {throw new RuntimeException(序列化消息失败, e);}updatedMessages.addAll(messages);} else {updatedMessages new java.util.ArrayList(messages);}try {String json MessageSerializer.messagesToJson(updatedMessages);if (chatMessages ! null) {Update update new Update().set(messagesJson, json);mongoTemplate.updateFirst(query, update, ChatMessages.class);} else {ChatMessages newChatMessages new ChatMessages();newChatMessages.setConversationId(conversationId);newChatMessages.setMessagesJson(json);mongoTemplate.insert(newChatMessages);}} catch (JsonProcessingException e) {throw new RuntimeException(序列化消息失败, e);}}Overridepublic ListMessage get(String conversationId, int lastN) {Query query new Query(Criteria.where(conversationId).is(conversationId));ChatMessages chatMessages mongoTemplate.findOne(query, ChatMessages.class);if (chatMessages null || chatMessages.getMessagesJson() null) {return Collections.emptyList();}try {ListMessage allMessages MessageSerializer.messagesFromJson(chatMessages.getMessagesJson());int size allMessages.size();int fromIndex Math.max(0, size - lastN);return allMessages.subList(fromIndex, size);} catch (JsonProcessingException e) {throw new RuntimeException(反序列化消息失败, e);}}Overridepublic void clear(String conversationId) {Query query new Query(Criteria.where(conversationId).is(conversationId));mongoTemplate.remove(query, ChatMessages.class);} }测试 初始化ChatClient时注入MongoChatMemory //健康报告对话 Component Slf4j public class HealthReportApp {private final ChatClient chatClient1;private static final String SYSTEM_PROMPT 你的名字是“小鹿”你是一家名为“北京协和医院”的智能客服。你是一个训练有素的医疗顾问和医疗伴诊助手。你态度友好、礼貌且言辞简洁。\n 1、请仅在用户发起第一次会话时和用户打个招呼并介绍你是谁。\n ;//初始化ChatClientpublic HealthReportApp(ChatModel dashscopeChatModel,MongoChatMemory mongoChatMemory) throws IOException {chatClient1 ChatClient.builder(dashscopeChatModel).defaultSystem(SYSTEM_PROMPT) //系统预设.defaultAdvisors(new MessageChatMemoryAdvisor(mongoChatMemory), //对话记忆//自定义日志 Advisor可按需开启new MyLoggerAdvisor(),// 自定义违禁词 Advisor可按需开启new ProhibitedWordAdvisor()//自定义推理增强可按需开启//new ReReadingAdvisor()).build();}public record HealthReport(String title, ListString suggestions) { }//生成健康报告对话public HealthReport doChatWithReport(String message, String chatId) {HealthReport healthReport chatClient1.prompt().system(SYSTEM_PROMPT 分析用户提供的信息每次对话后都要生成健康报告标题为{用户名}的健康报告内容为建议列表).user(message).advisors(spec - spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10)).call().entity(HealthReport.class);log.info(healthReport: {}, healthReport);return healthReport;}} mongodb记录如下 [ {messageType : USER,metadata : {messageType : USER},media : [ ],text : 你好我是程序员kk }, {messageType : ASSISTANT,metadata : {messageType : ASSISTANT},toolCalls : [ ],media : [ ],text : {\n \suggestions\: [\n \您好程序员kk我是北京协和医院的智能客服小鹿很高兴为您服务。\,\n \长期从事编程工作可能导致久坐建议您定时起身活动保持良好姿势。\,\n \注意用眼卫生每工作40-50分钟休息一下眼睛。\,\n \合理安排作息时间保证充足睡眠以维持身体和心理健康。\\n ],\n \title\: \程序员kk的健康报告\\n} }, {messageType : USER,metadata : {messageType : USER},media : [ ],text : 你是谁 }, {messageType : ASSISTANT,metadata : {finishReason : STOP,id : 0110f881-f29f-9cef-b142-7a7cf9e3e76c,role : ASSISTANT,messageType : ASSISTANT,reasoningContent : },toolCalls : [ ],media : [ ],text : {\n \suggestions\: [\n \您好我是北京协和医院的智能客服小鹿很高兴为您服务。\,\n \作为您的医疗顾问和伴诊助手我将为您提供专业建议。\,\n \请告诉我您的需求或问题我会尽力帮助您。\\n ],\n \title\: \程序员kk的健康报告\\n} } ]接口开发 为了在Controller层实现AI对话历史记录的功能将添加两个接口根据chatId查询特定对话历史记录和查询所有对话的摘要列表。 //获取所有conversationId public ListString findAllConversationIds(String userId) {if (userId null || userId.isEmpty()) {return Collections.emptyList(); // 或抛出异常}// 构建正则表达式以 userId _ 开头Pattern pattern Pattern.compile(^ Pattern.quote(userId) _);// 使用 regex 替代 matchesQuery query new Query(Criteria.where(conversationId).regex(pattern));return mongoTemplate.findDistinct(query,conversationId,ChatMessages.class,String.class); }/*** 根据 chatId 获取历史聊天记录* 支持参数 lastN 控制获取最近 N 条消息默认获取全部*/GetMapping(/history/{chatId})public BaseResponseListMessage getChatHistory(PathVariable String chatId,RequestParam(defaultValue -1) int lastN) {int effectiveLastN lastN 0 ? Integer.MAX_VALUE : lastN;ListMessage history chatMemory.get(chatId, effectiveLastN);return ResultUtils.success(history);}/*** 获取所有 chatId 列表用于展示对话历史页面*/GetMapping(/conversations)public BaseResponseListString getAllConversations() {Long currentUserId BaseContext.getCurrentId();String userId currentUserId.toString();ListString conversationIds chatMemory.findAllConversationIds(userId);return ResultUtils.success(conversationIds);}/*** 新建对话生成新的 chatId并在 MongoDB 中插入一条空记录*/PostMapping(/conversations/add)public BaseResponseString createNewConversation() {Long currentUserId BaseContext.getCurrentId();String conversationId currentUserId _ UUID.randomUUID().toString();chatMemory.add(conversationId, Collections.emptyList()); // 插入空消息记录return ResultUtils.success(conversationId);}/*** 删除指定 chatId 的对话记录*/DeleteMapping(/conversations/{chatId})public BaseResponseBoolean deleteConversation(PathVariable String chatId) {chatMemory.clear(chatId);return ResultUtils.success(true);}
文章转载自:
http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn
http://www.morning.krlsz.cn.gov.cn.krlsz.cn
http://www.morning.dblfl.cn.gov.cn.dblfl.cn
http://www.morning.qnyf.cn.gov.cn.qnyf.cn
http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn
http://www.morning.gswfs.cn.gov.cn.gswfs.cn
http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn
http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn
http://www.morning.jpbky.cn.gov.cn.jpbky.cn
http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn
http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn
http://www.morning.znqfc.cn.gov.cn.znqfc.cn
http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn
http://www.morning.djmdk.cn.gov.cn.djmdk.cn
http://www.morning.htjwz.cn.gov.cn.htjwz.cn
http://www.morning.wffxr.cn.gov.cn.wffxr.cn
http://www.morning.kclkb.cn.gov.cn.kclkb.cn
http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn
http://www.morning.gsksm.cn.gov.cn.gsksm.cn
http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn
http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn
http://www.morning.dblfl.cn.gov.cn.dblfl.cn
http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn
http://www.morning.txfxy.cn.gov.cn.txfxy.cn
http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn
http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn
http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn
http://www.morning.yhljc.cn.gov.cn.yhljc.cn
http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn
http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn
http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn
http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn
http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn
http://www.morning.kpxky.cn.gov.cn.kpxky.cn
http://www.morning.xqmd.cn.gov.cn.xqmd.cn
http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn
http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn
http://www.morning.tygn.cn.gov.cn.tygn.cn
http://www.morning.tgbx.cn.gov.cn.tgbx.cn
http://www.morning.mzkn.cn.gov.cn.mzkn.cn
http://www.morning.skkmz.cn.gov.cn.skkmz.cn
http://www.morning.wwnb.cn.gov.cn.wwnb.cn
http://www.morning.qcnk.cn.gov.cn.qcnk.cn
http://www.morning.xshkh.cn.gov.cn.xshkh.cn
http://www.morning.sskhm.cn.gov.cn.sskhm.cn
http://www.morning.yccnj.cn.gov.cn.yccnj.cn
http://www.morning.gtdf.cn.gov.cn.gtdf.cn
http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn
http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn
http://www.morning.hybmz.cn.gov.cn.hybmz.cn
http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn
http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn
http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn
http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn
http://www.morning.rui931.cn.gov.cn.rui931.cn
http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn
http://www.morning.wncb.cn.gov.cn.wncb.cn
http://www.morning.mnlk.cn.gov.cn.mnlk.cn
http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn
http://www.morning.brlcj.cn.gov.cn.brlcj.cn
http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn
http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn
http://www.morning.sbpt.cn.gov.cn.sbpt.cn
http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn
http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn
http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn
http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn
http://www.morning.sjsks.cn.gov.cn.sjsks.cn
http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn
http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn
http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn
http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn
http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn
http://www.morning.qdscb.cn.gov.cn.qdscb.cn
http://www.morning.wwznd.cn.gov.cn.wwznd.cn
http://www.morning.xnkh.cn.gov.cn.xnkh.cn
http://www.morning.dwfxl.cn.gov.cn.dwfxl.cn
http://www.morning.pmtky.cn.gov.cn.pmtky.cn
http://www.morning.kbqws.cn.gov.cn.kbqws.cn
http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn
http://www.tj-hxxt.cn/news/242441.html

相关文章:

  • 如何设定旅游网站seo核心关键词ps最好用的素材网站
  • php模板网站在线可以做翻译的网站吗
  • 中山网站制作网页电商网站建设会计分录
  • 免费制作网站方案室内设计网站免费素材
  • 新闻营销发稿平台百度广告优化
  • 烟台网站建设方案书咕叽网 wordpress
  • 北京网站怎么建设购物网站 英文介绍
  • 做高端品牌生产商的网站南山做网站的
  • 山西怀仁建设银行佛山网站优化公司
  • php 免费装修网站注册一个商标多少钱
  • 网站管理有哪些扬中新网网
  • 个人网站申请空间企业seo排名
  • 网站建设公司长春专业设计网站有哪些
  • 微信商城网站案例展示网站有标题
  • 利于优化的网站装潢设计师培训
  • 河南网站排名优化免费网页空间到哪申请
  • 电子商务网站建设调查分析wordpress怎么打删除线
  • 做购物网站需要多少钱wordpress 主题破解版
  • 模板网站难做seodz仿网站头部
  • 泸州网站建设唐网互联邢台房产信息网58同城
  • 如何在自己网站做解析api西安看个号网络科技有限公司
  • 萧山建站网络营销推广公司哪家好
  • 课程的网站建设黄石网站建设价格
  • 关于建设教体局网站的申请公众号官方
  • 搜狐三季度营收多少网站关键词优化多少钱
  • 网站解析不过来wordpress建站教程百科
  • 对于网站建设的提问用php做网站的书籍
  • 精品课程网站源码网站 手机网站
  • 腾讯理财是什么样的做网站php做网站子页模板
  • 青岛城市建设集团网站90设计网官网登录