Java調(diào)用基于Ollama本地大模型的實現(xiàn)
引言
隨著人工智能技術(shù)的飛速發(fā)展,大型語言模型(Large Language Models, LLMs)已成為自然語言處理領(lǐng)域的研究熱點。Ollama是一個強(qiáng)大的工具,它使得在本地部署和管理這些大型語言模型變得更加便捷。本文檔旨在指導(dǎo)Java開發(fā)者如何在Java應(yīng)用程序中調(diào)用基于Ollama部署的本地大型語言模型,實現(xiàn)文本生成、問答、文本分類等多種自然語言處理任務(wù)。
環(huán)境準(zhǔn)備
- 安裝Ollama和模型加載: 該內(nèi)容已經(jīng)在之間文檔進(jìn)行說明,這里不再贅述,讀者可以查看之前的文檔內(nèi)容。。
- Java環(huán)境: java我們現(xiàn)在java 17進(jìn)行開發(fā),因為我們依賴的io.springboot.ai,從查看資料的結(jié)果看,需要基于java17或者以上才能進(jìn)行開發(fā),不然可能在程序啟動的時候存在如下報錯
OllamaChatClient.class
類文件具有錯誤的版本 61.0, 應(yīng)為 52.0
請刪除該文件或確保該文件位于正確的類路徑子目錄中。
Java調(diào)用Ollama
我們通過基于一個SpringBoot和Maven方式并且通過接口展示的方式進(jìn)行舉例,我們程序代碼的結(jié)果如下

1. pom.xml設(shè)置
我們設(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è)置
我們設(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則是你在文檔搭建的大模型名稱
3. application啟動設(shè)置
我們設(shè)置testAiApplication主程序啟動的代碼如下
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. 接口暴露
我們編寫aiController暴露對應(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. 程序啟動
編寫好對應(yī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. 測試驗證
通過上述代碼我們可以知道,我們暴露的接口是/ollama/chat/v1,我們打開瀏覽器,測試對應(yīng)的接口信息,調(diào)用接口如下:http://127.0.0.1:8099/ollama/chat/v1?msg=你是誰
我們可以得到大模型返回的結(jié)果如下:

注意事項
- 安全性: 考慮到API可能暴露在公網(wǎng),務(wù)必采取適當(dāng)?shù)陌踩胧?,如使用HTTPS、API密鑰驗證等。
- 資源管理: 大型語言模型運(yùn)行時消耗大量計算資源。監(jiān)控和限制并發(fā)請求,避免資源耗盡。
- 錯誤處理: 實際應(yīng)用中要增加異常處理邏輯,確保程序健壯性。
結(jié)語
通過上述步驟,你可以在Java應(yīng)用程序中無縫集成基于Ollama的本地大型語言模型,為你的項目增添強(qiáng)大的自然語言處理能力。隨著Ollama及其支持的模型不斷更新,持續(xù)探索和優(yōu)化模型調(diào)用策略,將能進(jìn)一步提升應(yīng)用性能和用戶體驗。
到此這篇關(guān)于Java調(diào)用基于Ollama本地大模型的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java調(diào)用Ollama本地大模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你搞定SpringBoot中的熱部署devtools方法
這篇文章主要介紹了一篇文章帶你搞定SpringBoot中的熱部署devtools方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java實現(xiàn)登錄密碼強(qiáng)度校驗的項目實踐
本文主要介紹了Java實現(xiàn)登錄密碼強(qiáng)度校驗的項目實踐,包括使用正則表達(dá)式匹配校驗和密碼強(qiáng)度校驗工具類這兩種方法,具有一定的參考價值,感興趣的可以了解一下2024-01-01
在ChatGPT的API中支持多輪對話的實現(xiàn)方法
ChatGPT是由OpenAI研發(fā)的一種預(yù)訓(xùn)練語言模型,只能在OpenAI平臺上進(jìn)行訓(xùn)練,目前并不對外開放訓(xùn)練接口,這篇文章主要介紹了在ChatGPT的API中支持多輪對話的實現(xiàn)方法,需要的朋友可以參考下2023-02-02
Java mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實例代碼
這篇文章主要介紹了Java mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
Java實現(xiàn)迅雷地址轉(zhuǎn)成普通地址實例代碼
本篇文章主要介紹了Java實現(xiàn)迅雷地址轉(zhuǎn)成普通地址實例代碼,非常具有實用價值,有興趣的可以了解一下。2017-03-03
Java實現(xiàn)Linux下雙守護(hù)進(jìn)程
這篇文章主要介紹了Java實現(xiàn)Linux下雙守護(hù)進(jìn)程的思路、原理以及具體實現(xiàn)方式,非常的詳細(xì),希望對大家有所幫助2014-10-10
設(shè)計模式之原型模式_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了設(shè)計模式之原型模式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

