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

Spring AI 入門學(xué)習(xí)指南

 更新時(shí)間:2024年11月14日 10:13:44   作者:抱糖果彡  
本文介紹了如何使用SpringAI和Ollama進(jìn)行AI開發(fā),包括如何配置和使用聊天、圖像、語音轉(zhuǎn)文字和文字轉(zhuǎn)語音模型,以及如何在Java項(xiàng)目中集成Ollama模型,感興趣的朋友一起看看吧

Spring AI 初學(xué)

Spring AI 官方地址

”spring 不生產(chǎn) AI,只是 AI 工具的搬運(yùn)工“

項(xiàng)目可以查看gitee

Open AI

前期準(zhǔn)備

Open AI官方地址,需要使用魔法才能打開,同時(shí)購買很麻煩,建議淘寶進(jìn)行購買,只需要購買 open ai 的 apikey 即可。

apikey 形如 sk-xxxxxxxxxxxxxxxxx

項(xiàng)目創(chuàng)建

Idea 創(chuàng)建 SpringBoot Maven 項(xiàng)目(Spring AI基于1.0-SNAPSHOT版本,SpringBoot 3.2.6),依賴選擇Spring Web、 OpenAI。其他可以自行選擇

修改項(xiàng)目倉庫地址,中央倉庫暫時(shí)還沒 Spring AI 相關(guān) jar 包。倉庫地址改成快照倉庫地址,官方說明

    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>

項(xiàng)目中找到 pom.xml 文件,將 <spring-ai.version>0.8.1</spring-ai.version> 改為 <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>

yaml 配置文件中添加,openai 更多配置可以查看 org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration。

spring:
  ai:
    openai:
      # 購買的 api-key
      api-key: sk-xxxx
      # 如果是官方地址,則可以不填,默認(rèn)為 https://api.openai.com
      base-url: 

聊天

基礎(chǔ)使用

主要類 org.springframework.ai.openai.OpenAiChatModel,快照版本不同,可能名字不一樣,可以查看 org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration 中的聊天類是哪個(gè)。

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ChatTest {
    @Resource
    private OpenAiChatModel chatModel;
    @Test
    public void chat1(){
        String msg = "你好";
        //返回string數(shù)據(jù)
        String res = chatModel.call(msg);
        System.out.println(res);
    }
    @Test
    public void chat2(){
        String msg = "你好";
        //返回對象
        ChatResponse res = chatModel.call(new Prompt(msg));
        System.out.println(res);
        //獲取對話返回結(jié)果
        System.out.println(res.getResult().getOutput().getContent());
    }
    /**
    * 流式返回?cái)?shù)據(jù),類似打字效果,一個(gè)詞一個(gè)json數(shù)據(jù),整句話多個(gè)json數(shù)據(jù)
    */
    @Test
    public void chat3(){
        String msg = "你好";
        Prompt prompt = new Prompt(msg);
        Flux<ChatResponse> flux = chatModel.stream(prompt);
        flux.toStream().forEach(res -> {
            System.out.println(res.getResult().getOutput().getContent());
        });
    }
}

配置屬性

    @Test
    public void test3(){
        String msg = "你是誰";
        //采用 gpt-4-turbo 模型,配置屬性創(chuàng)建可以參考 OpenAiChatModel 構(gòu)造函數(shù)
        OpenAiChatOptions options = OpenAiChatOptions.builder()
                .withModel("gpt-4-turbo")
                .build();
        ChatResponse res = chatModel.call(new Prompt(msg, options));
        System.out.println(res);
        //獲取對話返回結(jié)果
        System.out.println(res.getResult().getOutput().getContent());
    }

聊天模型配置屬性可以查看 org.springframework.ai.autoconfigure.openai.OpenAiChatProperties,也可以在官網(wǎng)查看更詳細(xì)的信息。配置屬性也可以放在 yml 配置文件中,如 OpenAiChatProperties 的注解,需要以 spring.ai.openai.chat 開頭,例如將 gpt-4-turbo 配置在配置文件中,就是 OpenAiChatProperties 中 options 中的屬性。

spring:
  ai:
    openai:
      chat:
        options:
          model: gpt-4-turbo

多模型

可以配合圖片等讓聊天模型進(jìn)行回答。

    //給圖片來進(jìn)行聊天
    @Test
    public void test4() {
        //獲取圖片資源
        ClassPathResource resource = new ClassPathResource("2024052701.png");
        UserMessage userMessage = new UserMessage("說說你看到了什么", 
                List.of(new Media(MimeTypeUtils.IMAGE_PNG, resource)));
        ChatResponse res = chatModel.call(new Prompt(userMessage, OpenAiChatOptions.builder()
                .withModel("gpt-4-turbo")
                .build()));
        System.out.println(res);
        //獲取回答
        System.out.println(res.getResult().getOutput().getContent());
    }

圖像

基礎(chǔ)使用

