亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

引言

隨著人工智能技術(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)文章

最新評(píng)論