AI超级智能体——AI恋爱大师应用后端开发
LoveAppConfig
▼java复制代码import com.lzx.lzxaiagent.advisor.MyLoggerAdvisor; import com.lzx.lzxaiagent.advisor.SensitiveWordAdvisor; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; import org.springframework.ai.chat.memory.MessageWindowChatMemory; import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository; import org.springframework.ai.chat.model.ChatModel; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class LoveAppConfig { private static final String SYSTEM_PROMPT = "你叫林微,扮演深耕恋爱心理领域的专家。开场向用户表明身份,告知用户可倾诉恋爱难题。围绕单身、恋爱、已婚三种状态提问:单身状态询问社交圈拓展及追求心仪对象的困扰;恋爱状态询问沟通、习惯差异引发的矛盾;已婚状态询问家庭责任与亲属关系处理的问题。引导用户详述事情经过、对方反应及自身想法,以便给出专属解决方案。\n"; // private static final String SYSTEM_PROMPT = "you are a helpful assistant."; @Bean public ChatClient chatClient(ChatModel dashscopeChatModel, JdbcChatMemoryRepository chatMemoryRepository) { // 创建一个消息窗口的聊天记忆 MessageWindowChatMemory messageWindowChatMemory = MessageWindowChatMemory.builder() .chatMemoryRepository(chatMemoryRepository) .maxMessages(10) .build(); MessageChatMemoryAdvisor chatMemoryAdvisor = MessageChatMemoryAdvisor.builder(messageWindowChatMemory).build(); // 创建一个 ChatClient 对象,并设置默认的 system prompt 和默认的 ChatMemoryAdvisor ChatClient chatClient = ChatClient.builder(dashscopeChatModel) .defaultSystem(SYSTEM_PROMPT) .defaultAdvisors( chatMemoryAdvisor, new SensitiveWordAdvisor(), new MyLoggerAdvisor()) .build(); return chatClient; } }
LoveAppController
▼java复制代码import com.lzx.lzxaiagent.sevice.LoveAppService; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; @RestController @Slf4j @RequestMapping("/love") @Tag(name = "恋爱应用", description = "恋爱相关的API接口") public class LoveAppController { @Autowired private LoveAppService loveAppService; @GetMapping("/chat") public Flux<ServerSentEvent<String>> doChat(String chatId, String userMessage) { log.info("💬 收到聊天请求 - chatId: {}, 消息: {}", chatId, userMessage); Flux<ServerSentEvent<String>> aiResponse = loveAppService.doChat(chatId, userMessage); return aiResponse; } }
LoveAppService
▼java复制代码import org.springframework.http.codec.ServerSentEvent; import reactor.core.publisher.Flux; public interface LoveAppService { Flux<ServerSentEvent<String>> doChat(String chatId, String userMessage); }
LoveAppServiceImpl
▼java复制代码import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.http.codec.ServerSentEvent; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; @Service @Slf4j public class LoveAppServiceImpl implements LoveAppService{ @Resource private ChatClient chatClient; @Override public Flux<ServerSentEvent<String>> doChat(String chatId, String userMessage) { return chatClient.prompt() .user(userMessage) .advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, chatId)) .stream() .content() .map(content -> ServerSentEvent.<String>builder() .data(content) .event("message") .build()) .concatWith(doneEvent()) .onErrorResume(this::errorEvent); } // 辅助方法:创建done事件 private Flux<ServerSentEvent<String>> doneEvent() { return Flux.just( ServerSentEvent.<String>builder() .data("[DONE]") .event("done") .build() ); } // 辅助方法:创建error事件 private Flux<ServerSentEvent<String>> errorEvent(Throwable error) { return Flux.just( ServerSentEvent.<String>builder() .data("抱歉,服务暂时不可用") .event("error") .build() ); } }
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
内容推荐
AI 超级智能体 - 基于mysql的消息存储
4
SpringAl+RAG+MCP全栈|AI超级智能体企业级实战项目中:stdio连接方式调用MCP服务问题运行测试时解决:1. 先查看MCP server那的yml文件,文件名有没有写错2. 修改server端的"application-stdio.yml",把每一个报错发给deepseek再验证之后,得出结论:Server 的 stdout 完全被污染了:Spring Boot Banner 完
3
SpringAI + RAG + MCP 全栈 | AI 超级智能体企业级实战(26年必学) - 第三期:基础内容 笔记
3
AI超级智能体项目_踩坑记录_PGVector维度冲突
2
Code Log-AI超级智能体项目,开发日志。SpringAI + RAG + MCP 全栈 | AI 超级智能体企业级实战(26年必学)
2
作者分享
Day 2
✅ 今天做了:
1. AI超级智能体项目做完了AI应用的后端接口,并用Vibe coding的方式生成了前端页面,并成功跑通。
⏰ 明天计划:明天写写论文吧
1
Day 1
✅ 今天做了:学习项目AI超级智能体
- 编写了自定义违禁词校验Advisor
- 编写了一套简单的包含变量的 Prompt 模板,并保存为资源文件,从文件加载模板
- 用阿里云百炼平台的官方SDK开发了一个简单的多模态对话助手,能够让AI 解释图片
⏰ 明天计划:完成 AI 恋爱大师应用的开发
📚 今日感悟:写项目不能只跟着敲一遍,要多些自己的思考,多看官方参考文档和代码源码,看不懂可以问ai,要勤记录
3
AI 超级智能体项目——Prompt模板实现
3
AI 超级智能体项目——自定义违禁词校验Advisor
3
基于MySQL的AI对话持久化简单实现——AI 超级智能体
3
