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

Feature/conversation memory feature flag #1271

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.opensearch.ml.common.conversation;

import org.opensearch.common.settings.Setting;

/**
* Class containing a bunch of constant defining how the conversational indices are formatted
*/
Expand Down Expand Up @@ -97,4 +99,7 @@ public class ConversationalIndexConstants {
+ " }\n"
+ "}";

/** Feature Flag setting for conversational memory */
public static final Setting<Boolean> ML_COMMONS_MEMORY_FEATURE_ENABLED = Setting
.boolSetting("plugins.ml_commons.memory_feature_enabled", false, Setting.Property.NodeScope, Setting.Property.Dynamic);
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
*/
package org.opensearch.ml.memory.action.conversation;

import org.opensearch.OpenSearchException;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.common.conversation.ConversationalIndexConstants;
import org.opensearch.ml.memory.ConversationalMemoryHandler;
import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler;
import org.opensearch.tasks.Task;
Expand All @@ -39,27 +43,47 @@ public class CreateConversationTransportAction extends HandledTransportAction<Cr
private ConversationalMemoryHandler cmHandler;
private Client client;

private volatile boolean featureIsEnabled;

/**
* Constructor
* @param transportService for inter-node communications
* @param actionFilters for filtering actions
* @param cmHandler Handler for conversational memory operations
* @param client OS Client for dealing with OS
* @param clusterService for some cluster ops
*/
@Inject
public CreateConversationTransportAction(
TransportService transportService,
ActionFilters actionFilters,
OpenSearchConversationalMemoryHandler cmHandler,
Client client
Client client,
ClusterService clusterService
) {
super(CreateConversationAction.NAME, transportService, actionFilters, CreateConversationRequest::new);
this.cmHandler = cmHandler;
this.client = client;
@SuppressWarnings("unchecked")
Setting<Boolean> setting = (Setting<Boolean>) clusterService
.getClusterSettings()
.get(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey());
this.featureIsEnabled = setting.get(clusterService.getSettings());
clusterService.getClusterSettings().addSettingsUpdateConsumer(setting, it -> featureIsEnabled = it);
}

@Override
protected void doExecute(Task task, CreateConversationRequest request, ActionListener<CreateConversationResponse> actionListener) {
if (!featureIsEnabled) {
actionListener
.onFailure(
new OpenSearchException(
"The experimental Conversation Memory feature is not enabled. To enable, please update the setting "
+ ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey()
)
);
return;
}
String name = request.getName();
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().newStoredContext(true)) {
ActionListener<CreateConversationResponse> internalListener = ActionListener.runBefore(actionListener, () -> context.restore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
*/
package org.opensearch.ml.memory.action.conversation;

import org.opensearch.OpenSearchException;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.common.conversation.ConversationalIndexConstants;
import org.opensearch.ml.memory.ConversationalMemoryHandler;
import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler;
import org.opensearch.tasks.Task;
Expand All @@ -39,27 +43,47 @@ public class CreateInteractionTransportAction extends HandledTransportAction<Cre
private ConversationalMemoryHandler cmHandler;
private Client client;

private volatile boolean featureIsEnabled;

/**
* Constructor
* @param transportService for doing intra-cluster communication
* @param actionFilters for filtering actions
* @param cmHandler handler for conversational memory
* @param client client for general opensearch ops
* @param clusterService for some cluster ops
*/
@Inject
public CreateInteractionTransportAction(
TransportService transportService,
ActionFilters actionFilters,
OpenSearchConversationalMemoryHandler cmHandler,
Client client
Client client,
ClusterService clusterService
) {
super(CreateInteractionAction.NAME, transportService, actionFilters, CreateInteractionRequest::new);
this.client = client;
this.cmHandler = cmHandler;
@SuppressWarnings("unchecked")
Setting<Boolean> setting = (Setting<Boolean>) clusterService
.getClusterSettings()
.get(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey());
this.featureIsEnabled = setting.get(clusterService.getSettings());
clusterService.getClusterSettings().addSettingsUpdateConsumer(setting, it -> featureIsEnabled = it);
}

@Override
protected void doExecute(Task task, CreateInteractionRequest request, ActionListener<CreateInteractionResponse> actionListener) {
if (!featureIsEnabled) {
actionListener
.onFailure(
new OpenSearchException(
"The experimental Conversation Memory feature is not enabled. To enable, please update the setting "
+ ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey()
)
);
return;
}
String cid = request.getConversationId();
String inp = request.getInput();
String rsp = request.getResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
*/
package org.opensearch.ml.memory.action.conversation;

import org.opensearch.OpenSearchException;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.common.conversation.ConversationalIndexConstants;
import org.opensearch.ml.memory.ConversationalMemoryHandler;
import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler;
import org.opensearch.tasks.Task;
Expand All @@ -39,27 +43,47 @@ public class DeleteConversationTransportAction extends HandledTransportAction<De
private Client client;
private ConversationalMemoryHandler cmHandler;

private volatile boolean featureIsEnabled;

/**
* Constructor
* @param transportService for inter-node communications
* @param actionFilters for filtering actions
* @param cmHandler Handler for conversational memory operations
* @param client OS Client for dealing with OS
* @param clusterService for some cluster ops
*/
@Inject
public DeleteConversationTransportAction(
TransportService transportService,
ActionFilters actionFilters,
OpenSearchConversationalMemoryHandler cmHandler,
Client client
Client client,
ClusterService clusterService
) {
super(DeleteConversationAction.NAME, transportService, actionFilters, DeleteConversationRequest::new);
this.client = client;
this.cmHandler = cmHandler;
@SuppressWarnings("unchecked")
Setting<Boolean> setting = (Setting<Boolean>) clusterService
.getClusterSettings()
.get(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey());
this.featureIsEnabled = setting.get(clusterService.getSettings());
clusterService.getClusterSettings().addSettingsUpdateConsumer(setting, it -> featureIsEnabled = it);
}

@Override
public void doExecute(Task task, DeleteConversationRequest request, ActionListener<DeleteConversationResponse> listener) {
if (!featureIsEnabled) {
listener
.onFailure(
new OpenSearchException(
"The experimental Conversation Memory feature is not enabled. To enable, please update the setting "
+ ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey()
)
);
return;
}
String conversationId = request.getId();
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().newStoredContext(true)) {
ActionListener<DeleteConversationResponse> internalListener = ActionListener.runBefore(listener, () -> context.restore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

import java.util.List;

import org.opensearch.OpenSearchException;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.common.conversation.ConversationMeta;
import org.opensearch.ml.common.conversation.ConversationalIndexConstants;
import org.opensearch.ml.memory.ConversationalMemoryHandler;
import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler;
import org.opensearch.tasks.Task;
Expand All @@ -42,27 +46,47 @@ public class GetConversationsTransportAction extends HandledTransportAction<GetC
private Client client;
private ConversationalMemoryHandler cmHandler;

private volatile boolean featureIsEnabled;

/**
* Constructor
* @param transportService for inter-node communications
* @param actionFilters for filtering actions
* @param cmHandler Handler for conversational memory operations
* @param client OS Client for dealing with OS
* @param clusterService for some cluster ops
*/
@Inject
public GetConversationsTransportAction(
TransportService transportService,
ActionFilters actionFilters,
OpenSearchConversationalMemoryHandler cmHandler,
Client client
Client client,
ClusterService clusterService
) {
super(GetConversationsAction.NAME, transportService, actionFilters, GetConversationsRequest::new);
this.client = client;
this.cmHandler = cmHandler;
@SuppressWarnings("unchecked")
HenryL27 marked this conversation as resolved.
Show resolved Hide resolved
Setting<Boolean> setting = (Setting<Boolean>) clusterService
.getClusterSettings()
.get(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey());
this.featureIsEnabled = setting.get(clusterService.getSettings());
clusterService.getClusterSettings().addSettingsUpdateConsumer(setting, it -> featureIsEnabled = it);
}

@Override
public void doExecute(Task task, GetConversationsRequest request, ActionListener<GetConversationsResponse> actionListener) {
if (!featureIsEnabled) {
actionListener
.onFailure(
new OpenSearchException(
"The experimental Conversation Memory feature is not enabled. To enable, please update the setting "
+ ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey()
)
);
return;
}
int maxResults = request.getMaxResults();
int from = request.getFrom();
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().newStoredContext(true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@

import java.util.List;

import org.opensearch.OpenSearchException;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.common.conversation.ConversationalIndexConstants;
import org.opensearch.ml.common.conversation.Interaction;
import org.opensearch.ml.memory.ConversationalMemoryHandler;
import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler;
Expand All @@ -42,27 +46,47 @@ public class GetInteractionsTransportAction extends HandledTransportAction<GetIn
private Client client;
private ConversationalMemoryHandler cmHandler;

private volatile boolean featureIsEnabled;

/**
* Constructor
* @param transportService for inter-node communications
* @param actionFilters for filtering actions
* @param cmHandler Handler for conversational memory operations
* @param client OS Client for dealing with OS
* @param clusterService for some cluster ops
*/
@Inject
public GetInteractionsTransportAction(
TransportService transportService,
ActionFilters actionFilters,
OpenSearchConversationalMemoryHandler cmHandler,
Client client
Client client,
ClusterService clusterService
) {
super(GetInteractionsAction.NAME, transportService, actionFilters, GetInteractionsRequest::new);
this.client = client;
this.cmHandler = cmHandler;
@SuppressWarnings("unchecked")
Setting<Boolean> setting = (Setting<Boolean>) clusterService
.getClusterSettings()
.get(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey());
this.featureIsEnabled = setting.get(clusterService.getSettings());
clusterService.getClusterSettings().addSettingsUpdateConsumer(setting, it -> featureIsEnabled = it);
}

@Override
public void doExecute(Task task, GetInteractionsRequest request, ActionListener<GetInteractionsResponse> actionListener) {
if (!featureIsEnabled) {
actionListener
.onFailure(
new OpenSearchException(
"The experimental Conversation Memory feature is not enabled. To enable, please update the setting "
+ ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey()
)
);
return;
}
int maxResults = request.getMaxResults();
int from = request.getFrom();
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().newStoredContext(true)) {
Expand Down
Loading