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

这篇具有很好参考价值的文章主要介绍了Spring AI 来了,打造Java生态大模型应用开发新框架!。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

尽管 Python 长期主导 AI 大模型应用开发领域,但 Java 并未熄火!Spring AI 来了,正式告别实验期,迈向广泛应用新阶段!这意味着 Spring 生态体系的广大开发者,迎来 AI 大模型应用开发的新里程。
spring ai,人工智能,Spring相关,java相关,java,spring,人工智能,springAI

Spring AI 开发框架设计理念

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

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

spring ai,人工智能,Spring相关,java相关,java,spring,人工智能,springAI

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、回调、大模型响应等核心模块。
spring ai,人工智能,Spring相关,java相关,java,spring,人工智能,springAI文章来源地址https://www.toymoban.com/news/detail-849161.html

到了这里,关于Spring AI 来了,打造Java生态大模型应用开发新框架!的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • AIGC大模型时代下,该如何应用高性能计算PC集群打造游戏开发新模式?

    ACT | SIM | ETC | FTG | RAC AVG | RPG | FPS | MUG | PUZ ACT、SIM、ETC、FTG、RAC、RTS、STG、AVG、RPG、FPS、MUG、PUZ、SLG、SPG等游戏类型,需要高性能的计算机来支持运行。为了满足这些游戏的需求,国内服务器厂商不断推出新的产品,采用液冷散热技术,大模型构建和PC集群一体机等技术来提高

    2024年02月09日
    浏览(44)
  • AI 大模型应用开发实战纲要

    初探大模型:起源与发展 预热篇:解码注意力机制(Attention ) 变革里程碑:Transformer 的崛起 走向不同:GPT 与 Bert 的选择 GPT 模型家族:从始至今 GPT 模型家族:从始至今 从 GPT-1 到 GPT-3.5:一路的⻛云变幻 ChatGPT:赢在哪里 GPT-4:一个新的开始 大模型的未来:开源力量

    2024年02月07日
    浏览(35)
  • 大模型时代,如何快速开发AI应用

    本文分享自华为云社区 《【云享问答】第3期:大模型时代,如何快速开发AI应用》,作者:华为云社区精选。 大模型快速普及应用的当下,AI浪潮汹涌而至,对于开发者来说,开发一款属于自己的AI应用并不是遥不可及。华为云AI生态技术专家、中科院计算所博士坐阵,从数

    2024年02月08日
    浏览(36)
  • AI大模型开发架构设计(10)——AI大模型架构体系与典型应用场景

    1 AI大模型架构体系你了解多少? GPT 助手训练流程 横向来看,分为四步:预训练(无监督、99%算力+时间)、有监督微调、奖励模型、强化学习 纵向来看,每一部分需要:数据集(Dataset)、算法(Algorithm)、模型(Model)、花销 GPT 助手训练数据预处理 2个训练案例分析 2 AI 大

    2024年02月20日
    浏览(49)
  • Spring AI来了,Java开发者福音

    Spring AI来了,Java生态接入LLM大模型变得更加简单! 今天官宣Spring AI已经上架到Spring Initializr 上,它提供了一种更简洁的方式和AI交互,减轻Java业务中接入LLM模型应用的学习成本,目前在 https://start.spring.io/ 上可以使用并构建。 Spring AI 是一个人工智能工程的应用框架。其目标

    2024年03月21日
    浏览(36)
  • 全域Serverless+AI,华为云加速大模型应用开发

    日前,华为全联接大会2023在上海召开。华为云CTO张宇昕在大会上发布了基于Serverless技术的大模型应用开发框架,框架以面向AI领域全新升级的FunctionGraph 3.0为核心,将BaaS for AI 后端和开放平台快速无缝集成,助力企业轻松商用AI应用。 在“全域Serverless + AI 加速应用创新”专题

    2024年02月08日
    浏览(44)
  • AI大模型开发架构设计(2)——AI绘画技术架构&应用实践

    1 AI绘画整体流程 第一步:输入 Prompt 提示词: /mj 提示词 第二步:文生图(Text-to-Image)构图 第三步:图片渲染 第四步:图片展示 2 AI绘画技术架构 文生图核心算法原理 把人类创造的内容用一个高维的数学向量进行表示 如果内容到向量的“翻译” 足够合理 且能 代表内容的特

    2024年01月25日
    浏览(41)
  • 【基础篇001】⼤模型理论基础——初探大模型:起源与发展《AI 大模型应用开发实战指南》

      目录 基础篇:⼤模型理论基础 初探大模型:起源与发展 什么是大模型?

    2024年02月09日
    浏览(45)
  • 【AI大模型应用开发】【LangFuse: LangSmith平替,生产级AI应用维护平台】0. 快速上手 - 基本功能全面介绍与实践(附代码)

    大家好,我是同学小张,日常分享AI知识和实战案例 欢迎 点赞 + 关注 👏, 持续学习 , 持续干货输出 。 +v: jasper_8017 一起交流💬,一起进步💪。 微信公众号也可搜【同学小张】 🙏 本站文章一览: 前面我们介绍了LangChain无缝衔接的LangSmith平台,可以跟踪程序运行步骤,提

    2024年03月21日
    浏览(58)
  • 【AI大模型应用开发】【RAG评估】1. 通俗易懂:深度理解RAGAS评估方法的原理与应用

    大家好,我是同学小张,日常分享AI知识和实战案例 欢迎 点赞 + 关注 👏, 持续学习 , 持续干货输出 。 +v: jasper_8017 一起交流💬,一起进步💪。 微信公众号也可搜【同学小张】 🙏 本站文章一览: 上篇文章【AI大模型应用开发】【RAG评估】0. 综述:一文了解RAG评估方法、

    2024年04月13日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包