欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java調(diào)用基于Ollama本地大模型的實現(xiàn)

 更新時間:2025年03月25日 10:47:45   作者:奔波兒灞愛霸波爾奔  
本文主要介紹了Java調(diào)用基于Ollama本地大模型的實現(xiàn),實現(xiàn)文本生成、問答、文本分類等功能,開發(fā)者可以輕松配置和調(diào)用模型,具有一定的參考價值,感興趣的可以了解一下

引言

隨著人工智能技術(shù)的飛速發(fā)展,大型語言模型(Large Language Models, LLMs)已成為自然語言處理領(lǐng)域的研究熱點。Ollama是一個強大的工具,它使得在本地部署和管理這些大型語言模型變得更加便捷。本文檔旨在指導Java開發(fā)者如何在Java應(yīng)用程序中調(diào)用基于Ollama部署的本地大型語言模型,實現(xiàn)文本生成、問答、文本分類等多種自然語言處理任務(wù)。

環(huán)境準備

  • 安裝Ollama和模型加載: 該內(nèi)容已經(jīng)在之間文檔進行說明,這里不再贅述,讀者可以查看之前的文檔內(nèi)容。。
  • Java環(huán)境: java我們現(xiàn)在java 17進行開發(fā),因為我們依賴的io.springboot.ai,從查看資料的結(jié)果看,需要基于java17或者以上才能進行開發(fā),不然可能在程序啟動的時候存在如下報錯

OllamaChatClient.class
類文件具有錯誤的版本 61.0, 應(yīng)為 52.0
請刪除該文件或確保該文件位于正確的類路徑子目錄中。

Java調(diào)用Ollama

我們通過基于一個SpringBoot和Maven方式并且通過接口展示的方式進行舉例,我們程序代碼的結(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ù)必采取適當?shù)陌踩胧?,如使用HTTPS、API密鑰驗證等。
  • 資源管理: 大型語言模型運行時消耗大量計算資源。監(jiān)控和限制并發(fā)請求,避免資源耗盡。
  • 錯誤處理: 實際應(yīng)用中要增加異常處理邏輯,確保程序健壯性。

結(jié)語

通過上述步驟,你可以在Java應(yīng)用程序中無縫集成基于Ollama的本地大型語言模型,為你的項目增添強大的自然語言處理能力。隨著Ollama及其支持的模型不斷更新,持續(xù)探索和優(yōu)化模型調(diào)用策略,將能進一步提升應(yīng)用性能和用戶體驗。

到此這篇關(guān)于Java調(diào)用基于Ollama本地大模型的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java調(diào)用Ollama本地大模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論