The perfect combination of Spring AI and Tongyi Qianwen: building intelligent conversational applications

Written by
Jasper Cole
Updated on:June-22nd-2025
Recommendation

Master AI application development, starting with the integration of Spring AI and Tongyi Qianwen.

Core content:
1. Combination of artificial intelligence technology and Spring Framework
2. Main features and applications of Spring AI framework
3. Project environment construction and Tongyi Qianwen API key configuration

Yang Fangxian
Founder of 53A/Most Valuable Expert of Tencent Cloud (TVP)

Spring AI is a new member of the Spring ecosystem, which provides developers with a set of simple and powerful tools for integrating various AI big models. This article will introduce how to use Spring AI to integrate with Alibaba Cloud Tongyi Qianwen big models to build intelligent dialogue applications, helping you quickly master the core skills of AI application development.

introduction

With the rapid development of artificial intelligence technology, more and more companies want to integrate AI capabilities into their applications. As the most popular framework in the Java ecosystem, Spring Framework has launched the Spring AI project in response to this trend. This article will take you to explore how to use Spring AI in combination with the Tongyi Qianwen model to easily build intelligent conversational applications.

Introduction to Spring AI

Spring AI is a framework launched by the Spring team specifically to simplify AI application development. It provides a unified API interface that enables developers to easily integrate various AI model services. Currently, Spring AI supports multiple mainstream AI platforms, including OpenAI, Alibaba Cloud Tongyi Qianwen, etc.

Key features include:

  • Unified API abstraction layer
  • Simple configuration
  • Support multiple conversation modes
  • Built-in template engine
  • Streaming response support

Project environment construction

First, we need to add the necessary dependencies to the project. The following is the core configuration of pom.xml:

< properties >
    < java.version > 17 < /java.version >
    < spring-ai.version > 1.0.0-M6 < /spring-ai.version >
</ properties >
< dependencies >
    <!-- Spring AI Alibaba (Tongyi large model support) -->
    < dependency >
        <groupId> com.alibaba.cloud.ai </groupId>
        <artifactId> spring-ai - alibaba - starter </artifactId>
        <version> 1.0.0 - M6.1 </version>
    </ dependency >
    < dependency >
        <groupId> org.springframework.ai </groupId>
        <artifactId> spring - ai - core </artifactId>
        < version > 1.0.0-M6 </ version >
    </ dependency >
</ dependencies >

Configuration Tongyi Qianwen

Get an API key

Before we start using Tongyi Qianwen, we need to obtain an API key. Here are the detailed steps:

  1. Register an Alibaba Cloud account
  • Visit Alibaba Cloud official website (https://www.aliyun.com/)
  • If you don't have an account, click "Sign up for free" to complete the registration process
  • If you already have an account, log in directly
  • Open Tongyi Qianwen Service
    • After logging in, visit the Dashscope console (https://dashscope.console.aliyun.com/)
    • Read and agree to the Terms of Service
    • Activate the service (free quota for first use)
  • Create an API key
    • In the Tongyi Qianwen console, find "API Key Management

    Core function implementation

    1. Basic conversation function

    The most basic conversation function is very simple to implement. You only need to inject ChatClient and call its API:

@RestController
public class AIController {   
    private final  ChatClient chatClient; 

    public AIController (ChatClient.Builder chatClientBuilder)   {
        this .chatClient = chatClientBuilder.build();
    }

    @GetMapping ( "/chat" )
    public  String  chat (@RequestParam( "message" )  String message)  {
        return  chatClient.prompt()
                .user(message)
                .call()
                .content();
    }
}

2. Templated dialogue

Spring AI provides a powerful template function that allows you to preset dialogue templates:

@GetMapping ( "/template" )
public  String  templateChat (@RequestParam( "topic" )  String topic)  {
    PromptTemplate template =  new  PromptTemplate( "Please explain {topic} in concise language" );
    return  chatClient.prompt()
            .user(template.render(Map.of( "topic" , topic)))
            .call()
            .content();
}

3. Streaming Response

For long text generation, supporting streaming responses can provide a better user experience:

@GetMapping ( "/stream" )
public  Flux<String>  streamChat (@RequestParam String message)  {
    return  chatClient.prompt()
            .user(message)
            .stream()
            .content();
}

Technical Difficulties and Solutions

1. Processing of system messages

In practical applications, we may need to set specific roles or behavior rules for AI. This can be achieved through system messages:

@GetMapping ( "/chat/conversation" )
public  String  conversation (@RequestParam( "message" )  String message,
                         @RequestParam (value =  "systemMessage" , required =  false ) String  systemMessage) 
{
    var  promptBuilder = chatClient.prompt();
    if  (systemMessage !=  null  && !systemMessage.isEmpty()) {
        promptBuilder.system(systemMessage);
    }
    return  promptBuilder
            .user(message)
            .call()
            .content();
}

Summary and Outlook

Spring AI provides a powerful and concise framework for Java developers, making the integration of AI functions easier than ever before. By combining it with Tongyi Qianwen, we can quickly build feature-rich intelligent conversational applications.

In the future, as Spring AI continues to develop, we can expect:

  • Support for more AI models
  • More pre-processing and post-processing functions
  • Improved development tools and debugging support
  • More best practices and application scenarios

Write