|
|
@ -14,7 +14,11 @@ import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.ai.chat.client.ChatClient;
|
|
|
|
import org.springframework.ai.chat.client.ChatClient;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
|
|
|
|
|
|
|
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
|
|
|
|
|
|
|
|
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
|
|
|
|
|
|
|
import org.springframework.ai.ollama.OllamaChatModel;
|
|
|
|
|
|
|
|
import org.springframework.ai.ollama.api.OllamaOptions;
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
@ -24,7 +28,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.FluxSink;
|
|
|
|
import reactor.core.publisher.FluxSink;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Consumer;
|
|
|
@ -45,8 +48,9 @@ public class IndexController {
|
|
|
|
// --------------------------------- ollama chat ---------------------------------
|
|
|
|
// --------------------------------- ollama chat ---------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
private ChatClient chatClient;
|
|
|
|
private OllamaChatModel ollamaChatModel;
|
|
|
|
private static String prompt = "你好,你是一个研发工程师,擅长解决技术类问题。";
|
|
|
|
private String prompt = "你好,你是一个研发工程师,擅长解决技术类问题。";
|
|
|
|
|
|
|
|
private String modle = "qwen3:0.6b";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* ChatClient 简单调用
|
|
|
|
* ChatClient 简单调用
|
|
|
@ -54,13 +58,24 @@ public class IndexController {
|
|
|
|
@GetMapping("/chat/simple")
|
|
|
|
@GetMapping("/chat/simple")
|
|
|
|
@ResponseBody
|
|
|
|
@ResponseBody
|
|
|
|
public String simpleChat(@RequestParam(value = "input") String input) {
|
|
|
|
public String simpleChat(@RequestParam(value = "input") String input) {
|
|
|
|
String result = chatClient
|
|
|
|
|
|
|
|
|
|
|
|
// build chat-client
|
|
|
|
|
|
|
|
ChatClient ollamaChatClient = ChatClient
|
|
|
|
|
|
|
|
.builder(ollamaChatModel)
|
|
|
|
|
|
|
|
.defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build())
|
|
|
|
|
|
|
|
.defaultAdvisors(SimpleLoggerAdvisor.builder().build())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// call ollama
|
|
|
|
|
|
|
|
String response = ollamaChatClient
|
|
|
|
.prompt(prompt)
|
|
|
|
.prompt(prompt)
|
|
|
|
.user(input)
|
|
|
|
.user(input)
|
|
|
|
|
|
|
|
.options(OllamaOptions.builder().model(modle).build())
|
|
|
|
.call()
|
|
|
|
.call()
|
|
|
|
.content();
|
|
|
|
.content();
|
|
|
|
System.out.println("result: " + result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
logger.info("result: " + response);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -69,9 +84,19 @@ public class IndexController {
|
|
|
|
@GetMapping("/chat/stream")
|
|
|
|
@GetMapping("/chat/stream")
|
|
|
|
public Flux<String> streamChat(HttpServletResponse response, @RequestParam(value = "input") String input) {
|
|
|
|
public Flux<String> streamChat(HttpServletResponse response, @RequestParam(value = "input") String input) {
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
return chatClient
|
|
|
|
|
|
|
|
|
|
|
|
// build chat-client
|
|
|
|
|
|
|
|
ChatClient ollamaChatClient = ChatClient
|
|
|
|
|
|
|
|
.builder(ollamaChatModel)
|
|
|
|
|
|
|
|
.defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build())
|
|
|
|
|
|
|
|
.defaultAdvisors(SimpleLoggerAdvisor.builder().build())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// call ollama
|
|
|
|
|
|
|
|
return ollamaChatClient
|
|
|
|
.prompt(prompt)
|
|
|
|
.prompt(prompt)
|
|
|
|
.user(input)
|
|
|
|
.user(input)
|
|
|
|
|
|
|
|
.options(OllamaOptions.builder().model(modle).build())
|
|
|
|
.stream()
|
|
|
|
.stream()
|
|
|
|
.content();
|
|
|
|
.content();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -85,7 +110,7 @@ public class IndexController {
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/dify/simple")
|
|
|
|
@GetMapping("/dify/simple")
|
|
|
|
@ResponseBody
|
|
|
|
@ResponseBody
|
|
|
|
public String difySimple(@RequestParam(required = false, value = "input") String input) throws IOException {
|
|
|
|
public String difySimple(@RequestParam(required = false, value = "input") String input) throws Exception {
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> inputs = new HashMap<>();
|
|
|
|
Map<String, Object> inputs = new HashMap<>();
|
|
|
|
inputs.put("input", input);
|
|
|
|
inputs.put("input", input);
|
|
|
@ -167,7 +192,7 @@ public class IndexController {
|
|
|
|
sink.error(throwable);
|
|
|
|
sink.error(throwable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|