Early access to Spring AI, get started with Java AI application development in 5 minutes

Spring AI is an official community project of Spring, aiming to simplify the development of Java AI applications and allow Java developers to develop AI applications just like using Spring to develop ordinary applications.

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

Spring AI x Tongyi Qianwen Demo has been launched on sca.aliyun.com

Introduction to Spring AI

According to the Spring AI official website, the project is inspired by famous 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 applications will not only be available to Python developers, but will be widely used in many programming languages.

At its core, Spring AI provides abstractions as a basis for developing Java AI applications, providing the following capabilities:

  • Provides multiple large model service docking capabilities, including most mainstream large model services in the industry;
  • Supports flexible Prompt Template and model output parsing Output Parsing capabilities;
  • Support multi-modal generative AI capabilities, such as dialogue, Vincentian pictures, Vincentian speech, etc.;
  • Provides a common and portable API to access various model services and Embedding services, supports synchronous and streaming calls, and also supports passing customized parameters of specific models;
  • Basic components that support RAG capabilities, including DocumentLoader, TextSpillter, EmobeddingClient, VectorStore, etc.;
  • Support AI Spring Boot Starter to realize configuration automatic assembly.

Introduction to Spring Cloud Alibaba AI

Spring Cloud Alibaba AI is currently based on the Spring AI 0.8.1[1] version API to complete the access to the Tongyi series of large models. Tongyi Access is based on Alibaba Cloud Lingji Model Service[2]. Lingji Model Service is based on the concept of “Model-as-a-Service, MaaS” and focuses on various fields of AI. Models provide a variety of model services including model inference and model fine-tuning training through standardized APIs.

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

The following are the currently officially provided Spring Cloud Alibaba AI application development examples, which can be viewed by visiting http://sca.aliyun.com.

  • Chat conversation application
  • Vincent diagram application
  • Vincent Voice Application
  • Model output parsing OutputParser (implementing mapping from String to automatic POJO)
  • Use Prompt Template
  • Let the AI ​​model access external data (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. The underlying model service provided by Tongyi Qianwen is used. The complete sample source code can be viewed here[3].

Develop chat conversation applications

1. Add the 2023.0.1.0 version Spring Cloud Alibaba dependency to the project pom.xml:

<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. Write a chat service implementation class, and Spring AI automatically injects ChatClient and StreamingChatClient. ChatClient shields the interaction details of the underlying Tongyi large model.

@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 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 start 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 has been completed, which is exactly the same as the ordinary Spring Boot application development steps!

Verify application effect

After starting the application, you can verify the application effect in the following two ways.

method one

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

Returns the following response:

{
    "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."
}

Method 2

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

Apply for general API-KEY

In order for the example to be able to access the Tongyi large model normally, you need to activate the DashScope Lingji Model Service on Alibaba Cloud, apply for a valid API-KEY and update it to the application configuration file. For specific operation steps, please refer to the following document:https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key

future plan

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