springboot 整合 langchain4j 實(shí)現(xiàn)簡(jiǎn)單的問(wèn)答功能
最近在學(xué)習(xí)langchain4j,本文將介紹如何使用langchain4j快速實(shí)現(xiàn)一個(gè)簡(jiǎn)單的問(wèn)答功能,幫助大家快速入門。
1. 工具
- JDK 17
- Maven 3.9.9
- IntelliJ IDEA 2024.3.4 (Community Edition)
2. apikey
可以優(yōu)先選擇阿里云百煉申請(qǐng)一個(gè)apikey,免費(fèi)額度足夠測(cè)試使用。
3. springboot項(xiàng)目創(chuàng)建
3.1 項(xiàng)目創(chuàng)建
創(chuàng)建springboot項(xiàng)目,與其他springboot項(xiàng)目創(chuàng)建方式一樣,這里不再詳細(xì)介紹。
3.2 依賴引入
這里只提供了部分依賴示例,使用的版本是1.0.0-beta2,完整依賴可以參考langchain4j官方文檔,本文最后給出了完整pom.xml供大家參考。實(shí)際上三方依賴會(huì)經(jīng)常更新,隨著版本變化需要引入的依賴總是會(huì)有些區(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} # 需要替換成自己申請(qǐng)的apikey # base-url: https://api.deepseek.com/v1 # model-name: deepseek-reasoner # api-key: ${API_KEY} logging.level.dev.langchain4j: DEBUG
4. 代碼編寫
4.1 啟動(dòng)類
先準(zhǔn)備springboot啟動(dòng)類
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
編寫一個(gè)接口,注入ChatLanguageModel,寫一個(gè)最簡(jiǎn)單的接口測(cè)試。
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 測(cè)試
啟動(dòng)項(xiàng)目,用postman調(diào)用接口進(jìn)行簡(jiǎn)單測(cè)試:
參考
完整的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 實(shí)現(xiàn)簡(jiǎn)單的問(wèn)答功能的文章就介紹到這了,更多相關(guān)springboot langchain4j 問(wèn)答內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中 IO 常用IO操作類繼承結(jié)構(gòu)分析
本篇文章小編為大家介紹,java中 IO 常用IO操作類繼承結(jié)構(gòu)分析。需要的朋友參考下2013-04-04java中 spring 定時(shí)任務(wù) 實(shí)現(xiàn)代碼
java中 spring 定時(shí)任務(wù) 實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-03-03詳解spring cloud hystrix請(qǐng)求緩存(request cache)
這篇文章主要介紹了詳解spring cloud hystrix請(qǐng)求緩存(request cache),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Java數(shù)據(jù)結(jié)構(gòu)之圖的原理與實(shí)現(xiàn)
圖(Graph)是由頂點(diǎn)的有窮非空集合和頂點(diǎn)之間邊的集合組成,通常表示為:G(V,E),其中,G表示一個(gè)圖,V是圖G中頂點(diǎn)的集合,E是圖G中邊的集合。本文將詳細(xì)介紹圖的原理及其代碼實(shí)現(xiàn),需要的可以參考一下2022-01-01Java PDF 添加數(shù)字簽名的實(shí)現(xiàn)方法
這篇文章主要介紹了Java PDF 添加數(shù)字簽名的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12GSON實(shí)現(xiàn)Java對(duì)象與JSON格式對(duì)象相互轉(zhuǎn)換的完全教程
GSON是Google編寫并在在GitHub上開(kāi)源的Java序列化與反序列化JSON的類庫(kù),今天我們就來(lái)總結(jié)一下使用GSON實(shí)現(xiàn)Java對(duì)象與JSON格式對(duì)象相互轉(zhuǎn)換的完全教程2016-06-06mybatis中一對(duì)一關(guān)系association標(biāo)簽的使用
這篇文章主要介紹了mybatis中一對(duì)一關(guān)系association標(biāo)簽的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Spring?Data?JPA實(shí)現(xiàn)查詢結(jié)果返回map或自定義的實(shí)體類
這篇文章主要介紹了Spring?Data?JPA實(shí)現(xiàn)查詢結(jié)果返回map或自定義的實(shí)體類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12一個(gè)applicationContext 加載錯(cuò)誤導(dǎo)致的阻塞問(wèn)題及解決方法
這篇文章主要介紹了一個(gè)applicationContext 加載錯(cuò)誤導(dǎo)致的阻塞問(wèn)題及解決方法,需要的朋友可以參考下2018-11-11BeanUtils.copyProperties使用總結(jié)以及注意事項(xiàng)說(shuō)明
這篇文章主要介紹了BeanUtils.copyProperties使用總結(jié)以及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08