1. 环境准备与项目初始化
想要在SpringBoot项目中玩转百炼大模型Qwen的流式对话功能,首先得把开发环境搭建好。这里我推荐使用JDK 17和SpringBoot 3.4.2的组合,实测下来兼容性最好。别小看版本选择,我之前用JDK 11踩过坑,有些依赖死活装不上。
安装完基础环境后,打开你熟悉的IDE(我用的是IntelliJ IDEA),新建一个SpringBoot项目。这里有个小技巧:创建项目时可以直接勾选Web和Lombok依赖,省得后面手动添加。项目建好后,第一件事就是配置pom.xml文件。
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.4.2</spring-boot.version>
<langchain4j.version>1.1.0-beta7</langchain4j.version>
</properties>
<dependencies>
<!-- SpringBoot基础依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- LangChain4j核心依赖 -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
</dependency>
<!-- 阿里云百炼集成包 -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
</dependency>
</dependencies>
配置完依赖后,别忘了去阿里云百炼平台申请API Key。这个步骤很简单,注册登录后进入控制台,找到"API密钥管理"就能创建。建议把API Key保存在安全的地方,后面配置要用到。
2. 配置与基础调用
有了API Key,我们就可以配置应用了。在resources目录下新建application.yml文件,加入以下配置:
server:
port: 8080
langchain4j:
community:
dashscope:
chat-model:
api-key: your-api-key-here # 替换成你的真实API Ke

440

被折叠的 条评论
为什么被折叠?



