企业知识库问答系统(1.调用大模型)
Contents
企业知识库问答系统(1.调用大模型)
技术栈
LangChain4j
Spring Boot
Java21
Maven
过程
1.项目初始化
新建maven项目,pom文件引入依赖如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.homecj</groupId>
<artifactId>aiLearning</artifactId>
<version>1.0-SNAPSHOT</version>
<name>aiLearning</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>1.1.0-beta7</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>1.1.0-beta7</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-mcp</artifactId>
<version>1.1.0-beta7</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
<version>1.1.0-beta7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.引入通义模型
使用langchain4j提供的ChatModel.builder,传入apiKey和modelName参数即可定义一个通义模型。这里Spring会注册bean name为myQwenChatModel,类型ChatModel的bean。
@Configuration
@ConfigurationProperties(prefix = "langchain4j.community.dashscope.chat-model")
@Data
public class QwenConfig {
private String apiKey;
private String modelName;
@Bean
public ChatModel myQwenChatModel(){
return QwenChatModel.builder()
.apiKey(apiKey)
.modelName(modelName)
.build();
}
}
3.调用模型
调用刚才引入的模型,使用Autowired获取ChatModel类型的bean。
@Service
@Slf4j
public class Chat {
@Autowired
private ChatModel qwenModel;
public static final String SYSTEM_MESSAGE = """
你是一个聊天机器人,回复用户提出的问题。
""";
public String chat(String message){
SystemMessage systemMessage = SystemMessage.from(SYSTEM_MESSAGE);
UserMessage userMessage = UserMessage.from(message);
ChatResponse chatResponse = qwenModel.chat(systemMessage, userMessage);
AiMessage aiMessage = chatResponse.aiMessage();
log.info("AI 输出:" + aiMessage.toString());
return aiMessage.text();
}
}
4.测试
@SpringBootTest
public class ChatTest {
@Autowired
private QwenChat chatTemplate;
@Test
void chat(){
chatTemplate.chat("你好,你是谁");
}
}
输出如下
2025-11-12T14:37:42.084+08:00 INFO 17560 --- [aiLearning] [ main] cn.homecj.aiLearning.ai.QwenChat : AI 输出:AiMessage { text = "你好,我是来自阿里云的超大规模语言模型,我叫通义千问。" toolExecutionRequests = [] }