Java調(diào)用基于Ollama本地大模型的實(shí)現(xiàn)
引言
隨著人工智能技術(shù)的飛速發(fā)展,大型語(yǔ)言模型(Large Language Models, LLMs)已成為自然語(yǔ)言處理領(lǐng)域的研究熱點(diǎn)。Ollama是一個(gè)強(qiáng)大的工具,它使得在本地部署和管理這些大型語(yǔ)言模型變得更加便捷。本文檔旨在指導(dǎo)Java開(kāi)發(fā)者如何在Java應(yīng)用程序中調(diào)用基于Ollama部署的本地大型語(yǔ)言模型,實(shí)現(xiàn)文本生成、問(wèn)答、文本分類(lèi)等多種自然語(yǔ)言處理任務(wù)。
環(huán)境準(zhǔn)備
- 安裝Ollama和模型加載: 該內(nèi)容已經(jīng)在之間文檔進(jìn)行說(shuō)明,這里不再贅述,讀者可以查看之前的文檔內(nèi)容。。
- Java環(huán)境: java我們現(xiàn)在java 17進(jìn)行開(kāi)發(fā),因?yàn)槲覀円蕾?lài)的io.springboot.ai,從查看資料的結(jié)果看,需要基于java17或者以上才能進(jìn)行開(kāi)發(fā),不然可能在程序啟動(dòng)的時(shí)候存在如下報(bào)錯(cuò)
OllamaChatClient.class
類(lèi)文件具有錯(cuò)誤的版本 61.0, 應(yīng)為 52.0
請(qǐng)刪除該文件或確保該文件位于正確的類(lèi)路徑子目錄中。
Java調(diào)用Ollama
我們通過(guò)基于一個(gè)SpringBoot和Maven方式并且通過(guò)接口展示的方式進(jìn)行舉例,我們程序代碼的結(jié)果如下
1. pom.xml設(shè)置
我們?cè)O(shè)置我們的pom.xml中的內(nèi)容如下
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>testAI</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.springboot.ai</groupId> <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>6.1.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2. application.properties設(shè)置
我們?cè)O(shè)置我們的application.properties中的內(nèi)容如下
server.port=8099 spring.ai.ollama.base-url=http://10.31.128.110:9999 spring.ai.ollama.chat.options.model=Qwen2-7b:latest
其中上述的內(nèi)容中spring.ai.ollama.base-url為你本地使用ollama搭建的大模型地址,spring.ai.ollama.chat.options.model則是你在文檔搭建的大模型名稱(chēng)
3. application啟動(dòng)設(shè)置
我們?cè)O(shè)置testAiApplication主程序啟動(dòng)的代碼如下
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync @SpringBootApplication public class testAiApplication { public static void main(String[] args) { SpringApplication.run(testAiApplication.class, args); } }
4. 接口暴露
我們編寫(xiě)aiController暴露對(duì)應(yīng)的接口內(nèi)容
package org.example.controller; import org.springframework.ai.ollama.OllamaChatClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class aiController { @Autowired @Qualifier("ollamaChatClient") private OllamaChatClient ollamaChatClient; @GetMapping("/ollama/chat/v1") public String ollamaChat(@RequestParam String msg) { return this.ollamaChatClient.call(msg); } }
5. 程序啟動(dòng)
編寫(xiě)好對(duì)應(yīng)的代碼以后,我們可以啟動(dòng)我們的程序
2024-09-18T15:10:34.292+08:00 INFO 16896 — [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8099 (http) with context path ‘’
2024-09-18T15:10:34.328+08:00 INFO 16896 — [ restartedMain] org.example.testAiApplication : Started testAiApplication in 7.331 seconds (process running for 8.44)
6. 測(cè)試驗(yàn)證
通過(guò)上述代碼我們可以知道,我們暴露的接口是/ollama/chat/v1,我們打開(kāi)瀏覽器,測(cè)試對(duì)應(yīng)的接口信息,調(diào)用接口如下:http://127.0.0.1:8099/ollama/chat/v1?msg=你是誰(shuí)
我們可以得到大模型返回的結(jié)果如下:
注意事項(xiàng)
- 安全性: 考慮到API可能暴露在公網(wǎng),務(wù)必采取適當(dāng)?shù)陌踩胧?,如使用HTTPS、API密鑰驗(yàn)證等。
- 資源管理: 大型語(yǔ)言模型運(yùn)行時(shí)消耗大量計(jì)算資源。監(jiān)控和限制并發(fā)請(qǐng)求,避免資源耗盡。
- 錯(cuò)誤處理: 實(shí)際應(yīng)用中要增加異常處理邏輯,確保程序健壯性。
結(jié)語(yǔ)
通過(guò)上述步驟,你可以在Java應(yīng)用程序中無(wú)縫集成基于Ollama的本地大型語(yǔ)言模型,為你的項(xiàng)目增添強(qiáng)大的自然語(yǔ)言處理能力。隨著Ollama及其支持的模型不斷更新,持續(xù)探索和優(yōu)化模型調(diào)用策略,將能進(jìn)一步提升應(yīng)用性能和用戶(hù)體驗(yàn)。
到此這篇關(guān)于Java調(diào)用基于Ollama本地大模型的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java調(diào)用Ollama本地大模型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你搞定SpringBoot中的熱部署devtools方法
這篇文章主要介紹了一篇文章帶你搞定SpringBoot中的熱部署devtools方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09java double類(lèi)型相加精度問(wèn)題的解決
這篇文章主要介紹了java double類(lèi)型相加精度問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Java實(shí)現(xiàn)登錄密碼強(qiáng)度校驗(yàn)的項(xiàng)目實(shí)踐
本文主要介紹了Java實(shí)現(xiàn)登錄密碼強(qiáng)度校驗(yàn)的項(xiàng)目實(shí)踐,包括使用正則表達(dá)式匹配校驗(yàn)和密碼強(qiáng)度校驗(yàn)工具類(lèi)這兩種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01在ChatGPT的API中支持多輪對(duì)話的實(shí)現(xiàn)方法
ChatGPT是由OpenAI研發(fā)的一種預(yù)訓(xùn)練語(yǔ)言模型,只能在OpenAI平臺(tái)上進(jìn)行訓(xùn)練,目前并不對(duì)外開(kāi)放訓(xùn)練接口,這篇文章主要介紹了在ChatGPT的API中支持多輪對(duì)話的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-02-02Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢(xún)實(shí)例代碼
這篇文章主要介紹了Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢(xún)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Java實(shí)現(xiàn)迅雷地址轉(zhuǎn)成普通地址實(shí)例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)迅雷地址轉(zhuǎn)成普通地址實(shí)例代碼,非常具有實(shí)用價(jià)值,有興趣的可以了解一下。2017-03-03Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程
這篇文章主要介紹了Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程的思路、原理以及具體實(shí)現(xiàn)方式,非常的詳細(xì),希望對(duì)大家有所幫助2014-10-10設(shè)計(jì)模式之原型模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了設(shè)計(jì)模式之原型模式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08使用java項(xiàng)目搭建一個(gè)netty服務(wù)
這篇文章主要為大家詳細(xì)介紹了如何使用java項(xiàng)目搭建一個(gè)netty服務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10