在springboot?中使用?AIService的方法
在上一篇文章中,我們介紹了如何使用langchain4j實現(xiàn)簡單的問答功能,本篇文章我們將介紹如何在springboot中使用AIService。
1.實現(xiàn)原理
先看下@AiService注解所在的依賴langchain4j-spring-boot-starter中包含什么內(nèi)容:
1.1 event.AiServiceRegisteredEvent
這個類實現(xiàn)了ApplicationEvent,它的作用是當(dāng)一個AiService被注冊時觸發(fā)的事件。
1.2 @AiService
本篇主要需使用的注解,作用是將一個接口標(biāo)記為AiService,通過這個注解可以將一個接口轉(zhuǎn)換為一個AiService對象,這個對象可以用于調(diào)用langchain4j提供的各種功能。
1.3 AiServiceFactory
AiServiceFactory類實現(xiàn)了FactoryBean接口,表示這是一個Spring工廠Bean,包含了AiService的各種依賴組件和配置,可以通過這個方法創(chuàng)建AiService對象。
1.4 ClassPathAiServiceScanner
這個類的主要功能是掃描類路徑中的Bean定義,并篩選出帶有AiService注解的接口
1.5 AiServiceScannerProcessor
這個類實現(xiàn)了BeanDefinitionRegistryPostProcessor接口,主要功能是負(fù)責(zé)定義掃描當(dāng)前項目的類路徑,移除不需要的AiService配置。
1.6 AiServicesAutoConfig
這個類主要功能是將上下文中的所有組件用于配置和注冊AiService的Bean,并在完成后發(fā)布相關(guān)注冊的事件。
2.AiService的簡單實現(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: 4096
2.2 定義接口
寫一個簡單的接口,使用@AiService注解標(biāo)注
@AiService public interface Assistant { @SystemMessage("你是一位編程專家,你的名字叫小小明") 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. 測試一下
啟動項目,用postman調(diào)用接口進(jìn)行測試:
5. 在項目中使用多個模型
5.1 pom.xml
langchain4j支持使用多個模型,我們更新一下pom.xml文件,添加dashscope的依賴。
<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId> <version>${langchain4j.version}</version> </dependency>
5.2 配置文件
這里我們配置了兩個模型,一個是deepseek-chat,一個是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: DEBUG
5.3 定義接口
然后我們定義兩個接口,一個是dashscope的,一個是openai的。
@AiService(wiringMode = EXPLICIT, chatModel = "qwenChatModel") public interface DashscopeAssistant { @SystemMessage("你是一位編程專家,名字是kitty") String chat(String userMessage); } @AiService(wiringMode = EXPLICIT, chatModel = "openAiChatModel") public interface DeepseekAssistant { @SystemMessage("你是一位畫家,名字是tom") String chat(String userMessage); }
5.4 使用
最后我們在controller中注入這兩個接口,分別調(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.測試多個AIService
到此這篇關(guān)于在springboot 中使用 AIService的文章就介紹到這了,更多相關(guān)springboot 使用 AIService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java幸運28系統(tǒng)搭建數(shù)組的使用實例詳解
在本篇文章里小編給大家整理了關(guān)于Java幸運28系統(tǒng)搭建數(shù)組的使用實例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2019-09-09Java Benchmark 基準(zhǔn)測試的實例詳解
這篇文章主要介紹了Java Benchmark 基準(zhǔn)測試的實例詳解的相關(guān)資料,這里提供實例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08超詳細(xì)講解SpringBoot參數(shù)校驗實例
經(jīng)常需要提供接口與用戶交互(獲取數(shù)據(jù)、上傳數(shù)據(jù)等),由于這個過程需要用戶進(jìn)行相關(guān)的操作,為了避免出現(xiàn)一些錯誤的數(shù)據(jù)等,一般需要對數(shù)據(jù)進(jìn)行校驗,下面這篇文章主要給大家介紹了關(guān)于SpringBoot各種參數(shù)校驗的相關(guān)資料,需要的朋友可以參考下2022-05-05使用Java的Lucene搜索工具對檢索結(jié)果進(jìn)行分組和分頁
這篇文章主要介紹了使用Java的搜索工具Lucene對檢索結(jié)果進(jìn)行分組和分頁的方法,Luence是Java環(huán)境中的一個全文檢索引擎工具包,需要的朋友可以參考下2016-03-03Java中import java.util.Scanner的用處詳解
文章主要介紹Java中的Scanner類及其常用方法next()和nextLine()的區(qū)別,next()方法在遇到空格、Tab鍵、回車鍵等分隔符時結(jié)束輸入,而nextLine()方法則接收所有輸入,直到遇到回車鍵2024-11-11