Spring AI 来了,打造Java生态大模型应用开发新框架!

Spring AI 来了,打造Java生态大模型应用开发新框架!
  • Spring AI 开发框架设计理念
    • Spring AI 主要功能特性如下
  • Spring AI 应用开发案例
    • 案例一:基于大模型的对话应用开发
    • 案例二:RAG 检索增强应用开发
    • 案例三:Function Calling Agent 应用开发

尽管 Python 长期主导 AI 大模型应用开发领域,但 Java 并未熄火!Spring AI 来了,正式告别实验期,迈向广泛应用新阶段!这意味着 Spring 生态体系的广大开发者,迎来 AI 大模型应用开发的新里程。
在这里插入图片描述

Spring AI 开发框架设计理念

Spring AI 是一个 AI 工程师的应用框架,它提供了一个友好的 API 和开发 AI 应用的抽象,旨在简化 AI 大模型应用的开发工作。

Spring AI 吸取了知名 Python 项目的精髓,比如:LangChainLlamaIndexSpring AI 是基于这样一个理念创立的:未来的 AI 大模型应用将不仅限于 Python 开发者,而且会普及到多种编程语言中。Spring AI 的核心是提供了开发 AI 大模型应用所需的基本抽象模型,这些抽象拥有多种实现方式,使得开发者可以用很少的代码改动就能实现组件的轻松替换。

在这里插入图片描述

Spring AI 主要功能特性如下

  • 第一、 对主流 AI 大模型供应商提供了支持,比如:OpenAI、Microsoft、Amazon、Google HuggingFace、Ollama、MistralAI 支持,目前对国内大模型支持还不友好。
  • 第二、 支持 AI 大模型类型包括:聊天、文本到图像、文本到声音,比如:OpenAI with DALL-E、StabilityAI 等。
  • 第三、 支持主流的 Embedding Model 和向量数据库,比如:Azure Vector Search、Chroma、Milvus、Neo4j、PostgreSQL/PGVector、PineCone、Redis 等。
  • 第四、 把 AI 大模型输出映射到简单的 Java 对象(POJOs)上。
  • 第五、 支持了函数调用(Function calling)功能。
  • 第六、 为数据工程提供 ETL(数据抽取、转换和加载)框架。
  • 第七、 支持 Spring Boot 自动配置和快速启动,便于运行 AI 模型和管理向量库。
    当前,Spring AI 最新版本为 0.8.1,具体使用也比较简单,符合 Java 开发者的开发习惯。
    更详细的特性在这里:https://spring.io/projects/spring-ai

Spring AI 应用开发案例

接下来我们来看3个具体的开发案例,Spring AI 最新版本为 0.8.1,具体使用也比较简单,符合 Java 开发者的开发习惯。

案例一:基于大模型的对话应用开发


package org.springframework.ai.openai.samples.helloworld.simple;

import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class SimpleAiController {

  private final ChatClient chatClient;

  @Autowired
  public SimpleAiController(ChatClient chatClient) {
    this.chatClient = chatClient;
  }

  @GetMapping("/ai/simple")
  public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
    return Map.of("generation", chatClient.call(message));
  }
}

案例二:RAG 检索增强应用开发

