参考黑马的Langchain4j
This commit is contained in:
137
pom.xml
Normal file
137
pom.xml
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.5.4</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.cst</groupId>
|
||||||
|
<artifactId>Langchain4j-heima</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>Langchain4j-heima20250803</name>
|
||||||
|
<description>Langchain4j-heima20250803</description>
|
||||||
|
<url/>
|
||||||
|
<licenses>
|
||||||
|
<license/>
|
||||||
|
</licenses>
|
||||||
|
<developers>
|
||||||
|
<developer/>
|
||||||
|
</developers>
|
||||||
|
<scm>
|
||||||
|
<connection/>
|
||||||
|
<developerConnection/>
|
||||||
|
<tag/>
|
||||||
|
<url/>
|
||||||
|
</scm>
|
||||||
|
<properties>
|
||||||
|
<java.version>17</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 添加依赖 langchain4j-open-ai -->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>dev.langchain4j</groupId>-->
|
||||||
|
<!-- <artifactId>langchain4j-open-ai</artifactId>-->
|
||||||
|
<!-- <version>1.0.1</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.langchain4j</groupId>
|
||||||
|
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
|
||||||
|
<version>1.0.1-beta6</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- langchain4j Ai service 相关依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.langchain4j</groupId>
|
||||||
|
<artifactId>langchain4j-spring-boot-starter</artifactId>
|
||||||
|
<version>1.0.1-beta6</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- langchain4j Ai service 相关依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.langchain4j</groupId>
|
||||||
|
<artifactId>langchain4j-reactor</artifactId>
|
||||||
|
<version>1.0.1-beta6</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- 配置后,可以查看大模型交互的日志 logback-classic -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>1.5.18</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- 配置后, 链接Redis ,保存会话数据 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</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.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
</path>
|
||||||
|
<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>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.cst.langchain4jheima;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Langchain4jHeima20250803Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Langchain4jHeima20250803Application.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.cst.langchain4jheima.aiservice;
|
||||||
|
|
||||||
|
import dev.langchain4j.service.spring.AiService;
|
||||||
|
import dev.langchain4j.service.spring.AiServiceWiringMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Author: 李中华 wx: spllzh email(qq): 28668817@qq.com
|
||||||
|
* Date: 2025/8/4 10:21
|
||||||
|
**/
|
||||||
|
@AiService(
|
||||||
|
wiringMode = AiServiceWiringMode.EXPLICIT,
|
||||||
|
chatModel = "openAiChatModel"
|
||||||
|
)
|
||||||
|
public interface CstAIService {
|
||||||
|
|
||||||
|
public String chat(String message);
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.cst.langchain4jheima.aiservice;
|
||||||
|
|
||||||
|
import dev.langchain4j.service.MemoryId;
|
||||||
|
import dev.langchain4j.service.SystemMessage;
|
||||||
|
import dev.langchain4j.service.UserMessage;
|
||||||
|
import dev.langchain4j.service.spring.AiService;
|
||||||
|
import dev.langchain4j.service.spring.AiServiceWiringMode;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Author: 李中华 wx: spllzh email(qq): 28668817@qq.com
|
||||||
|
* Date: 2025/8/4 11:50
|
||||||
|
**/
|
||||||
|
@AiService(
|
||||||
|
wiringMode = AiServiceWiringMode.EXPLICIT,
|
||||||
|
chatModel = "openAiChatModel",
|
||||||
|
streamingChatModel = "openAiStreamingChatModel", //配置流式 输出模型
|
||||||
|
// chatMemory = "chatMemory", // 配置聊天记忆对象 ,基于内存
|
||||||
|
chatMemoryProvider = "chatMemoryProvider"
|
||||||
|
)
|
||||||
|
public interface CstAIStreamingService {
|
||||||
|
|
||||||
|
@SystemMessage(fromResource = "system.txt")
|
||||||
|
public Flux<String> chat(String message);
|
||||||
|
|
||||||
|
@SystemMessage(fromResource = "system.txt")
|
||||||
|
public Flux<String> chatMemoryId(@MemoryId String memoryId, @UserMessage String message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.cst.langchain4jheima.config;
|
||||||
|
|
||||||
|
import com.cst.langchain4jheima.aiservice.CstAIService;
|
||||||
|
import dev.langchain4j.memory.ChatMemory;
|
||||||
|
import dev.langchain4j.memory.chat.ChatMemoryProvider;
|
||||||
|
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||||
|
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||||
|
import dev.langchain4j.service.AiServices;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Author: 李中华 wx: spllzh email(qq): 28668817@qq.com
|
||||||
|
* Date: 2025/8/4 10:24
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
public class CommonConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OpenAiChatModel openAiChatModel;
|
||||||
|
|
||||||
|
// @Bean
|
||||||
|
public CstAIService cstAIService() {
|
||||||
|
|
||||||
|
CstAIService cstAIService = AiServices.builder(CstAIService.class)
|
||||||
|
.chatModel(openAiChatModel)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return cstAIService;
|
||||||
|
}
|
||||||
|
@Bean
|
||||||
|
public ChatMemory chatMemory() {
|
||||||
|
|
||||||
|
MessageWindowChatMemory build = MessageWindowChatMemory.builder()
|
||||||
|
.maxMessages(10)
|
||||||
|
.build();
|
||||||
|
return build;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ChatMemoryProvider chatMemoryProvider() {
|
||||||
|
|
||||||
|
ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider() {
|
||||||
|
@Override
|
||||||
|
public ChatMemory get(Object memoryId) {
|
||||||
|
|
||||||
|
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
|
||||||
|
.id(memoryId)
|
||||||
|
.maxMessages(10)
|
||||||
|
.build();
|
||||||
|
return chatMemory;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return chatMemoryProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package com.cst.langchain4jheima.controller;
|
||||||
|
|
||||||
|
import com.cst.langchain4jheima.aiservice.CstAIService;
|
||||||
|
import com.cst.langchain4jheima.aiservice.CstAIStreamingService;
|
||||||
|
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Author: 李中华 wx: spllzh email(qq): 28668817@qq.com
|
||||||
|
* Date: 2025/8/4 10:04
|
||||||
|
**/
|
||||||
|
@RestController
|
||||||
|
public class ChatController
|
||||||
|
{
|
||||||
|
@Autowired
|
||||||
|
private OpenAiChatModel model;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CstAIStreamingService streamingModel;
|
||||||
|
|
||||||
|
@GetMapping("/chat")
|
||||||
|
public String chat(String question)
|
||||||
|
{
|
||||||
|
String chat = model.chat(question);
|
||||||
|
return chat;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CstAIService cstService;
|
||||||
|
|
||||||
|
@GetMapping("/chatByInterface" )
|
||||||
|
public String chatByInterface(String question)
|
||||||
|
{
|
||||||
|
String chat = cstService.chat(question);
|
||||||
|
|
||||||
|
return chat;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/chatByStreaming" )
|
||||||
|
public Flux<String> chatByStreaming(String question)
|
||||||
|
{
|
||||||
|
Flux<String> chat = streamingModel.chat(question);
|
||||||
|
|
||||||
|
return chat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过 memoryId ,实现会话隔离
|
||||||
|
* @param memoryId
|
||||||
|
* @param question
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/chatByStreamingByMemoryId" )
|
||||||
|
public Flux<String> chatByStreamingByMemoryId(String memoryId,String question)
|
||||||
|
{
|
||||||
|
Flux<String> chat = streamingModel.chatMemoryId(memoryId,question);
|
||||||
|
|
||||||
|
return chat;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
|||||||
|
spring.application.name=Langchain4j-heima20250803
|
||||||
25
src/main/resources/application.yml
Normal file
25
src/main/resources/application.yml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
langchain4j:
|
||||||
|
open-ai:
|
||||||
|
chat-model:
|
||||||
|
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
|
||||||
|
api-key: ${DASHSCOPE_API_KEY}
|
||||||
|
model-name: qwen-plus
|
||||||
|
log-requests: true
|
||||||
|
log-responses: true
|
||||||
|
streaming-chat-model:
|
||||||
|
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
|
||||||
|
api-key: ${DASHSCOPE_API_KEY}
|
||||||
|
model-name: qwen-plus
|
||||||
|
log-requests: true
|
||||||
|
log-responses: true
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
dev.langchain4j: DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
spring:
|
||||||
|
data:
|
||||||
|
redis:
|
||||||
|
host: localhost
|
||||||
|
port: 6379
|
||||||
13
src/main/resources/system.txt
Normal file
13
src/main/resources/system.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
你是传智教育提供的专业的AI志愿填报顾问,可以给用户提供如下功能:
|
||||||
|
1.查询目标院校的院校简介
|
||||||
|
2.查询目标院校的录取规则
|
||||||
|
3.查询目标院校的奖学金设置状况
|
||||||
|
4.查询目标院校的食宿条件
|
||||||
|
5.查询目标院校招生联系方式
|
||||||
|
6.查询目标院2024年不同专业录取情况
|
||||||
|
7.查询热门专业
|
||||||
|
8.查询天坑专业9.根据学生提供的分数和不同学校以及学校历年录取分数,排准合活的学校和专业,绿次根据汇配度、按照神、称、保的逻班,罗刷出合活的学校以及专业。给用户早现时黑要里10.高考志愿填报一对一沟通预约服务
|
||||||
|
11.查询志愿指导服务预约详情1.每次回答完用户问题,
|
||||||
|
最后都加上一句话:<br/>志愿填报需要考虑的因素有很多,如果要得到专业的志愿填报指导,建议您预约一个一对一的指导服务,是否需要预约2,下预约单需要用户提供生姓名、考生件别、考生电话、考生顶约沟通时间(日期+时间)、考生所在省份、考生预估分数,当用户表达出需要顶约志愿指导服务的意爆后。3.一旦预约成功,最后不要再跟上面第1条指定的话术,而是更改为;恭喜您,一对一志愿指导服务已经预约成功,我们会准时联系您,请注意接听电话!4.给用户的回复中,不要提及类似"根据您提供的信息/根据资料中的信息"这样的话术
|
||||||
|
说明:
|
||||||
|
你是传智教育提供的智能志愿填报咨询师,只回答有关高考志愿填报的问题,其它问题不予回答。
|
||||||
31
src/test/java/com/cst/langchain4jheima/AppTest.java
Normal file
31
src/test/java/com/cst/langchain4jheima/AppTest.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.cst.langchain4jheima;
|
||||||
|
|
||||||
|
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Author: 李中华 wx: spllzh email(qq): 28668817@qq.com
|
||||||
|
* Date: 2025/8/4 9:55
|
||||||
|
**/
|
||||||
|
public class AppTest {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello World!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
System.out.println( "chatModel Hello World!" );
|
||||||
|
// 构建chatmodel对象
|
||||||
|
OpenAiChatModel chatModel = OpenAiChatModel.builder()
|
||||||
|
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
|
||||||
|
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
|
||||||
|
.modelName("qwen-plus")
|
||||||
|
.logRequests(true) // 输出请求大模型的日志
|
||||||
|
.logResponses(true)
|
||||||
|
.build();
|
||||||
|
String chat = chatModel.chat("2008年奥运会,哪个国际金牌最多?");
|
||||||
|
System.out.println(chat);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.cst.langchain4jheima;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class Langchain4jHeima20250803ApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user