自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring AI + Ollama 實現(xiàn) DeepSeek-R1 的API服務和調(diào)用

人工智能
Spring AI 呢,它就像是一個超級助手,專門為咱們開發(fā)人工智能應用提供各種便利。它基于咱們熟悉的 Spring 框架,有一套現(xiàn)成的工具和組件,能讓咱快速搭建起人工智能相關(guān)的功能。

兄弟們,今天咱來聊聊一個超有意思的技術(shù)組合 ——Spring AI + Ollama 實現(xiàn) deepseek - r1 的 API 服務和調(diào)用。咱都知道,人工智能這幾年那可是火得一塌糊涂,各種大模型你方唱罷我登場。deepseek - r1 就是其中一個挺厲害的模型,那怎么把它用起來,讓它為咱們的項目服務呢?這就輪到 Spring AI 和 Ollama 閃亮登場啦!

一、啥是 Spring AI、Ollama 和 deepseek - r1

(一)Spring AI

Spring AI 呢,它就像是一個超級助手,專門為咱們開發(fā)人工智能應用提供各種便利。它基于咱們熟悉的 Spring 框架,有一套現(xiàn)成的工具和組件,能讓咱快速搭建起人工智能相關(guān)的功能。比如說,它能幫咱們輕松地集成各種大模型,管理模型的調(diào)用,處理輸入輸出啥的,就像給咱們的項目裝上了一個智能引擎,讓開發(fā) AI 應用變得簡單又高效。

(二)Ollama

Ollama 又是什么呢?它是一個運行和管理大語言模型的平臺。它的好處就在于,咱們不用費老大勁去自己部署和管理模型了,Ollama 幫咱們把這些麻煩事都搞定。它支持好多主流的大模型,而且提供了簡單易用的 API,咱們可以通過這些 API 很方便地和模型進行交互,讓模型給咱們干活。

(三)deepseek - r1

deepseek - r1 可是個實力派選手,它是一個功能強大的大模型,在自然語言處理方面表現(xiàn)相當出色。能幫咱們做文本生成、問答系統(tǒng)、智能客服等等好多實用的功能。今天咱就來看看怎么把它和 Spring AI、Ollama 一起搭配,發(fā)揮出最大的威力。

二、準備工作

在開始動手之前,咱得先把環(huán)境搭好。首先,你得確保你的開發(fā)環(huán)境里有 Java。要是沒有的話,趕緊去下載安裝一個,現(xiàn)在 Java 最新版都挺好用的,安裝過程也不難,按照提示一步一步來就行。

然后呢,咱們得安裝 Maven。Maven 是干啥的呢?它就像是一個大管家,幫咱們管理項目里用到的各種依賴。比如說,咱們要用 Spring AI 和 Ollama,就可以通過 Maven 很方便地把它們加到咱們的項目里。安裝 Maven 也很簡單,去 Maven 的官網(wǎng)下載安裝包,解壓之后配置一下環(huán)境變量就搞定啦。

接下來,咱們還得安裝 Ollama。Ollama 的安裝也不復雜,它支持好幾種操作系統(tǒng)。你可以去 Ollama 的官網(wǎng),按照它給的安裝指南,根據(jù)你自己的操作系統(tǒng)來安裝。安裝好之后,啟動 Ollama,確保它在正常運行。

三、創(chuàng)建 Spring Boot 項目

環(huán)境搭好之后,咱就可以開始創(chuàng)建 Spring Boot 項目啦。打開你喜歡的 IDE,比如說 IntelliJ IDEA 或者 Eclipse。在 IDE 里創(chuàng)建一個新的 Spring Boot 項目,這里選擇 Maven 項目,然后在項目的pom.xml文件里添加 Spring AI 和 Ollama 相關(guān)的依賴。

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- Spring AI -->
    <dependency>
        <groupId>io.micronaut.ai</groupId>
        <artifactId>micronaut-ai-openai</artifactId>
        <version>1.0.0</version>
    </dependency>
    <!-- Ollama -->
    <dependency>
        <groupId>com.github.jillesvangurp</groupId>
        <artifactId>ollama-java</artifactId>
        <version>0.1.0</version>
    </dependency>
</dependencies>

這些依賴就是咱們項目的 “彈藥庫”,有了它們,咱們才能在項目里使用 Spring AI 和 Ollama 的各種功能。添加好依賴之后,Maven 會自動幫咱們下載這些依賴包,耐心等一會兒就行。

四、配置 Ollama 客戶端

