springboot 整合 langchain4j 實現(xiàn)簡單的問答功能
最近在學(xué)習(xí)langchain4j,本文將介紹如何使用langchain4j快速實現(xiàn)一個簡單的問答功能,幫助大家快速入門。
1. 工具
- JDK 17
- Maven 3.9.9
- IntelliJ IDEA 2024.3.4 (Community Edition)
2. apikey
可以優(yōu)先選擇阿里云百煉申請一個apikey,免費額度足夠測試使用。
3. springboot項目創(chuàng)建
3.1 項目創(chuàng)建
創(chuàng)建springboot項目,與其他springboot項目創(chuàng)建方式一樣,這里不再詳細(xì)介紹。
3.2 依賴引入
這里只提供了部分依賴示例,使用的版本是1.0.0-beta2,完整依賴可以參考langchain4j官方文檔,本文最后給出了完整pom.xml供大家參考。實際上三方依賴會經(jīng)常更新,隨著版本變化需要引入的依賴總是會有些區(qū)別,還是建議多查閱官方文檔。
<!-- springboot 父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.4</version> </parent> <dependencies> <!-- springboot web 模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- langchain4j 集成 springboot相關(guān)的依賴 --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> </dependency> </dependencies>
3.3 配置文件
application.yml 配置文件內(nèi)容如下:
langchain4j: open-ai: chat-model: log-requests: true log-responses: true temperature: 0.5 max-tokens: 4096 base-url: https://dashscope.aliyuncs.com/compatible-mode/v1 model-name: qwen-plus api-key: ${API_KEY} # 需要替換成自己申請的apikey # base-url: https://api.deepseek.com/v1 # model-name: deepseek-reasoner # api-key: ${API_KEY} logging.level.dev.langchain4j: DEBUG
4. 代碼編寫
4.1 啟動類
先準(zhǔn)備springboot啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4.2 controller
編寫一個接口,注入ChatLanguageModel,寫一個最簡單的接口測試。
import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.service.AiServices; import jakarta.annotation.Resource; 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 { @Resource ChatLanguageModel chatLanguageModel; @GetMapping("/chat") public String model(@RequestParam(value = "message") String message) { return chatLanguageModel.chat(message); } }
4.3 測試
啟動項目,用postman調(diào)用接口進行簡單測試:
參考
完整的pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.chenf24k.tools</groupId> <artifactId>cf-langchain</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.4</version> </parent> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <langchain4j.version>1.0.0-beta2</langchain4j.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-bom</artifactId> <version>${langchain4j.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> </dependency> <dependency> <groupId>org.mapdb</groupId> <artifactId>mapdb</artifactId> <version>3.0.9</version> <exclusions> <exclusion> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>app</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
到此這篇關(guān)于springboot 整合 langchain4j 實現(xiàn)簡單的問答功能的文章就介紹到這了,更多相關(guān)springboot langchain4j 問答內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中 IO 常用IO操作類繼承結(jié)構(gòu)分析
本篇文章小編為大家介紹,java中 IO 常用IO操作類繼承結(jié)構(gòu)分析。需要的朋友參考下2013-04-04java中 spring 定時任務(wù) 實現(xiàn)代碼
java中 spring 定時任務(wù) 實現(xiàn)代碼,需要的朋友可以參考一下2013-03-03詳解spring cloud hystrix請求緩存(request cache)
這篇文章主要介紹了詳解spring cloud hystrix請求緩存(request cache),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Java數(shù)據(jù)結(jié)構(gòu)之圖的原理與實現(xiàn)
圖(Graph)是由頂點的有窮非空集合和頂點之間邊的集合組成,通常表示為:G(V,E),其中,G表示一個圖,V是圖G中頂點的集合,E是圖G中邊的集合。本文將詳細(xì)介紹圖的原理及其代碼實現(xiàn),需要的可以參考一下2022-01-01Java PDF 添加數(shù)字簽名的實現(xiàn)方法
這篇文章主要介紹了Java PDF 添加數(shù)字簽名的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程
GSON是Google編寫并在在GitHub上開源的Java序列化與反序列化JSON的類庫,今天我們就來總結(jié)一下使用GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程2016-06-06mybatis中一對一關(guān)系association標(biāo)簽的使用
這篇文章主要介紹了mybatis中一對一關(guān)系association標(biāo)簽的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring?Data?JPA實現(xiàn)查詢結(jié)果返回map或自定義的實體類
這篇文章主要介紹了Spring?Data?JPA實現(xiàn)查詢結(jié)果返回map或自定義的實體類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12一個applicationContext 加載錯誤導(dǎo)致的阻塞問題及解決方法
這篇文章主要介紹了一個applicationContext 加載錯誤導(dǎo)致的阻塞問題及解決方法,需要的朋友可以參考下2018-11-11BeanUtils.copyProperties使用總結(jié)以及注意事項說明
這篇文章主要介紹了BeanUtils.copyProperties使用總結(jié)以及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08