Back to Repositories

Testing TongYi Chat Model Integration in ruoyi-vue-pro

This test suite validates the integration and functionality of the TongYiChatModel, a chat model implementation for the Alibaba Cloud Tongyi AI service. It focuses on testing both synchronous and streaming chat interactions with specific Chinese language capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of TongYiChatModel functionality:
  • Synchronous chat message processing using call() method
  • Asynchronous streaming responses using stream() method
  • System and user message handling
  • Chinese language processing with specific focus on classical Chinese text generation

Implementation Analysis

The testing approach utilizes JUnit 5 framework with a focus on integration testing patterns:
  • BeforeEach setup for MessageManager initialization
  • Mock API key configuration
  • Reactive programming testing with Flux
  • Test case isolation with @Disabled annotations

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • Alibaba Cloud AI SDK integration
  • Hutool reflection utilities
  • Spring AI chat message abstractions
  • Reactor Core for reactive streams testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test initialization and cleanup
  • Clear test case separation
  • Explicit test data preparation
  • Comprehensive API testing coverage
  • Proper handling of asynchronous operations

yunaiv/ruoyi-vue-pro

yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/TongYiChatModelTests.java

            
package cn.iocoder.yudao.framework.ai.chat;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatModel;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.utils.Constants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;

/**
 * {@link TongYiChatModel} 集成测试类
 *
 * @author fansili
 */
public class TongYiChatModelTests {

    private final Generation generation = new Generation();
    private final TongYiChatModel chatModel = new TongYiChatModel(generation,
            TongYiChatOptions.builder().withModel("qwen1.5-72b-chat").build());

    static {
        Constants.apiKey = "sk-Zsd81gZYg7";
    }

    @BeforeEach
    public void before() {
        // 防止 TongYiChatModel 调用空指针
        ReflectUtil.setFieldValue(chatModel, "msgManager", new MessageManager());
    }

    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        List<Message> messages = new ArrayList<>();
        messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
        messages.add(new UserMessage("1 + 1 = ?"));

        // 调用
        ChatResponse response = chatModel.call(new Prompt(messages));
        // 打印结果
        System.out.println(response);
        System.out.println(response.getResult().getOutput());
    }

    @Test
    @Disabled
    public void testStream() {
        // 准备参数
        List<Message> messages = new ArrayList<>();
        messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
        messages.add(new UserMessage("1 + 1 = ?"));

        // 调用
        Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages));
        // 打印结果
        flux.doOnNext(response -> {
//            System.out.println(response);
            System.out.println(response.getResult().getOutput());
        }).then().block();
    }

}