forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: HenryL27 <[email protected]>
- Loading branch information
Showing
9 changed files
with
460 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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
51 changes: 51 additions & 0 deletions
51
plugin/src/main/java/org/opensearch/ml/rest/RestMemoryGetConversationAction.java
This file contains 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,51 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.rest; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.ml.common.conversation.ActionConstants; | ||
import org.opensearch.ml.memory.action.conversation.GetConversationAction; | ||
import org.opensearch.ml.memory.action.conversation.GetConversationRequest; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.action.RestToXContentListener; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
public class RestMemoryGetConversationAction extends BaseRestHandler { | ||
private final static String GET_CONVERSATION_NAME = "conversational_get_conversation"; | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return ImmutableList.of(new Route(RestRequest.Method.GET, ActionConstants.GET_CONVERSATION_REST_PATH)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return GET_CONVERSATION_NAME; | ||
} | ||
|
||
@Override | ||
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
GetConversationRequest gcRequest = GetConversationRequest.fromRestRequest(request); | ||
return channel -> client.execute(GetConversationAction.INSTANCE, gcRequest, new RestToXContentListener<>(channel)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
plugin/src/main/java/org/opensearch/ml/rest/RestMemoryGetInteractionAction.java
This file contains 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,51 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.rest; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.ml.common.conversation.ActionConstants; | ||
import org.opensearch.ml.memory.action.conversation.GetInteractionAction; | ||
import org.opensearch.ml.memory.action.conversation.GetInteractionRequest; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.action.RestToXContentListener; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
public class RestMemoryGetInteractionAction extends BaseRestHandler { | ||
private final static String GET_INTERACTION_NAME = "conversational_get_interaction"; | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return ImmutableList.of(new Route(RestRequest.Method.GET, ActionConstants.GET_INTERACTION_REST_PATH)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return GET_INTERACTION_NAME; | ||
} | ||
|
||
@Override | ||
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
GetInteractionRequest giRequest = GetInteractionRequest.fromRestRequest(request); | ||
return channel -> client.execute(GetInteractionAction.INSTANCE, giRequest, new RestToXContentListener<>(channel)); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
plugin/src/test/java/org/opensearch/ml/rest/RestMemoryGetConversationActionIT.java
This file contains 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,81 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.rest; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.HttpHeaders; | ||
import org.apache.hc.core5.http.message.BasicHeader; | ||
import org.junit.Before; | ||
import org.opensearch.client.Response; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.ml.common.conversation.ActionConstants; | ||
import org.opensearch.ml.settings.MLCommonsSettings; | ||
import org.opensearch.ml.utils.TestHelper; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
public class RestMemoryGetConversationActionIT extends MLCommonsRestTestCase { | ||
@Before | ||
public void setupFeatureSettings() throws IOException { | ||
Response response = TestHelper | ||
.makeRequest( | ||
client(), | ||
"PUT", | ||
"_cluster/settings", | ||
null, | ||
"{\"persistent\":{\"" + MLCommonsSettings.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey() + "\":true}}", | ||
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "")) | ||
); | ||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
} | ||
|
||
public void testGetConversation() throws IOException { | ||
Response ccresponse = TestHelper | ||
.makeRequest( | ||
client(), | ||
"POST", | ||
ActionConstants.CREATE_CONVERSATION_REST_PATH, | ||
null, | ||
gson.toJson(Map.of(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD, "name")), | ||
null | ||
); | ||
assert (ccresponse != null); | ||
assert (TestHelper.restStatus(ccresponse) == RestStatus.OK); | ||
HttpEntity cchttpEntity = ccresponse.getEntity(); | ||
String ccentityString = TestHelper.httpEntityToString(cchttpEntity); | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> ccmap = gson.fromJson(ccentityString, Map.class); | ||
assert (ccmap.containsKey(ActionConstants.CONVERSATION_ID_FIELD)); | ||
String id = (String) ccmap.get(ActionConstants.CONVERSATION_ID_FIELD); | ||
|
||
Response gcresponse = TestHelper | ||
.makeRequest(client(), "GET", ActionConstants.GET_CONVERSATION_REST_PATH.replace("{conversation_id}", id), null, "", null); | ||
assert (gcresponse != null); | ||
assert (TestHelper.restStatus(gcresponse) == RestStatus.OK); | ||
HttpEntity gchttpEntity = gcresponse.getEntity(); | ||
String gcentitiyString = TestHelper.httpEntityToString(gchttpEntity); | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> gcmap = gson.fromJson(gcentitiyString, Map.class); | ||
assert (gcmap.containsKey(ActionConstants.CONVERSATION_ID_FIELD) && gcmap.get(ActionConstants.CONVERSATION_ID_FIELD).equals(id)); | ||
assert (gcmap.containsKey(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD) | ||
&& gcmap.get(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD).equals("name")); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
plugin/src/test/java/org/opensearch/ml/rest/RestMemoryGetConversationActionTests.java
This file contains 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,64 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 org.opensearch.ml.rest; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.mockito.ArgumentCaptor; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.ml.common.conversation.ActionConstants; | ||
import org.opensearch.ml.memory.action.conversation.GetConversationAction; | ||
import org.opensearch.ml.memory.action.conversation.GetConversationRequest; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestHandler.Route; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.test.rest.FakeRestRequest; | ||
|
||
public class RestMemoryGetConversationActionTests extends OpenSearchTestCase { | ||
public void testBasics() { | ||
RestMemoryGetConversationAction action = new RestMemoryGetConversationAction(); | ||
assert (action.getName().equals("conversational_get_conversation")); | ||
List<Route> routes = action.routes(); | ||
assert (routes.size() == 1); | ||
assert (routes.get(0).equals(new Route(RestRequest.Method.GET, ActionConstants.GET_CONVERSATION_REST_PATH))); | ||
} | ||
|
||
public void testPrepareRequest() throws Exception { | ||
RestMemoryGetConversationAction action = new RestMemoryGetConversationAction(); | ||
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) | ||
.withParams(Map.of(ActionConstants.CONVERSATION_ID_FIELD, "cid")) | ||
.build(); | ||
|
||
NodeClient client = mock(NodeClient.class); | ||
RestChannel channel = mock(RestChannel.class); | ||
action.handleRequest(request, channel, client); | ||
|
||
ArgumentCaptor<GetConversationRequest> argCaptor = ArgumentCaptor.forClass(GetConversationRequest.class); | ||
verify(client, times(1)).execute(eq(GetConversationAction.INSTANCE), argCaptor.capture(), any()); | ||
assert (argCaptor.getValue().getConversationId().equals("cid")); | ||
} | ||
} |
Oops, something went wrong.