Skip to content

Prompt Management Create API #3779

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
wants to merge 18 commits into
base: feature/prompt
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -37,6 +37,7 @@ public class CommonValue {
public static final String ML_MODEL_INDEX = ".plugins-ml-model";
public static final String ML_TASK_INDEX = ".plugins-ml-task";
public static final String ML_CONNECTOR_INDEX = ".plugins-ml-connector";
public static final String ML_PROMPT_INDEX = ".plugins-ml-prompt";
public static final String ML_CONFIG_INDEX = ".plugins-ml-config";
public static final String ML_CONTROLLER_INDEX = ".plugins-ml-controller";
public static final String ML_MAP_RESPONSE_KEY = "response";
Expand All @@ -54,6 +55,7 @@ public class CommonValue {
public static final String ML_MODEL_INDEX_MAPPING_PATH = "index-mappings/ml_model.json";
public static final String ML_TASK_INDEX_MAPPING_PATH = "index-mappings/ml_task.json";
public static final String ML_CONNECTOR_INDEX_MAPPING_PATH = "index-mappings/ml_connector.json";
public static final String ML_PROMPT_INDEX_MAPPING_PATH = "index-mappings/ml_prompt.json";
public static final String ML_CONFIG_INDEX_MAPPING_PATH = "index-mappings/ml_config.json";
public static final String ML_CONTROLLER_INDEX_MAPPING_PATH = "index-mappings/ml_controller.json";
public static final String ML_AGENT_INDEX_MAPPING_PATH = "index-mappings/ml_agent.json";
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/org/opensearch/ml/common/MLIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static org.opensearch.ml.common.CommonValue.ML_MODEL_GROUP_INDEX_MAPPING_PATH;
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX_MAPPING_PATH;
import static org.opensearch.ml.common.CommonValue.ML_PROMPT_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_PROMPT_INDEX_MAPPING_PATH;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX_MAPPING_PATH;

Expand All @@ -35,6 +37,7 @@ public enum MLIndex {
MODEL(ML_MODEL_INDEX, false, ML_MODEL_INDEX_MAPPING_PATH),
TASK(ML_TASK_INDEX, false, ML_TASK_INDEX_MAPPING_PATH),
CONNECTOR(ML_CONNECTOR_INDEX, false, ML_CONNECTOR_INDEX_MAPPING_PATH),
PROMPT(ML_PROMPT_INDEX, false, ML_PROMPT_INDEX_MAPPING_PATH),
CONFIG(ML_CONFIG_INDEX, false, ML_CONFIG_INDEX_MAPPING_PATH),
CONTROLLER(ML_CONTROLLER_INDEX, false, ML_CONTROLLER_INDEX_MAPPING_PATH),
AGENT(ML_AGENT_INDEX, false, ML_AGENT_INDEX_MAPPING_PATH),
Expand Down
251 changes: 251 additions & 0 deletions common/src/main/java/org/opensearch/ml/common/MLPrompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.common.CommonValue.TENANT_ID_FIELD;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
* MLPrompt is the class to store prompt information.
*/
@Getter
@EqualsAndHashCode
public class MLPrompt implements ToXContentObject, Writeable {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add java doc for every class and main method so that it's readable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a new file, a header is needed. Check the other classes and copy the header in all new files.
/*

  • Copyright OpenSearch Contributors
  • SPDX-License-Identifier: Apache-2.0
    */


public static final String PROMPT_ID_FIELD = "prompt_id";
public static final String NAME_FIELD = "name";
public static final String DESCRIPTION_FIELD = "description";
public static final String VERSION_FIELD = "version";
public static final String PROMPT_FIELD = "prompt";
public static final String TAGS_FIELD = "tags";
public static final String CREATE_TIME_FIELD = "create_time";
public static final String LAST_UPDATE_TIME_FIELD = "last_update_time";

private String promptId;
private String name;
private String description;
private String version;
private Map<String, String> prompt;
private List<String> tags;
private String tenantId;
private Instant createTime;
private Instant lastUpdateTime;

/**
* Constructor to pass values to the MLPrompt constructor
*
* @param promptId The prompt id of the MLPrompt
* @param name The name of the MLPrompt
* @param description The description of the MLPrompt
* @param version The version of the MLPrompt
* @param prompt The prompt of the MLPrompt -> contains system and user prompts
* @param tags The tags of the MLPrompt
* @param tenantId The tenant id of the MLPrompt
* @param createTime The create time of the MLPrompt
* @param lastUpdateTime The last update time of the MLPrompt
*/
@Builder(toBuilder = true)
public MLPrompt(
String promptId,
String name,
String description,
String version,
Map<String, String> prompt,
List<String> tags,
String tenantId,
Instant createTime,
Instant lastUpdateTime
) {
this.promptId = promptId;
this.name = name;
this.description = description;
this.version = version;
this.prompt = prompt;
this.tags = tags;
this.tenantId = tenantId;
this.createTime = createTime;
this.lastUpdateTime = lastUpdateTime;
}

/**
* Deserialize the Stream Input and constructs MLPrompt
*
* @param input Abstract class that describes Stream Input
* @throws IOException if an I/O exception occurred while reading from input stream
*/
public MLPrompt(StreamInput input) throws IOException {
this.promptId = input.readOptionalString();
this.name = input.readOptionalString();
this.description = input.readOptionalString();
this.version = input.readOptionalString();
this.prompt = input.readMap(s -> s.readString(), s -> s.readString());
this.tags = input.readList(StreamInput::readString);
this.tenantId = input.readOptionalString();
this.createTime = input.readOptionalInstant();
this.lastUpdateTime = input.readOptionalInstant();
}

/**
* Serialize and Writes the MLPrompt object to the output stream.
*
* @param out Abstract class that describes Stream Output
* @throws IOException if an I/O exception occurred while writing to the output stream
*/
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(promptId);
out.writeOptionalString(name);
out.writeOptionalString(description);
out.writeOptionalString(version);
out.writeMap(prompt, StreamOutput::writeString, StreamOutput::writeString);
out.writeCollection(tags, StreamOutput::writeString);
out.writeOptionalString(tenantId);
out.writeOptionalInstant(createTime);
out.writeOptionalInstant(lastUpdateTime);
}

/**
* Serialize and Writes the MLPrompt object to the XContentBuilder
*
* @param xContentBuilder XContentBuilder
* @param params Parameters that need to be written to xContentBuilder
* @return XContentBuilder
* @throws IOException if an I/O exception occurred while writing field values to xContent
*/
@Override
public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params) throws IOException {
XContentBuilder builder = xContentBuilder.startObject();
if (promptId != null) {
builder.field(PROMPT_ID_FIELD, promptId);
}
if (name != null) {
builder.field(NAME_FIELD, name);
}
if (description != null) {
builder.field(DESCRIPTION_FIELD, description);
}
if (version != null) {
builder.field(VERSION_FIELD, version);
}
if (prompt != null) {
builder.field(PROMPT_FIELD, prompt);
}
if (tags != null) {
builder.field(TAGS_FIELD, tags);
}
if (tenantId != null) {
builder.field(TENANT_ID_FIELD, tenantId);

Check warning on line 157 in common/src/main/java/org/opensearch/ml/common/MLPrompt.java

View check run for this annotation

Codecov / codecov/patch

common/src/main/java/org/opensearch/ml/common/MLPrompt.java#L157

Added line #L157 was not covered by tests
}
if (createTime != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tenantid is missing here.

builder.field(CREATE_TIME_FIELD, createTime.toEpochMilli());
}
if (lastUpdateTime != null) {
builder.field(LAST_UPDATE_TIME_FIELD, lastUpdateTime.toEpochMilli());
}
return builder.endObject();
}

/**
* Creates MLPrompt from stream input
*
* @param in Input Stream
* @return MLPrompt
* @throws IOException if an I/O exception occurred while reading from input stream
*/
public static MLPrompt fromStream(StreamInput in) throws IOException {
return new MLPrompt(in);

Check warning on line 176 in common/src/main/java/org/opensearch/ml/common/MLPrompt.java

View check run for this annotation

Codecov / codecov/patch

common/src/main/java/org/opensearch/ml/common/MLPrompt.java#L176

Added line #L176 was not covered by tests
}

/**
* Creates MLPrompt from XContentParser
*
* @param parser XContentParser
* @return MLPrompt
* @throws IOException if an I/O exception occurred while parsing the XContentParser into MLPrompt fields
*/
public static MLPrompt parse(XContentParser parser) throws IOException {
String promptId = null;
String name = null;
String description = null;
String version = null;
Map<String, String> prompt = null;
List<String> tags = null;
String tenantId = null;
Instant createTime = null;
Instant lastUpdateTime = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case PROMPT_ID_FIELD:
promptId = parser.text();
break;
case NAME_FIELD:
name = parser.text();
break;
case DESCRIPTION_FIELD:
description = parser.text();
break;
case VERSION_FIELD:
version = parser.text();
break;
case PROMPT_FIELD:
prompt = parser.mapStrings();
break;
case TAGS_FIELD:
tags = new ArrayList<>();
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
tags.add(parser.text());
}
break;
case TENANT_ID_FIELD:
tenantId = parser.text();
break;

Check warning on line 226 in common/src/main/java/org/opensearch/ml/common/MLPrompt.java

View check run for this annotation

Codecov / codecov/patch

common/src/main/java/org/opensearch/ml/common/MLPrompt.java#L225-L226

Added lines #L225 - L226 were not covered by tests
case CREATE_TIME_FIELD:
createTime = Instant.ofEpochMilli(parser.longValue());
break;
case LAST_UPDATE_TIME_FIELD:
lastUpdateTime = Instant.ofEpochMilli(parser.longValue());
break;
default:
parser.skipChildren();

Check warning on line 234 in common/src/main/java/org/opensearch/ml/common/MLPrompt.java

View check run for this annotation

Codecov / codecov/patch

common/src/main/java/org/opensearch/ml/common/MLPrompt.java#L234

Added line #L234 was not covered by tests
break;
}
}
return MLPrompt
.builder()
.promptId(promptId)
.name(name)
.description(description)
.version(version)
.prompt(prompt)
.tags(tags)
.tenantId(tenantId)
.createTime(createTime)
.lastUpdateTime(lastUpdateTime)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.transport.prompt;

import org.opensearch.action.ActionType;

public class MLCreatePromptAction extends ActionType<MLCreatePromptResponse> {
public static MLCreatePromptAction INSTANCE = new MLCreatePromptAction();
public static final String NAME = "cluster:admin/opensearch/ml/create_prompt";

private MLCreatePromptAction() {
super(NAME, MLCreatePromptResponse::new);
}
}
Loading
Loading