LangChain4j 入门教程
1. LangChain4j 介绍
LangChain4j 是一个用于简化 Java 应用程序集成大型语言模型(LLMs)的库。它提供了统一的 API、丰富的工具箱和众多示例,帮助你快速构建各种 LLM 驱动的应用。
支持的模型
LangChain4j 支持多种大型语言模型,包括但不限于:
- OpenAI 的 GPT-3
- Anthropic 的 Claude
- 腾讯的 Mixie
- 智谱AI 的 Qwen
2. LangChain4j 的使用
单次对话示例
以下是一个简单的单次对话代码示例:
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
public class SingleChatDemo {
public static void main(String[] args) {
ChatLanguageModel model = OpenAiChatModel.withApiKey("your-api-key");
String response = model.generate("Hello, how are you?");
System.out.println(response);
}
}
多轮对话示例
多轮对话需要使用 ChatMemory
来维持上下文:
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.chains.conversation.ConversationalChain;
public class MultiChatDemo {
public static void main(String[] args) {
ChatLanguageModel model = OpenAiChatModel.withApiKey("your-api-key");
ChatMemory memory = MessageWindowChatMemory.withMaxMessages(5);
ConversationalChain chain = ConversationalChain.builder()
.chatLanguageModel(model)
.chatMemory(memory)
.build();
String response = chain.execute("Hello, how are you?");
System.out.println(response);
response = chain.execute("What's the weather like today?");
System.out.println(response);
}
}
流式响应示例
流式响应可以实时获取模型的输出:
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.chains.conversation.TokenStream;
public class StreamingChatDemo {
public static void main(String[] args) {
ChatLanguageModel model = OpenAiChatModel.withApiKey("your-api-key");
TokenStream tokenStream = model.stream("Tell me a story.");
tokenStream.onNext(token -> System.out.print(token))
.onError(error -> error.printStackTrace())
.start();
}
}
3. 整合 Spring Boot
添加依赖
在 Spring Boot 项目的 pom.xml
文件中添加 LangChain4j Spring Boot Starter 依赖:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>0.34.0</version>
</dependency>
配置模型参数
在 application.properties
或 application.yml
文件中配置模型参数:
langchain4j.open-ai.chat-model.api-key=your-api-key
langchain4j.open-ai.chat-model.model-name=gpt-4o
创建控制器
创建一个 Spring Boot 控制器,并注入 ChatLanguageModel
:
import dev.langchain4j.model.chat.ChatLanguageModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/chat")
public class ChatController {
private final ChatLanguageModel chatLanguageModel;
@Autowired
public ChatController(ChatLanguageModel chatLanguageModel) {
this.chatLanguageModel = chatLanguageModel;
}
@GetMapping("/single")
public String singleChat(String message) {
return chatLanguageModel.generate(message);
}
@GetMapping("/multi")
public String multiChat(String message) {
// 实现多轮对话逻辑
return "response";
}
}
流式响应控制器
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
public class StreamingChatController {
private final StreamingChatLanguageModel streamingChatLanguageModel;
@Autowired
public StreamingChatController(StreamingChatLanguageModel streamingChatLanguageModel) {
this.streamingChatLanguageModel = streamingChatLanguageModel;
}
@GetMapping("/streaming")
public Flux<String> streamingChat(String message) {
return streamingChatLanguageModel.stream(message);
}
}
总结
通过以上步骤,你可以在 Spring Boot 项目中成功整合 LangChain4j,并实现单次对话、多轮对话和流式输出。这使得你的应用程序能够利用大型语言模型的强大功能,同时提供流畅的用户体验。