接下來,咱們得配置一下 Ollama 客戶端,這樣咱們的 Spring Boot 項目才能和 Ollama 進行通信。在 Spring Boot 項目的src/main/resources目錄下創(chuàng)建一個application.properties文件,然后在里面添加 Ollama 的配置信息。

ollama.url=http://localhost:11434
ollama.url=http://localhost:11434

這里咱們假設 Ollama 運行在本地,端口是 11434,要是你安裝的時候改了端口,記得把這里的端口號也改成你自己的。

五、編寫代碼調(diào)用 deepseek - r1

配置好之后,就到了最關(guān)鍵的部分啦,編寫代碼來調(diào)用 deepseek - r1。咱們先創(chuàng)建一個 Java 類,比如說叫OllamaService,在這個類里編寫調(diào)用 Ollama 和 deepseek - r1 的方法。

import com.github.jillesvangurp.ollama.OllamaClient;
import com.github.jillesvangurp.ollama.api.ChatCompletionRequest;
import com.github.jillesvangurp.ollama.api.ChatCompletionResponse;
import com.github.jillesvangurp.ollama.api.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
publicclass OllamaService {

    @Value("${ollama.url}")
    privateString ollamaUrl;

    publicString generateText(String prompt) {
        OllamaClient client = new OllamaClient(ollamaUrl);
        List<Message> messages = new ArrayList<>();
        messages.add(new Message("user", prompt));

        ChatCompletionRequest request = new ChatCompletionRequest("deepseek - r1", messages);
        ChatCompletionResponse response = null;
        try {
            response = client.createChatCompletion(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert response != null;
        return response.getChoices().get(0).getMessage().getContent();
    }
}

在這段代碼里,咱們首先創(chuàng)建了一個OllamaClient對象,用來和 Ollama 進行通信。然后創(chuàng)建了一個ChatCompletionRequest對象,這里指定使用deepseek - r1模型,并且把用戶的輸入(也就是prompt)作為一條消息傳進去。接著通過OllamaClient發(fā)送這個請求,得到ChatCompletionResponse,最后從響應里取出模型生成的文本返回。

六、在 Controller 中使用服務

代碼寫好了,怎么讓它跑起來呢?咱們還得創(chuàng)建一個 Controller,在 Controller 里調(diào)用這個OllamaService。創(chuàng)建一個OllamaController類。

import org.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.RestController;

@RestController
public class OllamaController {

    @Autowired
    private OllamaService ollamaService;

    @GetMapping("/generate")
    public String generateText(@RequestParam String prompt) {
        returnollamaService.generateText(prompt);
    }
}

在這個 Controller 里,咱們通過@Autowired注解把OllamaService注入進來,然后創(chuàng)建了一個/generate的接口,當用戶訪問這個接口并且傳入一個prompt參數(shù)的時候,就會調(diào)用OllamaService的generateText方法,返回模型生成的文本。

七、測試一下

現(xiàn)在,咱們的項目基本上就大功告成啦!啟動 Spring Boot 項目,等項目啟動成功之后,打開瀏覽器,輸入http://localhost:8080/generate?prompt=你好,給我講一個笑話(這里假設你的 Spring Boot 項目端口是 8080)。然后回車,稍等一會兒,你就能看到瀏覽器里返回了一段由 deepseek - r1 模型生成的笑話。

怎么樣,是不是挺簡單的?通過 Spring AI 和 Ollama,咱們輕輕松松就實現(xiàn)了對 deepseek - r1 模型的 API 服務和調(diào)用。當然啦,這只是一個簡單的示例,在實際項目中,你可以根據(jù)自己的需求對代碼進行擴展和優(yōu)化,比如說增加更多的參數(shù)配置,處理更復雜的輸入輸出等等。

好啦,今天關(guān)于 Spring AI + Ollama 實現(xiàn) deepseek - r1 的 API 服務和調(diào)用就講到這里啦。

責任編輯:武曉燕 來源: 石杉的架構(gòu)筆記
相關(guān)推薦

2025-02-19 08:00:00

2025-02-19 08:33:18

2025-02-13 08:51:23

DeepSeek大模型

2025-02-06 10:18:45

2025-03-13 08:13:47

DeepSeekMLLMVision-R1

2025-02-03 00:00:55

DeepSeekRAG系統(tǒng)

2025-03-20 10:20:16

2025-02-19 08:00:00

SpringBootOllamaDeepSeek

2025-02-08 09:44:11

DeepSeekAI模型

2025-02-25 10:03:20

2025-03-05 03:00:00

DeepSeek大模型調(diào)優(yōu)

2025-02-12 14:09:31

DeepSeekChatGPTAPI

2025-02-10 11:27:37

點贊
收藏

51CTO技術(shù)棧公眾號