laitimes

Spring AI Early Access, 5 minutes of Java AI application development

author:Not bald programmer
Spring AI Early Access, 5 minutes of Java AI application development

Spring AI is an official Spring community project that aims to simplify Java AI application development and enable Java developers to develop AI applications in the same way that they would develop regular applications with Spring.

Spring Cloud Alibaba AI is based on Spring AI, and on this basis, it provides comprehensive adaptation of Alibaba Cloud's Tongyi series of large models, allowing users to develop Java AI applications based on Tongyi large models in less than 5 minutes.

Spring AI Early Access, 5 minutes of Java AI application development

Spring AI x 通义千问 Demo 已上线至 sca.aliyun.com

Introduction to Spring AI

According to Spring AI's official website, the project is inspired by well-known Python projects such as LangChain and LlamaIndex, but Spring AI is not a direct copy of these projects. Spring AI believes that the next wave of generative AI generative applications will not only be for Python developers, but will be widely used in many programming languages.

At its core, Spring AI is about providing abstractions as a foundation for developing Java AI applications, providing the following capabilities:

  • Provide a variety of large-scale model service docking capabilities, including most mainstream large-scale model services in the industry.
  • 支持灵活的 Prompt Template 和模型输出解析 Output Parsing 能力;
  • Support multimodal generative AI capabilities, such as dialogue, Wensheng diagrams, Wensheng voices, etc.;
  • Provide a common portable API to access a variety of model services and embedding services, support synchronous and streaming calls, and also support passing custom parameters for specific models;
  • 支持 RAG 能力的基础组件,包括 DocumentLoader、TextSpillter、EmobeddingClient、VectorStore 等;
  • 支持 AI Spring Boot Starter 实现配置自动装配。

Spring Cloud Alibaba AI 简介

Spring Cloud Alibaba AI is currently based on the Spring AI 0.8.1[1] API to complete the integration of the general series of large models. Based on the concept of "Model-as-a-Service" (MaaS), Tongyi Access is based on Alibaba Cloud's Lingji Model Service[2], which provides a variety of model services, including model inference and model fine-tuning training, through standardized APIs around models in various AI domains.

In the latest version, Spring Cloud Alibaba AI has mainly completed the adaptation of several common generative models, including dialogue, Wensheng diagram, Wensheng speech, etc., developers can use Spring Cloud Alibaba AI to develop chat, image or speech generation AI applications based on general meaning, and the framework also provides practical capabilities such as OutParser, Prompt Template, and Stuff.

The following is an example of Spring Cloud Alibaba AI application development that is currently officially provided, which can be viewed on sca.aliyun.com.

  • Chat conversation app
  • Wensheng diagram application
  • Wensheng voice app
  • Model Output Parser (Implements String to Automatic POJO Mapping)
  • 使用 Prompt Template
  • 让 AI 模型接入外部数据(Prompt Stuff)

Experience the first Spring AI application development

This project demonstrates how to use spring-cloud-starter-alibaba-ai to complete an online chat AI application, using the model service provided by Tongyi Qianwen. The full sample source code can be found here [3].

Develop a chat conversation app

1. 在项目 pom.xml 中加入 2023.0.1.0 版本 Spring Cloud Alibaba 依赖:

<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>           

2. Add the following configuration to the application.yml configuration file:

spring:
  cloud:
    ai:
      tongyi:
        chat:
          options:
            # Replace the following key with a valid API-KEY.
            api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5axx           

3. 编写聊天服务实现类,由 Spring AI 自动注入 ChatClient、StreamingChatClient,ChatClient 屏蔽底层通义大模型交互细节。

@Service
public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {

  private final ChatClient chatClient;

  private final StreamingChatClient streamingChatClient;

  @Autowired
  public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) {
    this.chatClient = chatClient;
    this.streamingChatClient = streamingChatClient;
  }
}           

4. Provide a specific chat logic implementation

@Service
public class TongYiSimpleServiceImpl extends AbstractTongYiServiceImpl {

  // ......

  @Override
  public String completion(String message) {

    Prompt prompt = new Prompt(new UserMessage(message));

    return chatClient.call(prompt).getResult().getOutput().getContent();
  }

  @Override
  public Map<String, String> streamCompletion(String message) {

    StringBuilder fullContent = new StringBuilder();

    streamingChatClient.stream(new Prompt(message))
        .flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults()))
        .map(content -> content.getOutput().getContent())
        .doOnNext(fullContent::append)
        .last()
        .map(lastContent -> Map.of(message, fullContent.toString()))
        .block();

    log.info(fullContent.toString());

    return Map.of(message, fullContent.toString());
  }
}           

5. Write the Spring entry class and launch the application

@SpringBootApplication
public class TongYiApplication {
  public static void main(String[] args) {
    SpringApplication.run(TongYiApplication.class);
  }
}           

At this point, the simplest chat AI application development is complete, which is exactly the same as the normal Spring Boot application development steps!

Verify the app's performance

After you launch your app, you can verify the performance of your app in two ways.

Way 1

Enter http://localhost:8080/ai/example in the browser address bar

The following response is returned:

{
    "Tell me a joke": "Sure, here's a classic one for you:\n\nWhy was the math book sad?\n\nBecause it had too many problems.\n\nI hope that made you smile! If you're looking for more, just let me know."
}           

Way two

Go to the resources/static directory, open the index.html file in your browser, enter the question, and get the output response (make sure the api-key is valid):

Spring AI Early Access, 5 minutes of Java AI application development

APPLY FOR A GENERAL API-KEY

In order for the sample to be connected to the general model, you need to activate the DashScope model service on Alibaba Cloud, apply for a valid API-KEY, and update it to the application configuration file. For more information, see https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key

Planning for the future

The current version of Spring Cloud Alibaba AI has completed the adaptation of several common generative models, including dialogue, Wensheng graph, Wensheng speech, etc. In the next release, we will continue to complete more adaptations such as VectorStore, Embedding, and ETL Pipeline to simplify more AI application development scenarios such as RAG.

Spring AI Early Access, 5 minutes of Java AI application development

Related Links:

[1] Spring AI 0.8.1

[2] Spiritual product model service

[3] Full sample source code

Read on