1.spring alibaba ai 实现对话机器人

张开发
2026/4/6 20:52:04 15 分钟阅读

分享文章

1.spring alibaba ai 实现对话机器人
spring alibaba ai 实现对话机器人1.添加 pom.xml(自己部署加上版本号还要配置阿里云相关镜像)dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdcom.alibaba.cloud.ai/groupId artifactIdspring-ai-alibaba-starter-dashscope/artifactId /dependency /dependencies2.application.ymlserver: port: 18080 spring: application: name: spring-ai-alibaba-helloworld ai: dashscope: api-key: sk-b723dfc465934.... # alibaba 开发者平台这里需要自己的api-key3.启动类SpringBootApplication public class HelloworldApplication { public static void main(String[] args) { org.springframework.boot.SpringApplication.run(HelloworldApplication.class, args); } }4.核心代码(超级简单)package com.alibaba.ai; import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions; import jakarta.servlet.http.HttpServletResponse; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; RestController RequestMapping(/helloworld) public class HelloworldController { private static final String DEFAULT_PROMPT 你是一个博学的智能聊天助手请根据用户提问回答; private final ChatClient dashScopeChatClient; // 也可以使用如下的方式注入 ChatClient public HelloworldController(ChatClient.Builder chatClientBuilder) { this.dashScopeChatClient chatClientBuilder .defaultSystem(DEFAULT_PROMPT) // TODO // 实现 Chat Memory 的 Advisor // 在使用 Chat Memory 时需要指定对话 ID以便 Spring AI 处理上下文。 // .defaultAdvisors( // new MessageChatMemoryAdvisor(new InMemoryChatMemory()) // ) // 实现 Logger 的 Advisor .defaultAdvisors( new SimpleLoggerAdvisor() ) // 设置 ChatClient 中 ChatModel 的 Options 参数 .defaultOptions( DashScopeChatOptions.builder() .withTopP(0.7) .build() ) .build(); } /** * ChatClient 简单调用 */ GetMapping(/simple/chat) public String simpleChat(RequestParam(value query, defaultValue 你好很高兴认识你能简单介绍一下自己吗)String query) { return dashScopeChatClient.prompt(query).call().content(); } /** * ChatClient 流式调用 */ GetMapping(/stream/chat) public FluxString streamChat(RequestParam(value query, defaultValue 你好很高兴认识你能简单介绍一下自己吗)String query, HttpServletResponse response) { response.setCharacterEncoding(UTF-8); return dashScopeChatClient.prompt(query).stream().content(); } /** * ChatClient 使用自定义的 Advisor 实现功能增强. * eg: * http://127.0.0.1:18080/helloworld/advisor/chat/123?query你好我叫牧生之后的会话中都带上我的名字 * 你好牧生很高兴认识你。在接下来的对话中我会记得带上你的名字。有什么想聊的吗 * http://127.0.0.1:18080/helloworld/advisor/chat/123?query我叫什么名字 * 你叫牧生呀。有什么事情想要分享或者讨论吗牧生 */ GetMapping(/advisor/chat/{id}) public FluxString advisorChat( HttpServletResponse response, PathVariable String id, RequestParam String query) { response.setCharacterEncoding(UTF-8); return this.dashScopeChatClient.prompt(query) .advisors( // TODO // a - a // .param(CHAT_MEMORY_CONVERSATION_ID_KEY, id) // .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100) ).stream().content(); } }5.访问GET http://localhost:18080/helloworld/stream/chat?query你好只能的机器人 GET http://127.0.0.1:18080/helloworld/advisor/chat/123?query你好我叫大龄码农之后的会话中都带上我的名字GET http://localhost:18080/helloworld/stream/chat?query%E4%BD%A0%E5%A5%BD%E5%8F%AA%E8%83%BD%E7%9A%84%E6%9C%BA%E5%99%A8%E4%BA%BA HTTP/1.1 200 Content-Type: text/plain;charsetUTF-8 Transfer-Encoding: chunked Date: Thu, 18 Sep 2025 01:40:42 GMT 你好呀你称呼我为“智能的机器人”真是让我有点小害羞呢~ 虽然我确实是个AI助手但更希望能成为你贴心的小帮手。说起来今天有什么我可以帮你的吗无论是学习、工作还是生活中的小烦恼我都乐意倾听和帮忙哦(•̀ᴗ•́)و Response code: 200; Time: 2185ms (2 s 185 ms); Content length: 114 bytes (114 B) 无法保留 Cookiecookie storage file is included in ignored list: C:\Users\86134\IdeaProjects\AlibabaAi\.idea\httpRequests\http-client.cookiesGET http://127.0.0.1:18080/helloworld/advisor/chat/123?query%E4%BD%A0%E5%A5%BD%EF%BC%8C%E6%88%91%E5%8F%AB%E5%A4%A7%E9%BE%84%E7%A0%81%E5%86%9C%EF%BC%8C%E4%B9%8B%E5%90%8E%E7%9A%84%E4%BC%9A%E8%AF%9D%E4%B8%AD%E9%83%BD%E5%B8%A6%E4%B8%8A%E6%88%91%E7%9A%84%E5%90%8D%E5%AD%97 HTTP/1.1 200 Content-Type: text/plain;charsetUTF-8 Transfer-Encoding: chunked Date: Thu, 18 Sep 2025 01:56:20 GMT 你好大龄码农很高兴认识你这个名字听起来既亲切又有故事感。在接下来的对话中我会记得带上你的名字。有什么问题或话题想聊吗大龄码农 Response code: 200; Time: 1661ms (1 s 661 ms); Content length: 70 bytes (70 B) 无法保留 Cookiecookie storage file is included in ignored list: C:\Users\86134\IdeaProjects\AlibabaAi\.idea\httpRequests\http-client.cookies

更多文章