12.12 每日总结(学习Ai)

今天搞SpingAI的智能体,和智能对话。

所以在搞UUID,为每个对话分配。

将 UUID 存储到全局或共享的上下文
使用一个线程安全的数据结构(如 ConcurrentHashMap)来存储 UUID 与用户 ID 的映射。


@Component
public class ChatSessionManager {
private final Map<String, UUID> userSessionMap = new ConcurrentHashMap<>();

public void saveSession(String userId, UUID uuid) {
userSessionMap.put(userId, uuid);
}

public UUID getSession(String userId) {
return userSessionMap.get(userId);
}

public void removeSession(String userId) {
userSessionMap.remove(userId);
}
}
在 generateStreamAsString 方法中保存 UUID:


@Autowired
private ChatSessionManager chatSessionManager;

@CrossOrigin
@GetMapping(value = "/ai/generateStreamAsString", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> generateStreamAsString(@RequestParam(value = "message", defaultValue = "你好,你能干什么") String message) {
String tokenValue = StpUtil.getTokenValue();
String userId = StpUtil.getLoginIdByToken(tokenValue).toString();
User user = userService.getById(userId);
String nickName = user.getNickname();
UUID conversationId = UUID.fastUUID();

// Save UUID for the user
chatSessionManager.saveSession(userId, conversationId);

Flux<String> content = this.chatClient.prompt()
.user(message)
.system(promptSystemSpec -> promptSystemSpec.params(Map.of(
"current_date", LocalDate.now().toString(), "userId", userId, "nickName", nickName)))
.advisors(advisorSpec -> advisorSpec.params(Map.of(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100,
AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId)))
.stream()
.content();
return content.concatWith(Flux.just("[complete]"));
}

posted @ 2024-12-17 14:19  笠大  阅读(77)  评论(0)    收藏  举报