-
Notifications
You must be signed in to change notification settings - Fork 165
make memory optional in conversational agent #3626
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
Open
jngz-es
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
jngz-es:optional_memory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -168,8 +168,7 @@ public void run(MLAgent mlAgent, Map<String, String> inputParams, ActionListener | |
if (functionCalling != null) { | ||
functionCalling.configure(params); | ||
} | ||
|
||
String memoryType = mlAgent.getMemory().getType(); | ||
String memoryType = mlAgent.getMemory() == null ? null : mlAgent.getMemory().getType(); | ||
String memoryId = params.get(MLAgentExecutor.MEMORY_ID); | ||
String appType = mlAgent.getAppType(); | ||
String title = params.get(MLAgentExecutor.QUESTION); | ||
|
@@ -178,6 +177,11 @@ public void run(MLAgent mlAgent, Map<String, String> inputParams, ActionListener | |
String chatHistoryResponseTemplate = params.get(CHAT_HISTORY_RESPONSE_TEMPLATE); | ||
int messageHistoryLimit = getMessageHistoryLimit(params); | ||
|
||
if (memoryType == null || memoryType.isBlank()) { | ||
runAgent(mlAgent, params, listener, null, null, functionCalling); | ||
return; | ||
} | ||
|
||
ConversationIndexMemory.Factory conversationIndexMemoryFactory = (ConversationIndexMemory.Factory) memoryFactoryMap.get(memoryType); | ||
conversationIndexMemoryFactory.create(title, memoryId, appType, ActionListener.<ConversationIndexMemory>wrap(memory -> { | ||
// TODO: call runAgent directly if messageHistoryLimit == 0 | ||
|
@@ -317,7 +321,9 @@ private void runReAct( | |
AtomicReference<String> newPrompt = new AtomicReference<>(tmpSubstitutor.replace(prompt)); | ||
tmpParameters.put(PROMPT, newPrompt.get()); | ||
|
||
List<ModelTensors> traceTensors = createModelTensors(sessionId, parentInteractionId); | ||
List<ModelTensors> traceTensors = (conversationIndexMemory == null) | ||
? new ArrayList<>() | ||
: createModelTensors(sessionId, parentInteractionId); | ||
Comment on lines
+324
to
+326
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about passing conversationIndexMemory to this method and returning empty list rather than using ternary operator here? *ignore, seems like it will affect a lot of other methods using this |
||
int maxIterations = Integer.parseInt(tmpParameters.getOrDefault(MAX_ITERATION, DEFAULT_MAX_ITERATIONS)); | ||
for (int i = 0; i < maxIterations; i++) { | ||
int finalI = i; | ||
|
@@ -513,8 +519,8 @@ private void runReAct( | |
client.execute(MLPredictionTaskAction.INSTANCE, request, firstListener); | ||
} | ||
|
||
private static List<ModelTensors> createFinalAnswerTensors(List<ModelTensors> sessionId, List<ModelTensor> lastThought) { | ||
List<ModelTensors> finalModelTensors = sessionId; | ||
private static List<ModelTensors> createFinalAnswerTensors(List<ModelTensors> modelTensorsList, List<ModelTensor> lastThought) { | ||
List<ModelTensors> finalModelTensors = modelTensorsList; | ||
finalModelTensors.add(ModelTensors.builder().mlModelTensors(lastThought).build()); | ||
return finalModelTensors; | ||
} | ||
|
@@ -719,19 +725,21 @@ private void sendFinalAnswer( | |
public static List<ModelTensors> createModelTensors(String sessionId, String parentInteractionId) { | ||
List<ModelTensors> cotModelTensors = new ArrayList<>(); | ||
|
||
cotModelTensors | ||
.add( | ||
ModelTensors | ||
.builder() | ||
.mlModelTensors( | ||
List | ||
.of( | ||
ModelTensor.builder().name(MLAgentExecutor.MEMORY_ID).result(sessionId).build(), | ||
ModelTensor.builder().name(MLAgentExecutor.PARENT_INTERACTION_ID).result(parentInteractionId).build() | ||
) | ||
) | ||
.build() | ||
); | ||
if (sessionId != null && !sessionId.isBlank()) { | ||
cotModelTensors | ||
.add( | ||
ModelTensors | ||
.builder() | ||
.mlModelTensors( | ||
List | ||
.of( | ||
ModelTensor.builder().name(MLAgentExecutor.MEMORY_ID).result(sessionId).build(), | ||
ModelTensor.builder().name(MLAgentExecutor.PARENT_INTERACTION_ID).result(parentInteractionId).build() | ||
) | ||
) | ||
.build() | ||
); | ||
} | ||
return cotModelTensors; | ||
} | ||
|
||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the current behavior throw NPE here? wondering if there is some validation in the REST/Transport layer to check for memory, didn't see any tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memory is created by default in MLAgentExecutor right? so should we change some code there as well