feat(ai): 增加大模型对话测试

This commit is contained in:
v-zhangjc9
2025-06-12 20:29:37 +08:00
parent 5160c59ab0
commit 2e24bdb90b
2 changed files with 141 additions and 42 deletions

View File

@@ -20,6 +20,21 @@
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-ai</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-ai-dialect-openai</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -1,71 +1,155 @@
package com.lanyuanxiaoyao.service.ai.chat;
import java.io.IOException;
import java.net.http.HttpClient;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import org.noear.solon.ai.rag.Document;
import org.noear.solon.ai.reranking.RerankingModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.ai.deepseek.api.DeepSeekApi;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.openai.OpenAiEmbeddingOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.reactive.JdkClientHttpConnector;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.Disposable;
/**
* @author lanyuanxiaoyao
* @version 20250514
*/
@SuppressWarnings("NullableProblems")
public class TestSpringAIToolChat {
public static void main(String[] args) {
ChatClient client = ChatClient.builder(
DeepSeekChatModel.builder()
.deepSeekApi(
DeepSeekApi.builder()
.baseUrl("http://132.121.206.65:10086/v1")
public static void main(String[] args) throws IOException {
testChatModel();
testVisualModel();
// testEmbeddingModel();
// testRerankingModel();
}
private static void testChatModel() {
for (String model : List.of(
"Qwen3/qwen3-0.6b",
"Qwen3/qwen3-1.7b",
"Qwen3/qwen3-4b",
"Qwen3/qwen3-4b-q4km",
"Qwen3/qwen3-8b-q4km"
)) {
ChatClient client = chatClient(model);
String content = client.prompt()
.user("你好")
.call()
.content();
System.out.println(content);
}
}
private static void testVisualModel() {
for (String model : List.of(
"Qwen2.5/qwen2.5-vl-7b",
"Qwen2.5/qwen2.5-vl-7b-q4km",
"Qwen2.5/qwen2.5-vl-3b-instruct",
"Qwen2.5/qwen2.5-vl-3b-instruct-awq",
"Qwen2.5/qwen2.5-vl-7b-instruct",
"Qwen2.5/qwen2.5-vl-7b-instruct-awq",
"MiniCPM/minicpm-o-2.6-7.6b",
"MiniCPM/minicpm-o-2.6-7.6b-q4km"
)) {
ChatClient client = chatClient(model);
String content = client.prompt()
.user(spec -> spec.text("图片中有什么").media(MimeTypeUtils.IMAGE_PNG, new FileSystemResource("/Users/lanyuanxiaoyao/Pictures/deepseek.png")))
.call()
.content();
System.out.println(content);
}
}
private static void testEmbeddingModel() {
for (String model : List.of(
"Qwen3/qwen3-embedding-0.6b",
"Qwen3/qwen3-embedding-4b",
"Qwen3/qwen3-embedding-4b-q4km",
"Qwen3/qwen3-embedding-8b-q4km",
"BGE/bge-m3",
"BGE/bge-m3-q4km",
"MiniCPM/minicpm-embedding",
"MiniCPM/minicpm-embedding-light"
)) {
EmbeddingModel embeddingModel = embeddingModel(model);
float[] worlds = embeddingModel.embed("Hello world");
System.out.println(Arrays.toString(worlds));
}
}
private static void testRerankingModel() throws IOException {
for (String model : List.of(
"BGE/beg-reranker-v2",
"MiniCPM/minicpm-reranker",
"MiniCPM/minicpm-reranker-light",
"BGE/beg-reranker-v2",
"BGE/beg-reranker-v2-q4km"
)) {
RerankingModel rerankingModel = rerankingModel(model);
List<Document> list = rerankingModel.rerank(
"你好",
List.of(
new Document("go go go滚犊子"),
new Document("我是tom你最近过得好吗"),
new Document("666你就是大聪明")
)
);
list.forEach(System.out::println);
}
}
private static ChatClient chatClient(String model) {
return ChatClient.builder(
OpenAiChatModel.builder()
.openAiApi(
OpenAiApi.builder()
.baseUrl("http://132.121.206.65:10086")
.apiKey("*XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTj<MYQd$&@$sY7CgqNyea4giJi4")
.restClientBuilder(restClientBuilder())
.webClientBuilder(webClientBuilder())
.build()
)
.defaultOptions(
DeepSeekChatOptions.builder()
.model("Qwen3-1.7-vllm")
OpenAiChatOptions.builder()
.model(model)
.build()
)
.build()
)
.build();
ToolCallback datetimeTool = new ToolCallback() {
@Override
public ToolDefinition getToolDefinition() {
return ToolDefinition.builder()
.name("getCurrentTime")
.description("获取当前日期和时间")
// language=JSON
.inputSchema("""
{"type": null}
""")
.build();
}
}
@Override
public String call(String toolInput) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
};
Disposable disposable = client.prompt()
.user("当前时间?")
.toolCallbacks(datetimeTool)
.stream()
.content()
.subscribe(System.out::println);
while (!disposable.isDisposed()) {
}
private static EmbeddingModel embeddingModel(String model) {
return new OpenAiEmbeddingModel(
OpenAiApi.builder()
.baseUrl("http://132.121.206.65:10086")
.apiKey("*XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTj<MYQd$&@$sY7CgqNyea4giJi4")
.restClientBuilder(restClientBuilder())
.webClientBuilder(webClientBuilder())
.build(),
MetadataMode.EMBED,
OpenAiEmbeddingOptions.builder()
.model(model)
.build()
);
}
private static RerankingModel rerankingModel(String model) {
return RerankingModel.of("http://132.121.206.65:10086/v1/rerank")
.model(model)
.apiKey("*XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTj<MYQd$&@$sY7CgqNyea4giJi4")
.build();
}
private static HttpClient httpClient() {