█ 什么是大模型?
大模型,英文名叫 Large Model,大型模型。早期的时候,也叫 Foundation Model,基础模型。
大模型是一个简称。完整的叫法,应该是 "人工智能预训练大模型"。预训练,是一项技术,我们后面再解释。
我们现在口头上常说的大模型,实际上特指大模型的其中一类,也是用得最多的一类------语言大模型(Large Language Model,也叫大语言模型,简称 LLM)。
除了语言大模型之外,还有视觉大模型、多模态大模型等。现在,包括所有类别在内的大模型合集,被称为广义的大模型。而语言大模型,被称为狭义的大模型。
从本质来说,大模型,是包含超大规模参数(通常在十亿个以上)的神经网络模型。
最简单的 JavaAI 程序
- 引入依赖
XML
<dependencies>
<!--langchain4j-->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
</dependency>
<!--springMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
- 编写配置文件
Scala
langchain4j:
open-ai:
# 一下配置均从具体的大模型开放平台获取
chat-model:
api-key: sk-xxxx# 密钥
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1 # 模型访问链接
model-name: qwen-max # 模型名称
国内流行的几个大模型开放平台:
- 百度千帆大模型:千帆大模型平台-百度智能云千帆
- 智谱 AI:智谱AI开放平台
- 阿里百炼大模型:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台
- 腾讯混元大模型:腾讯混元大模型_大语言模型_自然语言大模型- 腾讯云
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/5
* @apiNote chat控制器
*/
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private ChatLanguageModel chatLanguageModel;
@GetMapping("/qwen")
public String chatAi(String message) {
String generate = chatLanguageModel.generate(message);
return generate;
}
}
- 访问控制器
LangChain4j 高级
包含 Java 代码配置模型,日志配置
- 引入依赖
XML
<dependencies>
<!--日志配置-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--LangChain4j-->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<!--LangChain4j-->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
</dependencies>
-
编写日志配置文件
logging:
level:
dev:
langchain4j: DEBUG
ai4j:
openai4j: DEBUG -
自定义模型
java
/**
* @version v1.0
* @apiNote 接口
* @author OldGj 2024/12/6
*/
public interface IServiceAssistant {
String chat(String message);
}
- 编写配置类
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 配置类
*/
@Configuration
public class LLMConfig {
/**
* 提供模型源信息
* @return
*/
@Bean
public ChatLanguageModel getChatLanguageModel() {
return OpenAiChatModel.builder()
.apiKey("sk-xxxx") // API密钥
.logRequests(true) // 开启请求日志
.logResponses(true) // 开启响应日志
.modelName("qwen-max") // 模型名称
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.build();
}
/**
* 不同AI服务与模型的解耦
* @return AI服务
*/
@Bean
public IServiceAssistant getIServiceAssistant() {
// 解读:IServiceAssistant这个AI服务类使用的大模型是 >> getChatLanguageModel()这个方法返回的大模型
return AiServices.builder(IServiceAssistant.class)
.chatLanguageModel(getChatLanguageModel())
.build();
}
}
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 控制器
*/
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private ChatLanguageModel chatLanguageModel;
@Autowired
private IServiceAssistant iServiceAssistant;
@GetMapping("/qwen")
public String chat(String message) {
return chatLanguageModel.generate(message);
}
// 自定义的AI服务,本质上使用的模型还是千问,和上面的接口一样
@GetMapping("/assistant")
public String chat2(String message) {
return iServiceAssistant.chat(message);
}
}
LangChain4j 流式输出
- 引入依赖
XML
<dependencies>
<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
<version>0.36.2</version>
</dependency>
</dependencies>
- 日志配置文件
java
logging:
level:
dev:
langchain4j: DEBUG
ai4j:
openai4j: DEBUG
- 编写 AI 服务接口「注意返回值为
Flux<String>
类型」
java
/**
* @version v1.0
* @apiNote AI服务接口
* @author OldGj 2024/12/6
*/
public interface IServiceAssistant {
Flux<String> chat(String message);
}
- 编写配置类
支持流式输出需要StreamingChatLanguageModel
这个 Bean
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 配置类
*/
@Configuration
public class LLMConfig {
/**
* 提供模型源信息 >> 流式输出
* @return
*/
@Bean
public StreamingChatLanguageModel streamingChatLanguageModel() {
return OpenAiStreamingChatModel.builder()
.apiKey("sk-xxxx") // 密钥
.logRequests(true) // 开启日志
.logResponses(true)
.modelName("qwen-max") // 模型名称
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.build();
}
/**
* 提供流式输出功能的AI服务
* @return
*/
@Bean
public IServiceAssistant getIServiceAssistant() {
return AiServices.builder(IServiceAssistant.class)
.streamingChatLanguageModel(streamingChatLanguageModel())
.build();
}
}
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 控制器
*/
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private IServiceAssistant iServiceAssistant;
@GetMapping("/assistant")
public Flux<String> chat(String message) {
return iServiceAssistant.chat(message);
}
}
- 结果展示
LangChain4j 视觉理解
- 引入依赖
XML
<dependencies>
<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
<version>0.36.2</version>
</dependency>
</dependencies>
- 编写配置文件
XML
logging:
level:
dev:
langchain4j: DEBUG
ai4j:
openai4j: DEBUG
spring:
servlet:
multipart:
max-file-size: 5MB
max-request-size: 5MB
- 编写模型配置类
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 配置类
*/
@Configuration
public class LLMConfig {
/**
* 提供模型源信息
* @return
*/
@Bean
public ChatLanguageModel getChatLanguageModel() {
return OpenAiChatModel.builder()
.apiKey("sk-xxxx") // 密钥
.logRequests(true) // 开启日志
.logResponses(true)
.modelName("qwen-vl-max") // 模型名称
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.build();
}
}
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 控制器
*/
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private ChatLanguageModel chatLanguageModel;
/**
* AI视觉理解
* @param file 上传的图片
* @param message 理解的方向
*/
@PostMapping("/image")
public String image(MultipartFile file, String message) throws IOException {
// 获得图片的base64编码
InputStream inputStream = file.getInputStream();
byte[] bytes = inputStream.readAllBytes();
String base64 = Base64.getEncoder().encodeToString(bytes);
// 构造用户数据
UserMessage userMessage =
UserMessage.from(
TextContent.from(message),
ImageContent.from(base64, "image/jpg")
);
return chatLanguageModel.generate(userMessage).content().text();
}
}
- 结果展示
LangChain4j 文生图
- 配置依赖
XML
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-zhipu-ai</artifactId>
<version>0.36.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-zhipu-ai</artifactId>
<version>0.36.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
<version>0.36.2</version>
</dependency>
</dependencies> <groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
<version>0.36.2</version>
</dependency>
</dependencies>
- 编写配置类
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 配置类
*/
@Configuration
public class LLMConfig {
@Bean
public ImageModel imageModel() {
return ZhipuAiImageModel.builder()
.apiKey("xxxx")
.connectTimeout(Duration.ofSeconds(10000))
.callTimeout(Duration.ofSeconds(10000))
.readTimeout(Duration.ofSeconds(10000))
.writeTimeout(Duration.ofSeconds(10000))
.logRequests(true)
.logResponses(true)
.build();
}
}
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 控制器
*/
@RestController
@RequestMapping("/generate")
public class ChatController {
@Autowired
private ImageModel imageModel;
@GetMapping("/image")
public String generateImage(String msg) {
Response<Image> imageResponse = imageModel.generate(msg);
return imageResponse.content().url().toString();
}
}
LangChain4j 文本记忆
- 配置依赖
XML
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapdb</groupId>
<artifactId>mapdb</artifactId>
<version>3.0.10</version>
</dependency>
</dependencies>
- 编写配置类
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 配置类
*/
@Configuration
public class LLMConfig {
/**
* 提供模型源信息
* @return
*/
@Bean
public ChatLanguageModel getChatLanguageModel() {
return OpenAiChatModel.builder()
.apiKey("sk-xxxx") // 密钥
.logRequests(true) // 开启日志
.logResponses(true)
.modelName("qwen-max") // 模型名称
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.build();
}
@Bean
public IServiceAssistant getIServiceAssistant() {
return AiServices.builder(IServiceAssistant.class)
.chatLanguageModel(getChatLanguageModel())
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10)) // 聊天会话的最大条数
.build();
}
}
- 编写 AI 接口
java
/**
* @version v1.0
* @apiNote 接口
* @author OldGj 2024/12/6
*/
public interface IServiceAssistant {
String chat(@MemoryId Long userId, @UserMessage String message);
}
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 控制器
*/
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private ChatLanguageModel chatLanguageModel;
@Autowired
private IServiceAssistant iServiceAssistant;
@GetMapping("/qwen")
public String chat(String message) {
return chatLanguageModel.generate(message);
}
@GetMapping("/memory")
public String chat2(Long userId, String message) {
return iServiceAssistant.chat(userId, message);
}
}
LangChain4j 本地文本记忆
mapdb 入门
java
public class AppTest extends TestCase {
/**
* mapdb写入
*/
public void test() {
// 创建或打开一个DB文件
File dbFile = new File("mydb");
DB db = DBMaker.fileDB(dbFile).make();
// 创建一个HashMap并设置名称
HTreeMap<String, String> map = db.hashMap("myMap")
.keySerializer(Serializer.STRING)
.valueSerializer(Serializer.STRING).createOrOpen();
// 在Map中存储数据
map.put("key1", "value1");
map.put("key2", "value2");
// 提交事务以确保数据被持久化到磁盘
db.commit();
// 关闭数据库连接
db.close();
}
/**
* mapdb读取
*/
public void test2() {
File dbFile = new File("mydb");
// 重新打开数据库以检索数据
DB db = DBMaker.fileDB(dbFile).make();
// 从Map中检索数据
HTreeMap<String, String> map = db.hashMap("myMap")
.keySerializer(Serializer.STRING)
.valueSerializer(Serializer.STRING).createOrOpen();
String value1 = map.get("key1");
String value2 = map.get("key2");
System.out.println("Value for key1: " + value1);
System.out.println("Value for key2: " + value2);
// 关闭数据库连接
db.close();
}
}
- 配置依赖
XML
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapdb</groupId>
<artifactId>mapdb</artifactId>
<version>3.0.10</version>
</dependency>
</dependencies>
- 编写 AI 接口
java
/**
* @version v1.0
* @apiNote 接口
* @author OldGj 2024/12/6
*/
public interface IServiceAssistant {
String chat(@MemoryId Integer userId, @UserMessage String message);
}
- 自定义的持久化聊天存储器
java
/**
* @version v1.0
* @author OldGj 2024/12/7
* @apiNote 自定义的持久化聊天存储器 - 使用MapDB进行本地缓存
*/
public class ChatMemoryStoreByMapDB implements ChatMemoryStore {
// 创建 MapDB
private final DB db = DBMaker.fileDB("./chat-memory.db")
.transactionEnable()
.make();
// 创建map对象
private final Map<Integer, String> map = db.hashMap("messages")
.keySerializer(Serializer.INTEGER)
.valueSerializer(Serializer.STRING)
.createOrOpen();
/**
* 获取消息
* @param
* @return
*/
@Override
public List<ChatMessage> getMessages(Object memoryId) {
String json = map.get((Integer) memoryId);
return ChatMessageDeserializer.messagesFromJson(json);
}
/**
* 更新消息
* @param
* @param list
*/
@Override
public void updateMessages(Object memoryId, List<ChatMessage> list) {
String json = ChatMessageSerializer.messagesToJson(list);
map.put((Integer) memoryId, json);
db.commit();
}
/**
* 删除消息
* @param
*/
@Override
public void deleteMessages(Object memoryId) {
map.remove((Integer) memoryId);
db.commit();
}
}
- 编写配置类
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 配置类
*/
@Configuration
public class LLMConfig {
/**
* 提供模型源信息
* @return
*/
@Bean
public ChatLanguageModel getChatLanguageModel() {
return OpenAiChatModel.builder()
.apiKey("sk-xxxx") // 密钥
.logRequests(true) // 开启日志
.logResponses(true)
.modelName("qwen-max") // 模型名称
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.build();
}
@Bean
public IServiceAssistant getIServiceAssistant() {
return AiServices.builder(IServiceAssistant.class)
.chatLanguageModel(getChatLanguageModel())
.chatMemoryProvider(chatMemoryProvider()) // 聊天存储器生产者
.build();
}
@Bean
public ChatMemoryStore chatMemoryStore() {
return new ChatMemoryStoreByMapDB();
}
@Bean
public ChatMemoryProvider chatMemoryProvider() {
return memoryId -> MessageWindowChatMemory.builder()
// 设置消息窗口 ID
.id(memoryId)
// 设置消息最大条数,默认为 10
.maxMessages(20)
// 设置聊天存储器,使用自定义的
.chatMemoryStore(chatMemoryStore())
.build();
}
}
- 编写控制器
java
/**
* @version v1.0
* @author OldGj 2024/12/6
* @apiNote 控制器
*/
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private IServiceAssistant iServiceAssistant;
@GetMapping("/memory")
public String chat2(Integer userId, String message) {
return iServiceAssistant.chat(userId, message);
}
}