Build a Java Chatbot with Spring AI and Alibaba Tongyi in 5 Minutes

This guide walks you through using Spring AI and Spring Cloud Alibaba AI to quickly create a Java chat application powered by Alibaba's Tongyi large‑model service, covering setup, configuration, core code, and verification steps for a functional generative AI chatbot.

Programmer DD
Programmer DD
Programmer DD
Build a Java Chatbot with Spring AI and Alibaba Tongyi in 5 Minutes

Spring AI is an official Spring community project that simplifies Java AI application development, allowing Java developers to build AI apps just like regular Spring apps.

Spring Cloud Alibaba AI builds on Spring AI, providing full integration with Alibaba Cloud Tongyi large models, enabling developers to create Java AI applications based on Tongyi models within five minutes.

Spring AI Overview

According to the Spring AI website, the project is inspired by popular Python projects such as LangChain and LlamaIndex, but is not a direct copy. Spring AI believes the next wave of generative AI applications will be ubiquitous across many programming languages.

The core of Spring AI offers abstractions for Java AI development, including:

Support for multiple large‑model services.

Flexible Prompt Template and Output Parsing capabilities.

Multimodal generative AI features such as chat, text‑to‑image, and text‑to‑speech.

A portable API for accessing various model and embedding services, with synchronous and streaming calls and custom model parameters.

RAG components such as DocumentLoader, TextSplitter, EmbeddingClient, and VectorStore.

AI Spring Boot Starter for automatic configuration.

Spring Cloud Alibaba AI Overview

Spring Cloud Alibaba AI currently integrates Tongyi series models based on Alibaba Cloud Lingji model service, which follows a Model‑as‑a‑Service (MaaS) approach and provides standardized APIs for inference, fine‑tuning, and other model services.

The latest version adapts common generative models for chat, text‑to‑image, and text‑to‑speech, and offers utilities such as OutputParser, Prompt Template, and Stuff.

Getting Started with a Spring AI Application

This project demonstrates how to use the spring-cloud-starter-alibaba-ai starter to build an online chat AI application powered by Tongyi Qianwen.

Develop a Chat Application

Add the Spring Cloud Alibaba dependency version 2023.0.1.0 to 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>

Configure the model in application.yml (replace the placeholder API key with a valid one):

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

Create a chat service implementation that Spring AI injects ( ChatClient, StreamingChatClient) to hide Tongyi model details.

@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;
  }
}

Implement the chat logic:

@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());
  }
}

Create the Spring Boot entry class and run the application:

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

After starting the application, you can verify its functionality either by accessing http://localhost:8080/ai/example in a browser or by opening resources/static/index.html and submitting a query.

Validate the Application

Example response for the query “Tell me a joke”:

{
  "Tell me a joke": "Sure, here's a classic one for you:

Why was the math book sad?

Because it had too many problems.

I hope that made you smile! If you're looking for more, just let me know."
}

Future Plans

The current version supports common generative models such as chat, text‑to‑image, and text‑to‑speech. Future releases will add VectorStore, Embedding, ETL Pipeline support, and further simplify RAG and other AI application scenarios.

JavaSpring BootSpring AIChatbotGenerative AIAlibaba Cloud
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.