主要類 org.springframework.ai.openai.OpenAiImageModel,快照版本不同,可能類不一樣??梢圆榭?org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration 中具體的圖像類是哪個(gè)。

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ImageTest {
    @Resource
    private OpenAiImageModel imageModel;
    @Test
    public void test(){
        //調(diào)用 image 模型的 call 方法獲取圖片
        ImageResponse res = imageModel.call(new ImagePrompt("山水畫"));
        //AI 繪制的圖片路徑
        String url = res.getResult().getOutput().getUrl();
        System.out.println(url);
    }
}

配置屬性

    @Test
    public void test2(){
        //使用 dall-e-2 繪畫,配置屬性模型創(chuàng)建可以參考 OpenAiImageModel 構(gòu)造函數(shù)
        OpenAiImageOptions options = OpenAiImageOptions.builder()
            .withModel(OpenAiImageApi.ImageModel.DALL_E_2.getValue())
            .build();
        ImageResponse res = imageModel.call(new ImagePrompt("山水畫", options));
        //獲取 AI 繪畫路徑
        String url = res.getResult().getOutput().getUrl();
        System.out.println(url);
    }

圖像模型屬性配置可以查看 org.springframework.ai.autoconfigure.openai.OpenAiImageProperties,也可以查看官網(wǎng)獲取更詳細(xì)的信息。當(dāng)然配置屬性也可以在 yml 中定義,如 OpenAiImageProperties 上的注解,需要以 spring.ai.openai.image 開頭,例如使用 dall-e-2 模型進(jìn)行繪畫

 spring:
  ai:
    openai:
      image:
        options:
          model: dall-e-2

語音

語音轉(zhuǎn)文字

基礎(chǔ)使用

主要類 org.springframework.ai.openai.OpenAiAudioTranscriptionModel,快照版本不同,可能名字不一樣,可以查看 org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration 中的語音轉(zhuǎn)文字翻譯類是哪個(gè)。

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
@SpringBootTest
public class AudioTest {
    //語音轉(zhuǎn)文字
    @Resource
    private OpenAiAudioTranscriptionModel transcriptionModel;
    @Test
    public void testTranscription1(){
        String res = transcriptionModel.call(new ClassPathResource("2024052702.mp3"));
        System.out.println(res);
    }
}

配置屬性

    @Test
    public void testTranscription2(){
        //創(chuàng)建模型屬性,采用 whisper-1 語音模型,配置屬性創(chuàng)建可以參考 OpenAiAudioTranscriptionModel 構(gòu)造函數(shù)
        OpenAiAudioTranscriptionOptions options = new OpenAiAudioTranscriptionOptions().builder()
                .withModel(OpenAiAudioApi.WhisperModel.WHISPER_1.getValue())
                .build();
        AudioTranscriptionResponse res = transcriptionModel.call(
            new AudioTranscriptionPrompt(new ClassPathResource("2024052702.mp3"), options));
        //獲取翻譯內(nèi)容
        String output = res.getResult().getOutput();
        System.out.println(output);
    }

語音轉(zhuǎn)文字模型屬性可以查看 org.springframework.ai.autoconfigure.openai.OpenAiAudioTranscriptionProperties,也可以在官網(wǎng)查看更詳細(xì)信息。當(dāng)然可以在 yml 配置中配置屬性,如 OpenAiAudioTranscriptionProperties 上的注解,以 spring.ai.openai.audio.transcription 開頭,例如采用 whisper-1 模型

spring:
  ai:
    openai:
      audio:
        transcription:
          options:
            model: whisper-1

文字轉(zhuǎn)語音

基礎(chǔ)使用

主要類 org.springframework.ai.openai.OpenAiAudioSpeechModel,快照版本不同,可能名字不一樣,可以查看 org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration 中的文字轉(zhuǎn)語音類是哪個(gè)。

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.ai.openai.OpenAiAudioSpeechModel;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileOutputStream;
import java.io.IOException;
@SpringBootTest
public class AudioTest2 {
    @Resource
    private OpenAiAudioSpeechModel speechModel;
    //byte數(shù)組轉(zhuǎn)文件
    private void byteArrayToFile(byte[] byteArray, String filePath) throws IOException {
        FileOutputStream fos = new FileOutputStream(filePath);
        fos.write(byteArray);
        fos.close();
    }
    @Test
    public void testSpeech() throws IOException {
        byte[] res = speechModel.call("我愛北京");
        byteArrayToFile(res,"D:\\project\\AI\\openai\\speech\\1.mp3");
    }
}

屬性配置

    @Test
    public void testSpeech2() throws IOException {
        //采用 tts-1-hd 模型,配置屬性創(chuàng)建可以參考 OpenAiAudioSpeechModel 構(gòu)造函數(shù)
        OpenAiAudioSpeechOptions options = new OpenAiAudioSpeechOptions().builder()
                .withModel(OpenAiAudioApi.TtsModel.TTS_1_HD.getValue())
                .build();
        SpeechPrompt prompt = new SpeechPrompt("我愛北京", options);
        SpeechResponse res = speechModel.call(prompt);
        byte[] bytes = res.getResult().getOutput();
        byteArrayToFile(bytes,"D:\\project\\AI\\openai\\speech\\1-hd.mp3");
    }