package org.springframework.samples.ai.azure.openai.rag;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.client.AiClient;
import org.springframework.ai.client.AiResponse;
import org.springframework.ai.client.Generation;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingClient;
import org.springframework.ai.loader.impl.JsonLoader;
import org.springframework.ai.prompt.Prompt;
import org.springframework.ai.prompt.SystemPromptTemplate;
import org.springframework.ai.prompt.messages.Message;
import org.springframework.ai.prompt.messages.UserMessage;
import org.springframework.ai.retriever.impl.VectorStoreRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.impl.InMemoryVectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RagService {

    private static final Logger logger = LoggerFactory.getLogger(RagService.class);

    @Value("classpath:/data/bikes.json")
    private Resource bikesResource;

    @Value("classpath:/prompts/system-qa.st")
    private Resource systemBikePrompt;

    private final AiClient aiClient;
    private final EmbeddingClient embeddingClient;

    public RagService(AiClient aiClient, EmbeddingClient embeddingClient) {
        this.aiClient = aiClient;
        this.embeddingClient = embeddingClient;
    }

    public Generation retrieve(String message) {

        // Step 1 - Load JSON document as Documents

        logger.info("Loading JSON as Documents");
        JsonLoader jsonLoader = new JsonLoader(bikesResource,
                "name", "price", "shortDescription", "description");
        List<Document> documents = jsonLoader.load();
        logger.info("Loading JSON as Documents");

        // Step 2 - Create embeddings and save to vector store

        logger.info("Creating Embeddings...");
        VectorStore vectorStore = new InMemoryVectorStore(embeddingClient);
        vectorStore.add(documents);
        logger.info("Embeddings created.");

        // Step 3 retrieve related documents to query

        VectorStoreRetriever vectorStoreRetriever = new VectorStoreRetriever(vectorStore);
        logger.info("Retrieving relevant documents");
        List<Document> similarDocuments = vectorStoreRetriever.retrieve(message);
        logger.info(String.format("Found %s relevant documents.", similarDocuments.size()));

        // Step 4 Embed documents into SystemMessage with the `system-qa.st` prompt template

        Message systemMessage = getSystemMessage(similarDocuments);
        UserMessage userMessage = new UserMessage(message);

        // Step 4 - Ask the AI model

        logger.info("Asking AI model to reply to question.");
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
        logger.info(prompt.toString());
        AiResponse response = aiClient.generate(prompt);
        logger.info("AI responded.");
        logger.info(response.getGeneration().toString());
        return response.getGeneration();
    }

    private Message getSystemMessage(List<Document> similarDocuments) {

        String documents = similarDocuments.stream().map(entry -> entry.getContent()).collect(Collectors.joining("\n"));
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemBikePrompt);
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("documents", documents));
        return systemMessage;

    }
}

案例三:Function Calling Agent 应用开发

Spring AI Function Calling 函数调用工作流程如下图所示:包含了 Prompt 提示词、大模型、业务服务 API、回调、大模型响应等核心模块。
在这里插入图片描述

大模型岗位需求

大模型时代,企业对人才的需求变了,AIGC相关岗位人才难求,薪资持续走高,AI运营薪资平均值约18457元,AI工程师薪资平均值约37336元,大模型算法薪资平均值约39607元。
在这里插入图片描述

掌握大模型技术你还能拥有更多可能性

• 成为一名全栈大模型工程师,包括Prompt,LangChain,LoRA等技术开发、运营、产品等方向全栈工程;

• 能够拥有模型二次训练和微调能力,带领大家完成智能对话、文生图等热门应用;

• 薪资上浮10%-20%,覆盖更多高薪岗位,这是一个高需求、高待遇的热门方向和领域;

• 更优质的项目可以为未来创新创业提供基石。

可能大家都想学习AI大模型技术,也想通过这项技能真正达到升职加薪,就业或是副业的目的,但是不知道该如何开始学习,因为网上的资料太多太杂乱了,如果不能系统的学习就相当于是白学。为了让大家少走弯路,少碰壁,这里我直接把全套AI技术和大模型入门资料、操作变现玩法都打包整理好,希望能够真正帮助到大家。

读者福利:如果大家对大模型感兴趣,这套大模型学习资料一定对你有用

零基础入门AI大模型

今天贴心为大家准备好了一系列AI大模型资源,包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

有需要的小伙伴,可以点击下方链接免费领取【保证100%免费

点击领取 《AI大模型&人工智能&入门进阶学习资源包》*

1.学习路线图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如果大家想领取完整的学习路线及大模型学习资料包,可以扫下方二维码获取
在这里插入图片描述
👉2.大模型配套视频👈

很多朋友都不喜欢晦涩的文字,我也为大家准备了视频教程,每个章节都是当前板块的精华浓缩。(篇幅有限,仅展示部分)

img

大模型教程

👉3.大模型经典学习电子书👈

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。(篇幅有限,仅展示部分,公众号内领取)

img

电子书

👉4.大模型面试题&答案👈

截至目前大模型已经超过200个,在大模型纵横的时代,不仅大模型技术越来越卷,就连大模型相关的岗位和面试也开始越来越卷了。为了让大家更容易上车大模型算法赛道,我总结了大模型常考的面试题。(篇幅有限,仅展示部分,公众号内领取)

img

大模型面试

**因篇幅有限,仅展示部分资料,**有需要的小伙伴,可以点击下方链接免费领取【保证100%免费

点击领取 《AI大模型&人工智能&入门进阶学习资源包》

**或扫描下方二维码领取 **

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员一粟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值