16 changed files with 170 additions and 61 deletions
-
47src/main/java/com/wok/supportbot/app/AssistantApp.java
-
5src/main/java/com/wok/supportbot/app/ProductInfoApp.java
-
25src/main/java/com/wok/supportbot/config/CorsConfig.java
-
89src/main/java/com/wok/supportbot/controller/AiController.java
-
15src/main/java/com/wok/supportbot/controller/HealthController.java
-
2src/main/java/com/wok/supportbot/rag/config/QueryExpanderConfig.java
-
7src/main/java/com/wok/supportbot/rag/config/QueryTransformerConfig.java
-
7src/main/java/com/wok/supportbot/rag/load/InMemoryVectorStoreConfig.java
-
7src/main/java/com/wok/supportbot/rag/load/PgVectorStoreConfig.java
-
4src/main/java/com/wok/supportbot/rag/preretrieval/CompressionQueryRewriter.java
-
2src/main/java/com/wok/supportbot/rag/preretrieval/MultiQueryExpanderRewriter.java
-
2src/main/java/com/wok/supportbot/rag/preretrieval/RewriteQueryRewriter.java
-
2src/main/java/com/wok/supportbot/rag/preretrieval/TranslationQueryRewriter.java
-
1src/test/java/com/wok/supportbot/PgVectorVectorStoreConfigTest.java
-
10src/test/java/com/wok/supportbot/QueryTransformerTests.java
-
6src/test/java/com/wok/supportbot/SupportBotApplicationTests.java
@ -0,0 +1,25 @@ |
|||||
|
package com.wok.supportbot.config; |
||||
|
|
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
|
||||
|
/** |
||||
|
* 全局跨域配置 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class CorsConfig implements WebMvcConfigurer { |
||||
|
|
||||
|
@Override |
||||
|
public void addCorsMappings(CorsRegistry registry) { |
||||
|
// 覆盖所有请求 |
||||
|
registry.addMapping("/**") |
||||
|
// 允许发送 Cookie |
||||
|
.allowCredentials(true) |
||||
|
// 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突) |
||||
|
.allowedOriginPatterns("*") |
||||
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") |
||||
|
.allowedHeaders("*") |
||||
|
.exposedHeaders("*"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
package com.wok.supportbot.controller; |
||||
|
|
||||
|
import com.wok.supportbot.app.AssistantApp; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.springframework.ai.chat.model.ChatModel; |
||||
|
import org.springframework.ai.tool.ToolCallback; |
||||
|
import org.springframework.http.MediaType; |
||||
|
import org.springframework.http.codec.ServerSentEvent; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
||||
|
import reactor.core.publisher.Flux; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
|
||||
|
|
||||
|
public class AiController { |
||||
|
|
||||
|
@Resource |
||||
|
private AssistantApp assistantApp; |
||||
|
|
||||
|
/** |
||||
|
* 同步调用 AI 智能客服应用 |
||||
|
* |
||||
|
* @param message |
||||
|
* @param chatId |
||||
|
* @return |
||||
|
*/ |
||||
|
@GetMapping("/assistant_app/chat/sync") |
||||
|
public String doChatWithAssistantAppSync(String message, String chatId) { |
||||
|
return assistantApp.doChat(message, chatId); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* SSE 流式调用 AI 智能客服应用 |
||||
|
* 返回Flux 响应式对象,并且添加 SSE 对应的 MediaType |
||||
|
* |
||||
|
* @param message |
||||
|
* @param chatId |
||||
|
* @return |
||||
|
*/ |
||||
|
@GetMapping(value = "/assistant_app/chat/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
||||
|
public Flux<String> doChatWithLoveAppSSE(String message, String chatId) { |
||||
|
return assistantApp.doChatByStream(message, chatId); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* SSE 流式调用 AI 智能客服应用 |
||||
|
* 返回 Flux 对象,并且设置泛型为 ServerSentEvent。使用这种方式可以省略 MediaType |
||||
|
* |
||||
|
* @param message |
||||
|
* @param chatId |
||||
|
* @return |
||||
|
*/ |
||||
|
@GetMapping(value = "/assistant_app/chat/server_sent_event") |
||||
|
public Flux<ServerSentEvent<String>> doChatWithAssistantAppServerSentEvent(String message, String chatId) { |
||||
|
return assistantApp.doChatByStream(message, chatId) |
||||
|
.map(chunk -> ServerSentEvent.<String>builder() |
||||
|
.data(chunk) |
||||
|
.build()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* SSE 流式调用 AI 智能客服应用 |
||||
|
* 使用 SSEEmiter,通过 send 方法持续向 SseEmitter 发送消息 |
||||
|
* |
||||
|
* @param message |
||||
|
* @param chatId |
||||
|
* @return |
||||
|
*/ |
||||
|
@GetMapping(value = "/assistant_app/chat/sse_emitter") |
||||
|
public SseEmitter doChatWithAssistantAppServerSseEmitter(String message, String chatId) { |
||||
|
// 创建一个超时时间较长的 SseEmitter |
||||
|
SseEmitter sseEmitter = new SseEmitter(180000L); // 3 分钟超时 |
||||
|
// 获取 Flux 响应式数据流并且直接通过订阅推送给 SseEmitter |
||||
|
assistantApp.doChatByStream(message, chatId) |
||||
|
.subscribe(chunk -> { |
||||
|
try { |
||||
|
sseEmitter.send(chunk); |
||||
|
} catch (IOException e) { |
||||
|
sseEmitter.completeWithError(e); |
||||
|
} |
||||
|
}, sseEmitter::completeWithError, sseEmitter::complete); |
||||
|
// 返回 |
||||
|
return sseEmitter; |
||||
|
} |
||||
|
} |
||||
@ -1,15 +0,0 @@ |
|||||
package com.wok.supportbot.controller; |
|
||||
|
|
||||
import org.springframework.web.bind.annotation.GetMapping; |
|
||||
import org.springframework.web.bind.annotation.RequestMapping; |
|
||||
import org.springframework.web.bind.annotation.RestController; |
|
||||
|
|
||||
@RestController |
|
||||
@RequestMapping("/health") |
|
||||
public class HealthController { |
|
||||
|
|
||||
@GetMapping |
|
||||
public String healthCheck() { |
|
||||
return "ok"; |
|
||||
} |
|
||||
} |
|
||||
@ -1,4 +1,4 @@ |
|||||
package com.wok.supportbot.config; |
|
||||
|
package com.wok.supportbot.rag.config; |
||||
|
|
||||
import org.springframework.ai.chat.client.ChatClient; |
import org.springframework.ai.chat.client.ChatClient; |
||||
import org.springframework.ai.chat.model.ChatModel; |
import org.springframework.ai.chat.model.ChatModel; |
||||
@ -1,19 +1,14 @@ |
|||||
package com.wok.supportbot.config; |
|
||||
|
package com.wok.supportbot.rag.config; |
||||
|
|
||||
import org.springframework.ai.chat.client.ChatClient; |
import org.springframework.ai.chat.client.ChatClient; |
||||
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor; |
|
||||
import org.springframework.ai.chat.model.ChatModel; |
import org.springframework.ai.chat.model.ChatModel; |
||||
import org.springframework.ai.rag.preretrieval.query.expansion.MultiQueryExpander; |
|
||||
import org.springframework.ai.rag.preretrieval.query.transformation.CompressionQueryTransformer; |
import org.springframework.ai.rag.preretrieval.query.transformation.CompressionQueryTransformer; |
||||
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer; |
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer; |
||||
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer; |
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer; |
||||
import org.springframework.ai.rag.preretrieval.query.transformation.TranslationQueryTransformer; |
import org.springframework.ai.rag.preretrieval.query.transformation.TranslationQueryTransformer; |
||||
import org.springframework.ai.rag.retrieval.search.DocumentRetriever; |
|
||||
import org.springframework.context.annotation.Bean; |
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
@Configuration |
@Configuration |
||||
public class QueryTransformerConfig { |
public class QueryTransformerConfig { |
||||
|
|
||||
@ -1,16 +1,11 @@ |
|||||
package com.wok.supportbot.load; |
|
||||
|
package com.wok.supportbot.rag.load; |
||||
|
|
||||
import com.wok.supportbot.extract.MarkdownDocumentLoader; |
|
||||
import jakarta.annotation.Resource; |
|
||||
import org.springframework.ai.document.Document; |
|
||||
import org.springframework.ai.embedding.EmbeddingModel; |
import org.springframework.ai.embedding.EmbeddingModel; |
||||
import org.springframework.ai.vectorstore.SimpleVectorStore; |
import org.springframework.ai.vectorstore.SimpleVectorStore; |
||||
import org.springframework.ai.vectorstore.VectorStore; |
import org.springframework.ai.vectorstore.VectorStore; |
||||
import org.springframework.context.annotation.Bean; |
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
/** |
/** |
||||
* 向量数据库配置(初始化基于内存的向量数据库 Bean) |
* 向量数据库配置(初始化基于内存的向量数据库 Bean) |
||||
*/ |
*/ |
||||
@ -1,10 +1,8 @@ |
|||||
package com.wok.supportbot.preretrieval; |
|
||||
|
package com.wok.supportbot.rag.preretrieval; |
||||
|
|
||||
import org.springframework.ai.chat.client.ChatClient; |
import org.springframework.ai.chat.client.ChatClient; |
||||
|
|
||||
import org.springframework.ai.chat.messages.AssistantMessage; |
|
||||
import org.springframework.ai.chat.messages.Message; |
import org.springframework.ai.chat.messages.Message; |
||||
import org.springframework.ai.chat.messages.UserMessage; |
|
||||
import org.springframework.ai.chat.model.ChatModel; |
import org.springframework.ai.chat.model.ChatModel; |
||||
import org.springframework.ai.rag.Query; |
import org.springframework.ai.rag.Query; |
||||
import org.springframework.ai.rag.preretrieval.query.transformation.CompressionQueryTransformer; |
import org.springframework.ai.rag.preretrieval.query.transformation.CompressionQueryTransformer; |
||||
@ -1,4 +1,4 @@ |
|||||
package com.wok.supportbot.preretrieval; |
|
||||
|
package com.wok.supportbot.rag.preretrieval; |
||||
|
|
||||
import org.springframework.ai.chat.client.ChatClient; |
import org.springframework.ai.chat.client.ChatClient; |
||||
import org.springframework.ai.chat.model.ChatModel; |
import org.springframework.ai.chat.model.ChatModel; |
||||
@ -1,4 +1,4 @@ |
|||||
package com.wok.supportbot.preretrieval; |
|
||||
|
package com.wok.supportbot.rag.preretrieval; |
||||
|
|
||||
import org.springframework.ai.chat.client.ChatClient; |
import org.springframework.ai.chat.client.ChatClient; |
||||
import org.springframework.ai.chat.model.ChatModel; |
import org.springframework.ai.chat.model.ChatModel; |
||||
@ -1,4 +1,4 @@ |
|||||
package com.wok.supportbot.preretrieval; |
|
||||
|
package com.wok.supportbot.rag.preretrieval; |
||||
|
|
||||
import org.springframework.ai.chat.client.ChatClient; |
import org.springframework.ai.chat.client.ChatClient; |
||||
import org.springframework.ai.chat.model.ChatModel; |
import org.springframework.ai.chat.model.ChatModel; |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue