Build a Cloud‑Native AI Chatbot with Spring AI Alibaba and ARMS Observability

This tutorial walks you through creating a Java‑based AI chat agent using Spring AI Alibaba, integrating Alibaba Cloud's large language model, adding function‑calling for weather queries, and enabling full observability with ARMS in a cloud‑native deployment.

Alibaba Cloud Observability
Alibaba Cloud Observability
Alibaba Cloud Observability
Build a Cloud‑Native AI Chatbot with Spring AI Alibaba and ARMS Observability

Overview

As large language models (LLMs) mature, many developers use Python to build AI services, but Java offers robust solutions for throughput, scalability, and micro‑service ecosystems. Spring AI provides a Java‑centric way to develop AI applications, and the Spring AI Alibaba project adds full support for Alibaba Cloud's Tongyi series models.

Quickly Build a Spring AI Application

1. Add the spring-ai-alibaba-starter dependency to pom.xml:

<dependency>
  <groupId>com.alibaba.cloud.ai</groupId>
  <artifactId>spring-ai-alibaba-starter</artifactId>
  <version>1.0.0-M3.2</version>
</dependency>

2. Configure application.yml with your Dashscope API key:

spring:
  application:
    name: chatmodel-example

  ai:
    dashscope:
      api-key: ${AI_DASHSCOPE_API_KEY}

3. Implement a controller that exposes a /weather-service endpoint and calls the model with a function:

@RestController
@RequestMapping("/ai/func")
public class FunctionCallingController {
  private final ChatClient chatClient;

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

  @GetMapping("/weather-service")
  public String weatherService(String subject) {
    return chatClient.prompt()
        .function("getWeather", "根据城市查询天气", new MockWeatherService())
        .user(subject)
        .call()
        .content();
  }
}

4. Provide the function implementation that the model can invoke:

public class MockWeatherService implements Function<MockWeatherService.Request, Response> {
  @Override
  public Response apply(Request request) {
    if (request.city().contains("杭州")) {
      return new Response(String.format("%s%s晴转多云, 气温32摄氏度。", request.date(), request.city()));
    } else if (request.city().contains("上海")) {
      return new Response(String.format("%s%s多云转阴, 气温31摄氏度。", request.date(), request.city()));
    } else {
      return new Response(String.format("暂时无法查询%s的天气状况。", request.city()));
    }
  }

  public record Request(@JsonProperty(required = true, value = "city") String city,
                       @JsonProperty(required = true, value = "date") String date) {}
}

5. Add the Spring Boot entry point:

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

Enable Observability and Deploy

1. Turn on observation flags in application.yml:

spring:
  ai:
    chat:
      client:
        observations:
          include-input: true
    observations:
      include-completion: true
      include-prompt: true

2. Add Micrometer and Actuator dependencies to pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>

3. Expose the global OpenTelemetry instance:

@Bean
public OpenTelemetry openTelemetry() {
  return GlobalOpenTelemetry.get();
}

4. Attach the Alibaba Java Agent at startup (replace placeholders with your path and license key):

-javaagent:/path-to-agent/aliyun-java-agent.jar
-Darms.licenseKey=YOUR_LICENSE_KEY
-Darms.appName=spring-ai-alibaba-chat-demo

If you run on Kubernetes (ACK), install the ack-onepilot plugin and label the pod instead of modifying the command.

Validate the Application

Call the endpoint, for example:

http://localhost:8080/ai/func/weather-service?subject=2024年8月12日杭州天气怎么样?

The response will be similar to:

2024年8月12日,杭州的天气预报为晴转多云,气温32摄氏度。请做好防晒措施,并留意实际天气变化。

Open the ARMS console, locate the spring-ai-alibaba-chat-demo service, and view the trace, which shows model invocation details such as response ID, model name, temperature, and input/output payloads.

ARMS trace overview
ARMS trace overview
Trace details
Trace details
Model input/output events
Model input/output events

Outlook

Spring AI Alibaba now fully supports the latest Spring AI observability features and adds observability for Tongyi multimodal models. Future work will enrich support for VectorStore, Retrieval, and Tool use cases, and deepen integration with ARMS to provide richer AI‑application dashboards.

Community link: https://github.com/alibaba/spring-ai-alibaba

Javacloud-nativeLLMSpring AIARMS
Alibaba Cloud Observability
Written by

Alibaba Cloud Observability

Driving continuous progress in observability technology!

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.