Back to Repositories

Testing QianFan AI Chat Model Integration in ruoyi-vue-pro

This test suite validates the integration with QianFan’s AI chat model implementation in the RuoYi Vue Pro framework, focusing on both synchronous and streaming chat responses. The tests specifically examine the YiYan chat model’s core functionality and API interaction patterns.

Test Coverage Overview

The test suite covers essential chat model operations with QianFan API integration.

Key areas tested include:
  • Synchronous chat responses using call() method
  • Streaming chat responses using stream() method
  • Message handling and prompt construction
  • API authentication and configuration

Implementation Analysis

The testing approach utilizes JUnit 5 framework with Spring AI integration for QianFan chat model testing.

Notable patterns include:
  • Direct API initialization with credentials
  • Custom chat model configuration using QianFanChatOptions
  • Reactive programming with Flux for streaming responses
  • Message list construction for chat prompts

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • Spring AI chat model abstractions
  • QianFan API client implementation
  • Reactor Core for reactive streams
  • ERNIE Tiny 8K model configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Separate test methods for different response types (sync vs stream)
  • Proper test isolation and setup
  • Clear test method naming conventions
  • Documented limitations and known issues
  • Flexible message handling architecture

yunaiv/ruoyi-vue-pro

yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/YiYanChatModelTests.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.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.qianfan.QianFanChatModel;
import org.springframework.ai.qianfan.QianFanChatOptions;
import org.springframework.ai.qianfan.api.QianFanApi;
import reactor.core.publisher.Flux;

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

/**
 * {@link QianFanChatModel} 的集成测试
 *
 * @author fansili
 */
public class YiYanChatModelTests {

    private final QianFanApi qianFanApi = new QianFanApi(
            "qS8k8dYr2nXunagK4SSU8Xjj",
            "pHGbx51ql2f0hOyabQvSZezahVC3hh3e");
    private final QianFanChatModel chatModel = new QianFanChatModel(qianFanApi,
            QianFanChatOptions.builder().withModel(QianFanApi.ChatModel.ERNIE_Tiny_8K.getValue()).build()
    );

    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        List<Message> messages = new ArrayList<>();
        // TODO @芋艿:文心一言,只要带上 system message 就报错,已经各种测试,很莫名!
//        messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
        messages.add(new UserMessage("1 + 1 = ?"));

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

    @Test
    @Disabled
    public void testStream() {
        // 准备参数
        List<Message> messages = new ArrayList<>();
        // TODO @芋艿:文心一言,只要带上 system message 就报错,已经各种测试,很莫名!
//        messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
        messages.add(new UserMessage("1 + 1 = ?"));

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

}