【代码优化】AI:适配 Spring AI 1.0.6 对 Ollama 的逻辑
This commit is contained in:
@@ -35,7 +35,6 @@ public class YudaoAiAutoConfiguration {
|
||||
return new AiModelFactoryImpl();
|
||||
}
|
||||
|
||||
|
||||
// ========== 各种 AI Client 创建 ==========
|
||||
|
||||
@Bean
|
||||
|
@@ -1,6 +1,20 @@
|
||||
package cn.iocoder.yudao.framework.ai.chat;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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 org.springframework.ai.ollama.OllamaChatModel;
|
||||
import org.springframework.ai.ollama.api.OllamaApi;
|
||||
import org.springframework.ai.ollama.api.OllamaModel;
|
||||
import org.springframework.ai.ollama.api.OllamaOptions;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link OllamaChatModel} 集成测试
|
||||
@@ -9,41 +23,43 @@ import org.springframework.ai.ollama.OllamaChatModel;
|
||||
*/
|
||||
public class LlamaChatModelTests {
|
||||
|
||||
// private final OllamaApi ollamaApi = new OllamaApi(
|
||||
// "http://127.0.0.1:11434");
|
||||
// private final OllamaChatModel chatModel = new OllamaChatModel(ollamaApi,
|
||||
// OllamaOptions.create().withModel(OllamaModel.LLAMA3.getModelName()));
|
||||
//
|
||||
// @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();
|
||||
// }
|
||||
private final OllamaChatModel chatModel = OllamaChatModel.builder()
|
||||
.ollamaApi(new OllamaApi("http://127.0.0.1:11434")) // Ollama 服务地址
|
||||
.defaultOptions(OllamaOptions.builder()
|
||||
.model(OllamaModel.LLAMA3.getName()) // 模型
|
||||
.build())
|
||||
.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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user