LangChain系列之LangChain4j集成Spring Bot
<<< 书接上文
2. 代码示例
以下是一个集成 LangChain4j API 的 Spring Boot 应用示例。
2.1 创建 Spring Boot 项目
你可以使用SpringInitializr
(https://start.spring.io/)来创建一个 Spring Boot 项目。选择 Maven 项目、Java 语言以及合适的 Spring Boot 版本。为了将 LangChain4j 集成到 Spring Boot 中,我们需要先在项目的 pom.xml
文件中添加相应的依赖:
<dependency><groupId>com.example</groupId><artifactId>langchain4j-open-ai</artifactId><version>your_jar_version</version>
</dependency>
2.2 创建配置类
在你的Spring Boot应用中创建一个配置类,用于配置 LangChain4j API。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.langchain4j.openai.OpenAiClient;@Configuration
public class LangChainConfig {@Beanpublic OpenAiClient openAiClient() {return new OpenAiClient("YOUR_API_KEY");}
}
这个配置类定义了一个使用@Configuration
注解的
LangChainConfig
类,标识它为Spring
容器中的配置类。openAiClient()方法使用
@Bean
注解,创建并返回一个带有你的 API 密钥的 OpenAiClient
实例。这个 Bean 随后可以注入到 Spring Boot 应用的其他组件中使用。
2.3 初始化 ChatModel
在配置好 Spring Boot 应用后,我们可以初始化一个 ChatModel,用于与语言模型进行交互。
import com.example.langchain4j.openai.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class ChatService {private final ChatModel chatModel;@Autowiredpublic ChatService(OpenAiClient openAiClient) {this.chatModel = openAiClient.createChatModel();}public String chat(String userPrompt) {return chatModel.sendUserPrompt(userPrompt).getResponse();}
}
在这个服务类中,我们定义了一个使用 @Service
注解的 ChatService
,将其标识为Spring的服务组件。构造函数使用@Autowired
注解注入 OpenAiClient
实例。然后通过 OpenAiClient
的
createChatModel()
方法初始化chatModel
。chat()
方法用于向聊天模型发送用户输入的提示,并返回模型的响应。
2.4 第一次调用 LLM
服务准备好后,我们可以对语言模型的调用。下面创建一个简单的控制器来测试这个功能:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ChatController {private final ChatService chatService;@Autowiredpublic ChatController(ChatService chatService) {this.chatService = chatService;}@GetMapping("/chat")public String chat(@RequestParam String prompt) {return chatService.chat(prompt);}
}
这个ChatController
类使用
@RestController注解,标识为一个RESTful Web服务控制器。它定义了一个/chat
的GET接口,接受一个名为 prompt
的参数。chat()
方法调用了 ChatService
中的 chat()
方法,并将响应结果返回给客户端。
2.5 运行你的应用
现在,你可以启动 Spring Boot 应用,并通过带有提示语(prompt)的请求访问 /chat
接口:
http://localhost:8080/chat?prompt=Hello
响应信息如下:
Hello! How can I assist you today?
3. 发送系统和用户提示
LangChain4j 支持向语言模型发送系统提示和用户提示。系统提示是由系统提供的指令或上下文,用于引导模型的行为和回答方向;用户提示则是用户输入的内容或问题,用来请求信息或执行任务。
下面是如何发送这两种提示的示例:
import com.example.langchain4j.openai.ChatModel;
import com.example.langchain4j.openai.ChatPrompt;public class ChatService {private final ChatModel chatModel;@Autowiredpublic ChatService(OpenAiClient openAiClient) {this.chatModel = openAiClient.createChatModel();}public String chatWithSystemPrompt(String systemPrompt, String userPrompt) {ChatPrompt prompt = new ChatPrompt();prompt.addSystemPrompt(systemPrompt);prompt.addUserPrompt(userPrompt);return chatModel.sendPrompt(prompt).getResponse();}
}
在这个更新后的ChatService
类中,我们定义了一个新方法chatWithSystemPrompt()
,用于创建一个ChatPrompt
对象。通过 addSystemPrompt()
方法添加系统提示,再通过addUserPrompt() 方法添加用户提示。最后,将组合后的提示发送给聊天模型,并返回模型的响应结果。
System prompt: "You are a helpful assistant."
User prompt: "What is the capital of France?"
Response: "The capital of France is Paris."
4. 结论
将 LangChain4j 集成到 Spring Boot 中,为与语言模型的交互提供了强大且稳定的框架。通过本文介绍的步骤,你可以搭建起使用 LangChain4j 的 Spring Boot应用,初始化ChatModel,并向语言模型发送提示以获取响应。此集成使你能够高效地在 Java 应用中利用语言模型的强大能力。