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.
Spring AI x Tongyi Qianwen Demo is available on sca.aliyun.com
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.
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)
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].
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.
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.)
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)
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.
Visit https://sca.aliyun.com/en/ to learn about the latest developments.
[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
OpenKruise v1.6: Enhanced Multi-domain Management Capability
508 posts | 48 followers
FollowAliware - May 20, 2019
Aliware - November 4, 2019
Changyi - February 16, 2020
Alibaba Cloud Community - November 28, 2023
Farruh - October 2, 2023
Alibaba Cloud Community - May 10, 2024
508 posts | 48 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreMulti-source metrics are aggregated to monitor the status of your business and services in real time.
Learn MoreAccelerate and secure the development, deployment, and management of containerized applications cost-effectively.
Learn MoreAccelerate AI-driven business and AI model training and inference with Alibaba Cloud GPU technology
Learn MoreMore Posts by Alibaba Cloud Native Community