完成AI的一些接口
This commit is contained in:
commit
27c57acb7f
|
|
@ -0,0 +1,39 @@
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
|
.idea/
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.szsd.ele.ai</groupId>
|
||||||
|
<artifactId>ele-ai</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>ai-chat</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.ai</groupId>
|
||||||
|
<artifactId>spring-ai-elasticsearch-store-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.ai</groupId>
|
||||||
|
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>co.elastic.clients</groupId>
|
||||||
|
<artifactId>elasticsearch-java</artifactId>
|
||||||
|
<version>8.13.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.22</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>ele-ai</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>3.2.8</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>cn.szsd.ai.chat.MainApp</mainClass>
|
||||||
|
</configuration>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package cn.szsd.ai.chat;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class MainApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MainApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cn.szsd.ai.chat.common;
|
||||||
|
|
||||||
|
import cn.hutool.log.StaticLog;
|
||||||
|
import cn.szsd.ai.chat.pojo.ElectromagneticResult;
|
||||||
|
import cn.szsd.ai.chat.service.ElectromagneticResultUtil;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(Throwable.class)
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public ElectromagneticResult<?> unknown(Throwable e) {
|
||||||
|
StaticLog.error(e.getMessage(), e);
|
||||||
|
return ElectromagneticResultUtil.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package cn.szsd.ai.chat.conf;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||||
|
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
|
||||||
|
import org.springframework.ai.chat.memory.InMemoryChatMemory;
|
||||||
|
import org.springframework.ai.vectorstore.VectorStore;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AppBeans {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private VectorStore vectorStore;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public QuestionAnswerAdvisor questionAnswerAdvisor() {
|
||||||
|
return new QuestionAnswerAdvisor(vectorStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageChatMemoryAdvisor messageChatMemoryAdvisor() {
|
||||||
|
return new MessageChatMemoryAdvisor(new InMemoryChatMemory());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
package cn.szsd.ai.chat.controller;
|
||||||
|
|
||||||
|
import cn.szsd.ai.chat.pojo.ElectromagneticResult;
|
||||||
|
import cn.szsd.ai.chat.pojo.QueryDTO;
|
||||||
|
import cn.szsd.ai.chat.pojo.UploadDTO;
|
||||||
|
import cn.szsd.ai.chat.service.ChatService;
|
||||||
|
import cn.szsd.ai.chat.service.ElectromagneticResultUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/data/ed/ai")
|
||||||
|
public class ChatController {
|
||||||
|
|
||||||
|
Logger log = Logger.getLogger(ChatController.class.getName());
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ChatService chatService;
|
||||||
|
|
||||||
|
@PostMapping("/chat")
|
||||||
|
public ElectromagneticResult<String> test(@RequestBody QueryDTO queryDTO) {
|
||||||
|
log.info("question is --->" + queryDTO.getMsg());
|
||||||
|
String chat = chatService.chat(queryDTO);
|
||||||
|
log.info("answer is --->" + chat);
|
||||||
|
return ElectromagneticResultUtil.success(chat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/upload")
|
||||||
|
public ElectromagneticResult<?> upload(@RequestBody UploadDTO uploadDTO) {
|
||||||
|
chatService.add(uploadDTO.getContent());
|
||||||
|
return ElectromagneticResultUtil.success("");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
package cn.szsd.ai.chat.pojo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class ElectromagneticResult<T> implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求成功还是失败
|
||||||
|
*/
|
||||||
|
private Boolean success;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误消息
|
||||||
|
*/
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据对象传给前台的json
|
||||||
|
*/
|
||||||
|
private T data;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package cn.szsd.ai.chat.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class QueryDTO {
|
||||||
|
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
// private String userId;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package cn.szsd.ai.chat.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class UploadDTO {
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
package cn.szsd.ai.chat.service;
|
||||||
|
|
||||||
|
import cn.szsd.ai.chat.pojo.QueryDTO;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
|
import org.springframework.ai.chat.client.advisor.*;
|
||||||
|
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.document.Document;
|
||||||
|
import org.springframework.ai.ollama.OllamaChatModel;
|
||||||
|
import org.springframework.ai.vectorstore.SearchRequest;
|
||||||
|
import org.springframework.ai.vectorstore.VectorStore;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ChatService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private VectorStore vectorStore;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OllamaChatModel model;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MessageChatMemoryAdvisor messageChatMemoryAdvisor;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private QuestionAnswerAdvisor questionAnswerAdvisor;
|
||||||
|
|
||||||
|
public void add(String content) {
|
||||||
|
List<Document> documents = Stream.of(content).map(Document::new).collect(Collectors.toList());
|
||||||
|
vectorStore.write(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String chat(QueryDTO queryDTO) {
|
||||||
|
return ChatClient.builder(model).defaultAdvisors(messageChatMemoryAdvisor, questionAnswerAdvisor).build().prompt()
|
||||||
|
.user(queryDTO.getMsg())
|
||||||
|
.advisors(advisorSpec -> advisorSpec
|
||||||
|
// .param(CHAT_MEMORY_CONVERSATION_ID_KEY, queryDTO.getUserId())
|
||||||
|
.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100))
|
||||||
|
.call()
|
||||||
|
.content();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Flux<ChatResponse> chat1(String msg, String userId) {
|
||||||
|
Prompt prompt = new Prompt(new UserMessage(msg));
|
||||||
|
return this.model.stream(prompt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package cn.szsd.ai.chat.service;
|
||||||
|
|
||||||
|
import cn.szsd.ai.chat.pojo.ElectromagneticResult;
|
||||||
|
|
||||||
|
public class ElectromagneticResultUtil {
|
||||||
|
|
||||||
|
public static <T> ElectromagneticResult<T> success(T data) {
|
||||||
|
ElectromagneticResult<T> electromagneticResult = new ElectromagneticResult<>();
|
||||||
|
electromagneticResult.setSuccess(true);
|
||||||
|
electromagneticResult.setData(data);
|
||||||
|
return electromagneticResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <T> ElectromagneticResult<T> fail(String msg) {
|
||||||
|
ElectromagneticResult<T> electromagneticResult = new ElectromagneticResult<>();
|
||||||
|
electromagneticResult.setSuccess(false);
|
||||||
|
electromagneticResult.setErrorMessage(msg);
|
||||||
|
return electromagneticResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
spring:
|
||||||
|
elasticsearch:
|
||||||
|
password: LCFLr9iQx*mKtwfjJj40
|
||||||
|
username: elastic
|
||||||
|
uris: http://127.0.0.1:9200
|
||||||
|
|
||||||
|
ai:
|
||||||
|
ollama:
|
||||||
|
base-url: http://localhost:11434
|
||||||
|
chat:
|
||||||
|
options:
|
||||||
|
model: deepseek-r1:7b
|
||||||
|
num-g-p-u: 1
|
||||||
|
embedding:
|
||||||
|
enabled: true
|
||||||
|
model: nomic-embed-text
|
||||||
|
|
||||||
|
vectorstore:
|
||||||
|
elasticsearch:
|
||||||
|
initialize-schema: true
|
||||||
|
index-name: rag
|
||||||
|
dimensions: 768
|
||||||
|
similarity: cosine
|
||||||
|
batching-strategy: TOKEN_COUNT
|
||||||
|
|
||||||
|
mvc:
|
||||||
|
async:
|
||||||
|
request-timeout: 3600000
|
||||||
|
|
||||||
|
server:
|
||||||
|
port: 8186
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>cn.szsd.ele.ai</groupId>
|
||||||
|
<artifactId>ele-ai</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>ai-chat</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<spring.boot.version>3.3.8</spring.boot.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.ai</groupId>
|
||||||
|
<artifactId>spring-ai-bom</artifactId>
|
||||||
|
<version>1.0.0-M5</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</releases>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<name>Central Portal Snapshots</name>
|
||||||
|
<id>central-portal-snapshots</id>
|
||||||
|
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</releases>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
Loading…
Reference in New Issue