完成AI相关接口
This commit is contained in:
parent
27c57acb7f
commit
bd40c34382
|
|
@ -31,7 +31,6 @@
|
||||||
<groupId>org.springframework.ai</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
|
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,12 @@ import cn.szsd.ai.chat.pojo.QueryDTO;
|
||||||
import cn.szsd.ai.chat.pojo.UploadDTO;
|
import cn.szsd.ai.chat.pojo.UploadDTO;
|
||||||
import cn.szsd.ai.chat.service.ChatService;
|
import cn.szsd.ai.chat.service.ChatService;
|
||||||
import cn.szsd.ai.chat.service.ElectromagneticResultUtil;
|
import cn.szsd.ai.chat.service.ElectromagneticResultUtil;
|
||||||
|
import cn.szsd.ai.chat.utils.ChatTaskThread;
|
||||||
|
import cn.szsd.ai.chat.utils.ThreadUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
@ -20,11 +23,13 @@ public class ChatController {
|
||||||
private ChatService chatService;
|
private ChatService chatService;
|
||||||
|
|
||||||
@PostMapping("/chat")
|
@PostMapping("/chat")
|
||||||
public ElectromagneticResult<String> test(@RequestBody QueryDTO queryDTO) {
|
public ElectromagneticResult<String> test(@RequestBody QueryDTO queryDTO) throws Exception {
|
||||||
log.info("question is --->" + queryDTO.getMsg());
|
log.info("question is --->" + queryDTO.getMsg());
|
||||||
String chat = chatService.chat(queryDTO);
|
ChatTaskThread chatTaskThread = new ChatTaskThread(chatService, queryDTO);
|
||||||
log.info("answer is --->" + chat);
|
Future<String> future = ThreadUtil.getThreadPool().submit(chatTaskThread);
|
||||||
return ElectromagneticResultUtil.success(chat);
|
String res = future.get();
|
||||||
|
log.info("answer is --->" + res);
|
||||||
|
return ElectromagneticResultUtil.success(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload")
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package cn.szsd.ai.chat.service;
|
||||||
|
|
||||||
import cn.szsd.ai.chat.pojo.QueryDTO;
|
import cn.szsd.ai.chat.pojo.QueryDTO;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.ai.chat.client.advisor.*;
|
import org.springframework.ai.chat.client.advisor.*;
|
||||||
import org.springframework.ai.chat.messages.UserMessage;
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
|
|
@ -9,19 +10,15 @@ import org.springframework.ai.chat.model.ChatResponse;
|
||||||
import org.springframework.ai.chat.prompt.Prompt;
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
import org.springframework.ai.document.Document;
|
import org.springframework.ai.document.Document;
|
||||||
import org.springframework.ai.ollama.OllamaChatModel;
|
import org.springframework.ai.ollama.OllamaChatModel;
|
||||||
import org.springframework.ai.vectorstore.SearchRequest;
|
|
||||||
import org.springframework.ai.vectorstore.VectorStore;
|
import org.springframework.ai.vectorstore.VectorStore;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
|
@Slf4j
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ChatService {
|
public class ChatService {
|
||||||
|
|
||||||
|
|
@ -43,6 +40,9 @@ public class ChatService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String chat(QueryDTO queryDTO) {
|
public String chat(QueryDTO queryDTO) {
|
||||||
|
|
||||||
|
log.info("Start call model to answer");
|
||||||
|
|
||||||
return ChatClient.builder(model).defaultAdvisors(messageChatMemoryAdvisor, questionAnswerAdvisor).build().prompt()
|
return ChatClient.builder(model).defaultAdvisors(messageChatMemoryAdvisor, questionAnswerAdvisor).build().prompt()
|
||||||
.user(queryDTO.getMsg())
|
.user(queryDTO.getMsg())
|
||||||
.advisors(advisorSpec -> advisorSpec
|
.advisors(advisorSpec -> advisorSpec
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package cn.szsd.ai.chat.utils;
|
||||||
|
|
||||||
|
import cn.szsd.ai.chat.pojo.QueryDTO;
|
||||||
|
import cn.szsd.ai.chat.service.ChatService;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ChatTaskThread implements Callable<String> {
|
||||||
|
|
||||||
|
private ChatService chatService;
|
||||||
|
private QueryDTO queryDTO;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String call() throws Exception {
|
||||||
|
return chatService.chat(queryDTO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package cn.szsd.ai.chat.utils;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
public final class ThreadUtil {
|
||||||
|
|
||||||
|
private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(1, 1, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
|
||||||
|
|
||||||
|
public static ExecutorService getThreadPool() {
|
||||||
|
return THREAD_POOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue