当前位置: 首页 > article >正文

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() 方法初始化chatModelchat()方法用于向聊天模型发送用户输入的提示,并返回模型的响应。

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 应用中利用语言模型的强大能力。

http://www.lryc.cn/news/2398193.html

相关文章:

  • Python爬虫解析动态网页:从渲染到数据提取
  • LLMs之MCP:如何使用 Gradio 构建 MCP 服务器
  • VBA模拟进度条
  • MySQL强化关键_019_索引优化
  • 高性能MCU的MPU与Cache优化详解
  • 关于list集合排序的常见方法
  • 不动产登记区块链系统(Vue3 + Go + Gin + Hyperledger Fabric)
  • 从 GPT 的发展看大模型的演进
  • 基于大模型的短暂性脑缺血发作(TIA)全流程预测与诊疗辅助系统详细技术方案
  • JSCH使用SFTP详细教程
  • Trae CN IDE 中 PHP 开发的具体流程和配置指南
  • 【Qt】构建目录设置
  • 【仿生机器人】极具前瞻性的架构——认知-情感-记忆“三位一体的仿生机器人系统架构
  • Web后端快速入门(Maven)
  • 机器学习算法:逻辑回归
  • 企业展示型网站模板HTML5网站模板下载指南
  • ArrayList和LinkedList(深入源码加扩展)
  • Unity UI 性能优化--Sprite 篇
  • AI健康小屋+微高压氧舱:科技如何重构我们的健康防线?
  • OpenCV C++ 学习笔记(五):颜色空间转换、数值类型转换、图像混合、图像缩放
  • 如何做接口测试?
  • 【JMeter】性能测试知识和工具
  • SOC-ESP32S3部分:25-HTTP请求
  • 字符编码全解析:ASCII、GBK、Unicode、UTF-8与ANSI
  • 《前端面试题:HTML5、CSS3、ES6新特性》
  • MaxCompute开发UDF和UDTF案例
  • 49套夏日小清新计划总结日系卡通ppt模板
  • 告别硬编码!用工厂模式优雅构建可扩展的 Spring Boot 应用 [特殊字符]
  • Express教程【006】:使用Express写接口
  • mongodb集群之分片集群