当前位置:   article > 正文

Java程序员的AI框架,它来了

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应用中,从而开启新一代智能应用的篇章。

入门示例:

话不多说,先看示例:

  1. @RestController
  2. @RequestMapping("/chat")
  3. public class SimpleAiController {
  4. private final ChatClient chatClient;
  5. @Autowired
  6. public SimpleAiController(ChatClient chatClient) {
  7. this.chatClient = chatClient;
  8. }
  9. @GetMapping("/ai/simple")
  10. public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
  11. return Map.of("generation", chatClient.call(message));
  12. }
  13. }

调用Restful接口:

  1. >curl localhost:8080/ai/simple
  2. {"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 框架

具体使用:

环境配置:

  1. JDK要求版本:17
  2. 添加以下存储库定义:
    1. <repositories>
    2. <repository>
    3. <id>spring-milestones</id>
    4. <name>Spring Milestones</name>
    5. <url>https://repo.spring.io/milestone</url>
    6. <snapshots>
    7. <enabled>false</enabled>
    8. </snapshots>
    9. </repository>
    10. <repository>
    11. <id>spring-snapshots</id>
    12. <name>Spring Snapshots</name>
    13. <url>https://repo.spring.io/snapshot</url>
    14. <releases>
    15. <enabled>false</enabled>
    16. </releases>
    17. </repository>
    18. </repositories>

    1. repositories {
    2. mavenCentral()
    3. maven { url 'https://repo.spring.io/milestone' }
    4. maven { url 'https://repo.spring.io/snapshot' }
    5. }
  3. 依赖管理:
    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframework.ai</groupId>
    5. <artifactId>spring-ai-bom</artifactId>
    6. <version>0.8.1-SNAPSHOT</version>
    7. <type>pom</type>
    8. <scope>import</scope>
    9. </dependency>
    10. </dependencies>
    11. </dependencyManagement>

    1. dependencies {
    2. implementation platform("org.springframework.ai:spring-ai-bom:0.8.1-SNAPSHOT")
    3. // Replace the following with the starter dependencies of specific modules you wish to use
    4. implementation 'org.springframework.ai:spring-ai-openai'
    5. }
  4. 配置文件:
    1. spring.ai.openai.api-key=YOUR_API_KEY
    2. spring.ai.openai.chat.options.model=gpt-3.5-turbo
    3. spring.ai.openai.chat.options.temperature=0.7
  5. 开始使用

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

  1. 非流式:  
    1. @RestController
    2. @RequestMapping("/chat")
    3. public class SimpleAiController {
    4. private final ChatClient chatClient;
    5. @Autowired
    6. public SimpleAiController(ChatClient chatClient) {
    7. this.chatClient = chatClient;
    8. }
    9. @GetMapping("/ai/simple")
    10. public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
    11. return Map.of("generation", chatClient.call(message));
    12. }
    13. }
    调用:
    1. >curl localhost:8080/ai/simple
    2. {"generation":"Why couldn't the bicycle stand up by itself? Because it was two tired!"}
  2. 流式:
     

    1. @RestController
    2. @RequestMapping("/chat")
    3. public class SimpleAiController {
    4. private final StreamingChatClient streamChatClient;
    5. @Autowired
    6. public SimpleAiController(StreamingChatClient streamChatClient) {
    7. this.streamChatClient=streamChatClient;
    8. }
    9. //流式
    10. @GetMapping("/ai/stream")
    11. public void streamCompletion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
    12. streamChatClient.stream(message).subscribe(
    13. m -> System.out.println("Received message: " + m),
    14. throwable -> System.err.println("Error occurred: " + throwable.getMessage()),
    15. () -> System.out.println("Stream completed")
    16. );
    17. return;
    18. }
    19. }

    调用:
     

    >curl http://localhost:8080/ai/stream

    结果:

OpenAI图片生成:

  1. @RestController
  2. @RequestMapping("/chat")
  3. public class SimpleAiController {
  4. private final ImageClient imageClient;
  5. @Autowired
  6. public SimpleAiController(ImageClient imageClient) {
  7. this.imageClient=imageClient;
  8. }
  9. @GetMapping("/ai/image")
  10. public void imageCompletion() {
  11. ImageResponse response = imageClient.call(
  12. new ImagePrompt("A cute baby dog",
  13. OpenAiImageOptions.builder()
  14. .withQuality("hd")
  15. .withN(1)
  16. .withHeight(1024)
  17. .withWidth(1024)
  18. .withModel(OpenAiImageApi.DEFAULT_IMAGE_MODEL)
  19. .build())
  20. );
  21. response.getResults().stream().forEach(r -> System.out.println("Image: " + r.getOutput()));
  22. }
  23. }

调用:

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

结果:

访问Url:

其他:

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

网络环境解决方案:

借助阿里云FC函数代理

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/659599
推荐阅读
相关标签
  

闽ICP备14008679号