Spring Boot集成LangChain4j

Spring Boot集成LangChain4j

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?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>

<!-- 继承Spring Boot父POM,提供默认配置和依赖管理 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- 优先从本地查找父POM -->
</parent>

<!-- 项目坐标信息 -->
<groupId>com.lixiang</groupId>
<artifactId>langchan4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>langchan4j</name>
<description>Demo project for Spring Boot</description>

<!-- 项目元信息 -->
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>

<!-- 自定义属性 -->
<properties>
<java.version>17</java.version> <!-- 指定Java版本 -->
</properties>

<!-- 项目依赖 -->
<dependencies>
<!-- Spring Boot Web Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Lombok 简化代码 -->
<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>

<!-- LangChain4J 相关依赖 -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>1.0.0-beta3</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>1.0.0-beta3</version>
</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>

<!-- Spring Boot Maven插件 -->
<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>
1
2
langchain4j.community.dashscope.chat-model.api-key=sk-937aee4c3e***********654d04b84634d
langchain4j.community.dashscope.chat-model.model-name=qwen-plus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.lixiang.langchain4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.model.chat.ChatLanguageModel;
import jakarta.annotation.Resource;

/**
* 聊天控制器,提供与AI模型的交互接口
*/
@RestController // 标识这是一个REST控制器
public class ChatController {
/**
* 注入LangChain4J的聊天语言模型
* 具体实现由Spring Boot自动配置决定
*/
@Resource
ChatLanguageModel chatLanguageModel;


/**
* 处理聊天请求
* @param message 用户输入的消息,默认为"Hello"
* @return AI模型的回复
*/
@GetMapping("/chat") // 映射GET请求到/chat路径
public String model(@RequestParam(value = "message", defaultValue = "Hello") String message) {
// 调用语言模型的chat方法并返回结果
return chatLanguageModel.chat(message);
}
}

Spring Boot集成AiServices

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lixiang</groupId>
<artifactId>langchain4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ex00700</name>
<description>Demo project for Spring Boot</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.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>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>1.0.0-beta3</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>1.0.0-beta3</version>
</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>
1
2
langchain4j.community.dashscope.chat-model.api-key=sk-937aee4c3e***********654d04b84634d
langchain4j.community.dashscope.chat-model.model-name=qwen-plus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.lixiang.langchain4j;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;

@Configuration
public class LangChain4jConfiguration {
/*
* The following components will be automatically wired into the AI Service if
* available in the application context:
*
* ChatLanguageModel
* StreamingChatLanguageModel
* ChatMemory
* ChatMemoryProvider
* ContentRetriever
* RetrievalAugmentor
*/
@Bean
ChatMemory chatMemory() {
return MessageWindowChatMemory.withMaxMessages(10);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.lixiang.langchain4j;

import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;
import dev.langchain4j.service.spring.AiService;

/**
* AI助手服务接口
* 使用LangChain4j框架实现的AI服务,提供聊天和自我介绍功能
*/
@AiService
interface Assistant {
/**
* 与AI进行愤怒风格的对话
* @param userMessage 用户输入的消息内容
* @return AI愤怒风格的回复
*/
@UserMessage("你愤怒的说到:{{msg}}")
String chat(@V("msg") String userMessage);

/**
* 获取AI的自我介绍信息
* @return 包含姓名、年龄、性别、住址等信息的Person对象
*/
@SystemMessage("你是美籍华裔")
@UserMessage("请自我介绍,包含姓名、年龄、性别、住址等信息")
Person intro();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.lixiang.langchain4j;

import dev.langchain4j.model.output.structured.Description;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* 人员信息实体类
* 用于存储AI助手的个人信息
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Description("Person人员信息类")
public class Person {
/** 姓名 */
@Description("姓名")
private String name;

/** 年龄 */
private int age;

/** 性别 */
private String gender;

/** 住址 */
private String address;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.lixiang.langchain4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

/**
* 聊天控制器
* 提供与AI助手交互的RESTful API接口
*/
@RestController
public class ChatController {
@Resource
private Assistant assistant;

/**
* 处理聊天请求
* @param message 用户消息,默认值为"你欠的钱什么时候还?"
* @return AI助手的回复
*/
@GetMapping("/chat")
public String model(@RequestParam(value = "message", defaultValue = "你欠的钱什么时候还?") String message) {
return assistant.chat(message);
}

/**
* 获取AI助手的自我介绍信息
* @return 包含个人信息的Person对象
*/
@GetMapping("/intro")
public Person intro() {
return assistant.intro();
}
}