新增模块
parent
f6f9668e41
commit
dc808e5ea2
@ -0,0 +1,155 @@
|
||||
<?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>com.supervision</groupId>
|
||||
<artifactId>know_sub</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>know_sub_etl</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-tika-document-reader</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入ollama的依赖.版本号来自于 dependencyManagement中 spring-ai-bom中的版本号.-->
|
||||
<dependency>
|
||||
<groupId>io.springboot.ai</groupId>
|
||||
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-elasticsearch-store</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.supervision</groupId>
|
||||
<artifactId>know_sub_common</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.supervision</groupId>
|
||||
<artifactId>know_sub_model</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<name>aliyun central repo</name>
|
||||
<url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
|
||||
<layout>default</layout>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<parameters>true</parameters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package som.supervision.knowsub;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@MapperScan(basePackages = {"com.supervision.**.mapper"})
|
||||
@EnableConfigurationProperties
|
||||
public class KnowSubEtlApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(KnowSubEtlApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.supervision.knowsub;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@MapperScan(basePackages = {"com.supervision.**.mapper"})
|
||||
@EnableConfigurationProperties
|
||||
public class KnowSubRagApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(KnowSubRagApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.supervision.knowsub.controller;
|
||||
|
||||
import com.supervision.knowsub.service.RagService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Tag(name = "RAG问答")
|
||||
@RestController
|
||||
@RequestMapping("rag")
|
||||
@RequiredArgsConstructor
|
||||
public class RagController {
|
||||
|
||||
private final RagService ragService;
|
||||
|
||||
|
||||
|
||||
@Operation(summary = "问答")
|
||||
@GetMapping("question")
|
||||
public String ask(String question) {
|
||||
return ragService.ask(question);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.supervision.knowsub.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HtmlContext {
|
||||
|
||||
private String htmlContext;
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.supervision.knowsub.service;
|
||||
|
||||
public interface RagService {
|
||||
|
||||
String ask(String question);
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
package com.supervision.knowsub.util;
|
||||
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.ChatResponse;
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.ollama.OllamaChatClient;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Slf4j
|
||||
public class AiChatUtil {
|
||||
|
||||
private static final ExecutorService chatExecutor = ThreadUtil.newFixedExecutor(5, 5, "chat", new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
|
||||
private static final OllamaChatClient chatClient = SpringBeanUtil.getBean(OllamaChatClient.class);
|
||||
|
||||
/**
|
||||
* 单轮对话
|
||||
*
|
||||
* @param chat 对话的内容
|
||||
* @return jsonObject
|
||||
*/
|
||||
public static Optional<JSONObject> chat(String chat) {
|
||||
Prompt prompt = new Prompt(List.of(new UserMessage(chat)));
|
||||
Future<String> submit = chatExecutor.submit(new ChatTask(chatClient, prompt));
|
||||
try {
|
||||
return Optional.of(JSONUtil.parseObj(submit.get()));
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
log.error("调用大模型生成失败");
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持多轮对话,自定义消息
|
||||
*
|
||||
* @param messageList 消息列表
|
||||
* @return jsonObject
|
||||
*/
|
||||
public static Optional<JSONObject> chat(List<Message> messageList) {
|
||||
Prompt prompt = new Prompt(messageList);
|
||||
Future<String> submit = chatExecutor.submit(new ChatTask(chatClient, prompt));
|
||||
try {
|
||||
return Optional.of(JSONUtil.parseObj(submit.get()));
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
log.error("调用大模型生成失败");
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持序列化的方式
|
||||
*
|
||||
* @param messageList 消息列表
|
||||
* @param clazz 需要序列化的对象
|
||||
* @param <T> 需要序列化的对象的泛型
|
||||
* @return 对应对象类型, 不支持列表类型
|
||||
*/
|
||||
public static <T> Optional<T> chat(List<Message> messageList, Class<T> clazz) {
|
||||
Prompt prompt = new Prompt(messageList);
|
||||
Future<String> submit = chatExecutor.submit(new ChatTask(chatClient, prompt));
|
||||
try {
|
||||
String s = submit.get();
|
||||
return Optional.ofNullable(JSONUtil.toBean(s, clazz));
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
log.error("调用大模型生成失败", e);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持序列化的方式的对话
|
||||
*
|
||||
* @param chat 对话的消息
|
||||
* @param clazz 需要序列化的对象
|
||||
* @param <T> 需要序列化的对象的泛型
|
||||
* @return 对应对象类型, 不支持列表类型
|
||||
*/
|
||||
public static <T> Optional<T> chat(String chat, Class<T> clazz) {
|
||||
Prompt prompt = new Prompt(List.of(new UserMessage(chat)));
|
||||
Future<String> submit = chatExecutor.submit(new ChatTask(chatClient, prompt));
|
||||
try {
|
||||
String s = submit.get();
|
||||
return Optional.ofNullable(JSONUtil.toBean(s, clazz));
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
log.error("调用大模型生成失败");
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private record ChatTask(OllamaChatClient chatClient, Prompt prompt) implements Callable<String> {
|
||||
@Override
|
||||
public String call() {
|
||||
ChatResponse call = chatClient.call(prompt);
|
||||
return call.getResult().getOutput().getContent();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue