commit b8418fc60cbbd09a453ac2331324ecab8637b00c Author: xueqingkun Date: Sun Apr 27 10:06:55 2025 +0800 初始化代码 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fdca2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ +/log/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..40897ad --- /dev/null +++ b/pom.xml @@ -0,0 +1,93 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.4.4 + + + com.supervision + pdf-qa-server + 0.0.1-SNAPSHOT + pdf-qa-server + pdf-qa-server + + + 17 + 1.0.0-M7 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.ai + spring-ai-starter-model-ollama + + + + org.postgresql + postgresql + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + cn.hutool + hutool-all + 5.8.26 + + + + + + org.springframework.ai + spring-ai-bom + ${spring-ai.version} + pom + import + + + + + + + + + + + diff --git a/src/main/java/com/supervision/pdfqaserver/PdfQaServerApplication.java b/src/main/java/com/supervision/pdfqaserver/PdfQaServerApplication.java new file mode 100644 index 0000000..eafc706 --- /dev/null +++ b/src/main/java/com/supervision/pdfqaserver/PdfQaServerApplication.java @@ -0,0 +1,14 @@ +package com.supervision.pdfqaserver; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication +public class PdfQaServerApplication { + + public static void main(String[] args) { + SpringApplication.run(PdfQaServerApplication.class, args); + } + +} diff --git a/src/main/java/com/supervision/pdfqaserver/controller/ChatController.java b/src/main/java/com/supervision/pdfqaserver/controller/ChatController.java new file mode 100644 index 0000000..3af4fd9 --- /dev/null +++ b/src/main/java/com/supervision/pdfqaserver/controller/ChatController.java @@ -0,0 +1,41 @@ +package com.supervision.pdfqaserver.controller; + +import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.StrUtil; +import com.supervision.pdfqaserver.dto.R; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +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.ollama.OllamaChatModel; +import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Slf4j +@RestController +@RequestMapping("/ollama") +@RequiredArgsConstructor +@CrossOrigin(origins = "*", maxAge = 3600) +public class ChatController { + + private final OllamaChatModel ollamaChatModel; + + @PostMapping("/chat") + public R pageList(@RequestBody Map message) { + List messages = new ArrayList<>(); + if (StrUtil.isNotEmpty(message.get("system"))){ + messages.add(new SystemMessage(message.get("system"))); + } + if (StrUtil.isNotEmpty(message.get("user"))){ + messages.add(new UserMessage(message.get("user"))); + } + log.info("system: {} , user: {}",message.get("system"),message.get("user")); + Assert.notEmpty(messages, "消息不能为空"); + String response = ollamaChatModel.call(messages.toArray(new Message[0])); + log.info("response:{}",response); + return R.ok(response); + } +} diff --git a/src/main/java/com/supervision/pdfqaserver/dto/DocumentDTO.java b/src/main/java/com/supervision/pdfqaserver/dto/DocumentDTO.java new file mode 100644 index 0000000..deb4096 --- /dev/null +++ b/src/main/java/com/supervision/pdfqaserver/dto/DocumentDTO.java @@ -0,0 +1,41 @@ +package com.supervision.pdfqaserver.dto; + +import lombok.Data; + +/** + * 文档内容 + */ +@Data +public class DocumentDTO { + /** + * 文档id + */ + private String id; + /** + * 文档序号 + */ + private Integer index; + + /** + * 内容类型 0:文本 1:表格 + */ + private String type; + + /** + * 文档内容 + */ + private String content; + + /** + * 文档页码 + */ + private Integer pageNum; + + + /** + * 文件名 + */ + private String fileName; + + +} diff --git a/src/main/java/com/supervision/pdfqaserver/dto/R.java b/src/main/java/com/supervision/pdfqaserver/dto/R.java new file mode 100644 index 0000000..eed864e --- /dev/null +++ b/src/main/java/com/supervision/pdfqaserver/dto/R.java @@ -0,0 +1,131 @@ +package com.supervision.pdfqaserver.dto; + +import lombok.Data; + +import java.io.Serializable; +import java.util.*; + +/** + * 响应信息主体 + * + * @author qimaoyu + */ +@Data +public class R implements Serializable { + + private static final long serialVersionUID = 1L; + + public static final String TOTAL_COUNT = "total"; + public static final String RESULT_LIST = "result"; + + /** + * 成功 + */ + public static final int SUCCESS = 200; + + /** + * 失败 + */ + public static final int FAIL = 500; + + private int code; + + private String msg; + + private T data; + + public static R ok() { + return restResult(null, SUCCESS, null); + } + + public static R judgeResult(Boolean bo, String successMessage, String failMessage) { + if (bo) { + return restResult(null, SUCCESS, successMessage); + } else { + return restResult(null, FAIL, failMessage); + } + } + + public static R okMsg(String msg) { + return restResult(null, SUCCESS, msg); + } + + public static R ok(T data) { + return restResult(data, SUCCESS, null); + } + + public static R ok(T data, String msg) { + return restResult(data, SUCCESS, msg); + } + + public static R fail() { + return restResult(null, FAIL, null); + } + + public static R fail(String msg) { + return restResult(null, FAIL, msg); + } + + public static R fail(T data) { + return restResult(data, FAIL, null); + } + + public static R fail(int code, String msg) { + return restResult(null, code, msg); + } + + + + private static R restResult(T data, int code, String msg) { + R apiResult = new R<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } + + + public static Map buildDataMap(List list) { + Map dataMap = new HashMap<>(); + if (list == null) { + dataMap.put(TOTAL_COUNT, 0); + dataMap.put(RESULT_LIST, new ArrayList<>()); + } else { + dataMap.put(TOTAL_COUNT, list.size()); + dataMap.put(RESULT_LIST, list); + } + return dataMap; + } + + public static Map buildDataMap(List list, Long total) { + Map dataMap = new HashMap<>(); + dataMap.put(TOTAL_COUNT, total); + dataMap.put(RESULT_LIST, list); + return dataMap; + } + + public static Map buildDataMap(Set list) { + Map dataMap = new HashMap<>(); + if (list == null) { + dataMap.put(TOTAL_COUNT, 0); + dataMap.put(RESULT_LIST, new ArrayList<>()); + } else { + dataMap.put(TOTAL_COUNT, list.size()); + dataMap.put(RESULT_LIST, list); + } + return dataMap; + } + + public static Map buildDataMap(Object object) { + if (object == null) { + return null; + } + List resultList = new ArrayList<>(); + resultList.add(object); + Map dataMap = new HashMap<>(); + dataMap.put(TOTAL_COUNT, resultList.size()); + dataMap.put(RESULT_LIST, resultList); + return dataMap; + } + +} diff --git a/src/main/java/com/supervision/pdfqaserver/dto/TruncateDTO.java b/src/main/java/com/supervision/pdfqaserver/dto/TruncateDTO.java new file mode 100644 index 0000000..1c27e52 --- /dev/null +++ b/src/main/java/com/supervision/pdfqaserver/dto/TruncateDTO.java @@ -0,0 +1,32 @@ +package com.supervision.pdfqaserver.dto; + +import lombok.Data; + +/** + * 分段内容 + */ +@Data +public class TruncateDTO { + + /** + * 分段id + */ + private String id; + + /** + * 分段类型 0:文本 1:表格 + */ + private String type; + + /** + * 分段内容 + */ + private String content; + + /** + * 表格标题 + */ + private String title; + + +} diff --git a/src/main/java/com/supervision/pdfqaserver/service/DocumentSlicer.java b/src/main/java/com/supervision/pdfqaserver/service/DocumentSlicer.java new file mode 100644 index 0000000..87916fa --- /dev/null +++ b/src/main/java/com/supervision/pdfqaserver/service/DocumentSlicer.java @@ -0,0 +1,19 @@ +package com.supervision.pdfqaserver.service; + +import com.supervision.pdfqaserver.dto.DocumentDTO; +import com.supervision.pdfqaserver.dto.TruncateDTO; + +import java.util.List; + +/** + * 文档切分器 + */ +public interface DocumentSlicer { + + /** + * 切分文档 + * @param documents 文档列表 + * @return + */ + List slice(List documents); +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..e66b0ba --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,13 @@ +spring: + application: + name: pdf-qa-server + ai: + ollama: + baseUrl: http://192.168.10.70:11434 + chat: + model: qwen2.5:32b + options: + max_tokens: 512 + top_p: 0.9 + top_k: 40 + temperature: 0.7 diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..28b28c3 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,160 @@ + + + logback + + + + + + + + + + + + + + + + + debug + + + %d{yyyy-MM-dd HH:mm:ss.SSS}---[%thread] %-5level %logger{50} - %msg%n + + UTF-8 + + + + + + + + ${log.path}/web_debug.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 2 + 500MB + + + + debug + ACCEPT + DENY + + + + + + + ${log.path}/web_info.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${log.path}/web-info-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 2 + 1GB + + + + info + ACCEPT + DENY + + + + + + + ${log.path}/web_warn.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + ${log.path}/web-warn-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 2 + 500MB + + + + warn + ACCEPT + DENY + + + + + + + ${log.path}/web_error.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + ${log.path}/web-error-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 2 + 500MB + + + + ERROR + ACCEPT + DENY + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/supervision/pdfqaserver/PdfQaServerApplicationTests.java b/src/test/java/com/supervision/pdfqaserver/PdfQaServerApplicationTests.java new file mode 100644 index 0000000..9d1600a --- /dev/null +++ b/src/test/java/com/supervision/pdfqaserver/PdfQaServerApplicationTests.java @@ -0,0 +1,13 @@ +package com.supervision.pdfqaserver; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PdfQaServerApplicationTests { + + @Test + void contextLoads() { + } + +}