SpringAI动态调整大模型平台

发布于:2025-08-08 ⋅ 阅读:(10) ⋅ 点赞:(0)

一、添加所用到大模型的配置

#在application.properties中添加配置


#deepseek
spring.ai.deepseek.api-key=你的key
spring.ai.deepseek.chat.options.model=deepseek-chat

#阿里云百炼
spring.ai.dashscope.api-key=你的key
spring.ai.dashscope.audio.synthesis.options.model=qwen-plus

#本地ollama
spring.ai.ollama.chat.options.model=deepseek-r1:1.5b

二、构造出这些个模型

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

 public ChatClientController(OllamaChatModel ollamaChatModel,
	                                DashScopeChatModel dashScopeChatModel,
	                                DeepSeekChatModel deepSeekChatModel) {
	        platforms.put("ollama", ollamaChatModel);
	        platforms.put("dashscope", dashScopeChatModel);
	        platforms.put("deepseek", deepSeekChatModel);
}

三、动态构造ChatClient参数

  • 在 Spring AI 中, temperature 参数的取值范围通常为‌0到1‌(部分模型可能支持更高值)。
  • 该参数用于控制生成内容的随机性和多样性:
    1.低温‌(接近0):输出更确定,适用于需要高准确性的场景,如法律文本、技术文档等。 ‌
    ‌2.中温‌(如0.5-0.7):平衡随机性与一致性,适合对话生成、创意写作等场景。 ‌
    ‌3.高温‌(接近1):输出更灵活,适用于广告文案、故事创作等需要多样性的任务。 ‌
 /**
     *
     * @param msg 提示词
     * @param model 选用哪个模型
     * @param platform 哪个平台  阿里/ollama/deepseek
     * @param temperature 温度通常设定在0-1的范围内  double类型   数字越大可以理解为AI思想越活跃
     * @return
     */
 public String adjustPlatform(String msg, String model, String platform, double temperature) {
        //得到对应平台的ChatModel 
        ChatModel chatModel = platforms.get(platform);
        //动态构造出所需要的模型
        ChatClient chatClient = ChatClient
                .builder(chatModel)
                .defaultOptions(ChatOptions.builder().model(model).temperature(temperature).build())
                .build();

        String content = chatClient.prompt()
                .user(msg)
                .call()
                .content();

        return content;
    }

四、效果展示
在这里插入图片描述
五、完整代码

@RestController
@RequestMapping("/chatClient")
public class ChatClientController {

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

    public ChatClientController(OllamaChatModel ollamaChatModel,
                                DashScopeChatModel dashScopeChatModel,
                                DeepSeekChatModel deepSeekChatModel) {
        platforms.put("ollama", ollamaChatModel);
        platforms.put("dashscope", dashScopeChatModel);
        platforms.put("deepseek", deepSeekChatModel);
    }

    /**
     *
     * @param msg 提示词
     * @param model 选用哪个模型
     * @param platform 哪个平台  阿里/ollama/deepseek
     * @param temperature 温度通常设定在0-1的范围内  double类型   数字越大可以理解为AI思想越活跃
     * @return
     */
    @GetMapping("/adjustPlatform")
    public String adjustPlatform(String msg, String model, String platform, double temperature) {
        ChatModel chatModel = platforms.get(platform);
        //动态构造出所需要的模型
        ChatClient chatClient = ChatClient
                .builder(chatModel)
                .defaultOptions(ChatOptions.builder().model(model).temperature(temperature).build())
                .build();

        String content = chatClient.prompt()
                .user(msg).call().content();

        return content;
    }


}

网站公告

今日签到

点亮在社区的每一天
去签到