當前位置: 妍妍網 > 碼農

阿裏也出手了!Spring Cloud Alibaba AI 初體驗

2024-07-07碼農

戳上方藍字「 哪咤編程 」關註我

什麽是Spring AI

Spring AI是從著名的Python計畫 LangChain 和 LlamaIndex 中汲取靈感,它不是這些計畫的直接移植,它的成立信念是, 「下一波生成式人工智慧應用程式將不僅適用於 Python 開發人員,而且將在許多程式語言中無處不在」

我們可以從Spring AI的官網描述中,總結出Spring AI的幾個核心的關鍵詞:

  • 提供抽象能力

  • 簡化AI套用的開發

  • 模型與向量支持

  • AI整合與自動配置

  • Spring AI簡化了我們構建 「大型復雜的AI套用」 的過程,當然如果你的計畫僅僅是需要呼叫一個AI介面,那其實直接呼叫官方SDK反而更方便。

    Spring AI提供的功能如下:

  • 支持所有主要的模型提供商,如OpenAI,Microsoft,Amazon,Google和Huggingface。支持的模型型別包括聊天和文本到影像。

  • 跨 AI 提供商的可移植 API,用於聊天和嵌入模型。支持同步和流 API 選項。還支持下拉以存取特定於模型的功能。

  • 「將 AI 模型輸出對映到 POJO」

  • 支持所有主要的向量資料庫,例如 Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Qdrant、Redis 和 Weaviate。

  • 跨 Vector Store 提供程式的可移植 API,包括新穎的類似 SQL 的後設資料過濾器 API,該 API 也是可移植的。

  • AI 模型和向量儲存的 「Spring Boot stater」

  • 用於數據工程的 ETL 框架

  • 國內直接使用ChatGPT4o:

    用官方一半價格的錢,用跟官方 ChatGPT4.0 一模一樣功能的工具。

    國內直接使用 ChatGPT4o

    1. 無需魔法,同時支持電腦、手機,瀏覽器直接使用

    2. ChatGPT3.5永久免費

    3. 支持 Chat GPT-4o文本對話、 Copi lot編程、DALL-E AI繪畫、AI語音對話、論文外掛程式Consensus等

    長按辨識下方二維碼,備註ai, 發給你

    什麽是Spring Cloud Alibaba AI

    原始的Spring AI並沒有國內相關大模型的接入,對國內開發者不太友好。

    總的來說,Spring Cloud Alibaba AI 目前基於Spring AI 0.8.1版本 API 完成通義系列大模型的接入。

    在當前最新版本中,Spring Cloud Alibaba AI 主要完成了幾種常見生成式模型的適配,包括對話、文生圖、文生語音等,開發者可以使用 Spring Cloud Alibaba AI 開發基於通義的聊天、圖片或語音生成 AI 套用,框架還提供 OutParser、Prompt Template、Stuff 等實用能力。

    Spring Cloud Alibaba AI官方還提供了包括 「聊天對話、文生圖、文生語音」 等多種套用的開發範例,具體可以前往官網檢視: 快速開始 | https://sca.aliyun.com [1]

    動手體驗Spring Cloud Alibaba AI

    首先新建一個Maven計畫,JDK選的是17版本。

    Maven檔需要引入 spring-cloud-alibaba-dependencies spring-cloud-starter-alibaba-ai 兩個依賴。

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2023.0.1.0</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
    </dependency>
    </dependencies>

    配置阿裏雲通義千問的Api-Key,沒有的讀者可以從官網上申請。

    server:
    port:8080
    spring:
    application:
    name:alibaba-spring-ai-demo
    cloud:
    ai:
    tongyi:
    api-key:你的api-key

    新建SpringBoot啟動類:

    @SpringBootApplication
    public classMyAiApplication{
    publicstaticvoidmain(String[] args){
    SpringApplication.run(MyAiApplication. class,args);
    }
    }

    對接文本模型

    我們首先測試如何對接文本大模型。

    新建一個控制器類:新建 /simple 介面,用來測試基本QA。

    @RestController
    @RequestMapping("/ai")
    @CrossOrigin
    public classTongYiController{
    @Autowired
    @Qualifier("tongYiSimpleServiceImpl")
    private TongYiService tongYiSimpleService;
    @GetMapping("/simple")
    public String completion(
    @RequestParam(value = "message", defaultValue = "AI時代下Java開發者該何去何從?")

    String message
    {
    return tongYiSimpleService.completion(message);
    }
    }

    新建一個 TongyiService 服務類:

    publicinterfaceTongYiService{
    /**
    * 基本問答
    */

    String completion(String message);
    /**
    * 文生圖
    */

    ImageResponse genImg(String imgPrompt);
    /**
    * 語音合成
    */

    String genAudio(String text);
    }

    具體的實作類如下:由 Spring AI 自動註入 ChatClient StreamingChatClient ChatClient 遮蔽底層通義大模型互動細節,後者用於流式呼叫。

    對於QA而言,僅僅透過 client.call(prompt) 一行程式碼就可以完成對模型的呼叫。

    @Service
    @Slf4j
    public classTongYiSimpleServiceImplextendsAbstractTongYiServiceImpl{
    /**
    * 自動註入ChatClient、StreamingChatClient,遮蔽模型呼叫細節
    */

    privatefinal ChatClient chatClient;
    privatefinal StreamingChatClient streamingChatClient;
    @Autowired
    publicTongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient){
    this.chatClient = chatClient;
    this.streamingChatClient = streamingChatClient;
    }
    /**
    * 具體實作:
    */

    @Override
    public String completion(String message){
    Prompt prompt = new Prompt(new UserMessage(message));
    return chatClient.call(prompt).getResult().getOutput().getContent();
    }
    }

    我們發送一個請求,prompt是 AI時代下Java開發者該何去何從? 測試結果如下:

    文生圖模型

    這裏只給出service的程式碼,其它程式碼同上面的文本問答。

    可以看到,只需要例項化一個 imagePrompt ,再呼叫模型即可。

    @Slf4j
    @Service
    public classTongYiImagesServiceImplextendsAbstractTongYiServiceImpl{
    privatestaticfinal Logger logger = LoggerFactory.getLogger(TongYiService. class);
    privatefinal ImageClient imageClient;
    @Autowired
    publicTongYiImagesServiceImpl(ImageClient client){
    this.imageClient = client;
    }
    @Override
    public ImageResponse genImg(String imgPrompt){
    var prompt = new ImagePrompt(imgPrompt);
    return imageClient.call(prompt);
    }
    }

    測試的prompt是: Painting a boy coding in front of the desk, with his dog. ,測試結果如下,效果還是很不錯的:

    語音合成模型

    @Slf4j
    @Service
    public classTongYiAudioSimpleServiceImplextendsAbstractTongYiServiceImpl{
    privatestaticfinal Logger logger = LoggerFactory.getLogger(TongYiService. class);
    privatefinal SpeechClient speechClient;
    @Autowired
    publicTongYiAudioSimpleServiceImpl(SpeechClient client){
    this.speechClient = client;
    }
    @Override
    public String genAudio(String text){
    logger.info("gen audio prompt is: {}", text);
    var resWAV = speechClient.call(text);
    // save的程式碼省略,就是將音訊保存到本地而已
    return save(resWAV, SpeechSynthesisAudioFormat.WAV.getValue());
    }
    }

    測試結果也是成功的:

    使用體驗小結

    不得不說,阿裏在Java開發領域一直是走在國內的前沿的,我也期待阿裏繼續完善Spring Cloud Alibaba AI的相關功能,為我們這些國內Java開發者提供更加方便的開發工具。

    本文僅僅簡單測試了文本問答、文生圖以及語音合成三個功能,(最後一個沒列出來),Spring Cloud Alibaba AI還有很多豐富的功能,如流式呼叫、POJO轉換、AI Role等功能,各位讀者感興趣可以自行前往官方example倉庫檢視。後續也我打算利用Spring Cloud Alibaba AI嘗試構建一個RAG問答套用。

    下面給出我的使用小結:

    1. 「簡化開發」 。個人開發者如果僅僅需要簡答的問答介面,無需使用Spring AI,然而,當計畫中需要開發比較復雜的AI功能,如果僅僅使用官方的SDK,寫出的程式碼可能不太容易長期維護。

    2. 「響應時間」 。介面響應時間還有很大的最佳化空間,可以看到基本的文本問答的響應就耗費了10s,不過這也取決於所處理任務的大小。

    3. 「模型選擇」 。之前使用SDK可以自己選擇通義提供的各種模型,而使用Spring AI框架,暫時不知道如何選擇其它模型進行呼叫,有知道的掘友也可以在評論區說一下。

    未來,Spring Cloud Alibaba AI還將繼續完成 VectorStore、Embedding、ETL Pipeline 等更多適配,簡化 RAG 等更多 AI 套用開發場景。身為Java開發者,我也將繼續關註Spring Cloud Alibaba 社群的最新動態。

    Reference

    [1]

    快速開始 | https://sca.aliyun.com: https://sca.aliyun.com/docs/2023/user-guide/ai/quick-start/

    來源:juejin.cn/post/7361752279726866432

    ·················END·················

    用官方一半價格的錢,用跟官方 ChatGPT4.0 一模一樣功能的工具。

    國內直接使用ChatGPT4o

    1. 無需魔法,同時支持手機、電腦,瀏覽器直接使用

    2. 帳號獨享

    3. ChatGPT3.5永久免費

    長按辨識下方二維碼,備註ai,發給你

    回復gpt,獲取ChatGPT4o直接使用地址

    點選閱讀原文,國內直接使用ChatGpt4o