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

Spring AI,Java工程師也能玩轉(zhuǎn)大模型

開(kāi)發(fā) 前端
Spring AI是一個(gè)人工智能工程應(yīng)用框架。它的目標(biāo)是將Spring生態(tài)系統(tǒng)的設(shè)計(jì)原則,如可移植性和模塊化設(shè)計(jì),應(yīng)用到AI領(lǐng)域,并推廣使用POJO作為AI應(yīng)用程序的構(gòu)建模塊。

在這個(gè)人工智能的春天,我們迎來(lái)了Spring AI。在這篇文章中,將介紹Spring AI以及如何將其與Ollama本地模型集成。

一、Spring AI簡(jiǎn)介

圖片圖片

官方正式宣布,Spring AI已經(jīng)列入Spring Initializr。它提供了一種更簡(jiǎn)潔的方式來(lái)與AI交互,降低了將LLM模型集成到Java操作中的學(xué)習(xí)曲線。它現(xiàn)在可以在start.spring.io上使用和構(gòu)建。

Spring AI是一個(gè)人工智能工程應(yīng)用框架。它的目標(biāo)是將Spring生態(tài)系統(tǒng)的設(shè)計(jì)原則,如可移植性和模塊化設(shè)計(jì),應(yīng)用到AI領(lǐng)域,并推廣使用POJO作為AI應(yīng)用程序的構(gòu)建模塊。

二、特性

可移植的API支持跨AI提供商的交互,包括聊天、文本到圖像和嵌入模型。它支持同步和流API選項(xiàng)。它還支持配置參數(shù)以訪問(wèn)特定模型。

支持的聊天模型:

  • OpenAI。
  • Azure Open AI。
  • Amazon Bedrock。
  • Anthropic的Claude。
  • Cohere的Command。
  • AI21 Labs的Jurassic-2。
  • Meta的LLama 2。
  • Amazon的Titan。
  • Google Vertex AI。
  • HuggingFace——HuggingFace上的眾多模型,如Llama2。
  • Ollama——支持在沒(méi)有GPU的情況下在本地運(yùn)行AI模型。

支持的文本到圖像模型:

  • OpenAI與DALL-E。
  • StabilityAI。

支持的向量模型:

  • OpenAI。
  • Azure OpenAI。
  • Ollama。
  • ONNX。
  • PostgresML。
  • Bedrock Cohere。
  • Bedrock Titan。
  • Google VertexAI。

官方文檔:spring.io/projects/spring-ai

三、快速入門

使用IDEA快速啟動(dòng)一個(gè)新項(xiàng)目,選擇需要的AI模型依賴項(xiàng)。

在這里,以O(shè)llama模型為例:

圖片圖片

3.1 Ollama

Ollama使我們能夠在不需要GPU資源的情況下在本地計(jì)算機(jī)上輕松構(gòu)建大型模型,并提供控制臺(tái)和RestfulAPI,以便在Ollama上快速測(cè)試和集成大型模型。

Ollama支持哪些模型?

圖片圖片

Ollama網(wǎng)站:ollama.com/library

提示:

  • Gemma是Google Meta最近發(fā)布的一個(gè)模型。
  • llama2模型對(duì)中文支持不太友好,而gemma模型對(duì)中文更加友好。

3.2 引入依賴項(xiàng)

提示:Spring AI相關(guān)的依賴項(xiàng)不在Maven中央資源庫(kù)中,因此需要配置Spring的資源庫(kù)。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>${spring-ai.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.3 啟動(dòng)Ollama模型

在本地計(jì)算機(jī)控制臺(tái)中運(yùn)行ollama run gemma:2b(這里使用gemma模型)。

圖片圖片

第一次運(yùn)行會(huì)下載模型文件(約3GB,可能需要一些時(shí)間)。

下載模型資源后,模型將自動(dòng)啟動(dòng),如上所示,你可以在控制臺(tái)中測(cè)試和與模型交互。

3.4 配置Ollama模型

修改該項(xiàng)目的application.yml配置文件,添加以下內(nèi)容:

spring:
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        model: gemma:2b

3.5 測(cè)試

@Test
void contextLoads() {
    String message = """
                Who is Donald Trump?                               
            """;
    System.out.println(chatClient.call(message));
}

圖片圖片

3.6 流式訪問(wèn)

@Test
  void streamChat() throws ExecutionException, InterruptedException {
      // 構(gòu)建一個(gè)異步函數(shù)來(lái)手動(dòng)關(guān)閉測(cè)試函數(shù)
      CompletableFuture<Void> future = new CompletableFuture<>();

      String message = """
              year-end work summary report
              """;
      PromptTemplate promptTemplate = new PromptTemplate("""
              You are a Java development engineer, and you are good at writing the company’s year-end work summary report.
              Write a 100-word summary report based on: {message} scenario
              """);
      Prompt prompt = promptTemplate.create(Map.of("message", message));
      chatClient.stream(prompt).subscribe(
              chatResponse -> {
                  System.out.println("response: " + chatResponse.getResult().getOutput().getContent());
              },
              throwable -> {
                  System.err.println("err: " + throwable.getMessage());
              },
              () -> {
                  System.out.println("complete~!");
                  // 關(guān)閉函數(shù)
                  future.complete(null);
              }
      );
      future.get();
  }

責(zé)任編輯:武曉燕 來(lái)源: Java學(xué)研大本營(yíng)
相關(guān)推薦

2024-11-12 13:41:49

2012-07-19 14:42:58

Windows 8微軟

2023-07-27 07:37:48

2025-01-23 17:00:00

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

2023-04-25 10:05:56

Windows 98ChatGPT聊天機(jī)器人

2009-02-20 10:14:00

路由器路由應(yīng)用ADSL

2018-08-16 09:41:46

開(kāi)源項(xiàng)目資源

2023-04-07 11:33:24

AIAI大模型

2018-10-16 17:57:57

開(kāi)源架構(gòu)

2020-03-06 15:38:10

編程語(yǔ)言PythonJava

2021-07-05 10:29:59

AI 工程師人工智能

2023-03-28 16:25:23

ChatGPT

2025-03-20 09:48:25

2024-04-10 10:28:47

2025-03-14 10:00:00

黑客AI網(wǎng)絡(luò)安全

2019-11-08 11:08:29

測(cè)試工程師AI人工智能

2024-07-02 10:24:35

2024-09-20 11:30:14

2020-05-27 09:30:52

算法工程師程序員

2024-09-24 07:31:52

點(diǎn)贊
收藏

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