×
Community Blog Excel at Java AI Application Development within 5 Minutes

Excel at Java AI Application Development within 5 Minutes

This article describes how to develop a Java AI application in 5 minutes using Spring AI and Alibaba Cloud's Tongyi series of large models.

By Jun Liu

Spring AI is an official Spring community project designed to simplify the development of Java AI applications, so that Java developers can build AI applications as easily as they build regular applications using Spring.

Spring Cloud Alibaba AI is based on Spring AI and provides full compatibility with Alibaba Cloud's Tongyi series of large models, allowing users to develop Java AI applications based on Tongyi large models in a matter of five minutes.

1
Spring AI x Tongyi Qianwen Demo is available on sca.aliyun.com

Introduction to Spring AI

According to its official website, the Spring AI project is inspired by well-known Python projects such as LangChain and LlamaIndex, but Spring AI is beyond a mere replication. Spring AI was created with the belief that the next wave of generative AI applications will not be only for Python developers but will be ubiquitous across many programming languages.

At its core, Spring AI provides abstractions that serve as the foundation for developing Java AI applications. It offers the following:

• Integration with various large model services, including most industry-leading large model services;

• Flexible prompt templates and output parsing capabilities;

• Multi-modal generative AI capabilities, such as dialog, text-to-image, and text-to-speech;

• General-purpose and portable APIs for accessing various model services and embedding services, supporting synchronous and streaming calling and passing of custom parameters for specific models;

• Basic components that support retrieval-augmented generation (RAG) capabilities, including DocumentLoader, TextSpillter, EmobeddingClient, and VectorStore; and

• AI Spring Boot Starter for automatic configuration assembly.

Introduction to Spring Cloud Alibaba AI

Spring Cloud Alibaba AI currently uses the Spring AI API version 0.8.1[1] to access the Tongyi series of large models. Such access relies on Alibaba Cloud's DashScope service[2], which is designed based on the concept of Model as a Service (MaaS), aggregates AI models in different fields, and provides various model services such as model inference, model fine-tuning, and model training through standard APIs.

The latest version of Spring Cloud Alibaba AI provides compatibility with multiple common generative models, including dialog, text-to-image, and text-to-speech models. Developers can use Spring Cloud Alibaba AI to develop chat, image generation, or speech generation AI applications in the framework of the Tongyi series of large models. The framework also provides practical capabilities such as output parsing, prompt templates, and Stuff.

Official examples of application development using Spring Cloud Alibaba AI are available on sca.aliyun.com.

• Developing a chat application

• Developing a text-to-image application

• Developing a text-to-speech application

• Parsing model output using OutputParser (implementing automatic mapping from String to POJO)

• Using prompt templates

• Connecting an AI model to external data (Prompt Stuff)

Develop Your First Spring AI Application

This project demonstrates how to use spring-cloud-starter-alibaba-ai to develop an online chat AI application based on the model service provided by Tongyi Qianwen. The complete sample code is available[3].

Develop a Chat Application

1.  Add the 2023.0.1.0 Spring Cloud Alibaba dependency to the pom.xml file.

<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, which is automatically injected into the ChatClient and the StreamingChatClient by Spring AI. Details of underlying interaction of Tongyi Qianwen are shielded from the 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.  Write code to implement the business logic of the chat application.

@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 a Spring entry class and start the application.

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

Now you've finished developing a simple chat AI application, following the same steps for building a regular Spring Boot application.

Verify the Running of the Application

After the application is started, you can use one of the following methods to verify the running of the application.

Method 1

In the address bar of your browser, enter http://localhost:8080/ai/example.

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

Method 2

Enter the resources/static directory and open the index.html file in a browser. Enter a question and click Send. An answer is given immediately. (Make sure that your API key is valid.)

2

Apply for an API Key for accessing Tongyi Large Models

To successfully access a Tongyi large model, you must activate the DashScope service on Alibaba Cloud, apply for a valid API key, and then update the API key to the application configuration file. For more information, visit https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key (currently available in Chinese)

Future Plans

The current version of Spring Cloud Alibaba AI provides compatibility with multiple common generative models, including dialog, text-to-image, and text-to-speech models. The upcoming versions will provide compatibility with more models such as VectorStore, Embedding, and ETL Pipeline to make AI application development easier in RAG and other scenarios.

3

Visit https://sca.aliyun.com/en/ to learn about the latest developments.

References

[1] Spring AI 0.8.1
https://docs.spring.io/spring-ai/reference/0.8-SNAPSHOT/index.html
[2] DashScope (currently available in Chinese)
https://help.aliyun.com/zh/dashscope/
[3] Complete sample code
https://github.com/alibaba/spring-cloud-alibaba/tree/2023.x/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld

0 1 0
Share on

You may also like

Comments

Related Products