Back to Repositories

Testing Azure OpenAI Chat Integration in RuoYi-Vue-Pro

This test suite validates the integration of Azure OpenAI Chat Model within the RuoYi Vue Pro framework, focusing on both synchronous and streaming chat interactions. The tests demonstrate the implementation of chat functionality using Spring AI’s Azure OpenAI integration.

Test Coverage Overview

The test suite covers essential Azure OpenAI Chat Model functionalities including:
  • Synchronous chat responses using the call() method
  • Streaming chat responses using the stream() method
  • System and user message handling
  • Integration with Azure OpenAI client configuration

Implementation Analysis

The testing approach utilizes JUnit 5 framework with @Test and @Disabled annotations for controlled execution. The implementation demonstrates proper initialization of Azure OpenAI client with endpoint configuration, credentials management, and chat model setup using Spring AI’s AzureOpenAiChatModel.

Technical Details

Testing tools and configuration include:
  • JUnit Jupiter for test execution
  • Spring AI Azure OpenAI integration libraries
  • Azure OpenAI Client with custom endpoint configuration
  • Project Reactor’s Flux for reactive streaming tests
  • Custom ClientOptions with application ID specification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper separation of client configuration and test methods
  • Clear test method naming conventions
  • Comprehensive testing of both synchronous and asynchronous operations
  • Proper resource management and configuration isolation

yunaiv/ruoyi-vue-pro

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

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

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.ClientOptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.azure.openai.AzureOpenAiChatModel;
import org.springframework.ai.azure.openai.AzureOpenAiChatOptions;
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;

import static org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiChatProperties.DEFAULT_DEPLOYMENT_NAME;

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

    private final OpenAIClient openAiApi = (new OpenAIClientBuilder())
            .endpoint("https://eastusprejade.openai.azure.com")
            .credential(new AzureKeyCredential("xxx"))
            .clientOptions((new ClientOptions()).setApplicationId("spring-ai"))
            .buildClient();
    private final AzureOpenAiChatModel chatModel = new AzureOpenAiChatModel(openAiApi,
            AzureOpenAiChatOptions.builder().withDeploymentName(DEFAULT_DEPLOYMENT_NAME).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();
    }

}