文字轉(zhuǎn)語音模型屬性可以查看 org.springframework.ai.autoconfigure.openai.OpenAiAudioSpeechProperties,也可以在官網(wǎng)查看更詳細(xì)信息。當(dāng)然可以在 yml 配置中配置屬性,如 OpenAiAudioSpeechProperties 上的注解,以 spring.ai.openai.audio.speech 開頭,例如采用 tts-1-hd 模型

spring:
  ai:
    openai:
      audio:
        speech:
          options:
            model: tts-1-hd

ollama

安裝 ollama

ollama 官網(wǎng)提供了下載地址,可以自己選擇版本安裝。ollama 主要提供了一些語言模型可以讓用戶在本地運(yùn)行模型。

安裝運(yùn)行模型

在 ollama 官網(wǎng)右上角提供下載模型。在模型中選擇想本地安裝使用的模型,如 谷歌語言模型 gemma,搜索查詢。選擇要下載的數(shù)據(jù)集,右邊選擇復(fù)制。

window 端打開 cmd 命令窗口,粘貼剛才復(fù)制的命令,回車下載安裝。

安裝完之后,使用剛才復(fù)制的命令,運(yùn)行 gemma 模型,可以向模型提問。

java 集成 ollama

Idea 創(chuàng)建 SpringBoot Maven 項(xiàng)目(Spring AI基于1.0-SNAPSHOT版本,SpringBoot 3.2.6),依賴選擇Spring Web、 Ollama。其他可以自行選擇

修改項(xiàng)目倉庫地址,中央倉庫暫時(shí)還沒 Spring AI 相關(guān) jar 包。倉庫地址改成快照倉庫地址,官方說明

    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>

項(xiàng)目中找到 pom.xml 文件,將 <spring-ai.version>0.8.1</spring-ai.version> 改為 <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>

簡單用法

SpringBoot 測試類

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SimpleTest {
    @Resource
    private OllamaChatModel chatModel;
    @Test
    public void test(){
        //使用下載的 gemma:7b 配置模型屬性,模型屬性創(chuàng)建可以參考 OllamaChatModel 構(gòu)造函數(shù)
        OllamaOptions options = OllamaOptions.create().withModel("gemma:7b");
        Prompt prompt = new Prompt("你好", options);
        //調(diào)用聊天模型,獲取返回值對象
        ChatResponse res = chatModel.call(prompt);
        //獲取 AI 回答字符串
        System.out.println(res.getResult().getOutput().getContent());
    }
    /**
     * 流式返回?cái)?shù)據(jù),類似打字效果,一個(gè)詞一個(gè)json數(shù)據(jù),整句話多個(gè)json數(shù)據(jù)
     */
    @Test
    public void test2(){
        //使用下載的 gemma:7b 配置模型屬性,模型屬性配置可以參考 OllamaChatModel 構(gòu)造函數(shù)
        OllamaOptions options = OllamaOptions.create().withModel("gemma:7b");
        Prompt prompt = new Prompt("你好", options);
        Flux<ChatResponse> flux = chatModel.stream(prompt);
        flux.toStream().forEach(res -> {
            System.out.println(res.getResult().getOutput().getContent());
        });
    }
}

模型屬性可以查看 org.springframework.ai.autoconfigure.ollama.OllamaChatProperties,也可以在官網(wǎng)查看具體的屬性。屬性配置也可以在 yml 文件上定義,如 OllamaChatProperties 上的注解,需要以 spring.ai.ollama.chat 開頭,如配置 gemma:7b、訪問其他服務(wù)器的 ollama(安裝ollama的服務(wù)器環(huán)境變量配置增加 OLLAMA_HOST=0.0.0.0:11434):

spring:
  ai:
    ollama:
      base-url: http://192.168.8.16:11434
      chat:
        model: gemma:7b

調(diào)用 ollama API

ollama 具體 API 方法可以查看官網(wǎng)

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
public class OllamaApiTest {
    @Test
    public void test() throws Exception{
        CloseableHttpClient client = HttpClients.createDefault();
        //ollama 默認(rèn)端口是 11434
        HttpPost post = new HttpPost("http://127.0.0.1:11434/api/generate");
        post.addHeader("Content-type","application/json; charset=utf-8");
        //參數(shù)
        Map<String, Object> map = new HashMap<>();
        map.put("model","gemma:7b");
        map.put("prompt","你好");
        //不以流式返回
        map.put("stream",false);
        StringEntity stringEntity = new StringEntity(JSONObject.valueToString(map),Charset.forName("UTF-8"));
        post.setEntity(stringEntity);
        CloseableHttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        System.out.println(EntityUtils.toString(entity));
    }
}

到此這篇關(guān)于Spring AI 入門學(xué)習(xí)指南的文章就介紹到這了,更多相關(guān)Spring AI 初學(xué)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論