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

Spring AI與Ollama整合離線通義千問模型

人工智能
我們成功地將 SpringAI 與通義千問大語言模型整合在一起,并實(shí)現(xiàn)了一個(gè)簡單的聊天功能。這種整合方式不僅簡化了開發(fā)流程,還提供了強(qiáng)大的靈活性,可以輕松切換不同的 AI 服務(wù)提供商。

背景介紹

在人工智能領(lǐng)域,大語言模型(LLM)的發(fā)展已經(jīng)取得了顯著進(jìn)展,這些模型在自然語言處理、知識(shí)圖譜構(gòu)建和智能問答等多個(gè)方面表現(xiàn)出色。SpringAI 項(xiàng)目旨在簡化包含人工智能功能的應(yīng)用程序的開發(fā),避免不必要的復(fù)雜性。該項(xiàng)目從著名的 Python 項(xiàng)目(例如 LangChain 和 LlamaIndex)中汲取靈感,但 SpringAI 并不是這些項(xiàng)目的直接移植。它支持所有主要模型,包括 OpenAI、Ollama、Azure OpenAI、Amazon Bedrock、Huggingface、Google VertextAI、Mistral AI 等。

本文將詳細(xì)介紹如何使用SpringAI基于Ollama整合通義千問等大語言模型,離線實(shí)現(xiàn)AI聊天,并提供一個(gè)示例來展示其實(shí)現(xiàn)過程。

一、環(huán)境準(zhǔn)備

在開始之前,需要確保開發(fā)環(huán)境滿足一定的要求,包括安裝 JDK、Maven 以及 Spring Boot。Ollama環(huán)境以及Qwen模型。

1. 安裝 Ollama

Ollama,是一個(gè)開源的大語言模型平臺(tái),它允許用戶在本地環(huán)境中運(yùn)行、創(chuàng)建和共享大型語言模型。Ollama提供了豐富的功能和特性,使得用戶可以在自己的計(jì)算機(jī)上輕松地部署和運(yùn)行大型語言模型。

下載

首先,需要在本地或服務(wù)器上安裝 Ollama。以下是在不同操作系統(tǒng)上的安裝步驟:

  • Windows:

下載 Ollama 的 Windows 安裝包并執(zhí)行安裝程序。

配置環(huán)境變量,將 Ollama 的安裝路徑添加到系統(tǒng)的 PATH 環(huán)境變量中。

2. 配置 Ollama

安裝完成后電腦右下角有一只駱駝的圖標(biāo)

3. 模型安裝

進(jìn)入Ollama倉庫中查詢自己需要的模型:https://ollama.com/library, 這里我們想下載qwen模型,通過下面的命令進(jìn)行下載:

-- 下載模型
ollama pull qwen
  
-- 運(yùn)行模型
ollama run qwen

4. 離線模型部署

服務(wù)器環(huán)境我們需要離線的模型文件,可以通過下面的命令,生成Modelfile,在服務(wù)器環(huán)境上傳即可。

-- 查看模型Modelfile
ollama show --modelfile qwen

-- 創(chuàng)建模型
ollama create <your-model-name> -f <./Modelfile>

a. 根據(jù)Modelfile內(nèi)容查看模型具體位置,并上傳到服務(wù)器 b. 保存Modelfile文件,并修改FROM子句,改為自己模型位置 c. 執(zhí)行create命令

二、SpringAI 項(xiàng)目設(shè)置

1. 創(chuàng)建 Spring Boot 項(xiàng)目

可以使用 Spring Initializr 創(chuàng)建一個(gè)新的 Spring Boot 項(xiàng)目,選擇以下依賴項(xiàng):

  • Spring Web
  • Spring Boot DevTools

2. 添加 Maven 依賴

在項(xiàng)目的 pom.xml 文件中,添加 Spring Cloud Alibaba AI 的依賴:注意這里將spring-ai-core升級(jí)了,不然會(huì)有問題

<project>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.0.0-M2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      
      <dependency>
          <groupId>org.springframework.ai</groupId>
          <artifactId>spring-ai-core</artifactId>
          <version>1.0.0-M2</version>
      </dependency>
      
      <dependency>
          <groupId>org.springframework.ai</groupId>
          <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.ai</groupId>
                  <artifactId>spring-ai-core</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
    </dependencies>
</project>

3. 配置文件

在 application.yml 文件中,添加Ollama服務(wù)配置并啟用通義千問模型:

spring:
  ai:
    ollama:
      base-url: http://127.0.0.1:11434
      chat:
        model: "qwen"
        enabled: true

server:
  servlet:
    encoding:
      charset: utf-8
      enabled: true
      force: true

三、編寫示例代碼

下面是一個(gè)簡單的示例,展示如何使用 SpringAI 與通義千問進(jìn)行交互,并實(shí)現(xiàn)一個(gè)基本的聊天功能。

1. 創(chuàng)建控制器類

創(chuàng)建一個(gè)名為 ChatController 的控制器類,用于處理用戶的聊天請(qǐng)求:

package cn.cycad.ai.qwen.controller;

import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chat")
public class ChatController {

    private final OllamaChatModel chatModel;

    @Autowired
    public ChatController(OllamaChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping
    public String generate(@RequestParam(value = "message") String message) {
        ChatResponse response = chatModel.call(new Prompt(message));
        return response.getResult().getOutput().getContent();
    }

    @GetMapping("/stream")
 public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        Flux<ChatResponse> stream = this.chatModel.stream(prompt);
        return stream.map(response->response.getResult().getOutput().getContent());
    }

}

2. 啟動(dòng)應(yīng)用

運(yùn)行 Spring Boot 應(yīng)用,然后訪問 http://localhost:8080/chat?message=你是誰 即可看到通義千問返回的回答。

四、總結(jié)與擴(kuò)展

通過上述步驟,我們成功地將 SpringAI 與通義千問大語言模型整合在一起,并實(shí)現(xiàn)了一個(gè)簡單的聊天功能。這種整合方式不僅簡化了開發(fā)流程,還提供了強(qiáng)大的靈活性,可以輕松切換不同的 AI 服務(wù)提供商。

責(zé)任編輯:武曉燕 來源: Java技術(shù)指北
相關(guān)推薦

2024-10-28 08:26:55

SpringAI語言模型1. 多模型

2024-06-11 09:20:48

2024-05-09 11:52:30

通義大模型通義

2023-04-07 14:01:18

ChatGPT人工智能

2023-08-03 19:11:45

2023-12-04 09:55:58

AI大模型

2025-03-06 10:18:38

2023-04-11 15:49:17

阿里云峰會(huì)人工智能

2023-04-11 13:40:22

阿里云大模型通義千問

2024-08-30 15:19:22

2023-12-01 13:36:01

阿里云通義千問

2024-12-02 08:00:00

2024-05-21 11:35:48

阿里云通義千問

2023-11-01 19:03:58

GPT-4

2024-01-26 13:19:00

模型數(shù)據(jù)

2024-02-06 12:50:08

AI訓(xùn)練

2023-10-31 15:26:02

阿里云通義千問

2023-12-01 12:31:22

AI模型
點(diǎn)贊
收藏

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