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

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.xdnf.cn/news/10721.html

相关文章:

  • AI“实体化”革命:具身智能如何重构体育、工业与未来生活
  • Android 中的 DataBinding 详解
  • 在图像分析算法部署中应对流行趋势的变化|文献速递-深度学习医疗AI最新文献
  • 大模型赋能:金融智能革命中的特征工程新纪元
  • 兼容老设备!EtherNet/IP转DeviceNet网关解决储能产线通讯难题
  • Celery 核心概念详解及示例
  • 深入解析C++引用:从别名机制到函数特性实践
  • 【语义分割专栏】2:U-net原理篇(由浅入深)
  • Docker 在 AI 开发中的实践:GPU 支持与深度学习环境的容器化
  • 【结构型模式】装饰器模式
  • Nginx+Tomcat 负载均衡群集
  • Ubuntu 22.04 安装 Nacos 记录
  • WordPress 6.5版本带来的新功能
  • 腾讯 ovCompose 开源,Kuikly 鸿蒙和 Compose DSL 开源,腾讯的“双”鸿蒙方案发布
  • 云原生时代 Kafka 深度实践:05性能调优与场景实战
  • Vue3中Axios的使用-附完整代码
  • sqlite3 命令行工具详细介绍
  • 虚拟现实教育终端技术方案——基于EFISH-SCB-RK3588的全场景国产化替代
  • DNS (Domain Name System) 域名系统 将域名解析为 IP 地址
  • OCC笔记:TopoDS_Edge上是否一定存在Geom_Curve
  • 【Vmware】虚拟机安装、镜像安装、Nat网络模式、本地VM8、ssh链接保姆篇(图文教程)
  • J. Adv. Res. | DAP-seq助力解析大麦HvbZIP87基因让小麦抗病又高产的新机制
  • 吃透 Golang 基础:数据结构之 Map
  • UGUI Text/TextMeshPro字体组件
  • 从一堆数字里长出一棵树:中序 + 后序构建二叉树的递归密码
  • chromedriver 下载失败
  • 阿里云百炼全解析:一站式大模型开发平台的架构与行业实践
  • 智启未来:AI重构制造业供应链的五大革命性突破
  • 鸿蒙仓颉语言开发实战教程:购物车页面
  • AI Agent开发第78课-大模型结合Flink构建政务类长公文、长文件、OA应用Agent