在springboot?中使用?AIService的方法
在上一篇文章中,我們介紹了如何使用langchain4j實(shí)現(xiàn)簡(jiǎn)單的問(wèn)答功能,本篇文章我們將介紹如何在springboot中使用AIService。
1.實(shí)現(xiàn)原理
先看下@AiService注解所在的依賴langchain4j-spring-boot-starter中包含什么內(nèi)容:

1.1 event.AiServiceRegisteredEvent
這個(gè)類(lèi)實(shí)現(xiàn)了ApplicationEvent,它的作用是當(dāng)一個(gè)AiService被注冊(cè)時(shí)觸發(fā)的事件。

1.2 @AiService
本篇主要需使用的注解,作用是將一個(gè)接口標(biāo)記為AiService,通過(guò)這個(gè)注解可以將一個(gè)接口轉(zhuǎn)換為一個(gè)AiService對(duì)象,這個(gè)對(duì)象可以用于調(diào)用langchain4j提供的各種功能。

1.3 AiServiceFactory
AiServiceFactory類(lèi)實(shí)現(xiàn)了FactoryBean接口,表示這是一個(gè)Spring工廠Bean,包含了AiService的各種依賴組件和配置,可以通過(guò)這個(gè)方法創(chuàng)建AiService對(duì)象。

1.4 ClassPathAiServiceScanner
這個(gè)類(lèi)的主要功能是掃描類(lèi)路徑中的Bean定義,并篩選出帶有AiService注解的接口

1.5 AiServiceScannerProcessor
這個(gè)類(lèi)實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor接口,主要功能是負(fù)責(zé)定義掃描當(dāng)前項(xiàng)目的類(lèi)路徑,移除不需要的AiService配置。

1.6 AiServicesAutoConfig
這個(gè)類(lèi)主要功能是將上下文中的所有組件用于配置和注冊(cè)AiService的Bean,并在完成后發(fā)布相關(guān)注冊(cè)的事件。

2.AiService的簡(jiǎn)單實(shí)現(xiàn)
2.1 配置文件
langchain4j:
open-ai:
chat-model:
api-key: ${API_KEY}
base-url: https://api.deepseek.com/v1
model-name: deepseek-chat
log-requests: true
log-responses: true
temperature: 0.5
max-tokens: 40962.2 定義接口
寫(xiě)一個(gè)簡(jiǎn)單的接口,使用@AiService注解標(biāo)注
@AiService
public interface Assistant {
@SystemMessage("你是一位編程專(zhuān)家,你的名字叫小小明")
String chat(String userMessage);
}3. 使用AiService
@RestController
public class ChatController {
@Resource
ChatLanguageModel chatLanguageModel;
@Resource
Assistant assistant;
@GetMapping("/chat")
public String model(@RequestParam(value = "message") String message) {
return chatLanguageModel.chat(message);
}
@GetMapping("/chatWithService")
public String service(@RequestParam(value = "message") String message) {
return assistant.chat(message);
}
}4. 測(cè)試一下
啟動(dòng)項(xiàng)目,用postman調(diào)用接口進(jìn)行測(cè)試:

5. 在項(xiàng)目中使用多個(gè)模型
5.1 pom.xml
langchain4j支持使用多個(gè)模型,我們更新一下pom.xml文件,添加dashscope的依賴。
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>${langchain4j.version}</version>
</dependency>5.2 配置文件
這里我們配置了兩個(gè)模型,一個(gè)是deepseek-chat,一個(gè)是qwen-plus。
langchain4j:
open-ai:
chat-model:
api-key: ${API_KEY}
base-url: https://api.deepseek.com/v1
model-name: deepseek-chat
log-requests: true
log-responses: true
temperature: 0.5
max-tokens: 4096
community:
dashscope:
chat-model:
api-key: ${API_KEY}
model-name: qwen-plus
temperature: 0.5
max-tokens: 4096
logging.level.dev.langchain4j: DEBUG5.3 定義接口
然后我們定義兩個(gè)接口,一個(gè)是dashscope的,一個(gè)是openai的。
@AiService(wiringMode = EXPLICIT, chatModel = "qwenChatModel")
public interface DashscopeAssistant {
@SystemMessage("你是一位編程專(zhuān)家,名字是kitty")
String chat(String userMessage);
}
@AiService(wiringMode = EXPLICIT, chatModel = "openAiChatModel")
public interface DeepseekAssistant {
@SystemMessage("你是一位畫(huà)家,名字是tom")
String chat(String userMessage);
}5.4 使用
最后我們?cè)赾ontroller中注入這兩個(gè)接口,分別調(diào)用。
@RestController
public class ChatController {
@Resource
DashscopeAssistant dashscopeAssistant;
@Resource
DeepseekAssistant deepseekAssistant;
@GetMapping("/chatWithQwen")
public String qwen(@RequestParam(value = "message") String message) {
return dashscopeAssistant.chat(message);
}
@GetMapping("/chatWithDeepseek")
public String deepseek(@RequestParam(value = "message") String message) {
return deepseekAssistant.chat(message);
}
}6.測(cè)試多個(gè)AIService


到此這篇關(guān)于在springboot 中使用 AIService的文章就介紹到這了,更多相關(guān)springboot 使用 AIService內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java排序算法之歸并排序簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Java排序算法之歸并排序簡(jiǎn)單實(shí)現(xiàn),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2019-09-09
Java Benchmark 基準(zhǔn)測(cè)試的實(shí)例詳解
這篇文章主要介紹了Java Benchmark 基準(zhǔn)測(cè)試的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
SpringBoot集成Swagger3的實(shí)現(xiàn)
本文主要介紹了SpringBoot集成Swagger3的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
超詳細(xì)講解SpringBoot參數(shù)校驗(yàn)實(shí)例
經(jīng)常需要提供接口與用戶交互(獲取數(shù)據(jù)、上傳數(shù)據(jù)等),由于這個(gè)過(guò)程需要用戶進(jìn)行相關(guān)的操作,為了避免出現(xiàn)一些錯(cuò)誤的數(shù)據(jù)等,一般需要對(duì)數(shù)據(jù)進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于SpringBoot各種參數(shù)校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2022-05-05
使用Java的Lucene搜索工具對(duì)檢索結(jié)果進(jìn)行分組和分頁(yè)
這篇文章主要介紹了使用Java的搜索工具Lucene對(duì)檢索結(jié)果進(jìn)行分組和分頁(yè)的方法,Luence是Java環(huán)境中的一個(gè)全文檢索引擎工具包,需要的朋友可以參考下2016-03-03
Java中import java.util.Scanner的用處詳解
文章主要介紹Java中的Scanner類(lèi)及其常用方法next()和nextLine()的區(qū)別,next()方法在遇到空格、Tab鍵、回車(chē)鍵等分隔符時(shí)結(jié)束輸入,而nextLine()方法則接收所有輸入,直到遇到回車(chē)鍵2024-11-11

