人工智能 开发语言 Java程序员的AI框架,它来了 - 用于数据工程的 ETL 框架

📅 2026-11-30 👤 拼多多助力营销技巧 📂 2027拼多多助力 👁️ 322 次阅读

人工智能 开发语言 Java程序员的AI框架,它来了

摘要:

在本文中,我们将探索Spring AI——一个为Java开发者社区设计的革命性的人工智能框架。了解其核心功能、如何集成到现有的Spring应用中,以及它如何简化AI项目的开发。

前言:

在当今这个由数据和智能驱动的时代,人工智能(AI)已经成为推动业务创新和优化用户体验的关键力量。从个性化推荐系统到自然语言处理,AI技术正在以前所未有的速度渗透到我们生活的每一个角落。然而,尽管AI的承诺如此诱人,对于许多Java开发者来说,将AI集成到现有的应用和服务中仍然是一项挑战。这是因为大多数AI框架和工具往往要求开发者具备专门的知识,或者需要在使用不同的技术栈之间进行昂贵的转换。

Java一直是企业级应用开发的主力军,以其成熟的生态系统、强大的性能和跨平台的能力而受到广泛赞誉。但是,当涉及到AI时,Java社区似乎缺乏一个既能与Spring等现有框架无缝集成,又能为开发者提供易于上手的AI解决方案的平台。

那么,对于Java开发者来说,有没有一个易于上手且功能强大的AI框架呢?答案是肯定的。随着Spring AI的推出,Java开发者现在有了一个专为他们量身打造的AI框架。Spring AI不仅承诺将AI的力量带到Java开发者的指尖,而且还保证与Java开发者已经熟悉的Spring生态系统无缝集成。这无疑是Java和AI领域的一大进步。

在本文中,我们将深入探讨Spring AI,了解它如何为Java开发者提供一个平滑的AI集成路径,以及它如何帮助开发者快速构建和部署智能应用。无论您是AI新手还是有经验的开发者,Spring AI都旨在使您能够轻松地将AI的强大功能集成到您的Java应用中,从而开启新一代智能应用的篇章。

入门示例:

话不多说,先看示例:

@RestController

@RequestMapping("/chat")

public class SimpleAiController {

private final ChatClient chatClient;

@Autowired

public SimpleAiController(ChatClient chatClient) {

this.chatClient = chatClient;

}

@GetMapping("/ai/simple")

public Mapcompletion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {

return Map.of("generation", chatClient.call(message));

}

}

调用Restful接口:

>curl localhost:8080/ai/simple

{"generation":"Why couldn't the bicycle stand up by itself? Because it was two tired!"}

可以看到,还是一贯的Spring风格,调用十分简单。以后用Java写AI应用再也不用写复杂的封装代码,或者到Github寻找一些不能确定靠不靠谱的三方框架了,Java程序员开发AI应用也能像写CRUD代码一样轻松了。(这里推荐一个比较优秀的Java AI框架-langchian4j,使用十分简单,之前在公司开发AI功能的时候发现的,但因为是三方框架,不敢直接集成到公司项目里,最后只能含泪封装了一大堆参数)

Spring AI简介:

Spring AI旨在简化包含人工智能功能的应用程序的开发,而不会造成不必要的复杂性。

该项目从著名的 Python 项目中汲取灵感,例如 LangChain 和 LlamaIndex,但 Spring AI 并不是这些项目的直接移植。该项目成立的信念是,下一波生成式 AI 应用程序将不仅适用于 Python 开发人员,而且将在许多编程语言中无处不在。

Spring AI 的核心是提供抽象,作为开发 AI 应用程序的基础。这些抽象具有多种实现,只需最少的代码更改即可轻松交换组件。

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 也是可移植的。 Function calling 函数调用 用于 AI 模型和矢量存储的 Spring Boot 自动配置和启动器。 用于数据工程的 ETL 框架

具体使用:

环境配置:

JDK要求版本:17添加以下存储库定义:

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

false

或 repositories {

mavenCentral()

maven { url 'https://repo.spring.io/milestone' }

maven { url 'https://repo.spring.io/snapshot' }

} 依赖管理:

org.springframework.ai

spring-ai-bom

0.8.1-SNAPSHOT

pom

import

或 dependencies {

implementation platform("org.springframework.ai:spring-ai-bom:0.8.1-SNAPSHOT")

// Replace the following with the starter dependencies of specific modules you wish to use

implementation 'org.springframework.ai:spring-ai-openai'

} 配置文件: spring.ai.openai.api-key=YOUR_API_KEY

spring.ai.openai.chat.options.model=gpt-3.5-turbo

spring.ai.openai.chat.options.temperature=0.7 开始使用

OpenAI 文本生成流式/非流式输出:

非流式: @RestController

@RequestMapping("/chat")

public class SimpleAiController {

private final ChatClient chatClient;

@Autowired

public SimpleAiController(ChatClient chatClient) {

this.chatClient = chatClient;

}

@GetMapping("/ai/simple")

public Mapcompletion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {

return Map.of("generation", chatClient.call(message));

}

} 调用: >curl localhost:8080/ai/simple

{"generation":"Why couldn't the bicycle stand up by itself? Because it was two tired!"} 流式: @RestController 拼多多帮助力入口

@RequestMapping("/chat")

public class SimpleAiController {

private final StreamingChatClient streamChatClient;

@Autowired

public SimpleAiController(StreamingChatClient streamChatClient) {

this.streamChatClient=streamChatClient;

}

//流式

@GetMapping("/ai/stream")

public void streamCompletion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {

streamChatClient.stream(message).subscribe(

m ->System.out.println("Received message: " + m),

throwable ->System.err.println("Error occurred: " + throwable.getMessage()),

() ->System.out.println("Stream completed")

);

return;

}

} 调用: >curl http://localhost:8080/ai/stream 结果:

OpenAI图片生成:

@RestController

@RequestMapping("/chat")

public class SimpleAiController {

private final ImageClient imageClient;

@Autowired

public SimpleAiController(ImageClient imageClient) {

this.imageClient=imageClient;

}

@GetMapping("/ai/image")

public void imageCompletion() {

ImageResponse response = imageClient.call(

new ImagePrompt("A cute baby dog",

OpenAiImageOptions.builder()

.withQuality("hd")

.withN(1)

.withHeight(1024)

.withWidth(1024)

.withModel(OpenAiImageApi.DEFAULT_IMAGE_MODEL)

.build())

);

}

}

调用:

>curl http://localhost:8080/chat/ai/image

结果:

访问Url:

其他:

可以看到调用方式还是非常简单,其他功能可以参考Spring AI官方文档,相信即使是没有接触过AI应用开发的Javaer也能够很快上手。

网络环境解决方案:

借助阿里云FC函数代理

人工智能 开发语言 Java程序员的AI框架,它来了

参考阅读

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