|
@@ -2,10 +2,12 @@ package cn.jlsxwkj.moudles.chat;
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
-import cn.jlsxwkj.common.config.UserConfig;
|
|
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
import cn.jlsxwkj.moudles.chat.message.Message;
|
|
import cn.jlsxwkj.moudles.chat.message.Message;
|
|
import jakarta.annotation.Resource;
|
|
import jakarta.annotation.Resource;
|
|
|
|
+import org.springframework.ai.autoconfigure.ollama.OllamaConnectionDetails;
|
|
|
|
+import org.springframework.ai.chat.model.ChatModel;
|
|
|
|
+import org.springframework.ai.ollama.api.OllamaOptions;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.scheduler.Scheduler;
|
|
import reactor.core.scheduler.Scheduler;
|
|
@@ -25,7 +27,9 @@ import java.util.List;
|
|
public class ChatUtil {
|
|
public class ChatUtil {
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
- private UserConfig userConfig;
|
|
|
|
|
|
+ private ChatModel chatModel;
|
|
|
|
+ @Resource
|
|
|
|
+ private OllamaConnectionDetails ollamaConnectionDetails;
|
|
private InputStream inputStream;
|
|
private InputStream inputStream;
|
|
private InputStreamReader inputStreamReader;
|
|
private InputStreamReader inputStreamReader;
|
|
private BufferedReader bufferedReader;
|
|
private BufferedReader bufferedReader;
|
|
@@ -40,13 +44,13 @@ public class ChatUtil {
|
|
*/
|
|
*/
|
|
public Flux<ChatResponse> chat(List<Message> messages, boolean isStream) {
|
|
public Flux<ChatResponse> chat(List<Message> messages, boolean isStream) {
|
|
ChatRequest chatRequest = new ChatRequest();
|
|
ChatRequest chatRequest = new ChatRequest();
|
|
- chatRequest.setModel(userConfig.getModel());
|
|
|
|
|
|
+ OllamaOptions options = (OllamaOptions) chatModel.getDefaultOptions();
|
|
|
|
+ chatRequest.setModel(options.getModel());
|
|
chatRequest.setMessages(messages);
|
|
chatRequest.setMessages(messages);
|
|
chatRequest.setStream(isStream);
|
|
chatRequest.setStream(isStream);
|
|
|
|
+ chatRequest.setOptions(options);
|
|
Log.info(this.getClass(), JSONUtil.toJsonStr(chatRequest));
|
|
Log.info(this.getClass(), JSONUtil.toJsonStr(chatRequest));
|
|
- Log.info(this.getClass(), userConfig.getModel());
|
|
|
|
- Log.info(this.getClass(), userConfig.getBaseUrl());
|
|
|
|
- inputStream = HttpRequest.post(userConfig.getBaseUrl() + "/api/chat")
|
|
|
|
|
|
+ inputStream = HttpRequest.post(ollamaConnectionDetails.getBaseUrl() + "/api/chat")
|
|
.body(JSONUtil.toJsonStr(chatRequest))
|
|
.body(JSONUtil.toJsonStr(chatRequest))
|
|
.execute(isStream)
|
|
.execute(isStream)
|
|
.bodyStream();
|
|
.bodyStream();
|