《SpringAI 入门教程》6. ChatClient API使用

40 阅读1分钟

SpringAI基于ChatModel封装的通用API,具有易用、通用、优雅等特性。

  1. ChatClient配置

ChatClient的创建依赖具体的模型的ChatModel对象

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatClientConfig {
    @Bean
    public ChatClient deepSeekChatClient(DeepSeekChatModel deepSeekChatModel) {
        return ChatClient.create(deepSeekChatModel);
    }

    @Bean
    public ChatClient dashScopeChatClient(DashScopeChatModel dashScopeChatModel) {
        return ChatClient.create(dashScopeChatModel);
    }

    @Bean
    public ChatClient ollamaChatClient(OllamaChatModel ollamaChatModel) {
        return ChatClient.create(ollamaChatModel);
    }

    @Bean
    public ChatClient zhipuaiChatClient(ZhiPuAiChatModel zhipuaiChatModel) {
        return ChatClient.create(zhipuaiChatModel);
    }
}
  1. 多平台、多模型,动态切换

根据传入的平台,模型,参数,动态调用底层大模型实现


import cn.upfive.ai.demo.dto.MultiModelOptions;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MultiChatClientController {

    private final Map<String, ChatModel> platforms = new HashMap<>();

    public MultiChatClientController(@Autowired DeepSeekChatModel deepSeekChatModel
            , @Autowired ZhiPuAiChatModel zhiPuAiChatModel
            , @Autowired OllamaChatModel ollamaChatModel
            , @Autowired DashScopeChatModel dashScopeChatModel) {
        platforms.put("deepseek", deepSeekChatModel);
        platforms.put("zhipuai", zhiPuAiChatModel);
        platforms.put("ollama", ollamaChatModel);
        platforms.put("dashscope", dashScopeChatModel);
    }

    @GetMapping("/chat")
    public Flux<String> chat(@RequestParam("userInput") String userInput, MultiModelOptions options) {
        String platform = options.getPlatform();
        ChatModel chatModel = platforms.get(platform);
        ChatClient chatClient = ChatClient.builder(chatModel)
                .defaultOptions(ChatOptions.builder()
                        .model(options.getModel())
                        .temperature(options.getTemperature())
                        .build())
                .build();
        return chatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }
}
http://localhost:8080/chat?userInput=%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%BD%A0%E6%98%AF%E8%B0%81%EF%BC%9F&platform=dashscope&model=qwen-plus&temperature=0.5