Skip to content
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

make memory optional in conversational agent #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.StepListener;
Expand Down Expand Up @@ -127,6 +128,11 @@ public void run(MLAgent mlAgent, Map<String, String> params, ActionListener<Obje
String title = params.get(MLAgentExecutor.QUESTION);
int messageHistoryLimit = getMessageHistoryLimit(params);

if (StringUtils.isEmpty(memoryType)) {
runAgent(mlAgent, params, listener, null, null);
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
Expand All @@ -151,8 +157,8 @@ public void run(MLAgent mlAgent, Map<String, String> params, ActionListener<Obje
);
}

StringBuilder chatHistoryBuilder = new StringBuilder();
if (!messageList.isEmpty()) {
StringBuilder chatHistoryBuilder = new StringBuilder();
String chatHistoryPrefix = params.getOrDefault(PROMPT_CHAT_HISTORY_PREFIX, CHAT_HISTORY_PREFIX);
chatHistoryBuilder.append(chatHistoryPrefix);
for (Message message : messageList) {
Expand Down Expand Up @@ -220,7 +226,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);
int maxIterations = Integer.parseInt(tmpParameters.getOrDefault(MAX_ITERATION, "3")) * 2;
for (int i = 0; i < maxIterations; i++) {
int finalI = i;
Expand Down Expand Up @@ -401,8 +409,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;
}
Expand Down Expand Up @@ -572,19 +580,21 @@ private void sendFinalAnswer(
private 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 (!StringUtils.isEmpty(sessionId)) {
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;
}

Expand Down
Loading