Back to Repositories

Testing ZhiPu AI Chat Model Integration in RuoYi-Vue-Pro

This test suite validates the ZhiPuAI Chat Model integration in the RuoYi Vue Pro framework, focusing on both synchronous and streaming chat interactions. The tests demonstrate proper message handling and response processing for the ZhiPu AI API implementation.

Test Coverage Overview

The test suite covers essential functionality of the ZhiPuAiChatModel with comprehensive validation of both standard chat calls and streaming responses.
  • Tests synchronous chat completion with system and user messages
  • Validates streaming chat responses using reactive programming
  • Covers message construction and response handling
  • Tests integration with ZhiPu AI’s GLM-4 model

Implementation Analysis

The testing approach utilizes JUnit 5 framework with Spring AI integration for chat model testing.
  • Implements @Test and @Disabled annotations for controlled test execution
  • Uses Spring AI’s message abstractions for chat interactions
  • Leverages Reactor’s Flux for handling streaming responses
  • Demonstrates proper API key and model configuration

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • Spring AI chat model abstractions
  • ZhiPu AI API integration
  • Reactor Core for reactive streams
  • Custom message handling with SystemMessage and UserMessage types

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation and setup
  • Clear test method naming conventions
  • Comprehensive API integration testing
  • Proper handling of async operations
  • Structured test organization with clear responsibilities

yunaiv/ruoyi-vue-pro

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

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

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 org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
import reactor.core.publisher.Flux;

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

/**
 * {@link ZhiPuAiChatModel} 的集成测试
 *
 * @author 芋道源码
 */
public class ZhiPuAiChatModelTests {

    private final ZhiPuAiApi zhiPuAiApi = new ZhiPuAiApi("32f84543e54eee31f8d56b2bd6020573.3vh9idLJZ2ZhxDEs");
    private final ZhiPuAiChatModel chatModel = new ZhiPuAiChatModel(zhiPuAiApi,
            ZhiPuAiChatOptions.builder().withModel(ZhiPuAiApi.ChatModel.GLM_4.getModelName()).build());

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

}