|
|
@@ -55,6 +55,7 @@ public class ChatController {
|
|
|
|
|
|
@PostMapping("/ask")
|
|
|
public ResponseEntity<Map<String, Object>> chatAsk(@RequestBody ChatRequest request) {
|
|
|
+ final String userKey = getCurrentUserKey();
|
|
|
String query = request.getMessage();
|
|
|
String normalized = qaCache.normalize(query);
|
|
|
String cid = request.getConversationId() != null && !request.getConversationId().isBlank()
|
|
|
@@ -132,14 +133,14 @@ public class ChatController {
|
|
|
// 检查全局缓存
|
|
|
Map<String, Object> cached = qaCache.get(normalized);
|
|
|
if (cached != null) {
|
|
|
- return streamCached(cached, cid, query, normalized);
|
|
|
+ return streamCached(cached, cid, query, normalized, userKey);
|
|
|
}
|
|
|
|
|
|
// 未命中缓存:尝试抢占处理权,避免并发重复调 LLM
|
|
|
if (!qaCache.tryMarkPending(normalized)) {
|
|
|
Map<String, Object> waited = qaCache.waitForCache(normalized);
|
|
|
if (waited != null) {
|
|
|
- return streamCached(waited, cid, query, normalized);
|
|
|
+ return streamCached(waited, cid, query, normalized, userKey);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -191,7 +192,8 @@ public class ChatController {
|
|
|
|
|
|
/** 将缓存命中结果以流式 SSE 形式返回 */
|
|
|
private Flux<ServerSentEvent<String>> streamCached(Map<String, Object> cached, String cid,
|
|
|
- String query, String normalized) {
|
|
|
+ String query, String normalized,
|
|
|
+ String userKey) {
|
|
|
String answer = (String) cached.get("answer");
|
|
|
String intent = (String) cached.getOrDefault("intent", "");
|
|
|
@SuppressWarnings("unchecked")
|
|
|
@@ -224,6 +226,7 @@ public class ChatController {
|
|
|
|
|
|
@PostMapping("/ask-image")
|
|
|
public ResponseEntity<Map<String, Object>> chatAskImage(@RequestBody ImageChatRequest request) {
|
|
|
+ final String userKey = getCurrentUserKey();
|
|
|
String cid = request.getConversationId() != null && !request.getConversationId().isBlank()
|
|
|
? request.getConversationId()
|
|
|
: UUID.randomUUID().toString();
|
|
|
@@ -266,6 +269,7 @@ public class ChatController {
|
|
|
|
|
|
@PostMapping(value = "/stream-image", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
|
public Flux<ServerSentEvent<String>> chatStreamImage(@RequestBody ImageChatRequest request) {
|
|
|
+ final String userKey = getCurrentUserKey();
|
|
|
final String cid = request.getConversationId() != null && !request.getConversationId().isBlank()
|
|
|
? request.getConversationId()
|
|
|
: UUID.randomUUID().toString();
|
|
|
@@ -352,6 +356,7 @@ public class ChatController {
|
|
|
|
|
|
@PostMapping("/ask-multimodal")
|
|
|
public ResponseEntity<Map<String, Object>> chatAskMultimodal(@RequestBody MultimodalChatRequest request) {
|
|
|
+ final String userKey = getCurrentUserKey();
|
|
|
String cid = request.getConversationId() != null && !request.getConversationId().isBlank()
|
|
|
? request.getConversationId()
|
|
|
: UUID.randomUUID().toString();
|
|
|
@@ -409,6 +414,7 @@ public class ChatController {
|
|
|
|
|
|
@PostMapping(value = "/stream-multimodal", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
|
public Flux<ServerSentEvent<String>> chatStreamMultimodal(@RequestBody MultimodalChatRequest request) {
|
|
|
+ final String userKey = getCurrentUserKey();
|
|
|
final String cid = request.getConversationId() != null && !request.getConversationId().isBlank()
|
|
|
? request.getConversationId()
|
|
|
: UUID.randomUUID().toString();
|
|
|
@@ -435,12 +441,12 @@ public class ChatController {
|
|
|
})
|
|
|
.doOnComplete(() -> {
|
|
|
sink.tryEmitNext(ServerSentEvent.<String>builder().data("\n\n").build());
|
|
|
- doStreamAnswer(sink, cid, request, ocrBuilder.toString(), mediaLabel);
|
|
|
+ doStreamAnswer(sink, cid, request, ocrBuilder.toString(), mediaLabel, userKey);
|
|
|
})
|
|
|
.doOnError(sink::tryEmitError)
|
|
|
.subscribe();
|
|
|
} else {
|
|
|
- doStreamAnswer(sink, cid, request, "", "");
|
|
|
+ doStreamAnswer(sink, cid, request, "", "", userKey);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
sink.tryEmitError(e);
|
|
|
@@ -537,7 +543,8 @@ public class ChatController {
|
|
|
|
|
|
/** 流式多模态:OCR 完成后,走 RAG + 生成 */
|
|
|
private void doStreamAnswer(Sinks.Many<ServerSentEvent<String>> sink, String cid,
|
|
|
- MultimodalChatRequest request, String ocrText, String mediaLabel) {
|
|
|
+ MultimodalChatRequest request, String ocrText, String mediaLabel,
|
|
|
+ String userKey) {
|
|
|
if (!ocrText.isEmpty()) {
|
|
|
sink.tryEmitNext(ServerSentEvent.<String>builder().event("status")
|
|
|
.data("📚 检索药典知识库...").build());
|