Back to Repositories

Testing OpenAI Chat Model Integration in ruoyi-vue-pro

This test suite validates the integration with OpenAI’s chat model functionality through Spring AI, focusing on both synchronous and streaming chat responses. The tests demonstrate proper message handling and response processing for AI-powered conversations.

Test Coverage Overview

The test suite covers essential OpenAI chat model interactions through two main test cases:
  • Synchronous chat completion using call() method
  • Streaming chat responses using stream() method
  • System and user message handling
  • Response processing and output validation

Implementation Analysis

The testing approach utilizes JUnit 5 framework with Spring AI integration, implementing both blocking and reactive testing patterns. The tests demonstrate proper initialization of OpenAiChatModel with custom API configuration and model selection using GPT-4.

Notable patterns include message list construction, prompt creation, and response handling using both synchronous and reactive approaches.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Spring AI framework for OpenAI integration
  • Reactor Core for reactive streams testing
  • Custom OpenAiApi configuration with specific endpoint
  • OpenAiChatOptions for model configuration

Best Practices Demonstrated

The test suite exhibits several testing best practices:
  • Proper test isolation using @Test and @Disabled annotations
  • Clear test method naming conventions
  • Structured test setup with explicit parameter preparation
  • Appropriate error handling and response validation
  • Reactive programming patterns for stream testing

yunaiv/ruoyi-vue-pro

yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/OpenAIChatModelTests.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.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import reactor.core.publisher.Flux;

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

/**
 * {@link OpenAiChatModel} 集成测试
 *
 * @author 芋道源码
 */
public class OpenAIChatModelTests {

    private final OpenAiApi openAiApi = new OpenAiApi(
            "https://api.holdai.top",
            "sk-dZEPiVaNcT3FHhef51996bAa0bC74806BeAb620dA5Da10Bf");
    private final OpenAiChatModel chatModel = new OpenAiChatModel(openAiApi,
            OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_O).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();
    }

}