Getting Started with Spring Cloud Alibaba AI: Integrating Tongyi Large Models in Spring Boot
This article introduces Spring Cloud Alibaba AI, explains its relationship to Spring AI, and provides a step‑by‑step tutorial—including Maven setup, dependency configuration, code examples, and sample calls—to integrate Alibaba's Tongyi large‑model services for text QA, image generation, and speech synthesis in a Java Spring Boot application.
Alibaba has launched Spring Cloud Alibaba AI on top of Spring AI, offering Chinese developers an easy way to connect to the Tongyi series of large language models. This article gives a brief overview of Spring AI, its inspiration from LangChain and LlamaIndex, and its goal to make generative AI applications available across many programming languages.
What is Spring AI?
Spring AI draws ideas from popular Python projects such as LangChain and LlamaIndex , providing abstraction, simplifying AI development, supporting models and vector stores, and offering auto‑configuration for AI integration.
Provide abstraction capabilities
Simplify AI application development
Model and vector support
AI integration and auto‑configuration
Spring AI simplifies building large, complex AI applications , though for simple API calls the official SDK may be more convenient.
What is Spring Cloud Alibaba AI?
Spring Cloud Alibaba AI extends Spring AI (v0.8.1) to integrate Alibaba's domestic Tongyi large models, supporting chat, text‑to‑image, and text‑to‑speech capabilities. The framework provides examples such as chat dialogs, image generation, and audio synthesis, with documentation at https://sca.aliyun.com/docs/2023/user-guide/ai/quick-start/ .
Hands‑On Experience with Spring Cloud Alibaba AI
First, create a Maven project using JDK 17 and add the following dependencies:
<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>Configure the Tongyi API key in application.yml (obtainable from the Alibaba Cloud console):
server:
port: 8080
spring:
application:
name: alibaba-spring-ai-demo
cloud:
ai:
tongyi:
api-key: your-api-keyCreate the Spring Boot entry point:
@SpringBootApplication
public class MyAiApplication {
public static void main(String[] args) {
SpringApplication.run(MyAiApplication.class, args);
}
}Connecting to a Text Model
Define a controller at /ai/simple for basic QA:
@RestController
@RequestMapping("/ai")
@CrossOrigin
public class TongYiController {
@Autowired
@Qualifier("tongYiSimpleServiceImpl")
private TongYiService tongYiSimpleService;
@GetMapping("/simple")
public String completion(@RequestParam(value = "message", defaultValue = "AI时代下Java开发者该何去何从?") String message) {
return tongYiSimpleService.completion(message);
}
}Service interface:
public interface TongYiService {
/** Basic Q&A */
String completion(String message);
/** Text‑to‑image */
ImageResponse genImg(String imgPrompt);
/** Speech synthesis */
String genAudio(String text);
}Implementation using Spring AI's ChatClient and StreamingChatClient :
@Service
@Slf4j
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;
}
@Override
public String completion(String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return chatClient.call(prompt).getResult().getOutput().getContent();
}
}Testing with the prompt AI时代下Java开发者该何去何从? yields a response (see screenshot).
Image Generation Model
Service implementation for image generation:
@Service
@Slf4j
public class TongYiImagesServiceImpl extends AbstractTongYiServiceImpl {
private final ImageClient imageClient;
@Autowired
public TongYiImagesServiceImpl(ImageClient client) {
this.imageClient = client;
}
@Override
public ImageResponse genImg(String imgPrompt) {
var prompt = new ImagePrompt(imgPrompt);
return imageClient.call(prompt);
}
}Prompt example: Painting a boy coding in front of the desk, with his dog. . The generated images are shown below.
Speech Synthesis Model
@Service
@Slf4j
public class TongYiAudioSimpleServiceImpl extends AbstractTongYiServiceImpl {
private final SpeechClient speechClient;
@Autowired
public TongYiAudioSimpleServiceImpl(SpeechClient client) {
this.speechClient = client;
}
@Override
public String genAudio(String text) {
logger.info("gen audio prompt is: {}", text);
var resWAV = speechClient.call(text);
// Save logic omitted – simply store the WAV file locally
return save(resWAV, SpeechSynthesisAudioFormat.WAV.getValue());
}
}The synthesis test succeeds, producing an audio file (screenshot shown).
Usage Summary
Spring Cloud Alibaba AI greatly simplifies development of complex AI features in Java. While simple QA can be done with the raw SDK, the framework offers better maintainability for richer scenarios such as streaming calls, POJO mapping, and role‑based AI.
Simplifies development : For basic QA the SDK suffices, but for advanced AI workflows the framework reduces boilerplate and improves long‑term maintainability.
Response time : Current text QA takes about 10 seconds, leaving room for performance optimization.
Model selection : The SDK allows explicit model choice; Spring AI currently abstracts this, and community input is welcome.
Future work includes adding VectorStore, Embedding, and ETL pipeline adapters to further support RAG and other AI application patterns.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.