《SpringAI入门教程》5. ChatModel 使用

74 阅读1分钟
  1. DeepSeekChatModel

import org.junit.jupiter.api.Test;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = SpringAIDemo.class)
public class DeepSeekTest {

    @Test
    void testDeepSeek(@Autowired DeepSeekChatModel deepSeekChatModel) {
        String res = deepSeekChatModel.call("hello");
        System.out.println(res);
    }
}

运行结果:

Hello! 😊 How can I assist you today?
  1. DashScopeChatModel

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = SpringAIDemo.class)
public class DashScopeTest {


    @Test
    void testDashScope(@Autowired DashScopeChatModel dashScopeChatModel) {
        String content = dashScopeChatModel.call("hello, who are you ?");
        System.out.println(content);
    }
}

运行结果

Hello! I am Qwen, a large-scale language model independently developed by Alibaba Group. I can assist you with answering questions, writing, coding, expressing opinions, and more. How can I help you today?
  1. ZhiPuAiChatModel

import org.junit.jupiter.api.Test;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = SpringAIDemo.class)
public class ZhipuTest {

    @Test
    void testZhipuAi(@Autowired ZhiPuAiChatModel zhiPuAiChatModel) {
        String content = zhiPuAiChatModel.call("hello, who are you?");
        System.out.println(content);
    }
}

运行结果

Hello! I am an AI assistant named ChatGLM(智谱清言), which is developed based on the language model trained by Zhipu AI in 2023. My job is to provide appropriate answers and support to users' questions and requests.
  1. OllamaChatModel

import org.junit.jupiter.api.Test;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = SpringAIDemo.class)
public class OllamaTest {

    @Test
    void testOllama(@Autowired OllamaChatModel chatModel) {
        String content = chatModel.call("hello, who are you ?");
        System.out.println(content);
    }
}

运行结果

<think>
Okay, the user asked, "Hello, who are you?" I need to respond appropriately. First, I should acknowledge their greeting and explain my role. I can mention I'm an AI assistant designed to help with various tasks. It's important to keep the response friendly and open-ended to encourage further conversation. Let me make sure the response is clear and meets the user's expectations.
</think>

Hello! I'm an AI assistant designed to help with a wide range of tasks, from answering questions to providing guidance on various topics. How can I assist you today? 😊
  1. Prompt使用

import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = SpringAIDemo.class)
public class PromptTest {

    /**
* 传入 Prompt
*
* @param deepSeekChatModel
*/
@Test
    void testDeepSeekWithPrompt(@Autowired DeepSeekChatModel deepSeekChatModel) {
        ChatResponse res = deepSeekChatModel.call(new Prompt("hello, who are you?"));
        System.out.println(res.getResult().getOutput().getText());
    }
}
  1. Options使用

模型的配置,设置,参数。


import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = SpringAIDemo.class)
public class OptionsTest {


    @Test
    void testOptions(@Autowired OllamaChatModel chatModel) {
        OllamaOptions options = OllamaOptions.builder()
                .model("gemma3:270m")
                .temperature(0.5d)
                .build();
        ChatResponse res = chatModel.call(new Prompt("hello, who are you?", options));
        System.out.println(res.getResult().getOutput().getText());
    }
}
  1. Stream使用



import org.junit.jupiter.api.Test;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import reactor.core.publisher.Flux;

@SpringBootTest(classes = SpringAIDemo.class)
public class StreamTest {


    /**
* 流式处理
*
* @param  deepSeekChatModel
*/
@Test
    void testDeepSeekStream(@Autowired DeepSeekChatModel deepSeekChatModel) {
        Flux<String> res = deepSeekChatModel.stream("hello, who are you?");
        res.toIterable().forEach(System.out::println);
    }
}