From e604b6821dc8a722c27d9f9a571a2955236bf2e2 Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:33 +0800 Subject: [PATCH] Add File --- .../core/prompt/HistoriesPrompt.java | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 agents-flex-core/src/main/java/com/agentsflex/core/prompt/HistoriesPrompt.java diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/prompt/HistoriesPrompt.java b/agents-flex-core/src/main/java/com/agentsflex/core/prompt/HistoriesPrompt.java new file mode 100644 index 0000000..53bc478 --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/prompt/HistoriesPrompt.java @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agentsflex.core.prompt; + +import com.agentsflex.core.memory.ChatMemory; +import com.agentsflex.core.memory.DefaultChatMemory; +import com.agentsflex.core.message.AbstractTextMessage; +import com.agentsflex.core.message.Message; +import com.agentsflex.core.message.SystemMessage; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; + +public class HistoriesPrompt extends Prompt { + + private ChatMemory memory = new DefaultChatMemory(); + + private SystemMessage systemMessage; + + private int maxAttachedMessageCount = 10; + + private boolean historyMessageTruncateEnable = false; + private int historyMessageTruncateLength = 1000; + private Function historyMessageTruncateProcessor; + + // 临时消息不回存入 memory,只会当做 “过程消息” 参与大模型交互 + // 比如用于 Function call 等场景 + private List temporaryMessages; + + public SystemMessage getSystemMessage() { + return systemMessage; + } + + public void setSystemMessage(SystemMessage systemMessage) { + this.systemMessage = systemMessage; + } + + public int getMaxAttachedMessageCount() { + return maxAttachedMessageCount; + } + + public void setMaxAttachedMessageCount(int maxAttachedMessageCount) { + this.maxAttachedMessageCount = maxAttachedMessageCount; + } + + public boolean isHistoryMessageTruncateEnable() { + return historyMessageTruncateEnable; + } + + public void setHistoryMessageTruncateEnable(boolean historyMessageTruncateEnable) { + this.historyMessageTruncateEnable = historyMessageTruncateEnable; + } + + public int getHistoryMessageTruncateLength() { + return historyMessageTruncateLength; + } + + public void setHistoryMessageTruncateLength(int historyMessageTruncateLength) { + this.historyMessageTruncateLength = historyMessageTruncateLength; + } + + public Function getHistoryMessageTruncateProcessor() { + return historyMessageTruncateProcessor; + } + + public void setHistoryMessageTruncateProcessor(Function historyMessageTruncateProcessor) { + this.historyMessageTruncateProcessor = historyMessageTruncateProcessor; + } + + public HistoriesPrompt() { + } + + public HistoriesPrompt(ChatMemory memory) { + this.memory = memory; + } + + public void addMessage(Message message) { + memory.addMessage(message); + } + + public void addMessageTemporary(Message message) { + if (temporaryMessages == null) { + temporaryMessages = new ArrayList<>(); + } + temporaryMessages.add(message); + } + + public void addMessages(Collection messages) { + memory.addMessages(messages); + } + + public ChatMemory getMemory() { + return memory; + } + + public void setMemory(ChatMemory memory) { + this.memory = memory; + } + + public List getTemporaryMessages() { + return temporaryMessages; + } + + public void setTemporaryMessages(List temporaryMessages) { + this.temporaryMessages = temporaryMessages; + } + + public void clearTemporaryMessages() { + temporaryMessages.clear(); + temporaryMessages = null; + } + + @Override + public List toMessages() { + List messages = memory.getMessages(); + if (messages == null) messages = new ArrayList<>(); + + if (messages.size() > maxAttachedMessageCount) { + messages = messages.subList(messages.size() - maxAttachedMessageCount, messages.size()); + } + + if (historyMessageTruncateEnable) { + for (Message message : messages) { + if (message instanceof AbstractTextMessage) { + String content = ((AbstractTextMessage) message).getContent(); + if (historyMessageTruncateProcessor != null) { + content = historyMessageTruncateProcessor.apply(content); + } else if (content.length() > historyMessageTruncateLength) { + content = content.substring(0, historyMessageTruncateLength); + } + ((AbstractTextMessage) message).setContent(content); + } + } + } + + Message firstMessage = messages.get(0); + if (!(firstMessage instanceof SystemMessage) && systemMessage != null) { + messages.add(0, systemMessage); + } + + if (temporaryMessages != null) { + messages.addAll(temporaryMessages); + } + + return messages; + } + + +}