SpringBoot接入千问大模型 流式输出
环境准备
Spring Boot=2.7.6
maven=3.8.1
-
需要搭建好一个最基本的SpringBoot应用,至少包含
spring-boot-starter-web
和spring-boot-starter-webflux
依赖。 -
注册阿里云账号,开通模型使用权限。
阿里云控制台:https://www.aliyun.com/product/bailian
API文档:https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api
-
取得
sk-*********
样式的apiKey。 -
选择DashScope方式接入,导入SDK依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <version>2.16.9</version> </dependency>
-
构建一个简单的前端页面,用于发送和接收消息
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>AI Chat</title> <link rel="stylesheet" href="./css/styles.css"> </head> <body> <div class="chat-container"> <h1>AI Chat</h1> <div class="chat-messages" id="messages"> </div> <form class="chat-input" id="chat-form" action="#"> <input type="text" placeholder="输入消息..." required> <button type="submit" class="btn-primary">发送</button> <button type="button" class="btn-danger" id="btn-close-chat">停止</button> </form> </div> </body> </html>
body { font-family: Arial, sans-serif; background-color: #f4f4f9; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } h1 { text-align: center; color: #333; }