-
Notifications
You must be signed in to change notification settings - Fork 327
[fel] add default memory #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CodeCasterX
merged 4 commits into
ModelEngine-Group:3.5.x
from
loveTsong:fel-enhance-default-memory
Jul 10, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd9bcfa
[fel] implement memory for recent N histories
loveTsong 55cdf06
[fel] record LLM results to memory
loveTsong d1de888
[fel] apply RecentMemory as default memory for conversation
loveTsong 66c5a32
[fel] add null check for incoming message
loveTsong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...ork/fel/java/fel-core/src/main/java/modelengine/fel/core/memory/support/RecentMemory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fel.core.memory.support; | ||
|
|
||
| import modelengine.fel.core.chat.ChatMessage; | ||
| import modelengine.fel.core.memory.Memory; | ||
| import modelengine.fel.core.template.BulkStringTemplate; | ||
| import modelengine.fel.core.template.support.DefaultBulkStringTemplate; | ||
| import modelengine.fitframework.inspection.Validation; | ||
| import modelengine.fitframework.util.MapBuilder; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static modelengine.fitframework.inspection.Validation.notNull; | ||
|
|
||
| /** | ||
| * 表示使用最近一定次数历史记录的实现。 | ||
| * | ||
| * @author 宋永坦 | ||
| * @since 2025-07-04 | ||
| */ | ||
| public class RecentMemory implements Memory { | ||
| private final LinkedBlockingQueue<ChatMessage> records; | ||
| private final BulkStringTemplate bulkTemplate; | ||
| private final Function<ChatMessage, Map<String, String>> extractor; | ||
|
|
||
| /** | ||
| * 指定最大保留历史记录数量的构造方法。 | ||
| * | ||
| * @param maxCount 表示最大保留历史记录数量的 {@code int}。 | ||
| * @throws IllegalArgumentException 当 {@code maxCount < 0} 时。 | ||
| */ | ||
| public RecentMemory(int maxCount) { | ||
| this(maxCount, | ||
| new DefaultBulkStringTemplate("{{type}}:{{text}}", "\n"), | ||
| message -> MapBuilder.<String, String>get() | ||
| .put("type", message.type().getRole()) | ||
| .put("text", message.text()) | ||
| .build()); | ||
| } | ||
|
|
||
| /** | ||
| * 指定最大保留历史记录数量、渲染模板、抽取方法的构造方法。 | ||
| * | ||
| * @param maxCount 表示最大保留历史记录数量的 {@code int}。 | ||
| * @param bulkTemplate 表示批量字符串模板的 {@link BulkStringTemplate}。 | ||
| * @param extractor 表示将 {@link ChatMessage} 转换成 | ||
| * {@link Map}{@code <}{@link String}, {@link String}{@code >} 的处理函数。 | ||
| * @throws IllegalArgumentException 当 {@code maxCount < 0}、{@code bulkTemplate}、{@code extractor} 为 {@code null} 时。 | ||
| */ | ||
| public RecentMemory(int maxCount, BulkStringTemplate bulkTemplate, | ||
| Function<ChatMessage, Map<String, String>> extractor) { | ||
| Validation.greaterThanOrEquals(maxCount, 0, "The max count should >= 0."); | ||
| this.records = new LinkedBlockingQueue<>(maxCount); | ||
| this.bulkTemplate = notNull(bulkTemplate, "The bulkTemplate cannot be null."); | ||
| this.extractor = notNull(extractor, "The extractor cannot be null."); | ||
| } | ||
|
|
||
| @Override | ||
| public void add(ChatMessage message) { | ||
CodeCasterX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!this.records.offer(message)) { | ||
| this.records.poll(); | ||
| this.records.offer(message); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void set(List<ChatMessage> messages) { | ||
| messages.forEach(this::add); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| this.records.clear(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ChatMessage> messages() { | ||
| return this.records.stream().toList(); | ||
| } | ||
|
|
||
| @Override | ||
| public String text() { | ||
| return this.records.stream() | ||
| .map(this.extractor) | ||
| .collect(Collectors.collectingAndThen(Collectors.toList(), this.bulkTemplate::render)); | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
...fel/java/fel-core/src/test/java/modelengine/fel/core/memory/support/RecentMemoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fel.core.memory.support; | ||
|
|
||
| import modelengine.fel.core.chat.ChatMessage; | ||
| import modelengine.fel.core.chat.support.AiMessage; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /** | ||
| * 表示 {@link RecentMemory} 的测试。 | ||
| * | ||
| * @author 宋永坦 | ||
| * @since 2025-07-04 | ||
| */ | ||
| class RecentMemoryTest { | ||
| private final List<ChatMessage> inputChatMessages = | ||
| Arrays.asList(new AiMessage("1"), new AiMessage("2"), new AiMessage("3")); | ||
|
|
||
| @Test | ||
| void shouldKeepAllMessagesWhenAddGivenLessMessage() { | ||
| RecentMemory recentMemory = new RecentMemory(4); | ||
| this.inputChatMessages.forEach(recentMemory::add); | ||
| List<ChatMessage> messages = recentMemory.messages(); | ||
|
|
||
| assertEquals(inputChatMessages.size(), messages.size()); | ||
| for (int i = 0; i < inputChatMessages.size(); ++i) { | ||
| assertEquals(inputChatMessages.get(i).text(), messages.get(i).text()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldKeepMaxCountMessagesWhenAddGivenOverMaxCountMessages() { | ||
| RecentMemory recentMemory = new RecentMemory(2); | ||
| this.inputChatMessages.forEach(recentMemory::add); | ||
| List<ChatMessage> messages = recentMemory.messages(); | ||
|
|
||
| assertEquals(2, messages.size()); | ||
| assertEquals(inputChatMessages.get(1).text(), messages.get(0).text()); | ||
| assertEquals(inputChatMessages.get(2).text(), messages.get(1).text()); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldKeepMaxCountMessagesWhenSetGivenOverMaxCountMessages() { | ||
| RecentMemory recentMemory = new RecentMemory(2); | ||
| recentMemory.set(this.inputChatMessages); | ||
| List<ChatMessage> messages = recentMemory.messages(); | ||
|
|
||
| assertEquals(2, messages.size()); | ||
| assertEquals(inputChatMessages.get(1).text(), messages.get(0).text()); | ||
| assertEquals(inputChatMessages.get(2).text(), messages.get(1).text()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...l/java/fel-flow/src/test/java/modelengine/fel/engine/operators/models/LlmEmitterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fel.engine.operators.models; | ||
|
|
||
| import modelengine.fel.core.chat.ChatMessage; | ||
| import modelengine.fel.core.chat.Prompt; | ||
| import modelengine.fel.core.chat.support.AiMessage; | ||
| import modelengine.fel.core.chat.support.ChatMessages; | ||
| import modelengine.fel.core.memory.Memory; | ||
| import modelengine.fel.core.tool.ToolCall; | ||
| import modelengine.fel.engine.util.StateKey; | ||
| import modelengine.fit.waterflow.domain.context.FlowSession; | ||
| import modelengine.fitframework.flowable.Choir; | ||
| import modelengine.fitframework.util.StringUtils; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /** | ||
| * 表示 {@link LlmEmitter} 的测试。 | ||
| * | ||
| * @author 宋永坦 | ||
| * @since 2025-07-05 | ||
| */ | ||
| class LlmEmitterTest { | ||
| @Test | ||
| void shouldAddMemoryWhenCompleteGivenLlmOutput() { | ||
| String output = "data1"; | ||
| Prompt prompt = ChatMessages.fromList(Collections.emptyList()); | ||
| Choir<ChatMessage> dataSource = Choir.create(emitter -> { | ||
| emitter.emit(new AiMessage(output)); | ||
| emitter.complete(); | ||
| }); | ||
| FlowSession flowSession = new FlowSession(); | ||
| Memory mockMemory = Mockito.mock(Memory.class); | ||
| ArgumentCaptor<ChatMessage> captor = ArgumentCaptor.forClass(ChatMessage.class); | ||
| Mockito.doNothing().when(mockMemory).add(captor.capture()); | ||
| flowSession.setInnerState(StateKey.HISTORY, mockMemory); | ||
|
|
||
| LlmEmitter<ChatMessage> llmEmitter = new LlmEmitter<>(dataSource, prompt, flowSession); | ||
| llmEmitter.start(flowSession); | ||
|
|
||
| List<ChatMessage> captured = captor.getAllValues(); | ||
| assertEquals(2, captured.size()); | ||
| assertEquals(StringUtils.EMPTY, captured.get(0).text()); | ||
| assertEquals(output, captured.get(1).text()); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldNotAddMemoryWhenCompleteGivenLlmToolCallOutput() { | ||
| String output = "data1"; | ||
| Prompt prompt = ChatMessages.fromList(Collections.emptyList()); | ||
| Choir<ChatMessage> dataSource = Choir.create(emitter -> { | ||
| emitter.emit(new AiMessage(output, Arrays.asList(ToolCall.custom().id("id1").build()))); | ||
| emitter.complete(); | ||
| }); | ||
| FlowSession flowSession = new FlowSession(); | ||
| Memory mockMemory = Mockito.mock(Memory.class); | ||
| flowSession.setInnerState(StateKey.HISTORY, mockMemory); | ||
|
|
||
| LlmEmitter<ChatMessage> llmEmitter = new LlmEmitter<>(dataSource, prompt, flowSession); | ||
| llmEmitter.start(flowSession); | ||
|
|
||
| Mockito.verify(mockMemory, Mockito.times(0)).add(Mockito.any()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.